diff --git a/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java b/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java index f0d4f50..79d888c 100644 --- a/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java +++ b/src/main/java/de/jottyfan/minecraft/block/BlockPlant.java @@ -1,8 +1,6 @@ package de.jottyfan.minecraft.block; -import java.util.List; -import java.util.Random; - +import de.jottyfan.minecraft.Quickly; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.Identifier; @@ -14,7 +12,6 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.CropBlock; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.storage.loot.LootParams.Builder; import net.minecraft.world.phys.BlockHitResult; /** @@ -24,37 +21,29 @@ import net.minecraft.world.phys.BlockHitResult; */ public class BlockPlant extends CropBlock { - private Identifier seed; - private Identifier fruit; + private String seedName; + private String fruitName; - public BlockPlant(Properties properties, Identifier seed, Identifier fruit) { + public BlockPlant(Properties properties, String seedName, String fruitName) { super(properties); - this.seed = seed; - this.fruit = fruit; - } - - @Override - protected List getDrops(BlockState state, Builder builder) { - List list = List.of(new ItemStack(getSeed())); - if (isMaxAge(state)) { - list.add(new ItemStack(getSeed(), new Random().nextInt(2))); - list.add(new ItemStack(getFruit(), new Random().nextFloat() > 0.9f ? 2 : 1)); - } - return list; - } - - private Item getFruit() { - return BuiltInRegistries.ITEM.getValue(fruit); + this.seedName = seedName; + this.fruitName = fruitName; } public Item getSeed() { - return BuiltInRegistries.ITEM.getValue(seed); + return BuiltInRegistries.ITEM.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, seedName)); + } + + public Item getFruit() { + return BuiltInRegistries.ITEM.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, fruitName)); } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { if (!level.isClientSide() && isMaxAge(state)) { + int fruitAmount = (level.getRandom().nextFloat() > 0.9f) ? 2 : 1; + Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getFruit(), fruitAmount)); Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getSeed())); level.setBlock(pos, state.setValue(AGE, 0), 2); return InteractionResult.SUCCESS; diff --git a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java index 79cc794..1d4c807 100644 --- a/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java +++ b/src/main/java/de/jottyfan/minecraft/block/QuicklyBlocks.java @@ -30,7 +30,7 @@ public class QuicklyBlocks { public static final Block OREDEEPSLATETURQUOISE = registerBlock("oredeepslateturquoise", p -> new BlockOreDeepslateTurquoise(p)); public static final Block COTTONPLANT = registerBlock("blockcottonplant", Properties.ofFullCopy(Blocks.WHEAT), - p -> new BlockPlant(p, Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cottonseed"), Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cotton"))); + p -> new BlockPlant(p, "cottonseed", "cotton")); private static final Block registerBlock(String name, Properties properties) { return QuicklyBlocks.registerBlock(name, properties, p -> new Block(p)); diff --git a/src/main/java/de/jottyfan/minecraft/item/Plant.java b/src/main/java/de/jottyfan/minecraft/item/Plant.java index d851769..d3c615a 100644 --- a/src/main/java/de/jottyfan/minecraft/item/Plant.java +++ b/src/main/java/de/jottyfan/minecraft/item/Plant.java @@ -1,5 +1,6 @@ package de.jottyfan.minecraft.item; +import de.jottyfan.minecraft.Quickly; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.Identifier; @@ -17,11 +18,15 @@ import net.minecraft.world.level.block.state.BlockState; */ public class Plant extends Item { - private Identifier identifier; + private String plantName; - public Plant(Properties properties, Identifier identifier) { + public Plant(Properties properties, String plantName) { super(properties); - this.identifier = identifier; + this.plantName = plantName; + } + + private Block getPlant() { + return BuiltInRegistries.BLOCK.getValue(Identifier.fromNamespaceAndPath(Quickly.MOD_ID, plantName)); } @Override @@ -30,8 +35,7 @@ public class Plant extends Item { if (this.asItem().equals(context.getItemInHand().getItem())) { BlockState state = context.getLevel().getBlockState(pos); if (Blocks.FARMLAND.equals(state.getBlock()) && context.getLevel().getBlockState(pos.above()).isAir()) { - Block plantBlock = BuiltInRegistries.BLOCK.getValue(identifier); - context.getLevel().setBlock(pos.above(), plantBlock.defaultBlockState(), 2); + context.getLevel().setBlock(pos.above(), getPlant().defaultBlockState(), 2); context.getItemInHand().shrink(1); } } diff --git a/src/main/java/de/jottyfan/minecraft/item/QuicklyItems.java b/src/main/java/de/jottyfan/minecraft/item/QuicklyItems.java index 1234c39..5167e22 100644 --- a/src/main/java/de/jottyfan/minecraft/item/QuicklyItems.java +++ b/src/main/java/de/jottyfan/minecraft/item/QuicklyItems.java @@ -24,7 +24,7 @@ public class QuicklyItems { public static final Item TURQUOISEINGOT = registerItem("turquoiseingot"); public static final Item COTTON = registerItem("cotton"); public static final Item COTTONPLANT = registerItem("cottonplant"); - public static final Item COTTONSEED = registerItem("cottonseed", properties -> new Plant(properties, Identifier.fromNamespaceAndPath(Quickly.MOD_ID, "cottonplant"))); + public static final Item COTTONSEED = registerItem("cottonseed", properties -> new Plant(properties, "blockcottonplant")); private static final Item registerItem(String name) { return QuicklyItems.registerItem(name, new Item.Properties());