From 1021b960ebeb5e64e4338ffae4886c1eee31405f Mon Sep 17 00:00:00 2001 From: RoboXGamer Date: Tue, 29 Oct 2024 11:24:59 +0530 Subject: [PATCH 1/4] Save the Inventory of the DiskAssemblerBlockEntity --- .../DiskAssemblerBlockEntity.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 a54f128..1c8e974 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 @@ -4,10 +4,13 @@ import dev.wolfieboy09.qstorage.registries.QSBlockEntities; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.CompoundTag; 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 java.util.Objects; @@ -80,4 +83,25 @@ public EnergyStorage getEnergyHandler(@Nullable Direction side) { public ItemStackHandler getInventoryHandler() { return this.inventory; } + + @Override + protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) { + super.saveAdditional(tag, registries); + tag.put("Inventory", this.inventory.serializeNBT(registries)); + } + + @Override + protected void loadAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) { + super.loadAdditional(tag, registries); + if (tag.contains("Inventory")) { + this.inventory.deserializeNBT(registries, tag.getCompound("Inventory")); + } + } + + @Override + public @NotNull CompoundTag getUpdateTag(HolderLookup.@NotNull Provider registries) { + CompoundTag tag = super.getUpdateTag(registries); + tag.put("Inventory", this.inventory.serializeNBT(registries)); + return tag; + } } From eee2761dba7a4387debd2e1878ee63b5413aa4b6 Mon Sep 17 00:00:00 2001 From: RoboXGamer Date: Tue, 29 Oct 2024 11:25:17 +0530 Subject: [PATCH 2/4] Small Formatting Change --- .../qstorage/block/disk_assembler/DiskAssemblerScreen.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From dec510193f08233423465e48703e8edf072e1a0d Mon Sep 17 00:00:00 2001 From: RoboXGamer Date: Tue, 29 Oct 2024 11:25:43 +0530 Subject: [PATCH 3/4] Add back item capability for inv of DiskAssemblerBlockEntity --- .../dev/wolfieboy09/qstorage/registries/QSCapabilities.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/wolfieboy09/qstorage/registries/QSCapabilities.java b/src/main/java/dev/wolfieboy09/qstorage/registries/QSCapabilities.java index b18982d..94a88c8 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/registries/QSCapabilities.java +++ b/src/main/java/dev/wolfieboy09/qstorage/registries/QSCapabilities.java @@ -14,6 +14,6 @@ public class QSCapabilities { @SubscribeEvent public static void register(@NotNull RegisterCapabilitiesEvent event) { event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, QSBlockEntities.DISK_ASSEMBLER.get(), DiskAssemblerBlockEntity::getEnergyHandler); - // event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, QSBlockEntities.DISK_ASSEMBLER.get(), (block, dir) -> block.getInventoryHandler()); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, QSBlockEntities.DISK_ASSEMBLER.get(), (block, dir) -> block.getInventoryHandler()); } } From 3ee76e1d20cc039852fc06cc8f9aecfc61b12945 Mon Sep 17 00:00:00 2001 From: RoboXGamer Date: Wed, 30 Oct 2024 15:58:24 +0530 Subject: [PATCH 4/4] Fixed a bunch of things now Energy is in sync and items are also client side in sync the items dropped on breaking DiskAssemblerBlock --- .../api/energy/ExtendedEnergyStorage.java | 54 ++++++++--- .../block/AbstractEnergyBlockEntity.java | 91 +++++++------------ .../disk_assembler/DiskAssemblerBlock.java | 9 +- .../DiskAssemblerBlockEntity.java | 34 ++++--- .../disk_assembler/DiskAssemblerMenu.java | 15 +-- 5 files changed, 105 insertions(+), 98 deletions(-) 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