Skip to content

Commit

Permalink
Add support for more crops on the soil
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Oct 18, 2024
1 parent 3d82566 commit 08aceef
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,65 @@ public static void bonemealMe(ServerLevel pLevel, BlockPos pPos) {
}
}

public static List<ItemStack> harvestStemCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) {
List<ItemStack> 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<ItemStack> harvest2TallCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) {
List<ItemStack> drops = new ArrayList<>();
if (crop.getBlock() instanceof SugarCaneBlock || crop.getBlock() instanceof CactusBlock || crop.is(Blocks.BAMBOO)) {
List<BlockPos> 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<ItemStack> harvestCrop(ServerLevel pLevel, BlockPos cropPos, BlockState crop) {
List<ItemStack> drops = new ArrayList<>();
if (crop.getBlock() instanceof CropBlock cropBlock) {
if (crop.getBlock() instanceof BushBlock) {
BlockEntity blockEntity = pLevel.getBlockEntity(cropPos);
List<ItemStack> 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);
Expand Down Expand Up @@ -165,7 +221,7 @@ public static List<ItemStack> harvestCrop(ServerLevel pLevel, BlockPos cropPos,
drops.addAll(Block.getDrops(pLevel.getBlockState(clearPos), pLevel, clearPos, blockEntity));
pLevel.destroyBlock(clearPos, false);
}
}
}*/

return drops;
}
Expand Down Expand Up @@ -193,17 +249,27 @@ public static void dropDrops(ServerLevel pLevel, List<ItemStack> 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<ItemStack> 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<ItemStack> 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<ItemStack> 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<ItemStack> drops = harvestCrop(pLevel, secondPos, secondState);
List<ItemStack> drops = harvest2TallCrop(pLevel, secondPos, secondState);
if (!drops.isEmpty()) {
teleportDrops(pLevel, pPos, drops, secondPos);
dropDrops(pLevel, drops, secondPos);
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
public net.minecraft.client.renderer.entity.LivingEntityRenderer layers # layers
public net.minecraft.world.level.block.AttachedStemBlock fruit # fruit

0 comments on commit 08aceef

Please sign in to comment.