Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bunch of things #8

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
package dev.wolfieboy09.qstorage.api.energy;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.neoforged.neoforge.energy.EnergyStorage;

/**
* An extension of the {@link EnergyStorage} class, providing additional methods for managing energy storage.
*/
public class ExtendedEnergyStorage extends EnergyStorage {

private final BlockEntity be;
/**
* Creates a new ExtendedEnergyStorage instance with the specified capacity.
*
* @param capacity the maximum amount of energy that can be stored
* @param be the block entity associated with this storage
*/
public ExtendedEnergyStorage(int capacity) {
public ExtendedEnergyStorage(int capacity,BlockEntity be) {
super(capacity);
this.be = be;
}

/**
* Creates a new ExtendedEnergyStorage instance with the specified capacity and maximum transfer amount.
*
* @param capacity the maximum amount of energy that can be stored
* @param maxTransfer the maximum amount of energy that can be transferred in a single operation
* @param be the block entity associated with this storage
*/
public ExtendedEnergyStorage(int capacity, int maxTransfer) {
public ExtendedEnergyStorage(int capacity, int maxTransfer, BlockEntity be) {
super(capacity, maxTransfer);
this.be = be;
}

/**
* Creates a new ExtendedEnergyStorage instance with the specified capacity, maximum receive amount, and maximum extract amount.
*
* @param capacity the maximum amount of energy that can be stored
* @param maxReceive the maximum amount of energy that can be received in a single operation
* @param maxExtract the maximum amount of energy that can be extracted in a single operation
*/
public ExtendedEnergyStorage(int capacity, int maxReceive, int maxExtract) {

public ExtendedEnergyStorage(int capacity, int maxReceive, int maxExtract, BlockEntity be) {
super(capacity, maxReceive, maxExtract);
this.be = be;
}

/**
Expand All @@ -44,11 +43,13 @@ public ExtendedEnergyStorage(int capacity, int maxReceive, int maxExtract) {
* @param maxReceive the maximum amount of energy that can be received in a single operation
* @param maxExtract the maximum amount of energy that can be extracted in a single operation
* @param energy the initial amount of energy stored
* @param be the block entity associated with this storage
*/
public ExtendedEnergyStorage(int capacity, int maxReceive, int maxExtract, int energy) {
public ExtendedEnergyStorage(int capacity, int maxReceive, int maxExtract, int energy, BlockEntity be) {
super(capacity, maxReceive, maxExtract, energy);
this.be = be;
}

/**
* Sets the current energy amount to the specified value, clamping it to the range [0, capacity].
*
Expand All @@ -60,6 +61,7 @@ public void setEnergy(int energy) {
if(energy > this.capacity)
energy = this.capacity;
this.energy = energy;
onEnergyChanged();
}

/**
Expand Down Expand Up @@ -97,4 +99,26 @@ public int getMaxReceive() {
public int getMaxExtract() {
return this.maxExtract;
}
}

@Override
public int receiveEnergy(int toReceive, boolean simulate) {
var toReturn = super.receiveEnergy(toReceive, simulate);
if (!simulate) {
onEnergyChanged();
}
return toReturn;
}

@Override
public int extractEnergy(int toExtract, boolean simulate) {
var toReturn = super.extractEnergy(toExtract, simulate);
if (!simulate) {
onEnergyChanged();
}
return toReturn;
}

public void onEnergyChanged() {
this.be.setChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.energy.IEnergyStorage;
Expand All @@ -16,62 +16,35 @@
@ParametersAreNonnullByDefault
public abstract class AbstractEnergyBlockEntity extends GlobalBlockEntity implements IEnergyStorage {
protected final ExtendedEnergyStorage energyStorage;

protected ContainerData energyData = new ContainerData() {
private int energy = 0;
@Override
public int get(int i) {
return energy;
}

@Override
public void set(int i, int i1) {
energy = i1;
}

@Override
public int getCount() {
return 1;
}
};

public AbstractEnergyBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState, int capacity, int maxTransfer) {
super(type, pos, blockState);
this.energyStorage = new ExtendedEnergyStorage(capacity, maxTransfer) {
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
int energyReceived = Math.min(maxReceive, this.getMaxReceive());
if (!simulate) {
this.addEnergy(energyReceived);
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL_IMMEDIATE);
}
return energyReceived;
}

@Override
public int extractEnergy(int maxExtract, boolean simulate) {
int energyExtracted = Math.min(maxExtract, this.getMaxExtract());
if (!simulate) {
this.removeEnergy(energyExtracted);
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL_IMMEDIATE);
}
return energyExtracted;
}
};
this.energyStorage = new ExtendedEnergyStorage(capacity, maxTransfer,this);
}


public AbstractEnergyBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState, int capacity, int maxRecieve, int maxExtract) {
public AbstractEnergyBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState, int capacity, int maxReceive, int maxExtract) {
super(type, pos, blockState);
this.energyStorage = new ExtendedEnergyStorage(capacity, maxRecieve, maxExtract) {
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
int energyReceived = Math.min(maxReceive, this.getMaxReceive());
if (!simulate) {
this.addEnergy(energyReceived);
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL_IMMEDIATE);
}
return energyReceived;
}

@Override
public int extractEnergy(int maxExtract, boolean simulate) {
int energyExtracted = Math.min(maxExtract, this.getMaxExtract());
if (!simulate) {
this.removeEnergy(energyExtracted);
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL_IMMEDIATE);
}
return energyExtracted;
}
};
this.energyStorage = new ExtendedEnergyStorage(capacity, maxReceive, maxExtract,this);
}


public ExtendedEnergyStorage getEnergyStorage() {
return this.energyStorage;
}
Expand Down Expand Up @@ -145,23 +118,23 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
if (tag.contains("Energy")) {
this.energyStorage.deserializeNBT(registries, tag.get("Energy"));
}
Tag energyTag = tag.get("Energy");
if (energyTag == null) return;
this.energyStorage.deserializeNBT(registries, energyTag);
loadExtra(tag, registries);
}

@Override
public void handleUpdateTag(@NotNull CompoundTag tag, @NotNull HolderLookup.Provider lookupProvider) {
Tag energyTag = tag.get("Energy");
if (energyTag == null) return;
this.energyStorage.deserializeNBT(lookupProvider, energyTag);
handleUpdate(tag, lookupProvider);
}

public void syncToClient() {
setChanged();
if (level == null) return;
level.sendBlockUpdated(worldPosition, this.getBlockState(), this.getBlockState(), Block.UPDATE_CLIENTS);
@Override
public void setChanged() {
super.setChanged();
this.energyData.set(0, this.energyStorage.getEnergyStored());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,12 @@ protected RenderShape getRenderShape(BlockState state) {
return new DiskAssemblerBlockEntity(blockPos, blockState);
}

@Override
protected @Nullable MenuProvider getMenuProvider(BlockState state, Level level, BlockPos pos) {
return new SimpleMenuProvider((id, playerInv, serverPlayer) -> new DiskAssemblerMenu(id, pos, playerInv, serverPlayer), Component.translatable("block.qstorage.disk_assembler"));
}

@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
if (player instanceof ServerPlayer serverPlayer) {
serverPlayer.openMenu(Objects.requireNonNull(state.getMenuProvider(level, pos)), pos);
if (level.getBlockEntity(pos) instanceof DiskAssemblerBlockEntity be) {
serverPlayer.openMenu(be, pos);
}
return InteractionResult.SUCCESS;
}
return super.useWithoutItem(state, level, pos, player, hitResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,43 @@
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.energy.EnergyStorage;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Objects;

@ParametersAreNonnullByDefault
public class DiskAssemblerBlockEntity extends AbstractEnergyBlockEntity {
public class DiskAssemblerBlockEntity extends AbstractEnergyBlockEntity implements MenuProvider {
private final Component TITLE = Component.translatable("block.qstorage.disk_assembler");
private int progress = 0;
private int crafting_ticks = 0;
private int energy_required = 0;

public DiskAssemblerBlockEntity(BlockPos pos, BlockState blockState) {
super(QSBlockEntities.DISK_ASSEMBLER.get(), pos, blockState, getEnergyCapacity(), 1000, 0);
}


@Override
public @NotNull Component getDisplayName() {
return TITLE;
}

@Override
public @Nullable AbstractContainerMenu createMenu(int id, Inventory playerInv, Player player) {
this.energyData.set(0, this.energyStorage.getEnergyStored());
return new DiskAssemblerMenu(id, this.getBlockPos(), playerInv, player, this.energyData);
}

public static class DiskAssemblerSlot {
public static final int MAIN_SLOT_1 = 0;
public static final int MAIN_SLOT_2 = 1;
Expand All @@ -45,17 +61,12 @@ protected void onContentsChanged(int slot) {
resetProgress();
}
setChanged();
if (level != null && !level.isClientSide()) {
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL);
}
}
};

protected void resetProgress() {
if (this.progress != 0) {
this.progress = 0;
setChanged();
Objects.requireNonNull(level).sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL);
}
}

Expand Down Expand Up @@ -102,7 +113,6 @@ private boolean isCrafting() {
private void resetCrafting() {
crafting_ticks = 0;
energy_required = 0;
syncToClient();
}

public SimpleContainer getInputContainer() {
Expand All @@ -115,13 +125,14 @@ public SimpleContainer getInputContainer() {

public SimpleContainer getOutputContainer() {
SimpleContainer container = new SimpleContainer(1);
container.setItem(8, inventory.getStackInSlot(8));
container.setItem(0, inventory.getStackInSlot(inventory.getSlots() - 1));
return container;
}

@Override
public void saveExtra(CompoundTag tag, HolderLookup.Provider registries) {
super.saveExtra(tag, registries);
tag.putInt("progress", this.progress);
tag.putInt("energy_required", this.energy_required);
tag.putInt("crafting_ticks", this.crafting_ticks);
tag.put("inventory", this.inventory.serializeNBT(registries));
Expand All @@ -130,6 +141,7 @@ public void saveExtra(CompoundTag tag, HolderLookup.Provider registries) {
@Override
protected void loadExtra(CompoundTag tag, HolderLookup.Provider registries) {
super.loadExtra(tag, registries);
this.progress = tag.getInt("progress");
this.energy_required = tag.getInt("energy_required");
this.crafting_ticks = tag.getInt("crafting_ticks");
this.inventory.deserializeNBT(registries, tag.getCompound("inventory"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.SlotItemHandler;
import net.neoforged.neoforge.items.wrapper.PlayerInvWrapper;
Expand All @@ -35,13 +36,13 @@ public DiskAssemblerMenu(int id, BlockPos pos, Inventory playerInventory, Player
this(id, pos, playerInventory, playerIn, new SimpleContainerData(4));
}

public DiskAssemblerMenu(int id, BlockPos pos, Inventory playerInventory, Player playerIn, ContainerData containerData) {
public DiskAssemblerMenu(int id, BlockPos pos, Inventory playerInventory, Player player, ContainerData containerData) {
super(QSMenuTypes.DISK_ASSEMBLER.get(), id);
addDataSlots(containerData);
DiskAssemblerBlockEntity blockEntity = (DiskAssemblerBlockEntity) playerIn.getCommandSenderWorld().getBlockEntity(pos);
if (blockEntity == null) return;
this.blockEntity = blockEntity;
this.level = playerInventory.player.level();
this.level = player.level();
BlockEntity blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof DiskAssemblerBlockEntity be)) return;
this.blockEntity = be;
this.data = containerData;

PlayerInvWrapper playerInvWrapper = new PlayerInvWrapper(playerInventory);
Expand All @@ -67,7 +68,7 @@ public DiskAssemblerMenu(int id, BlockPos pos, Inventory playerInventory, Player
}
}

ItemStackHandler itemStackHandler = blockEntity.getInventory();
ItemStackHandler itemStackHandler = be.getInventory();

addSlot(new SlotItemHandler(itemStackHandler, 0, 17, 27));
addSlot(new SlotItemHandler(itemStackHandler, 1, 17, 45));
Expand Down Expand Up @@ -117,7 +118,7 @@ public boolean stillValid(@NotNull Player player) {

@Override
public int getEnergy() {
return this.blockEntity.getEnergyStored();
return this.data.get(0);
}

@Override
Expand Down
Loading
Loading