diff --git a/src/main/java/dev/wolfieboy09/qstorage/api/energy/ExtendedEnergyStorage.java b/src/main/java/dev/wolfieboy09/qstorage/api/energy/ExtendedEnergyStorage.java index 5930e68..773af57 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/api/energy/ExtendedEnergyStorage.java +++ b/src/main/java/dev/wolfieboy09/qstorage/api/energy/ExtendedEnergyStorage.java @@ -1,19 +1,22 @@ 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; } /** @@ -21,20 +24,16 @@ public ExtendedEnergyStorage(int capacity) { * * @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; } /** @@ -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]. * @@ -60,6 +61,7 @@ public void setEnergy(int energy) { if(energy > this.capacity) energy = this.capacity; this.energy = energy; + onEnergyChanged(); } /** @@ -97,4 +99,26 @@ public int getMaxReceive() { public int getMaxExtract() { return this.maxExtract; } -} \ No newline at end of file + + @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(); + } +} diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java b/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java index 906f5e2..58449f5 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java @@ -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; @@ -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; } @@ -145,12 +118,12 @@ 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"); @@ -158,10 +131,10 @@ public void handleUpdateTag(@NotNull CompoundTag tag, @NotNull HolderLookup.Prov 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()); } } diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlock.java b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlock.java index 6cb4d86..8e592b5 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlock.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlock.java @@ -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); diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlockEntity.java b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlockEntity.java index ce775c3..f7af49a 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlockEntity.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerBlockEntity.java @@ -6,19 +6,24 @@ 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; @@ -26,7 +31,18 @@ public class DiskAssemblerBlockEntity extends AbstractEnergyBlockEntity { 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; @@ -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); } } @@ -102,7 +113,6 @@ private boolean isCrafting() { private void resetCrafting() { crafting_ticks = 0; energy_required = 0; - syncToClient(); } public SimpleContainer getInputContainer() { @@ -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)); @@ -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")); diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerMenu.java b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerMenu.java index fb3e94e..bdf5903 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerMenu.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerMenu.java @@ -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; @@ -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); @@ -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)); @@ -117,7 +118,7 @@ public boolean stillValid(@NotNull Player player) { @Override public int getEnergy() { - return this.blockEntity.getEnergyStored(); + return this.data.get(0); } @Override diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerScreen.java b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerScreen.java index 8c8fa4e..391bdbb 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerScreen.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/disk_assembler/DiskAssemblerScreen.java @@ -1,6 +1,7 @@ package dev.wolfieboy09.qstorage.block.disk_assembler; import com.mojang.blaze3d.systems.RenderSystem; +import dev.wolfieboy09.qstorage.api.util.FormattingUtil; import dev.wolfieboy09.qstorage.api.util.ResourceHelper; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; @@ -37,7 +38,7 @@ public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, int energyStored = this.menu.getEnergy(); int maxEnergy = this.menu.getMaxEnergy(); - Component text = Component.literal("Energy: " + energyStored + " / " + maxEnergy); + Component text = Component.literal("Energy: " + FormattingUtil.formatNumber(energyStored) + " / " + FormattingUtil.formatNumber(maxEnergy) + "FE"); if(isHovering(163, 10, 3, 62, pMouseX, pMouseY)) { pGuiGraphics.renderTooltip(this.font, text, pMouseX, pMouseY); }