From 7c531cdbef770b1fb1a4837908dc02b7c0637df1 Mon Sep 17 00:00:00 2001 From: Jottyfan Date: Sat, 7 Feb 2026 21:06:43 +0100 Subject: [PATCH] corrected drops on harvesting with special tools --- gradle.properties | 2 +- .../jottyfan/minecraft/block/BlockPlant.java | 15 ++-- .../minecraft/block/DropDefinition.java | 82 +++++++++++++++++++ .../minecraft/block/QuicklyBlocks.java | 6 +- .../java/de/jottyfan/minecraft/item/QHoe.java | 38 ++++++--- .../loot_table/blocks/blockcanolaplant.json | 56 +++++++++++++ .../loot_table/blocks/blockcottonplant.json | 55 +++++++++++++ 7 files changed, 231 insertions(+), 23 deletions(-) create mode 100644 src/main/java/de/jottyfan/minecraft/block/DropDefinition.java create mode 100644 src/main/resources/data/quickly/loot_table/blocks/blockcanolaplant.json create mode 100644 src/main/resources/data/quickly/loot_table/blocks/blockcottonplant.json diff --git a/gradle.properties b/gradle.properties index 2fc959a..5bc53bb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ loader_version=0.18.4 loom_version=1.15-SNAPSHOT # Mod Properties -mod_version=26.1.6 +mod_version=26.1.6.1 maven_group=de.jottyfan.minecraft archives_base_name=quickly diff --git a/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java b/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java index 7da88d1..e5c651b 100644 --- a/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java +++ b/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java @@ -2,7 +2,6 @@ package de.jottyfan.minecraft.block; import de.jottyfan.minecraft.item.QuicklyItems; import net.minecraft.core.BlockPos; -import net.minecraft.resources.Identifier; import net.minecraft.world.Containers; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; @@ -24,13 +23,13 @@ import net.minecraft.world.phys.shapes.VoxelShape; */ public class BlockPlant extends CropBlock { - private Identifier seedName; - private Identifier fruitName; + private DropDefinition seedDefinition; + private DropDefinition fruitDefinition; - public BlockPlant(Properties properties, Identifier seedName, Identifier fruitName) { + public BlockPlant(Properties properties, DropDefinition seedDefinition, DropDefinition fruitDefinition) { super(properties.noOcclusion().dynamicShape()); - this.seedName = seedName; - this.fruitName = fruitName; + this.seedDefinition = seedDefinition; + this.fruitDefinition = fruitDefinition; } @Override @@ -49,11 +48,11 @@ public class BlockPlant extends CropBlock { } public ItemLike getSeed() { - return QuicklyItems.of(seedName); + return QuicklyItems.of(seedDefinition.getIdentifier()); } public ItemLike getFruit() { - return QuicklyItems.of(fruitName); + return QuicklyItems.of(fruitDefinition.getIdentifier()); } @Override diff --git a/src/main/java/de/jottyfan/minecraft/block/DropDefinition.java b/src/main/java/de/jottyfan/minecraft/block/DropDefinition.java new file mode 100644 index 0000000..792693f --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/block/DropDefinition.java @@ -0,0 +1,82 @@ +package de.jottyfan.minecraft.block; + +import java.util.Random; + +import net.minecraft.resources.Identifier; + +/** + * + * @author jotty + * + */ +public class DropDefinition { + private Identifier identifier; + private Integer minValue; + private Integer maxValue; + + /** + * defines the drop definition; the item or block from identifier should be + * dropped x times (minValue <= x <= maxValue) + * + * @param identifier the identifier of the item or block + * @param minValue the minimum of dropped items or blocks + * @param maxValue the maximum of dropped items or blocks + */ + public static final DropDefinition of(Identifier identifier, Integer minValue, Integer maxValue) { + DropDefinition bean = new DropDefinition(); + bean.setIdentifier(identifier); + bean.setMinValue(minValue); + bean.setMaxValue(maxValue); + return bean; + } + + public int getRandomSize() { + if (maxValue - minValue < 1) { + return new Random().nextInt(maxValue); + } else { + return minValue + new Random().nextInt(maxValue - minValue); + } + } + + /** + * @return the identifier + */ + public Identifier getIdentifier() { + return identifier; + } + + /** + * @param identifier the identifier to set + */ + public void setIdentifier(Identifier identifier) { + this.identifier = identifier; + } + + /** + * @return the minValue + */ + public Integer getMinValue() { + return minValue; + } + + /** + * @param minValue the minValue to set + */ + public void setMinValue(Integer minValue) { + this.minValue = minValue; + } + + /** + * @return the maxValue + */ + public Integer getMaxValue() { + return maxValue; + } + + /** + * @param maxValue the maxValue to set + */ + public void setMaxValue(Integer maxValue) { + this.maxValue = maxValue; + } +} diff --git a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java index aeb05d8..c92b427 100644 --- a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java +++ b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java @@ -32,9 +32,11 @@ public class QuicklyBlocks { public static final Block OREDEEPSLATETURQUOISE = registerBlock(ID.OREDEEPSLATETURQUOISE, properties -> new BlockOre(properties, SoundEvents.AMETHYST_BLOCK_CHIME, ID.RAWTURQUOISE)); public static final Block COTTONPLANT = registerBlock(ID.BLOCKCOTTONPLANT, Properties.ofFullCopy(Blocks.WHEAT), - properties -> new BlockPlant(properties, ID.COTTONSEED, ID.COTTON)); + properties -> new BlockPlant(properties, DropDefinition.of(ID.COTTONSEED, 1, 1), + DropDefinition.of(ID.COTTON, 1, 1))); public static final Block CANOLAPLANT = registerBlock(ID.BLOCKCANOLAPLANT, Properties.ofFullCopy(Blocks.WHEAT), - properties -> new BlockPlant(properties, ID.CANOLASEED, ID.CANOLA)); + properties -> new BlockPlant(properties, DropDefinition.of(ID.CANOLASEED, 1, 1), + DropDefinition.of(ID.CANOLA, 1, 1))); public static final Block LAVAHOARDER = registerBlock(ID.LAVAHOARDER, Properties.of().strength(2.5f).lightLevel(state -> state.getValue(Lavahoarder.FILLED) ? 15 : 0), Lavahoarder::new); diff --git a/src/main/java/de/jottyfan/minecraft/item/QHoe.java b/src/main/java/de/jottyfan/minecraft/item/QHoe.java index a3cf26d..b412b57 100644 --- a/src/main/java/de/jottyfan/minecraft/item/QHoe.java +++ b/src/main/java/de/jottyfan/minecraft/item/QHoe.java @@ -6,8 +6,10 @@ import com.google.common.collect.Lists; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.server.level.ServerLevel; import net.minecraft.tags.BlockTags; import net.minecraft.tags.ItemTags; +import net.minecraft.world.Containers; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.HoeItem; import net.minecraft.world.item.ItemStack; @@ -18,6 +20,8 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.CropBlock; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.storage.loot.LootParams; +import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.phys.BlockHitResult; /** @@ -26,18 +30,19 @@ import net.minecraft.world.phys.BlockHitResult; * */ public class QHoe extends HoeItem implements ToolRangeable { - public QIP properties; public QHoe(QIP properties) { - super(new ToolMaterial(BlockTags.INCORRECT_FOR_DIAMOND_TOOL, properties.getDurability(), 7f, 1f, 15, ItemTags.DIAMOND_TOOL_MATERIALS), 7f, 3.1f, properties); + super(new ToolMaterial(BlockTags.INCORRECT_FOR_DIAMOND_TOOL, properties.getDurability(), 7f, 1f, 15, + ItemTags.DIAMOND_TOOL_MATERIALS), 7f, 3.1f, properties); this.properties = properties; } @Override - public InteractionResult useOn(UseOnContext context) { - InteractionResult res = super.useOn(context); - boolean isCrop = context.getLevel().getBlockState(context.getClickedPos()).getBlock() instanceof CropBlock; + public InteractionResult useOn(UseOnContext context) { + InteractionResult res = super.useOn(context); + Block block = context.getLevel().getBlockState(context.getClickedPos()).getBlock(); + boolean isCrop = block instanceof CropBlock; if (!InteractionResult.PASS.equals(res) || isCrop) { HarvestRange range = properties.getHarvestRange(); for (int x = -range.getxRange(); x <= range.getxRange(); x++) { @@ -55,6 +60,7 @@ public class QHoe extends HoeItem implements ToolRangeable { } } } + return InteractionResult.SUCCESS; } return res; } @@ -69,13 +75,21 @@ public class QHoe extends HoeItem implements ToolRangeable { } private void harvestIfPossible(BlockPos pos, Level level) { - BlockState blockState = level.getBlockState(pos); - Block block = blockState.getBlock(); - if (block instanceof CropBlock) { - CropBlock cBlock = (CropBlock) block; - if (cBlock.isMaxAge(blockState)) { - Block.dropResources(blockState, level, pos); - level.setBlockAndUpdate(pos, cBlock.defaultBlockState()); + if (!level.isClientSide()) { + BlockState blockState = level.getBlockState(pos); + Block block = blockState.getBlock(); + if (block instanceof CropBlock cBlock) { + if (cBlock.isMaxAge(blockState)) { + LootParams.Builder builder = new LootParams.Builder((ServerLevel) level) + .withParameter(LootContextParams.ORIGIN, pos.getCenter()) + .withParameter(LootContextParams.BLOCK_STATE, blockState) + .withOptionalParameter(LootContextParams.BLOCK_ENTITY, level.getBlockEntity(pos)) + .withParameter(LootContextParams.TOOL, ItemStack.EMPTY); + for (ItemStack stack : blockState.getDrops(builder)) { + Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), stack); + } + level.setBlockAndUpdate(pos, cBlock.defaultBlockState()); + } } } } diff --git a/src/main/resources/data/quickly/loot_table/blocks/blockcanolaplant.json b/src/main/resources/data/quickly/loot_table/blocks/blockcanolaplant.json new file mode 100644 index 0000000..95609d6 --- /dev/null +++ b/src/main/resources/data/quickly/loot_table/blocks/blockcanolaplant.json @@ -0,0 +1,56 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "quickly:canola", + "functions": [ + { + "function": "minecraft:set_count", + "count": { + "min": 1, + "max": 2 + }, + "add": false + } + ], + "conditions": [ + { + "condition": "minecraft:location_check", + "predicate": { + "block": { + "properties": { + "age": "7" + } + } + } + } + ] + } + ] + }, + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "quickly:canolaseed", + "functions": [ + { + "function": "minecraft:apply_bonus", + "enchantment": "minecraft:fortune", + "formula": "minecraft:binomial_with_bonus_count", + "parameters": { + "extra": 2, + "probability": 0.5 + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/data/quickly/loot_table/blocks/blockcottonplant.json b/src/main/resources/data/quickly/loot_table/blocks/blockcottonplant.json new file mode 100644 index 0000000..c363114 --- /dev/null +++ b/src/main/resources/data/quickly/loot_table/blocks/blockcottonplant.json @@ -0,0 +1,55 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "quickly:cotton", + "functions": [ + { + "function": "minecraft:set_count", + "count": { + "min": 1, + "max": 3 + } + } + ], + "conditions": [ + { + "condition": "minecraft:location_check", + "predicate": { + "block": { + "properties": { + "age": "7" + } + } + } + } + ] + } + ] + }, + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "quickly:cottonseed", + "functions": [ + { + "function": "minecraft:apply_bonus", + "enchantment": "minecraft:fortune", + "formula": "minecraft:binomial_with_bonus_count", + "parameters": { + "extra": 2, + "probability": 0.5 + } + } + ] + } + ] + } + ] +} \ No newline at end of file