Skip to content

Commit

Permalink
Support all of animal
Browse files Browse the repository at this point in the history
  • Loading branch information
flier268 committed Dec 17, 2023
1 parent 5b46c50 commit 647423b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 104 deletions.
66 changes: 0 additions & 66 deletions src/main/java/com/flier268/autoharvest/CropManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,72 +93,6 @@ public class CropManager {
}
});

public static final Multimap<Item, Class<? extends AnimalEntity>> FEED_MAP;
public static final Multimap<Item, Class<? extends AnimalEntity>> 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);
Expand Down
58 changes: 20 additions & 38 deletions src/main/java/com/flier268/autoharvest/TickListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<Class<? extends AnimalEntity>> needShearAnimalList = CropManager.SHEAR_MAP.get(handItem.getItem());
for (Class<? extends AnimalEntity> 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<Class<? extends AnimalEntity>> needFeedAnimalList = CropManager.FEED_MAP.get(handItem.getItem());
for (Class<? extends AnimalEntity> 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() {
Expand Down

0 comments on commit 647423b

Please sign in to comment.