From 647423bf3762aa70c42cfea24f911fa1b959fda1 Mon Sep 17 00:00:00 2001 From: flier268 Date: Mon, 18 Dec 2023 00:41:48 +0800 Subject: [PATCH] Support all of animal --- .../com/flier268/autoharvest/CropManager.java | 66 ------------------- .../flier268/autoharvest/TickListener.java | 58 ++++++---------- 2 files changed, 20 insertions(+), 104 deletions(-) diff --git a/src/main/java/com/flier268/autoharvest/CropManager.java b/src/main/java/com/flier268/autoharvest/CropManager.java index 1202ff8..d446b2f 100644 --- a/src/main/java/com/flier268/autoharvest/CropManager.java +++ b/src/main/java/com/flier268/autoharvest/CropManager.java @@ -93,72 +93,6 @@ public class CropManager { } }); - public static final Multimap> FEED_MAP; - public static final Multimap> SHEAR_MAP; - static { - FEED_MAP = ArrayListMultimap.create(); - FEED_MAP.put(Items.GOLDEN_CARROT, HorseEntity.class); - - FEED_MAP.put(Items.WHEAT, SheepEntity.class); - FEED_MAP.put(Items.WHEAT, CowEntity.class); - FEED_MAP.put(Items.WHEAT, MooshroomEntity.class); - - FEED_MAP.put(Items.CARROT, PigEntity.class); - FEED_MAP.put(Items.POTATO, PigEntity.class); - FEED_MAP.put(Items.BEETROOT, PigEntity.class); - - FEED_MAP.put(Items.PUMPKIN_SEEDS, ChickenEntity.class); - FEED_MAP.put(Items.MELON_SEEDS, ChickenEntity.class); - FEED_MAP.put(Items.WHEAT_SEEDS, ChickenEntity.class); - FEED_MAP.put(Items.BEETROOT_SEEDS, ChickenEntity.class); - - FEED_MAP.put(Items.ROTTEN_FLESH, WolfEntity.class); - - FEED_MAP.put(Items.DANDELION, RabbitEntity.class); - FEED_MAP.put(Items.CARROT, RabbitEntity.class); - FEED_MAP.put(Items.WHEAT_SEEDS, ParrotEntity.class); - - // 1.13 - FEED_MAP.put(Items.SEAGRASS, TurtleEntity.class); - - // 1.14 - FEED_MAP.put(Items.KELP, PandaEntity.class); - FEED_MAP.put(Items.SWEET_BERRIES, FoxEntity.class); - FEED_MAP.put(Items.COD, CatEntity.class); - FEED_MAP.put(Items.SALMON, CatEntity.class); - - // 1.15 - FEED_MAP.put(Items.DANDELION, BeeEntity.class); - FEED_MAP.put(Items.POPPY, BeeEntity.class); - FEED_MAP.put(Items.BLUE_ORCHID, BeeEntity.class); - FEED_MAP.put(Items.ALLIUM, BeeEntity.class); - FEED_MAP.put(Items.AZURE_BLUET, BeeEntity.class); - FEED_MAP.put(Items.RED_TULIP, BeeEntity.class); - FEED_MAP.put(Items.ORANGE_TULIP, BeeEntity.class); - FEED_MAP.put(Items.WHITE_TULIP, BeeEntity.class); - FEED_MAP.put(Items.PINK_TULIP, BeeEntity.class); - FEED_MAP.put(Items.OXEYE_DAISY, BeeEntity.class); - FEED_MAP.put(Items.CORNFLOWER, BeeEntity.class); - FEED_MAP.put(Items.LILY_OF_THE_VALLEY, BeeEntity.class); - FEED_MAP.put(Items.WITHER_ROSE, BeeEntity.class); - FEED_MAP.put(Items.SUNFLOWER, BeeEntity.class); - FEED_MAP.put(Items.LILAC, BeeEntity.class); - FEED_MAP.put(Items.ROSE_BUSH, BeeEntity.class); - FEED_MAP.put(Items.PEONY, BeeEntity.class); - - // 1.16 - FEED_MAP.put(Items.WARPED_FUNGUS, StriderEntity.class); - FEED_MAP.put(Items.CRIMSON_FUNGUS, HoglinEntity.class); - - // 1.17 - FEED_MAP.put(Items.WHEAT, GoatEntity.class); - // disabled due to complexity of interaction - // FEED_MAP.put(Items.TROPICAL_FISH_BUCKET, AxolotlEntity.class); - - SHEAR_MAP = ArrayListMultimap.create(); - SHEAR_MAP.put(Items.SHEARS, SheepEntity.class); - } - public static boolean isWeedBlock(World w, BlockPos pos) { Block b = w.getBlockState(pos).getBlock(); return WEED_BLOCKS.contains(b); diff --git a/src/main/java/com/flier268/autoharvest/TickListener.java b/src/main/java/com/flier268/autoharvest/TickListener.java index b60794b..27d4c18 100644 --- a/src/main/java/com/flier268/autoharvest/TickListener.java +++ b/src/main/java/com/flier268/autoharvest/TickListener.java @@ -1,7 +1,5 @@ package com.flier268.autoharvest; -import java.util.Collection; - import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -206,7 +204,7 @@ private void plantTick() { for (int deltaZ = -configure.effect_radius.value; deltaZ <= configure.effect_radius.value; ++deltaZ) { BlockPos pos = new BlockPos(X + deltaX, Y, Z + deltaZ); if (CropManager.canPaint(w.getBlockState(pos), handItem) == false) - continue; + continue; if (CropManager.canPlantOn(handItem.getItem(), w, pos)) { if (w.getBlockState(pos.down()).getBlock() == Blocks.KELP) continue; @@ -216,7 +214,7 @@ private void plantTick() { BlockHitResult blockHitResult = new BlockHitResult(new Vec3d(X + deltaX + 0.5, Y, Z + deltaZ + 0.5), Direction.UP, downPos, false); MinecraftClient.getInstance().interactionManager.interactBlock(MinecraftClient.getInstance().player, - Hand.MAIN_HAND, blockHitResult); + Hand.MAIN_HAND, blockHitResult); minusOneInHand(); return; } @@ -314,44 +312,28 @@ private void feedTick() { p.getZ() - configure.effect_radius.value, p.getX() + configure.effect_radius.value, p.getY() + configure.effect_radius.value, p.getZ() + configure.effect_radius.value); - Collection> needShearAnimalList = CropManager.SHEAR_MAP.get(handItem.getItem()); - for (Class type : needShearAnimalList) { - for (AnimalEntity e : p.getEntityWorld().getEntitiesByClass( - type, - box, - animalEntity -> { - if (animalEntity instanceof SheepEntity) { - return !animalEntity.isBaby() && !((SheepEntity) animalEntity).isSheared(); - } - return false; - })) { - lastUsedItem = handItem.copy(); - assert MinecraftClient.getInstance().interactionManager != null; - MinecraftClient.getInstance().interactionManager.interactEntity(p, e, Hand.MAIN_HAND); - return; - } + + // Shear + var canShearSheep = p.getEntityWorld().getEntitiesByClass(SheepEntity.class, box, sheepEntity -> { + return !sheepEntity.isBaby() && !sheepEntity.isSheared(); + }); + for (SheepEntity sheepEntity : canShearSheep) { + lastUsedItem = handItem.copy(); + MinecraftClient.getInstance().interactionManager + .interactEntity(p, sheepEntity, Hand.MAIN_HAND); } - /* - * Special handling for axolotls: the food is a single use item and after it is - * used, it is replaced with a water - * bucket. The interaction is resolved on the server - if the client doesn't - * match, the next animal to be - * interacted with gets scooped up rather than fed. - */ - Collection> needFeedAnimalList = CropManager.FEED_MAP.get(handItem.getItem()); - for (Class type : needFeedAnimalList) { - for (AnimalEntity e : p.getEntityWorld().getEntitiesByClass( - type, - box, - animalEntity -> animalEntity.getBreedingAge() >= 0 && !animalEntity.isInLove())) { - lastUsedItem = handItem.copy(); - assert MinecraftClient.getInstance().interactionManager != null; - MinecraftClient.getInstance().interactionManager - .interactEntity(p, e, Hand.MAIN_HAND); + var canFeedAnimal = p.getEntityWorld().getEntitiesByClass(AnimalEntity.class, box, animalEntity -> { + if (animalEntity.isBreedingItem(handItem) && animalEntity.getBreedingAge() >= 0 && animalEntity.canEat()) { + return true; } + return false; + }); + for (AnimalEntity animalEntity : canFeedAnimal) { + lastUsedItem = handItem.copy(); + MinecraftClient.getInstance().interactionManager + .interactEntity(p, animalEntity, Hand.MAIN_HAND); } - } private long getWorldTime() {