| 1 | package com.jellypudding.offlineclient.modules.world;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 5 | import com.jellypudding.offlineclient.event.events.Render3DEvent;
|
| 6 | import com.jellypudding.offlineclient.event.events.TickEvent;
|
| 7 | import com.jellypudding.offlineclient.mixin.MultiPlayerGameModeAccessor;
|
| 8 | import com.jellypudding.offlineclient.module.Category;
|
| 9 | import com.jellypudding.offlineclient.module.ExclusivityGroup;
|
| 10 | import com.jellypudding.offlineclient.module.Module;
|
| 11 | import com.jellypudding.offlineclient.render.DrawBatch;
|
| 12 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 13 | import com.jellypudding.offlineclient.setting.ChoiceListSetting;
|
| 14 | import com.jellypudding.offlineclient.setting.ColorSetting;
|
| 15 | import com.jellypudding.offlineclient.setting.EnumSetting;
|
| 16 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 17 | import com.jellypudding.offlineclient.util.BlockMiner;
|
| 18 | import com.jellypudding.offlineclient.util.BlockUtil;
|
| 19 | import com.jellypudding.offlineclient.util.FaceMode;
|
| 20 | import com.jellypudding.offlineclient.util.InputUtil;
|
| 21 | import com.jellypudding.offlineclient.util.InventoryUtil;
|
| 22 | import com.jellypudding.offlineclient.util.RotationPriority;
|
| 23 | import com.jellypudding.offlineclient.util.SwingMode;
|
| 24 | import net.minecraft.core.BlockPos;
|
| 25 | import net.minecraft.core.Direction;
|
| 26 | import net.minecraft.tags.BlockTags;
|
| 27 | import net.minecraft.tags.FluidTags;
|
| 28 | import net.minecraft.world.InteractionHand;
|
| 29 | import net.minecraft.world.item.Item;
|
| 30 | import net.minecraft.world.item.ItemStack;
|
| 31 | import net.minecraft.world.item.Items;
|
| 32 | import net.minecraft.world.level.block.Blocks;
|
| 33 | import net.minecraft.world.level.block.CaveVines;
|
| 34 | import net.minecraft.world.level.block.ChorusFlowerBlock;
|
| 35 | import net.minecraft.world.level.block.CocoaBlock;
|
| 36 | import net.minecraft.world.level.block.CropBlock;
|
| 37 | import net.minecraft.world.level.block.NetherWartBlock;
|
| 38 | import net.minecraft.world.level.block.PipeBlock;
|
| 39 | import net.minecraft.world.level.block.PitcherCropBlock;
|
| 40 | import net.minecraft.world.level.block.SeaPickleBlock;
|
| 41 | import net.minecraft.world.level.block.SweetBerryBushBlock;
|
| 42 | import net.minecraft.world.level.block.state.BlockState;
|
| 43 | import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
|
| 44 | import net.minecraft.world.level.material.FluidState;
|
| 45 | import net.minecraft.world.phys.AABB;
|
| 46 | import net.minecraft.world.phys.Vec3;
|
| 47 |
|
| 48 | import java.util.ArrayList;
|
| 49 | import java.util.HashMap;
|
| 50 | import java.util.HashSet;
|
| 51 | import java.util.List;
|
| 52 | import java.util.Map;
|
| 53 | import java.util.Set;
|
| 54 | import java.util.function.Predicate;
|
| 55 |
|
| 56 | // Cuts the ripe crops around you and puts the seeds back in the ground. It
|
| 57 | // never walks anywhere and only farms what is already in reach.
|
| 58 | public final class AutoFarm extends Module {
|
| 59 |
|
| 60 | // A block test that needs the state as well as the position.
|
| 61 | private interface PlantTest {
|
| 62 | boolean matches(BlockPos pos, BlockState state);
|
| 63 | }
|
| 64 |
|
| 65 | // One kind of plant. The spot test marks where a seed belongs and the mine
|
| 66 | // and interact tests say when the grown part is ready to take.
|
| 67 | private record Plant(String name, Item seed, PlantTest spot, Predicate<BlockPos> surface,
|
| 68 | PlantTest mine, PlantTest interact) {
|
| 69 | }
|
| 70 |
|
| 71 | private static final PlantTest NEVER = (pos, state) -> false;
|
| 72 |
|
| 73 | private static final List<Plant> PLANTS = List.of(
|
| 74 | new Plant("Amethyst", Items.BUDDING_AMETHYST,
|
| 75 | (pos, state) -> state.is(Blocks.BUDDING_AMETHYST), pos -> true,
|
| 76 | (pos, state) -> state.is(Blocks.AMETHYST_CLUSTER), NEVER),
|
| 77 | new Plant("Bamboo", Items.BAMBOO,
|
| 78 | (pos, state) -> (state.is(Blocks.BAMBOO) || state.is(Blocks.BAMBOO_SAPLING))
|
| 79 | && !below(pos).is(Blocks.BAMBOO) && !below(pos).is(Blocks.BAMBOO_SAPLING)
|
| 80 | && below(pos).is(BlockTags.SUPPORTS_BAMBOO),
|
| 81 | pos -> below(pos).is(BlockTags.SUPPORTS_BAMBOO),
|
| 82 | (pos, state) -> state.is(Blocks.BAMBOO) && isSpot("Bamboo", pos.below()), NEVER),
|
| 83 | new Plant("Beetroots", Items.BEETROOT_SEEDS,
|
| 84 | (pos, state) -> state.is(Blocks.BEETROOTS), AutoFarm::cropSurface,
|
| 85 | AutoFarm::ripeCrop, NEVER),
|
| 86 | new Plant("Cactus", Items.CACTUS,
|
| 87 | (pos, state) -> state.is(Blocks.CACTUS) && !below(pos).is(Blocks.CACTUS)
|
| 88 | && cactusSurface(pos),
|
| 89 | AutoFarm::cactusSurface,
|
| 90 | (pos, state) -> (state.is(Blocks.CACTUS) || state.is(Blocks.CACTUS_FLOWER))
|
| 91 | && isSpot("Cactus", pos.below()), NEVER),
|
| 92 | new Plant("Carrots", Items.CARROT,
|
| 93 | (pos, state) -> state.is(Blocks.CARROTS), AutoFarm::cropSurface,
|
| 94 | AutoFarm::ripeCrop, NEVER),
|
| 95 | new Plant("Chorus plants", Items.CHORUS_FLOWER,
|
| 96 | (pos, state) -> (state.is(Blocks.CHORUS_FLOWER) || state.is(Blocks.CHORUS_PLANT))
|
| 97 | && below(pos).is(BlockTags.SUPPORTS_CHORUS_FLOWER),
|
| 98 | pos -> below(pos).is(BlockTags.SUPPORTS_CHORUS_FLOWER),
|
| 99 | AutoFarm::ripeChorus, NEVER),
|
| 100 | new Plant("Cocoa beans", Items.COCOA_BEANS,
|
| 101 | (pos, state) -> state.getBlock() instanceof CocoaBlock, AutoFarm::cocoaSurface,
|
| 102 | (pos, state) -> state.getBlock() instanceof CocoaBlock
|
| 103 | && state.getValue(CocoaBlock.AGE) >= CocoaBlock.MAX_AGE, NEVER),
|
| 104 | new Plant("Glow berries", Items.GLOW_BERRIES,
|
| 105 | (pos, state) -> state.getBlock() instanceof CaveVines && vineCeiling(pos),
|
| 106 | AutoFarm::vineCeiling,
|
| 107 | (pos, state) -> state.getBlock() instanceof CaveVines && !vineCeiling(pos),
|
| 108 | (pos, state) -> state.getBlock() instanceof CaveVines
|
| 109 | && CaveVines.hasGlowBerries(state) && vineCeiling(pos)),
|
| 110 | new Plant("Kelp", Items.KELP,
|
| 111 | (pos, state) -> (state.is(Blocks.KELP) || state.is(Blocks.KELP_PLANT))
|
| 112 | && !below(pos).is(Blocks.KELP) && !below(pos).is(Blocks.KELP_PLANT)
|
| 113 | && kelpSurface(pos),
|
| 114 | AutoFarm::kelpSurface,
|
| 115 | (pos, state) -> (state.is(Blocks.KELP) || state.is(Blocks.KELP_PLANT))
|
| 116 | && isSpot("Kelp", pos.below()), NEVER),
|
| 117 | new Plant("Melons", Items.MELON_SEEDS,
|
| 118 | (pos, state) -> state.is(Blocks.MELON_STEM) || state.is(Blocks.ATTACHED_MELON_STEM),
|
| 119 | pos -> below(pos).is(BlockTags.SUPPORTS_MELON_STEM),
|
| 120 | (pos, state) -> state.is(Blocks.MELON), NEVER),
|
| 121 | new Plant("Nether wart", Items.NETHER_WART,
|
| 122 | (pos, state) -> state.getBlock() instanceof NetherWartBlock,
|
| 123 | pos -> below(pos).is(BlockTags.SUPPORTS_NETHER_WART),
|
| 124 | (pos, state) -> state.getBlock() instanceof NetherWartBlock
|
| 125 | && state.getValue(NetherWartBlock.AGE) >= NetherWartBlock.MAX_AGE, NEVER),
|
| 126 | new Plant("Pitcher plants", Items.PITCHER_POD,
|
| 127 | (pos, state) -> state.is(Blocks.PITCHER_CROP)
|
| 128 | && state.getValue(PitcherCropBlock.HALF) == DoubleBlockHalf.LOWER
|
| 129 | && cropSurface(pos),
|
| 130 | AutoFarm::cropSurface,
|
| 131 | (pos, state) -> state.is(Blocks.PITCHER_CROP)
|
| 132 | && state.getValue(PitcherCropBlock.AGE) >= PitcherCropBlock.MAX_AGE, NEVER),
|
| 133 | new Plant("Potatoes", Items.POTATO,
|
| 134 | (pos, state) -> state.is(Blocks.POTATOES), AutoFarm::cropSurface,
|
| 135 | AutoFarm::ripeCrop, NEVER),
|
| 136 | new Plant("Pumpkins", Items.PUMPKIN_SEEDS,
|
| 137 | (pos, state) -> state.is(Blocks.PUMPKIN_STEM) || state.is(Blocks.ATTACHED_PUMPKIN_STEM),
|
| 138 | pos -> below(pos).is(BlockTags.SUPPORTS_PUMPKIN_STEM),
|
| 139 | (pos, state) -> state.is(Blocks.PUMPKIN), NEVER),
|
| 140 | new Plant("Sea pickles", Items.SEA_PICKLE,
|
| 141 | (pos, state) -> state.getBlock() instanceof SeaPickleBlock && pickleSurface(pos),
|
| 142 | AutoFarm::pickleSurface,
|
| 143 | (pos, state) -> state.getBlock() instanceof SeaPickleBlock
|
| 144 | && state.getValue(SeaPickleBlock.PICKLES) > 1, NEVER),
|
| 145 | new Plant("Sugar cane", Items.SUGAR_CANE,
|
| 146 | (pos, state) -> state.is(Blocks.SUGAR_CANE) && !below(pos).is(Blocks.SUGAR_CANE)
|
| 147 | && caneSurface(pos),
|
| 148 | AutoFarm::caneSurface,
|
| 149 | (pos, state) -> state.is(Blocks.SUGAR_CANE) && isSpot("Sugar cane", pos.below()),
|
| 150 | NEVER),
|
| 151 | new Plant("Sweet berries", Items.SWEET_BERRIES,
|
| 152 | (pos, state) -> state.getBlock() instanceof SweetBerryBushBlock && berrySurface(pos),
|
| 153 | AutoFarm::berrySurface, NEVER,
|
| 154 | (pos, state) -> state.getBlock() instanceof SweetBerryBushBlock
|
| 155 | && state.getValue(SweetBerryBushBlock.AGE) > 1),
|
| 156 | new Plant("Torchflowers", Items.TORCHFLOWER_SEEDS,
|
| 157 | (pos, state) -> (state.is(Blocks.TORCHFLOWER) || state.is(Blocks.TORCHFLOWER_CROP))
|
| 158 | && cropSurface(pos),
|
| 159 | AutoFarm::cropSurface,
|
| 160 | (pos, state) -> state.is(Blocks.TORCHFLOWER), NEVER),
|
| 161 | new Plant("Twisting vines", Items.TWISTING_VINES,
|
| 162 | (pos, state) -> isTwisting(state) && twistingFloor(pos), AutoFarm::twistingFloor,
|
| 163 | (pos, state) -> isTwisting(state) && !twistingFloor(pos), NEVER),
|
| 164 | new Plant("Weeping vines", Items.WEEPING_VINES,
|
| 165 | (pos, state) -> isWeeping(state) && weepingCeiling(pos), AutoFarm::weepingCeiling,
|
| 166 | (pos, state) -> isWeeping(state) && !weepingCeiling(pos), NEVER),
|
| 167 | new Plant("Wheat", Items.WHEAT_SEEDS,
|
| 168 | (pos, state) -> state.is(Blocks.WHEAT), AutoFarm::cropSurface,
|
| 169 | AutoFarm::ripeCrop, NEVER));
|
| 170 |
|
| 171 | private static final List<String> PLANT_NAMES = PLANTS.stream().map(Plant::name).toList();
|
| 172 |
|
| 173 | // Vines grow fast enough to be a nuisance. They start switched off.
|
| 174 | private static final List<String> PLANTS_ON = PLANT_NAMES.stream()
|
| 175 | .filter(name -> !name.endsWith("vines")).toList();
|
| 176 |
|
| 177 | // Ticks before the same block is sent again.
|
| 178 | private static final int RETRY_TICKS = 10;
|
| 179 |
|
| 180 | // Ticks a slower block gets on top of its break time before it is given up on.
|
| 181 | private static final int SLOW_GRACE_TICKS = 20;
|
| 182 |
|
| 183 | // Ticks a cut spot waits for its seed.
|
| 184 | private static final int PLANT_TICKS = 60;
|
| 185 |
|
| 186 | // Vanilla repeats a held right click at this rate.
|
| 187 | private static final int USE_INTERVAL = 4;
|
| 188 |
|
| 189 | private static final AABB NODE = new AABB(BlockPos.ZERO).deflate(0.3);
|
| 190 | private static final AABB SHELL = new AABB(BlockPos.ZERO).deflate(1 / 16.0);
|
| 191 |
|
| 192 | private final NumberSetting range = new NumberSetting("Range",
|
| 193 | "How far from your eyes to farm.", 4.5, 1, 6, 0.1).max(6);
|
| 194 | private final BoolSetting harvest = new BoolSetting("Harvest",
|
| 195 | "Takes the parts of a plant that have finished growing.", true);
|
| 196 | private final ChoiceListSetting harvestPlants = new ChoiceListSetting("Harvest plants",
|
| 197 | "Which plants to take from.", () -> PLANT_NAMES, PLANTS_ON)
|
| 198 | .under(harvest);
|
| 199 | private final BoolSetting replant = new BoolSetting("Replant",
|
| 200 | "Puts a seed back into every spot you just cleared.", true);
|
| 201 | private final ChoiceListSetting replantPlants = new ChoiceListSetting("Replant plants",
|
| 202 | "Which plants to put back.", () -> PLANT_NAMES, PLANTS_ON)
|
| 203 | .under(replant);
|
| 204 | private final BoolSetting bonemeal = new BoolSetting("Bonemeal",
|
| 205 | "Feeds bone meal to the crops that are still growing.", false);
|
| 206 | private final NumberSetting perTick = new NumberSetting("Blocks per tick",
|
| 207 | "How many crops to cut each tick.", 4, 1, 16, 1);
|
| 208 | private final BoolSetting lineOfSight = new BoolSetting("Line of sight",
|
| 209 | "Skip anything your eyes cannot see.", false);
|
| 210 | private final EnumSetting<FaceMode> faceTarget = FaceMode.setting(FaceMode.SERVER);
|
| 211 | private final EnumSetting<SwingMode> swing = SwingMode.setting(SwingMode.BOTH);
|
| 212 | private final BoolSetting drawSpots = new BoolSetting("Draw spots",
|
| 213 | "Marks the spots being tracked for replanting.", true);
|
| 214 | private final ColorSetting spotColor = new ColorSetting("Spot colour",
|
| 215 | "Colour of the tracked spots.", 180, 0.79f, 0.88f, false)
|
| 216 | .under(drawSpots);
|
| 217 | private final BoolSetting drawHarvest = new BoolSetting("Draw harvest",
|
| 218 | "Marks the plants that are ready to take and how far the break has got.", true);
|
| 219 | private final ColorSetting harvestColor = new ColorSetting("Harvest colour",
|
| 220 | "Colour of the plants that are ready.", 120, 0.79f, 0.88f, false)
|
| 221 | .under(drawHarvest);
|
| 222 | private final BoolSetting drawReplant = new BoolSetting("Draw replant",
|
| 223 | "Marks the empty spots waiting for a seed.", true);
|
| 224 | private final ColorSetting replantColor = new ColorSetting("Replant colour",
|
| 225 | "Colour of the spots waiting for a seed.", 0, 0.79f, 0.88f, false)
|
| 226 | .under(drawReplant);
|
| 227 |
|
| 228 | private final Map<BlockPos, Integer> attempted = new HashMap<>();
|
| 229 | private final Map<BlockPos, Plant> spots = new HashMap<>();
|
| 230 | private final Map<BlockPos, Integer> spotExpiry = new HashMap<>();
|
| 231 |
|
| 232 | private List<BlockPos> shownHarvest = List.of();
|
| 233 | private List<BlockPos> shownReplant = List.of();
|
| 234 | private BlockPos mining;
|
| 235 |
|
| 236 | private BlockPos slowPending;
|
| 237 | private int slowDeadline;
|
| 238 | private int lastUse;
|
| 239 | private int lastTick;
|
| 240 |
|
| 241 | private final InventoryUtil.HotbarLoan loan = new InventoryUtil.HotbarLoan();
|
| 242 |
|
| 243 | public AutoFarm() {
|
| 244 | super("AutoFarm", "Harvests the ripe crops in reach and replants them.", Category.WORLD);
|
| 245 | addSettings(range, harvest, harvestPlants, replant, replantPlants, bonemeal, perTick,
|
| 246 | lineOfSight, faceTarget, swing, drawSpots, spotColor, drawHarvest, harvestColor,
|
| 247 | drawReplant, replantColor);
|
| 248 | searchTags("farm", "crops", "harvest", "replant", "wheat", "berries");
|
| 249 | }
|
| 250 |
|
| 251 | @Override
|
| 252 | public String getSuffix() {
|
| 253 | return range.getValueString();
|
| 254 | }
|
| 255 |
|
| 256 | // Break packets for anything slower than one hit share the one server slot.
|
| 257 | @Override
|
| 258 | public ExclusivityGroup getExclusivityGroup() {
|
| 259 | return ExclusivityGroup.MINING;
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | protected void onEnable() {
|
| 264 | reset();
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | protected void onDisable() {
|
| 269 | reset();
|
| 270 | loan.giveBack();
|
| 271 | }
|
| 272 |
|
| 273 | private void reset() {
|
| 274 | attempted.clear();
|
| 275 | spots.clear();
|
| 276 | spotExpiry.clear();
|
| 277 | shownHarvest = List.of();
|
| 278 | shownReplant = List.of();
|
| 279 | mining = null;
|
| 280 | slowPending = null;
|
| 281 | lastUse = 0;
|
| 282 | lastTick = 0;
|
| 283 | }
|
| 284 |
|
| 285 | @Subscribe
|
| 286 | private void onTick(TickEvent event) {
|
| 287 | if (!inGame() || mc.player.isSpectator()) {
|
| 288 | loan.giveBack();
|
| 289 | return;
|
| 290 | }
|
| 291 | // A held attack or an open container means the player is busy by hand.
|
| 292 | if (InputUtil.physicallyHeld(mc.options.keyAttack) || mc.player.isUsingItem()
|
| 293 | || mc.gui.screen() != null) {
|
| 294 | loan.giveBack();
|
| 295 | return;
|
| 296 | }
|
| 297 |
|
| 298 | int now = mc.player.tickCount;
|
| 299 | // The tick count restarts on a respawn.
|
| 300 | if (now < lastTick) {
|
| 301 | reset();
|
| 302 | }
|
| 303 | lastTick = now;
|
| 304 | attempted.values().removeIf(expiry -> expiry <= now);
|
| 305 | spotExpiry.entrySet().removeIf(entry -> {
|
| 306 | boolean gone = entry.getValue() <= now;
|
| 307 | if (gone) {
|
| 308 | spots.remove(entry.getKey());
|
| 309 | }
|
| 310 | return gone;
|
| 311 | });
|
| 312 | if (slowPending != null && (now >= slowDeadline || !readyToMine(slowPending))) {
|
| 313 | slowPending = null;
|
| 314 | }
|
| 315 |
|
| 316 | List<BlockPos> scan = BlockUtil.positionsWithin(range.getValue());
|
| 317 | for (BlockPos pos : scan) {
|
| 318 | Plant plant = spotPlant(pos);
|
| 319 | if (plant != null) {
|
| 320 | spots.put(pos.immutable(), plant);
|
| 321 | spotExpiry.put(pos.immutable(), now + PLANT_TICKS);
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | List<BlockPos> toMine = new ArrayList<>();
|
| 326 | List<BlockPos> toInteract = new ArrayList<>();
|
| 327 | List<BlockPos> toReplant = new ArrayList<>();
|
| 328 | sort(scan, toMine, toInteract, toReplant);
|
| 329 | shownHarvest = toMine;
|
| 330 | shownReplant = toReplant;
|
| 331 |
|
| 332 | boolean acted = replant.isOn() && plantSeeds(toReplant, now);
|
| 333 | if (!acted && harvest.isOn()) {
|
| 334 | acted = pickByHand(toInteract, now);
|
| 335 | }
|
| 336 | if (!acted && harvest.isOn()) {
|
| 337 | mineTick(toMine, now);
|
| 338 | } else {
|
| 339 | mining = null;
|
| 340 | }
|
| 341 | if (!acted && bonemeal.isOn()) {
|
| 342 | feedTick(scan, now);
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | private void sort(List<BlockPos> scan, List<BlockPos> toMine, List<BlockPos> toInteract,
|
| 347 | List<BlockPos> toReplant) {
|
| 348 | for (BlockPos pos : scan) {
|
| 349 | if (lineOfSight.isOn() && !BlockUtil.canSee(pos)) {
|
| 350 | continue;
|
| 351 | }
|
| 352 | BlockState state = BlockUtil.state(pos);
|
| 353 | if (state.canBeReplaced()) {
|
| 354 | Plant waiting = spots.get(pos);
|
| 355 | if (waiting != null && replantPlants.contains(waiting.name())
|
| 356 | && waiting.surface().test(pos)) {
|
| 357 | toReplant.add(pos.immutable());
|
| 358 | }
|
| 359 | continue;
|
| 360 | }
|
| 361 | for (Plant plant : PLANTS) {
|
| 362 | if (!harvestPlants.contains(plant.name())) {
|
| 363 | continue;
|
| 364 | }
|
| 365 | if (plant.interact().matches(pos, state)) {
|
| 366 | toInteract.add(pos.immutable());
|
| 367 | break;
|
| 368 | }
|
| 369 | if (plant.mine().matches(pos, state)) {
|
| 370 | toMine.add(pos.immutable());
|
| 371 | break;
|
| 372 | }
|
| 373 | }
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | // Creative with no turning can spam every crop at once. Anything else goes
|
| 378 | // block by block because only one angle rides the packet each tick.
|
| 379 | private void mineTick(List<BlockPos> toMine, int now) {
|
| 380 | boolean spam = mc.player.getAbilities().instabuild && faceTarget.is(FaceMode.OFF);
|
| 381 | int limit = spam ? toMine.size() : perTick.getInt();
|
| 382 | int sent = 0;
|
| 383 | mining = null;
|
| 384 | for (BlockPos pos : toMine) {
|
| 385 | if (sent >= limit) {
|
| 386 | break;
|
| 387 | }
|
| 388 | if (attempted.containsKey(pos)) {
|
| 389 | continue;
|
| 390 | }
|
| 391 | boolean instant = BlockUtil.canInstantBreak(pos);
|
| 392 | if (!instant && slowPending != null) {
|
| 393 | continue;
|
| 394 | }
|
| 395 | if (sent == 0) {
|
| 396 | mining = pos;
|
| 397 | faceTarget.getValue().face(BlockUtil.hitPoint(pos, BlockUtil.facingSide(pos)),
|
| 398 | RotationPriority.MINE);
|
| 399 | }
|
| 400 | BlockMiner.breakInstantly(pos);
|
| 401 | if (instant) {
|
| 402 | attempted.put(pos, now + RETRY_TICKS);
|
| 403 | } else {
|
| 404 | slowPending = pos;
|
| 405 | slowDeadline = now + BlockUtil.breakTicks(pos) + SLOW_GRACE_TICKS;
|
| 406 | attempted.put(pos, slowDeadline);
|
| 407 | }
|
| 408 | sent++;
|
| 409 | }
|
| 410 | if (sent > 0) {
|
| 411 | swing.getValue().swing(InteractionHand.MAIN_HAND);
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | // Berries and glow berries come off with a right click and leave the plant standing.
|
| 416 | private boolean pickByHand(List<BlockPos> toInteract, int now) {
|
| 417 | if (toInteract.isEmpty() || now - lastUse < USE_INTERVAL) {
|
| 418 | return false;
|
| 419 | }
|
| 420 | // A right click with bone meal would fertilise the bush instead.
|
| 421 | if (mc.player.getMainHandItem().is(Items.BONE_MEAL)) {
|
| 422 | int other = InventoryUtil.hotbarSlot(stack -> !stack.is(Items.BONE_MEAL));
|
| 423 | if (other == -1) {
|
| 424 | return false;
|
| 425 | }
|
| 426 | loan.select(other);
|
| 427 | return true;
|
| 428 | }
|
| 429 | for (BlockPos pos : toInteract) {
|
| 430 | Direction side = BlockUtil.facingSide(pos);
|
| 431 | faceTarget.getValue().face(BlockUtil.hitPoint(pos, side), RotationPriority.PLACE);
|
| 432 | if (BlockUtil.interact(pos, side)) {
|
| 433 | lastUse = now;
|
| 434 | return true;
|
| 435 | }
|
| 436 | }
|
| 437 | return false;
|
| 438 | }
|
| 439 |
|
| 440 | private boolean plantSeeds(List<BlockPos> toReplant, int now) {
|
| 441 | if (toReplant.isEmpty() || now - lastUse < USE_INTERVAL) {
|
| 442 | return false;
|
| 443 | }
|
| 444 | for (BlockPos pos : toReplant) {
|
| 445 | Plant plant = spots.get(pos);
|
| 446 | if (plant == null) {
|
| 447 | continue;
|
| 448 | }
|
| 449 | if (!holdSeed(plant.seed())) {
|
| 450 | continue;
|
| 451 | }
|
| 452 | faceTarget.getValue().face(Vec3.atCenterOf(pos), RotationPriority.PLACE);
|
| 453 | if (BlockUtil.placeAny(pos, false, false)) {
|
| 454 | swing.getValue().swing(InteractionHand.MAIN_HAND);
|
| 455 | spots.remove(pos);
|
| 456 | spotExpiry.remove(pos);
|
| 457 | lastUse = now;
|
| 458 | }
|
| 459 | return true;
|
| 460 | }
|
| 461 | return false;
|
| 462 | }
|
| 463 |
|
| 464 | // Takes the seed from anywhere in the inventory and conjures one in creative.
|
| 465 | private boolean holdSeed(Item seed) {
|
| 466 | if (mc.player.getMainHandItem().is(seed)) {
|
| 467 | return true;
|
| 468 | }
|
| 469 | int slot = InventoryUtil.findSlot(seed, InventoryUtil.WHOLE_INVENTORY);
|
| 470 | if (slot != -1) {
|
| 471 | return loan.select(slot) && mc.player.getMainHandItem().is(seed);
|
| 472 | }
|
| 473 | if (!mc.player.getAbilities().instabuild) {
|
| 474 | return false;
|
| 475 | }
|
| 476 | int free = InventoryUtil.freeHotbarSlot(InventoryUtil.selectedSlot());
|
| 477 | ItemStack stack = new ItemStack(seed);
|
| 478 | mc.player.getInventory().setSelectedSlot(free);
|
| 479 | mc.player.getInventory().setItem(free, stack);
|
| 480 | mc.gameMode.handleCreativeModeItemAdd(stack, InventoryUtil.networkSlot(free));
|
| 481 | return true;
|
| 482 | }
|
| 483 |
|
| 484 | private void feedTick(List<BlockPos> scan, int now) {
|
| 485 | if (now - lastUse < USE_INTERVAL) {
|
| 486 | return;
|
| 487 | }
|
| 488 | BlockPos target = findGrowing(scan);
|
| 489 | if (target == null) {
|
| 490 | return;
|
| 491 | }
|
| 492 | int slot = InventoryUtil.findSlot(Items.BONE_MEAL, InventoryUtil.WHOLE_INVENTORY);
|
| 493 | if (slot == -1 || !loan.select(slot)) {
|
| 494 | return;
|
| 495 | }
|
| 496 | Direction side = BlockUtil.facingSide(target);
|
| 497 | faceTarget.getValue().face(BlockUtil.hitPoint(target, side), RotationPriority.PLACE);
|
| 498 | if (BlockUtil.useOn(target, false, false)) {
|
| 499 | swing.getValue().swing(InteractionHand.MAIN_HAND);
|
| 500 | lastUse = now;
|
| 501 | }
|
| 502 | }
|
| 503 |
|
| 504 | private BlockPos findGrowing(List<BlockPos> scan) {
|
| 505 | for (BlockPos pos : scan) {
|
| 506 | BlockState state = BlockUtil.state(pos);
|
| 507 | if (state.getBlock() instanceof CropBlock crop && !crop.isMaxAge(state)) {
|
| 508 | return pos;
|
| 509 | }
|
| 510 | if (state.getBlock() instanceof CocoaBlock
|
| 511 | && state.getValue(CocoaBlock.AGE) < CocoaBlock.MAX_AGE) {
|
| 512 | return pos;
|
| 513 | }
|
| 514 | }
|
| 515 | return null;
|
| 516 | }
|
| 517 |
|
| 518 | private boolean readyToMine(BlockPos pos) {
|
| 519 | BlockState state = BlockUtil.state(pos);
|
| 520 | for (Plant plant : PLANTS) {
|
| 521 | if (harvestPlants.contains(plant.name()) && plant.mine().matches(pos, state)) {
|
| 522 | return true;
|
| 523 | }
|
| 524 | }
|
| 525 | return false;
|
| 526 | }
|
| 527 |
|
| 528 | private Plant spotPlant(BlockPos pos) {
|
| 529 | BlockState state = BlockUtil.state(pos);
|
| 530 | for (Plant plant : PLANTS) {
|
| 531 | if (plant.spot().matches(pos, state)) {
|
| 532 | return plant;
|
| 533 | }
|
| 534 | }
|
| 535 | return null;
|
| 536 | }
|
| 537 |
|
| 538 | @Subscribe
|
| 539 | private void onRender3D(Render3DEvent event) {
|
| 540 | if (!inGame()) {
|
| 541 | return;
|
| 542 | }
|
| 543 | DrawBatch batch = event.getBatch();
|
| 544 | if (drawSpots.isOn()) {
|
| 545 | int colour = spotColor.getColor();
|
| 546 | for (BlockPos pos : spots.keySet()) {
|
| 547 | batch.outlineBox(NODE.move(pos), colour, false);
|
| 548 | }
|
| 549 | }
|
| 550 | if (drawHarvest.isOn()) {
|
| 551 | int colour = harvestColor.getColor();
|
| 552 | for (BlockPos pos : shownHarvest) {
|
| 553 | batch.outlineBox(SHELL.move(pos), colour, false);
|
| 554 | }
|
| 555 | drawProgress(batch, colour);
|
| 556 | }
|
| 557 | if (drawReplant.isOn()) {
|
| 558 | int colour = replantColor.getColor();
|
| 559 | for (BlockPos pos : shownReplant) {
|
| 560 | batch.outlineBox(SHELL.move(pos), colour, false);
|
| 561 | }
|
| 562 | }
|
| 563 | }
|
| 564 |
|
| 565 | // The break shell shrinks from the whole block down to nothing as it goes.
|
| 566 | private void drawProgress(DrawBatch batch, int colour) {
|
| 567 | if (mining == null || !(mc.gameMode instanceof MultiPlayerGameModeAccessor access)) {
|
| 568 | return;
|
| 569 | }
|
| 570 | float progress = access.offlineclient$destroyProgress();
|
| 571 | if (progress <= 0 || !mining.equals(access.offlineclient$destroyBlockPos())) {
|
| 572 | return;
|
| 573 | }
|
| 574 | batch.solidBox(new AABB(BlockPos.ZERO).deflate(progress * 0.5).move(mining), colour, false);
|
| 575 | }
|
| 576 |
|
| 577 | private static BlockState below(BlockPos pos) {
|
| 578 | return BlockUtil.state(pos.below());
|
| 579 | }
|
| 580 |
|
| 581 | // Whether the plant of that name would put a seed here.
|
| 582 | private static boolean isSpot(String name, BlockPos pos) {
|
| 583 | for (Plant plant : PLANTS) {
|
| 584 | if (plant.name().equals(name)) {
|
| 585 | return plant.spot().matches(pos, BlockUtil.state(pos));
|
| 586 | }
|
| 587 | }
|
| 588 | return false;
|
| 589 | }
|
| 590 |
|
| 591 | private static boolean ripeCrop(BlockPos pos, BlockState state) {
|
| 592 | return state.getBlock() instanceof CropBlock crop && crop.isMaxAge(state);
|
| 593 | }
|
| 594 |
|
| 595 | private static boolean cropSurface(BlockPos pos) {
|
| 596 | return below(pos).is(BlockTags.SUPPORTS_CROPS);
|
| 597 | }
|
| 598 |
|
| 599 | private static boolean cocoaSurface(BlockPos pos) {
|
| 600 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 601 | if (BlockUtil.state(pos.relative(side)).is(BlockTags.SUPPORTS_COCOA)) {
|
| 602 | return true;
|
| 603 | }
|
| 604 | }
|
| 605 | return false;
|
| 606 | }
|
| 607 |
|
| 608 | // A cactus needs clear sides or it snaps as soon as it grows.
|
| 609 | private static boolean cactusSurface(BlockPos pos) {
|
| 610 | if (!below(pos).is(BlockTags.SUPPORTS_CACTUS)) {
|
| 611 | return false;
|
| 612 | }
|
| 613 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 614 | BlockState neighbour = BlockUtil.state(pos.relative(side));
|
| 615 | if (BlockUtil.blocksMotion(neighbour) || neighbour.getFluidState().is(FluidTags.LAVA)) {
|
| 616 | return false;
|
| 617 | }
|
| 618 | }
|
| 619 | return true;
|
| 620 | }
|
| 621 |
|
| 622 | private static boolean caneSurface(BlockPos pos) {
|
| 623 | BlockPos floor = pos.below();
|
| 624 | if (!BlockUtil.state(floor).is(BlockTags.SUPPORTS_SUGAR_CANE)) {
|
| 625 | return false;
|
| 626 | }
|
| 627 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 628 | BlockState neighbour = BlockUtil.state(floor.relative(side));
|
| 629 | if (neighbour.getFluidState().is(FluidTags.SUPPORTS_SUGAR_CANE_ADJACENTLY)
|
| 630 | || neighbour.is(BlockTags.SUPPORTS_SUGAR_CANE_ADJACENTLY)) {
|
| 631 | return true;
|
| 632 | }
|
| 633 | }
|
| 634 | return false;
|
| 635 | }
|
| 636 |
|
| 637 | private static boolean kelpSurface(BlockPos pos) {
|
| 638 | FluidState fluid = BlockUtil.state(pos).getFluidState();
|
| 639 | if (!fluid.is(FluidTags.WATER) || fluid.getAmount() != 8) {
|
| 640 | return false;
|
| 641 | }
|
| 642 | BlockPos floor = pos.below();
|
| 643 | return !BlockUtil.state(floor).is(BlockTags.CANNOT_SUPPORT_KELP)
|
| 644 | && BlockUtil.state(floor).isFaceSturdy(OfflineClient.MC.level, floor, Direction.UP);
|
| 645 | }
|
| 646 |
|
| 647 | private static boolean pickleSurface(BlockPos pos) {
|
| 648 | if (!below(pos).is(BlockTags.CORAL_BLOCKS)) {
|
| 649 | return false;
|
| 650 | }
|
| 651 | FluidState fluid = BlockUtil.state(pos).getFluidState();
|
| 652 | return fluid.is(FluidTags.WATER) && fluid.getAmount() == 8;
|
| 653 | }
|
| 654 |
|
| 655 | private static boolean berrySurface(BlockPos pos) {
|
| 656 | return below(pos).is(BlockTags.SUPPORTS_VEGETATION);
|
| 657 | }
|
| 658 |
|
| 659 | // The topmost vine hangs from something solid rather than from more vine.
|
| 660 | private static boolean vineCeiling(BlockPos pos) {
|
| 661 | BlockPos ceiling = pos.above();
|
| 662 | BlockState state = BlockUtil.state(ceiling);
|
| 663 | return !(state.getBlock() instanceof CaveVines)
|
| 664 | && state.isFaceSturdy(OfflineClient.MC.level, ceiling, Direction.DOWN);
|
| 665 | }
|
| 666 |
|
| 667 | private static boolean isTwisting(BlockState state) {
|
| 668 | return state.is(Blocks.TWISTING_VINES) || state.is(Blocks.TWISTING_VINES_PLANT);
|
| 669 | }
|
| 670 |
|
| 671 | private static boolean twistingFloor(BlockPos pos) {
|
| 672 | BlockPos floor = pos.below();
|
| 673 | BlockState state = BlockUtil.state(floor);
|
| 674 | return !isTwisting(state)
|
| 675 | && state.isFaceSturdy(OfflineClient.MC.level, floor, Direction.UP);
|
| 676 | }
|
| 677 |
|
| 678 | private static boolean isWeeping(BlockState state) {
|
| 679 | return state.is(Blocks.WEEPING_VINES) || state.is(Blocks.WEEPING_VINES_PLANT);
|
| 680 | }
|
| 681 |
|
| 682 | private static boolean weepingCeiling(BlockPos pos) {
|
| 683 | BlockPos ceiling = pos.above();
|
| 684 | BlockState state = BlockUtil.state(ceiling);
|
| 685 | return !isWeeping(state)
|
| 686 | && state.isFaceSturdy(OfflineClient.MC.level, ceiling, Direction.DOWN);
|
| 687 | }
|
| 688 |
|
| 689 | // A flower is done when it stops growing. A stem is only cut once every
|
| 690 | // flower it holds up has been taken.
|
| 691 | private static boolean ripeChorus(BlockPos pos, BlockState state) {
|
| 692 | if (state.is(Blocks.CHORUS_FLOWER)) {
|
| 693 | return state.getValueOrElse(ChorusFlowerBlock.AGE, 0) == ChorusFlowerBlock.DEAD_AGE
|
| 694 | || !BlockUtil.state(pos.above()).isAir();
|
| 695 | }
|
| 696 | return state.is(Blocks.CHORUS_PLANT) && !holdsFlower(pos, state, new HashSet<>());
|
| 697 | }
|
| 698 |
|
| 699 | private static boolean holdsFlower(BlockPos pos, BlockState state, Set<BlockPos> seen) {
|
| 700 | // A plant this large is left alone rather than walked in full.
|
| 701 | if (seen.size() > 1000) {
|
| 702 | return true;
|
| 703 | }
|
| 704 | if (!seen.add(pos.immutable())) {
|
| 705 | return false;
|
| 706 | }
|
| 707 | for (Direction side : Direction.values()) {
|
| 708 | if (side == Direction.DOWN
|
| 709 | || !state.getValueOrElse(PipeBlock.PROPERTY_BY_DIRECTION.get(side), false)) {
|
| 710 | continue;
|
| 711 | }
|
| 712 | BlockPos next = pos.relative(side);
|
| 713 | BlockState nextState = BlockUtil.state(next);
|
| 714 | if (nextState.is(Blocks.CHORUS_FLOWER)) {
|
| 715 | return true;
|
| 716 | }
|
| 717 | if (!nextState.is(Blocks.CHORUS_PLANT)) {
|
| 718 | continue;
|
| 719 | }
|
| 720 | // A sideways branch that also reaches down hangs off the main stem.
|
| 721 | if (side.getAxis().isHorizontal()
|
| 722 | && nextState.getValueOrElse(PipeBlock.DOWN, false)) {
|
| 723 | continue;
|
| 724 | }
|
| 725 | if (holdsFlower(next, nextState, seen)) {
|
| 726 | return true;
|
| 727 | }
|
| 728 | }
|
| 729 | return false;
|
| 730 | }
|
| 731 | }
|
| 732 |
|