From 08aceef72e41019e9d24127f96f1be49d8a50192 Mon Sep 17 00:00:00 2001 From: direwolf20 <39863894+Direwolf20-MC@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:41:38 -0400 Subject: [PATCH] Add support for more crops on the soil --- .../common/blocks/soil/GooSoilBase.java | 82 +++++++++++++++++-- .../resources/META-INF/accesstransformer.cfg | 3 +- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/direwolf20/justdirethings/common/blocks/soil/GooSoilBase.java b/src/main/java/com/direwolf20/justdirethings/common/blocks/soil/GooSoilBase.java index 9e75c83f..392c60ff 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/blocks/soil/GooSoilBase.java +++ b/src/main/java/com/direwolf20/justdirethings/common/blocks/soil/GooSoilBase.java @@ -120,9 +120,65 @@ public static void bonemealMe(ServerLevel pLevel, BlockPos pPos) { } } + public static List harvestStemCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) { + List drops = new ArrayList<>(); + if (crop.getBlock() instanceof AttachedStemBlock attachedStemBlock) { + for (Direction direction : Direction.values()) { + BlockPos testPos = cropPos.relative(direction); + BlockState fruitState = pLevel.getBlockState(testPos); + if (fruitState.is(attachedStemBlock.fruit)) { + BlockEntity blockEntity = pLevel.getBlockEntity(cropPos); + drops.addAll(Block.getDrops(fruitState, pLevel, testPos, blockEntity)); + pLevel.destroyBlock(testPos, false); + } + } + } + return drops; + } + + public static List harvest2TallCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) { + List drops = new ArrayList<>(); + if (crop.getBlock() instanceof SugarCaneBlock || crop.getBlock() instanceof CactusBlock || crop.is(Blocks.BAMBOO)) { + List posToCheck = new ArrayList<>(); + for (int i = 0; i < 10; i++) { //In case it grew a lot since last check + BlockPos pos = cropPos.above(i); + if (pLevel.getBlockState(pos).is(crop.getBlock())) + posToCheck.add(pos); + else + break; + } + for (int i = posToCheck.size() - 1; i >= 0; i--) { + BlockPos clearPos = posToCheck.get(i); + BlockEntity blockEntity = pLevel.getBlockEntity(clearPos); + drops.addAll(Block.getDrops(pLevel.getBlockState(clearPos), pLevel, clearPos, blockEntity)); + pLevel.destroyBlock(clearPos, false); + } + } + return drops; + } + public static List harvestCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) { List drops = new ArrayList<>(); - if (crop.getBlock() instanceof CropBlock cropBlock) { + if (crop.getBlock() instanceof BushBlock) { + BlockEntity blockEntity = pLevel.getBlockEntity(cropPos); + List potentialDrops = Block.getDrops(crop, pLevel, cropPos, blockEntity); + if (potentialDrops.isEmpty()) + return drops; + if (potentialDrops.size() > 1 || potentialDrops.get(0).getCount() > 1) { + BlockState placeState = Blocks.AIR.defaultBlockState(); + drops.addAll(potentialDrops); + for (ItemStack drop : drops) { + if (drop.getItem() instanceof BlockItem blockItem) { + placeState = blockItem.getBlock().defaultBlockState(); + drop.shrink(1); + break; + } + } + pLevel.destroyBlock(cropPos, false); + pLevel.setBlockAndUpdate(cropPos, placeState); + } + } + /*if (crop.getBlock() instanceof CropBlock cropBlock) { if (cropBlock.isMaxAge(crop)) { BlockState placeState = Blocks.AIR.defaultBlockState(); BlockEntity blockEntity = pLevel.getBlockEntity(cropPos); @@ -165,7 +221,7 @@ public static List harvestCrop(ServerLevel pLevel, BlockPos cropPos, drops.addAll(Block.getDrops(pLevel.getBlockState(clearPos), pLevel, clearPos, blockEntity)); pLevel.destroyBlock(clearPos, false); } - } + }*/ return drops; } @@ -193,17 +249,27 @@ public static void dropDrops(ServerLevel pLevel, List drops, BlockPos public static void autoHarvest(ServerLevel pLevel, BlockPos pPos) { BlockPos cropPos = pPos.above(); BlockState crop = pLevel.getBlockState(cropPos); - if (crop.getBlock() instanceof CropBlock || crop.is(Blocks.NETHER_WART)) { - List drops = harvestCrop(pLevel, cropPos, crop); - if (!drops.isEmpty()) { - teleportDrops(pLevel, pPos, drops, cropPos); - dropDrops(pLevel, drops, cropPos); + if (crop.getBlock() instanceof BushBlock) { + if (crop.getBlock() instanceof AttachedStemBlock) { + List drops = harvestStemCrop(pLevel, cropPos, crop); + if (!drops.isEmpty()) { + teleportDrops(pLevel, pPos, drops, cropPos); + dropDrops(pLevel, drops, cropPos); + } + } else if (crop.getBlock() instanceof StemBlock) { + //No-Op - Stem Blocks are fully grown without an attached fruit, so ignore them + } else { + List drops = harvestCrop(pLevel, cropPos, crop); + if (!drops.isEmpty()) { + teleportDrops(pLevel, pPos, drops, cropPos); + dropDrops(pLevel, drops, cropPos); + } } } else if (crop.getBlock() instanceof SugarCaneBlock || crop.getBlock() instanceof CactusBlock || crop.is(Blocks.BAMBOO)) { BlockPos secondPos = cropPos.above(); BlockState secondState = pLevel.getBlockState(secondPos); if (secondState.is(crop.getBlock())) { - List drops = harvestCrop(pLevel, secondPos, secondState); + List drops = harvest2TallCrop(pLevel, secondPos, secondState); if (!drops.isEmpty()) { teleportDrops(pLevel, pPos, drops, secondPos); dropDrops(pLevel, drops, secondPos); diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 9bbde6e9..5c579096 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -23,4 +23,5 @@ public net.minecraft.world.inventory.AbstractContainerMenu remoteSlots # remoteS public net.minecraft.world.entity.projectile.AbstractArrow piercingIgnoreEntityIds # piercingIgnoreEntityIds public net.minecraft.world.entity.projectile.AbstractArrow piercedAndKilledEntities # piercedAndKilledEntities public net.minecraft.world.entity.projectile.AbstractArrow setPierceLevel(B)V # setPierceLevel -public net.minecraft.client.renderer.entity.LivingEntityRenderer layers # layers \ No newline at end of file +public net.minecraft.client.renderer.entity.LivingEntityRenderer layers # layers +public net.minecraft.world.level.block.AttachedStemBlock fruit # fruit \ No newline at end of file