From 7060473b5d94043292409d9542d1681da1fac083 Mon Sep 17 00:00:00 2001 From: Maxx <53229958+MBatt1@users.noreply.github.com> Date: Sat, 23 Mar 2024 13:04:18 -0500 Subject: [PATCH] tree tap recipe chance --- .../blockentity/TreeTapBlockEntity.java | 12 +++++++-- .../id/paradiselost/recipe/TreeTapRecipe.java | 26 ++++++++++++------- .../recipe/TreeTapRecipeSerializer.java | 11 +++++--- .../features/ParadiseLostLakeFeature.java | 3 --- .../paradise_lost/recipes/tree_tap/water.json | 3 ++- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/id/paradiselost/blocks/blockentity/TreeTapBlockEntity.java b/src/main/java/net/id/paradiselost/blocks/blockentity/TreeTapBlockEntity.java index 21809935b..41a88f6ee 100644 --- a/src/main/java/net/id/paradiselost/blocks/blockentity/TreeTapBlockEntity.java +++ b/src/main/java/net/id/paradiselost/blocks/blockentity/TreeTapBlockEntity.java @@ -6,14 +6,18 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.HopperBlockEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.Packet; import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; import net.minecraft.server.world.ServerWorld; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; import net.minecraft.state.property.Properties; import net.minecraft.util.Hand; import net.minecraft.util.ItemScatterer; @@ -103,13 +107,17 @@ public void tryCraft() { } Optional recipe = this.world.getRecipeManager().getFirstMatch(ParadiseLostRecipeTypes.TREE_TAP_RECIPE_TYPE, this, this.world); - if (recipe.isPresent()) { + if (recipe.isPresent() && world.random.nextInt(recipe.get().getChance()) == 0) { ItemStack output = recipe.get().craft(this); stack.decrement(1); - // TODO: play a sound? + if (!world.isClient) world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 0.5f, world.getRandom().nextFloat() * 0.4f + 0.8f); this.inventory.set(0, ItemStack.EMPTY); inventoryChanged(); + BlockEntity possibleHopper = world.getBlockEntity(pos.down()); + if (possibleHopper instanceof HopperBlockEntity) { + output = HopperBlockEntity.transfer(this, (Inventory) possibleHopper, output, Direction.UP); + } ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), output); } } diff --git a/src/main/java/net/id/paradiselost/recipe/TreeTapRecipe.java b/src/main/java/net/id/paradiselost/recipe/TreeTapRecipe.java index 043da045a..2f41e1bf7 100644 --- a/src/main/java/net/id/paradiselost/recipe/TreeTapRecipe.java +++ b/src/main/java/net/id/paradiselost/recipe/TreeTapRecipe.java @@ -17,14 +17,16 @@ public class TreeTapRecipe implements Recipe { protected final Ingredient ingredient; protected final ItemStack output; - protected final Block tappedBlock; + protected final Block tappedBlock; + protected final int chance; - public TreeTapRecipe(Identifier id, String group, Ingredient ingredient, ItemStack output, Block tappedBlock) { + public TreeTapRecipe(Identifier id, String group, Ingredient ingredient, ItemStack output, Block tappedBlock, int chance) { this.id = id; this.group = group; this.ingredient = ingredient; this.output = output; - this.tappedBlock = tappedBlock; + this.tappedBlock = tappedBlock; + this.chance = chance; } @Override @@ -46,10 +48,10 @@ public boolean fits(int width, int height) { return true; } - @Override - public ItemStack getOutput() { - return output; - } + @Override + public ItemStack getOutput() { + return output; + } @Override public String getGroup() { @@ -57,9 +59,13 @@ public String getGroup() { } @Override - public Identifier getId() { - return id; - } + public Identifier getId() { + return id; + } + + public int getChance() { + return chance; + } @Override public RecipeSerializer getSerializer() { diff --git a/src/main/java/net/id/paradiselost/recipe/TreeTapRecipeSerializer.java b/src/main/java/net/id/paradiselost/recipe/TreeTapRecipeSerializer.java index c40fbe7e4..fac13b652 100644 --- a/src/main/java/net/id/paradiselost/recipe/TreeTapRecipeSerializer.java +++ b/src/main/java/net/id/paradiselost/recipe/TreeTapRecipeSerializer.java @@ -14,7 +14,7 @@ public record TreeTapRecipeSerializer(TreeTapRecipeSerializer.RecipeFactory recipeFactory) implements RecipeSerializer { public interface RecipeFactory { - TreeTapRecipe create(Identifier id, String group, Ingredient ingredient, ItemStack output, Block tappedBlock); + TreeTapRecipe create(Identifier id, String group, Ingredient ingredient, ItemStack output, Block tappedBlock, int chance); } @Override @@ -22,9 +22,10 @@ public TreeTapRecipe read(Identifier identifier, JsonObject jsonObject) { String group = JsonHelper.getString(jsonObject, "group", ""); Ingredient ingredient = Ingredient.fromJson(JsonHelper.getObject(jsonObject, "ingredient")); Block tappedBlock = Registry.BLOCK.get(Identifier.tryParse(JsonHelper.getString(jsonObject, "tapped_block"))); - ItemStack output = RecipeParser.getItemStackWithNbtFromJson(JsonHelper.getObject(jsonObject, "result")); + ItemStack output = RecipeParser.getItemStackWithNbtFromJson(JsonHelper.getObject(jsonObject, "result")); + int chance = JsonHelper.getInt(jsonObject, "chance"); - return this.recipeFactory.create(identifier, group, ingredient, output, tappedBlock); + return this.recipeFactory.create(identifier, group, ingredient, output, tappedBlock, chance); } @Override @@ -33,6 +34,7 @@ public void write(PacketByteBuf packetByteBuf, TreeTapRecipe recipe) { recipe.ingredient.write(packetByteBuf); packetByteBuf.writeIdentifier(Registry.BLOCK.getId(recipe.tappedBlock)); packetByteBuf.writeItemStack(recipe.output); + packetByteBuf.writeInt(recipe.chance); } @Override @@ -41,8 +43,9 @@ public TreeTapRecipe read(Identifier identifier, PacketByteBuf packetByteBuf) { Ingredient ingredient = Ingredient.fromPacket(packetByteBuf); Block tappedBlock = Registry.BLOCK.get(packetByteBuf.readIdentifier()); ItemStack output = packetByteBuf.readItemStack(); + int chance = packetByteBuf.readInt(); - return this.recipeFactory.create(identifier, group, ingredient, output, tappedBlock); + return this.recipeFactory.create(identifier, group, ingredient, output, tappedBlock, chance); } } diff --git a/src/main/java/net/id/paradiselost/world/feature/features/ParadiseLostLakeFeature.java b/src/main/java/net/id/paradiselost/world/feature/features/ParadiseLostLakeFeature.java index 165b1892a..aa80aea10 100644 --- a/src/main/java/net/id/paradiselost/world/feature/features/ParadiseLostLakeFeature.java +++ b/src/main/java/net/id/paradiselost/world/feature/features/ParadiseLostLakeFeature.java @@ -78,9 +78,6 @@ public boolean generate(FeatureContext context) { if (lakeEdge) { var state = context.getWorld().getBlockState(blockPos.add(xOff, yOff, zOff)); - if (state.isOf(ParadiseLostBlocks.AUREL_LEAF_PILE)) { - System.out.flush(); - } Material material = state.getMaterial(); // There is a liquid above the lake, abort diff --git a/src/main/resources/data/paradise_lost/recipes/tree_tap/water.json b/src/main/resources/data/paradise_lost/recipes/tree_tap/water.json index 469055eaf..3deb99702 100644 --- a/src/main/resources/data/paradise_lost/recipes/tree_tap/water.json +++ b/src/main/resources/data/paradise_lost/recipes/tree_tap/water.json @@ -7,5 +7,6 @@ "result": { "item": "minecraft:potion", "nbt": "{Potion: \"minecraft:water\"}" - } + }, + "chance": 1 }