Merge pull request #32 from Minestom/eat-block-goal

Create EatBlockGoal
This commit is contained in:
TheMode 2020-08-07 14:07:43 +02:00 committed by GitHub
commit 4e38b3dfb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 0 deletions

View File

@ -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));

View File

@ -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<Short, Short> eatBelowMap;
private final Map<Short, Short> 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<Short, Short> eatInMap,
@NotNull Map<Short, Short> 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;
}
}