From 3904238b4fbe4fb2569bcdf455607afbf5d08af4 Mon Sep 17 00:00:00 2001 From: Eugene Date: Sat, 6 Jul 2024 18:33:09 +0300 Subject: [PATCH] fix block facing compele behivor BrewingKegBlockEntity --- .../block/entity/BrewingKegBlockEntity.java | 64 +++++++++++++------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java b/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java index 6ca4687..6817ca8 100644 --- a/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java +++ b/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java @@ -75,12 +75,14 @@ public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreen private ItemStack drinkContainer = ItemStack.EMPTY; private float totalExperience = 0; + public final SingleVariantStorage fluidStorage = new SingleVariantStorage() { @Override protected FluidVariant getBlankVariant() { return FluidVariant.blank(); } + @Override protected long getCapacity(FluidVariant variant) { return FluidStack.convertDropletsToMb(FluidConstants.BUCKET) * 15; // 5k mB @@ -96,6 +98,7 @@ protected void onFinalCommit() { } }; + public BrewingKegBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.BREWING_KEG_BLOCK_ENTITY, pos, state); this.propertyDelegate = new PropertyDelegate() { @@ -132,6 +135,35 @@ public DefaultedList getItems() { return inventory; } + + public NbtCompound writeToNbtPublic(NbtCompound nbt) { + // Call the superclass method to ensure all necessary data is written + super.writeNbt(nbt); // Call super method to save base block entity data + + // Save DRINKS_DISPLAY_SLOT to NBT + NbtCompound displaySlotTag = new NbtCompound(); + this.getStack(DRINKS_DISPLAY_SLOT).writeNbt(displaySlotTag); + nbt.put("DisplaySlot", displaySlotTag); + + // Save water amount + nbt.putLong("brewing_keg.fluid_amount", fluidStorage.amount); + nbt.put("brewing_keg.fluid_variant", fluidStorage.variant.toNbt()); + + // Save container + NbtCompound containerTag = new NbtCompound(); + drinkContainer.writeNbt(containerTag); + nbt.put("Container", containerTag); + + // Save experience + nbt.putFloat("TotalExperience", totalExperience); + + // Return the modified NbtCompound + return nbt; + } + + + + @Override protected void writeNbt(NbtCompound nbt) { super.writeNbt(nbt); @@ -147,9 +179,14 @@ protected void writeNbt(NbtCompound nbt) { // Save DRINKS_DISPLAY_SLOT NbtCompound displaySlotTag = new NbtCompound(); - getStack(DRINKS_DISPLAY_SLOT).writeNbt(displaySlotTag); + ItemStack displayStack = getStack(DRINKS_DISPLAY_SLOT); + if (!displayStack.isEmpty()) { + displayStack.writeNbt(displaySlotTag); + } nbt.put("DisplaySlot", displaySlotTag); + // Save totalExperience + nbt.putFloat("TotalExperience", totalExperience); } @@ -171,14 +208,15 @@ public void readNbt(NbtCompound nbt) { } // Load DRINKS_DISPLAY_SLOT - NbtCompound displaySlotTag = nbt.getCompound("DisplaySlot"); - setStack(DRINKS_DISPLAY_SLOT, ItemStack.fromNbt(displaySlotTag)); - + if (nbt.contains("DisplaySlot", NbtType.COMPOUND)) { + NbtCompound displaySlotTag = nbt.getCompound("DisplaySlot"); + setStack(DRINKS_DISPLAY_SLOT, ItemStack.fromNbt(displaySlotTag)); + } else { + setStack(DRINKS_DISPLAY_SLOT, ItemStack.EMPTY); // Ensure it's clear if no DisplaySlot is found + } } - - private void debugFluidLevel() { System.out.println("Fluid: " + fluidStorage.variant.getFluid().toString()); System.out.println("Amount: " + fluidStorage.amount + " mB"); @@ -264,12 +302,6 @@ private void spawnExperienceOrb(World world, BlockPos pos, int xpAmount) { } - - - - - - private void processCrafting() { Optional match = getCurrentRecipe(); if (match.isPresent()) { @@ -295,9 +327,6 @@ private void processCrafting() { } - - - private boolean hasValidIngredients(BrewingRecipe recipe) { List ingredients = new ArrayList<>(); for (int slot : INGREDIENT_SLOTS) { @@ -489,9 +518,6 @@ private boolean craftItem(BrewingRecipe recipe, int craftedAmount) { } - - - private void extractFluid(int amount) { try (Transaction transaction = Transaction.openOuter()) { this.fluidStorage.extract(FluidVariant.of(Fluids.WATER), amount, transaction); @@ -534,5 +560,7 @@ public void markDirty() { public void trackRecipeExperience(float totalExperience) { this.totalExperience += totalExperience; + markDirty(); } + }