|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.hacks.autofarm.plants; |
| 9 | + |
| 10 | +import java.util.EnumMap; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Map.Entry; |
| 14 | + |
| 15 | +import com.google.common.collect.Maps; |
| 16 | + |
| 17 | +import net.minecraft.block.BlockState; |
| 18 | +import net.minecraft.block.Blocks; |
| 19 | +import net.minecraft.block.ChorusFlowerBlock; |
| 20 | +import net.minecraft.block.ConnectingBlock; |
| 21 | +import net.minecraft.item.Item; |
| 22 | +import net.minecraft.item.Items; |
| 23 | +import net.minecraft.state.property.BooleanProperty; |
| 24 | +import net.minecraft.util.math.BlockPos; |
| 25 | +import net.minecraft.util.math.Direction; |
| 26 | +import net.wurstclient.hacks.autofarm.AutoFarmPlantType; |
| 27 | +import net.wurstclient.settings.PlantTypeSetting; |
| 28 | +import net.wurstclient.util.BlockUtils; |
| 29 | + |
| 30 | +public final class ChorusFruitPlantType extends AutoFarmPlantType |
| 31 | +{ |
| 32 | + private static final EnumMap<Direction, BooleanProperty> CHORUS_GROWING_DIRECTIONS = |
| 33 | + Maps.newEnumMap(Map.of(Direction.NORTH, ConnectingBlock.NORTH, |
| 34 | + Direction.SOUTH, ConnectingBlock.SOUTH, Direction.WEST, |
| 35 | + ConnectingBlock.WEST, Direction.EAST, ConnectingBlock.EAST, |
| 36 | + Direction.UP, ConnectingBlock.UP)); |
| 37 | + |
| 38 | + @Override |
| 39 | + public final boolean isReplantingSpot(BlockPos pos, BlockState state) |
| 40 | + { |
| 41 | + return (state.isOf(Blocks.CHORUS_FLOWER) |
| 42 | + || state.isOf(Blocks.CHORUS_PLANT)) && hasPlantingSurface(pos); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public final boolean hasPlantingSurface(BlockPos pos) |
| 47 | + { |
| 48 | + return BlockUtils.getState(pos.down()).isOf(Blocks.END_STONE); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Item getSeedItem() |
| 53 | + { |
| 54 | + return Items.CHORUS_FLOWER; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean shouldHarvestByMining(BlockPos pos, BlockState state) |
| 59 | + { |
| 60 | + if(state.isOf(Blocks.CHORUS_FLOWER) |
| 61 | + && state.get(ChorusFlowerBlock.AGE, 0) == ChorusFlowerBlock.MAX_AGE) |
| 62 | + return true; |
| 63 | + |
| 64 | + if(!state.isOf(Blocks.CHORUS_PLANT)) |
| 65 | + return false; |
| 66 | + |
| 67 | + return !hasAttachedFlowers(pos, state, new HashSet<>()); |
| 68 | + } |
| 69 | + |
| 70 | + private boolean hasAttachedFlowers(BlockPos pos, BlockState state, |
| 71 | + HashSet<BlockPos> visited) |
| 72 | + { |
| 73 | + // If the plant is unreasonably large, just assume there's a flower |
| 74 | + // somewhere so we don't harvest it |
| 75 | + if(visited.size() > 1000) |
| 76 | + return true; |
| 77 | + |
| 78 | + if(!visited.add(pos)) |
| 79 | + return false; |
| 80 | + |
| 81 | + for(Entry<Direction, BooleanProperty> entry : CHORUS_GROWING_DIRECTIONS |
| 82 | + .entrySet()) |
| 83 | + { |
| 84 | + if(!state.get(entry.getValue(), false)) |
| 85 | + continue; |
| 86 | + |
| 87 | + BlockPos neighborPos = pos.offset(entry.getKey()); |
| 88 | + BlockState neighborState = BlockUtils.getState(neighborPos); |
| 89 | + if(neighborState.isOf(Blocks.CHORUS_FLOWER)) |
| 90 | + return true; |
| 91 | + |
| 92 | + if(neighborState.isOf(Blocks.CHORUS_PLANT) |
| 93 | + && hasAttachedFlowers(neighborPos, neighborState, visited)) |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected PlantTypeSetting createSetting() |
| 102 | + { |
| 103 | + return new PlantTypeSetting("Chorus Fruit", Items.CHORUS_FRUIT, true, |
| 104 | + true); |
| 105 | + } |
| 106 | +} |
0 commit comments