From 7e02b550bb384eef2d59e43b413228189c1764d6 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 6 Aug 2024 17:58:34 +0300 Subject: [PATCH] new craft system --- .../block/custom/BrewingKegBlock.java | 51 ++++++---------- .../block/entity/BrewingKegBlockEntity.java | 61 +++++++++++++++---- .../screen/BrewingKegScreen.java | 26 -------- 3 files changed, 68 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/megatrex4/ukrainian_dlight/block/custom/BrewingKegBlock.java b/src/main/java/com/megatrex4/ukrainian_dlight/block/custom/BrewingKegBlock.java index c800c3d..815eb1c 100644 --- a/src/main/java/com/megatrex4/ukrainian_dlight/block/custom/BrewingKegBlock.java +++ b/src/main/java/com/megatrex4/ukrainian_dlight/block/custom/BrewingKegBlock.java @@ -209,17 +209,6 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt } - -// I refused to use this, everything will be better saved in the block inventory NBT data - -// private void dropSlotContents(World world, BlockPos pos, BrewingKegBlockEntity brewingKegEntity, int slot) { -// ItemStack stack = brewingKegEntity.getStack(slot); -// if (!stack.isEmpty()) { -// ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), stack); -// brewingKegEntity.setStack(slot, ItemStack.EMPTY); -// } -// } - public static void spawnParticles(World world, BlockPos pos, BlockState state) { Random random = world.random; if (world != null) { @@ -239,9 +228,7 @@ public static void spawnParticles(World world, BlockPos pos, BlockState state) { @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { ItemStack heldStack = player.getStackInHand(hand); - BrewingKegBlockEntity blockEntity = (BrewingKegBlockEntity) world.getBlockEntity(pos); if (!world.isClient() && world.getBlockEntity(pos) instanceof BrewingKegBlockEntity brewingKegBlockEntity) { - // Use the instance of BrewingKegBlockEntity to call the method ItemStack serving = brewingKegBlockEntity.useHeldItemOnDrink(heldStack); if (serving != ItemStack.EMPTY) { @@ -249,33 +236,35 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt player.dropItem(serving, false); } world.playSound(null, pos, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, SoundCategory.BLOCKS, 1.f, 1.f); + return ActionResult.SUCCESS; } - // if player held item water bucket add 1000 mb to brewingKegBlockEntity - if (heldStack.isOf(Items.WATER_BUCKET)) { - long waterAmount = blockEntity.getWaterAmount(); - long waterCapacity = blockEntity.getWaterCapacity(); - if (waterAmount + 1000 <= waterCapacity) { - blockEntity.addWater(1000); - if (!world.isClient) { - world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - } - if (!player.isCreative()) { - player.setStackInHand(hand, new ItemStack(Items.BUCKET)); - } - return ActionResult.SUCCESS; - } - } else { - NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos); - if (screenHandlerFactory != null) { - player.openHandledScreen(screenHandlerFactory); + + if (heldStack.isOf(Items.WATER_BUCKET)) { + long waterAmount = brewingKegBlockEntity.getWaterAmount(); + long waterCapacity = brewingKegBlockEntity.getWaterCapacity(); + if (waterAmount + 1000 <= waterCapacity) { + brewingKegBlockEntity.addWater(1000); + world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); + if (!player.isCreative()) { + player.setStackInHand(hand, new ItemStack(Items.BUCKET)); } + return ActionResult.SUCCESS; } + } else { + NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos); + if (screenHandlerFactory != null) { + // Open the screen handler and set the container if needed + player.openHandledScreen(screenHandlerFactory); + } + } return ActionResult.SUCCESS; } return ActionResult.SUCCESS; } + + private boolean giveItemToPlayerOrDrop(World world, PlayerEntity player, ItemStack itemStack) { if (!player.getInventory().insertStack(itemStack)) { ItemEntity itemEntity = new ItemEntity(world, player.getX(), player.getY(), player.getZ(), itemStack); 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 1b5e6bd..2b61b0b 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 @@ -11,7 +11,7 @@ import com.megatrex4.ukrainian_dlight.util.FluidStack; - +import com.nhoryzon.mc.farmersdelight.entity.block.CookingPotBlockEntity; import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; @@ -82,6 +82,8 @@ public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreen private int maxProgress = 200; // Adjusted to match the brewing time in the JSON private Text customName; private ItemStack drinkContainer; + private ItemStack lastContainer; + @Override @@ -128,9 +130,10 @@ public int get(int index) { @Override public void set(int index, int value) { - switch (index) { - case 0 -> BrewingKegBlockEntity.this.progress = value; - case 1 -> BrewingKegBlockEntity.this.maxProgress = value; + if (index == 0) { + BrewingKegBlockEntity.this.progress = value; + } else if (index == 1) { + BrewingKegBlockEntity.this.maxProgress = value; } } @@ -141,6 +144,7 @@ public int size() { }; } + public long getMaxWaterLevel() { return fluidStorage.getCapacity(); } @@ -208,8 +212,14 @@ public void writeNbt(NbtCompound tag) { } tag.put("DisplaySlot", displaySlotTag); - tag.put(CompoundTagUtils.TAG_KEY_CONTAINER, drinkContainer.writeNbt(new NbtCompound())); - System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: " + drinkContainer); + if (!drinkContainer.isEmpty()) { + NbtCompound containerTag = new NbtCompound(); + drinkContainer.writeNbt(containerTag); + tag.put("DrinkContainer", containerTag); + System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: " + drinkContainer.getItem() + " " + drinkContainer.getName().getString()); + } else { + System.out.println("BrewingKegBlockEntity: writeNbt drinkContainer: EMPTY"); + } writeInventoryNbt(tag); NbtCompound compoundRecipes = new NbtCompound(); @@ -233,9 +243,14 @@ public void readNbt(NbtCompound tag) { Inventories.readNbt(tag, inventory); progress = tag.getInt("progress"); - drinkContainer = ItemStack.fromNbt(tag.getCompound(CompoundTagUtils.TAG_KEY_CONTAINER)); - // debug log drink container - System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: " + drinkContainer); + if (tag.contains("DrinkContainer", NbtType.COMPOUND)) { + NbtCompound containerTag = tag.getCompound("DrinkContainer"); + drinkContainer = ItemStack.fromNbt(containerTag); + System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: " + drinkContainer.getItem() + " " + drinkContainer.getName().getString()); + } else { + drinkContainer = ItemStack.EMPTY; + System.out.println("BrewingKegBlockEntity: readNbt drinkContainer: EMPTY"); + } // Load DRINKS_DISPLAY_SLOT if (tag.contains("DisplaySlot", NbtType.COMPOUND)) { @@ -333,11 +348,10 @@ public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEnt private boolean hasInput() { for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) { - if (!getStack(i).isEmpty()) { + if (i != WATER_SLOT && i != CONTAINER_SLOT && !getStack(i).isEmpty()) { return true; } } - return false; } @@ -385,7 +399,9 @@ private boolean processBrewing(BrewingRecipe recipe) { } trackRecipeExperience(recipe); this.extractFluid(recipe.getWaterAmount()); + for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) { + if (i == WATER_SLOT || i == CONTAINER_SLOT) continue; ItemStack itemStack = getStack(i); if (itemStack.getItem().hasRecipeRemainder() && world != null) { Direction direction = getCachedState().get(BrewingKegBlock.FACING).rotateYCounterclockwise(); @@ -447,16 +463,26 @@ public ItemStack getDrinkContainer() { } } + private void setDrinkContainer(ItemStack container) { + this.drinkContainer = container; + markDirty(); // Ensure that the block entity state is updated + } + + public ItemStack useHeldItemOnDrink(ItemStack container) { if (isContainerValid(container) && !getDrink().isEmpty()) { - container.decrement(1); - return getDrink().split(1); + ItemStack drink = getDrink().split(1); + // Update the drink container to reflect the new state + setDrinkContainer(container); + return drink; } return ItemStack.EMPTY; } + + public void handleWaterBucket() { ItemStack waterBucketStack = this.getStack(WATER_SLOT); @@ -592,6 +618,15 @@ private void moveDrinkToOutput() { } + public ItemStack getLastUsedContainer() { + // Ensure the container is updated correctly + return drinkContainer; + } + + + + + public void trackRecipeExperience(@Nullable Recipe recipe) { if (recipe != null) { Identifier recipeID = recipe.getId(); diff --git a/src/main/java/com/megatrex4/ukrainian_dlight/screen/BrewingKegScreen.java b/src/main/java/com/megatrex4/ukrainian_dlight/screen/BrewingKegScreen.java index 62a8bd0..279bec8 100644 --- a/src/main/java/com/megatrex4/ukrainian_dlight/screen/BrewingKegScreen.java +++ b/src/main/java/com/megatrex4/ukrainian_dlight/screen/BrewingKegScreen.java @@ -105,37 +105,11 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { @Override protected void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) { if (this.handler.getCursorStack().isEmpty() && this.focusedSlot != null && this.focusedSlot.hasStack()) { - if (focusedSlot.id == BrewingKegBlockEntity.DRINKS_DISPLAY_SLOT) { - List tooltip = new ArrayList<>(); - - ItemStack drink = focusedSlot.getStack(); - Text text = drink.getName(); - if (text instanceof MutableText mutableName) { - tooltip.add(mutableName.formatted(drink.getRarity().formatting)); - } else { - tooltip.add(text); - } - drink.getItem().appendTooltip(drink, handler.blockEntity.getWorld(), tooltip, TooltipContext.Default.BASIC); - - // get container name - ItemStack containerItem = this.handler.blockEntity.getDrinkContainer(); - String container = !containerItem.isEmpty() ? containerItem.getItem().getName().getString() : ""; - - tooltip.add(UkrainianDelight.i18n("tooltip.poured", container).formatted(Formatting.GRAY)); - - context.drawTooltip(textRenderer, tooltip, mouseX, mouseY); - } else { context.drawItemTooltip(textRenderer, focusedSlot.getStack(), mouseX, mouseY); - } } } - - - - - protected void drawMouseoverTankTooltip(DrawContext context, int mouseX, int mouseY) { int x = (width - backgroundWidth) / 2; int y = (height - backgroundHeight) / 2;