From 36ccf3bcab2c36eb6867a96995be8b014baa1150 Mon Sep 17 00:00:00 2001 From: Articdive <13535885+Articdive@users.noreply.github.com> Date: Fri, 7 Aug 2020 13:21:59 +0200 Subject: [PATCH] Create EatBlockGoal --- .../themode/demo/entity/ChickenCreature.java | 17 ++++ .../server/entity/ai/goal/EatBlockGoal.java | 86 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java diff --git a/src/main/java/fr/themode/demo/entity/ChickenCreature.java b/src/main/java/fr/themode/demo/entity/ChickenCreature.java index 69da988cb..a1b354089 100644 --- a/src/main/java/fr/themode/demo/entity/ChickenCreature.java +++ b/src/main/java/fr/themode/demo/entity/ChickenCreature.java @@ -1,11 +1,15 @@ package fr.themode.demo.entity; import net.minestom.server.attribute.Attribute; +import net.minestom.server.entity.ai.goal.EatBlockGoal; import net.minestom.server.entity.ai.goal.RandomStrollGoal; import net.minestom.server.entity.ai.target.PlayerTarget; import net.minestom.server.entity.type.EntityChicken; +import net.minestom.server.instance.block.Block; import net.minestom.server.utils.Position; +import java.util.HashMap; + public class ChickenCreature extends EntityChicken { public ChickenCreature(Position defaultPosition) { @@ -13,6 +17,19 @@ public class ChickenCreature extends EntityChicken { //goalSelectors.add(new DoNothingGoal(this, 500, 0.1f)); goalSelectors.add(new RandomStrollGoal(this, 2)); + goalSelectors.add(new EatBlockGoal(this, + new HashMap<>() { + { + put(Block.GRASS.getBlockId(), Block.AIR.getBlockId()); + } + }, + new HashMap<>() { + { + put(Block.GRASS_BLOCK.getBlockId(), Block.DIRT.getBlockId()); + } + }, + 100)) + ; //goalSelectors.add(new FollowTargetGoal(this)); diff --git a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java new file mode 100644 index 000000000..162b4c6eb --- /dev/null +++ b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java @@ -0,0 +1,86 @@ +package net.minestom.server.entity.ai.goal; + +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.ai.GoalSelector; +import net.minestom.server.instance.Instance; +import net.minestom.server.utils.BlockPosition; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.Random; + +public class EatBlockGoal extends GoalSelector { + private static final Random RANDOM = new Random(); + private final Map eatBelowMap; + private final Map eatInMap; + private int chancePerTick; + private int eatAnimationTick; + + /** + * @param entityCreature Creature that should eat a block. + * @param eatInMap Map containing the block IDs that the entity can eat (when inside the block) and the block ID of the replacement block. + * @param eatBelowMap Map containing block IDs that the entity can eat (when above the block) and the block ID of the replacement block. + * @param chancePerTick The chance that the entity eats. Settings this to N would mean there is a 1 in N chance. + */ + public EatBlockGoal( + @NotNull EntityCreature entityCreature, + @NotNull Map eatInMap, + @NotNull Map eatBelowMap, + int chancePerTick) { + super(entityCreature); + this.eatInMap = eatInMap; + this.eatBelowMap = eatBelowMap; + this.chancePerTick = chancePerTick; + } + + @Override + public boolean shouldStart() { + // TODO: is Baby + if (RANDOM.nextInt(chancePerTick) != 0) { + return false; + } + Instance instance = entityCreature.getInstance(); + final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1,0)); + final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0,2,0)); + + return eatInMap.containsKey(blockIdIn) || eatBelowMap.containsKey(blockIdBelow); + } + + @Override + public void start() { + this.eatAnimationTick = 40; + // TODO: EatBlockEvent call here. + // Stop moving + entityCreature.setPathTo(null); + } + + @Override + public void tick() { + this.eatAnimationTick = Math.max(0, this.eatAnimationTick - 1); + if (this.eatAnimationTick != 4) { + return; + } + Instance instance = entityCreature.getInstance(); + final BlockPosition currentPosition = entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1, 0); + final BlockPosition belowPosition = currentPosition.clone().subtract(0, 1, 0); + + final short blockIdIn = instance.getBlockId(currentPosition); + final short blockIdBelow = instance.getBlockId(belowPosition); + if (eatInMap.containsKey(blockIdIn)) { + instance.setBlock(currentPosition, eatInMap.get(blockIdIn)); + } else if (eatBelowMap.containsKey(blockIdBelow)) { + instance.setBlock(belowPosition, eatBelowMap.get(blockIdBelow)); + } + // TODO: Call Entity Eat Animation + } + + @Override + public boolean shouldEnd() { + return eatAnimationTick <= 0; + } + + @Override + public void end() { + this.eatAnimationTick = 0; + } +}