Skip to content

Commit

Permalink
tree tap recipe chance
Browse files Browse the repository at this point in the history
  • Loading branch information
MBatt1 committed Mar 23, 2024
1 parent 806a9ff commit 7060473
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,13 +107,17 @@ public void tryCraft() {
}

Optional<TreeTapRecipe> 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);
}
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/net/id/paradiselost/recipe/TreeTapRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ public class TreeTapRecipe implements Recipe<TreeTapBlockEntity> {

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
Expand All @@ -46,20 +48,24 @@ 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() {
return group;
}

@Override
public Identifier getId() {
return id;
}
public Identifier getId() {
return id;
}

public int getChance() {
return chance;
}

@Override
public RecipeSerializer<?> getSerializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
public record TreeTapRecipeSerializer(TreeTapRecipeSerializer.RecipeFactory recipeFactory) implements RecipeSerializer<TreeTapRecipe> {

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
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
Expand All @@ -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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ public boolean generate(FeatureContext<DynamicConfiguration> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"result": {
"item": "minecraft:potion",
"nbt": "{Potion: \"minecraft:water\"}"
}
},
"chance": 1
}

0 comments on commit 7060473

Please sign in to comment.