From d213bc95dd4e11a337fc86ffc2248c3da7ff293c Mon Sep 17 00:00:00 2001 From: wolfieboy09 <105564186+wolfieboy09@users.noreply.github.com> Date: Tue, 29 Oct 2024 18:28:04 -0400 Subject: [PATCH] yarrhahhahar more thingies --- .../block/AbstractEnergyBlockEntity.java | 53 +++++++++++++++---- .../DiskAssemblerBlockEntity.java | 34 ++++++++++++ 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java b/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java index 9d8abc0..906f5e2 100644 --- a/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java +++ b/src/main/java/dev/wolfieboy09/qstorage/block/AbstractEnergyBlockEntity.java @@ -86,14 +86,6 @@ public int getMaxEnergyStored() { return this.energyStorage.getMaxEnergyStored(); } - @Override - protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) { - super.loadAdditional(tag, registries); - if (tag.contains("Energy")) { - this.energyStorage.deserializeNBT(registries, tag.get("Energy")); - } - } - @Override public int receiveEnergy(int i, boolean b) { return this.energyStorage.receiveEnergy(i, b); @@ -104,24 +96,67 @@ public int extractEnergy(int i, boolean b) { return this.energyStorage.extractEnergy(i, b); } + // optional functions for blocks + + /** + * Gets called from {@link #saveAdditional} + * @param tag + * @param registries + */ + public void saveExtra(CompoundTag tag, HolderLookup.Provider registries) {} + + /** + * Gets called from {@link #loadAdditional} + * @param tag + * @param registries + */ + protected void loadExtra(CompoundTag tag, HolderLookup.Provider registries) {} + + /** + * Gets called from {@link #getUpdateTag} + * @param tag + * @param lookupProvider + * @return + */ + public CompoundTag updateTag(CompoundTag tag, HolderLookup.Provider lookupProvider) { return tag; } + + /** + * Gets called from {@link #handleUpdateTag} + * @param tag + * @param lookupProvider + */ + public void handleUpdate(CompoundTag tag, HolderLookup.Provider lookupProvider) {} + + @Override protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) { super.saveAdditional(tag, registries); tag.put("Energy", this.energyStorage.serializeNBT(registries)); + saveExtra(tag, registries); } @Override public @NotNull CompoundTag getUpdateTag(HolderLookup.Provider registries) { - CompoundTag tag = super.getUpdateTag(registries); + CompoundTag tag = updateTag(super.getUpdateTag(registries), registries); tag.put("Energy", this.energyStorage.serializeNBT(registries)); return tag; } + @Override + protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) { + super.loadAdditional(tag, registries); + if (tag.contains("Energy")) { + this.energyStorage.deserializeNBT(registries, tag.get("Energy")); + } + 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() { 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 73e826a..ce775c3 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,6 +4,8 @@ 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.SimpleContainer; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; @@ -12,8 +14,10 @@ import net.neoforged.neoforge.items.ItemStackHandler; import org.jetbrains.annotations.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.util.Objects; +@ParametersAreNonnullByDefault public class DiskAssemblerBlockEntity extends AbstractEnergyBlockEntity { private int progress = 0; private int crafting_ticks = 0; @@ -114,4 +118,34 @@ public SimpleContainer getOutputContainer() { container.setItem(8, inventory.getStackInSlot(8)); return container; } + + @Override + public void saveExtra(CompoundTag tag, HolderLookup.Provider registries) { + super.saveExtra(tag, registries); + tag.putInt("energy_required", this.energy_required); + tag.putInt("crafting_ticks", this.crafting_ticks); + tag.put("inventory", this.inventory.serializeNBT(registries)); + } + + @Override + protected void loadExtra(CompoundTag tag, HolderLookup.Provider registries) { + super.loadExtra(tag, registries); + this.energy_required = tag.getInt("energy_required"); + this.crafting_ticks = tag.getInt("crafting_ticks"); + this.inventory.deserializeNBT(registries, tag.getCompound("inventory")); + } + + @Override + public CompoundTag updateTag(CompoundTag tag, HolderLookup.Provider lookupProvider) { + tag.putInt("energy_required", this.energy_required); + tag.putInt("crafting_ticks", this.crafting_ticks); + return super.updateTag(tag, lookupProvider); + } + + @Override + public void handleUpdate(CompoundTag tag, HolderLookup.Provider lookupProvider) { + super.handleUpdate(tag, lookupProvider); + this.energy_required = tag.getInt("energy_required"); + this.crafting_ticks = tag.getInt("crafting_ticks"); + } }