2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.entity;
|
2019-08-10 08:44:35 +02:00
|
|
|
|
2020-07-24 02:31:10 +02:00
|
|
|
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
2020-09-01 21:16:07 +02:00
|
|
|
import com.extollit.gaming.ai.path.model.IPath;
|
2020-07-31 18:55:08 +02:00
|
|
|
import net.minestom.server.MinecraftServer;
|
2020-07-24 19:31:15 +02:00
|
|
|
import net.minestom.server.attribute.Attribute;
|
2020-08-06 11:56:43 +02:00
|
|
|
import net.minestom.server.entity.ai.GoalSelector;
|
|
|
|
import net.minestom.server.entity.ai.TargetSelector;
|
2020-07-24 02:49:55 +02:00
|
|
|
import net.minestom.server.entity.pathfinding.PFPathingEntity;
|
2020-05-28 19:15:55 +02:00
|
|
|
import net.minestom.server.event.entity.EntityAttackEvent;
|
2020-05-07 15:46:21 +02:00
|
|
|
import net.minestom.server.event.item.ArmorEquipEvent;
|
2020-08-06 12:33:45 +02:00
|
|
|
import net.minestom.server.instance.Chunk;
|
2020-07-24 02:31:10 +02:00
|
|
|
import net.minestom.server.instance.Instance;
|
2020-08-06 12:33:45 +02:00
|
|
|
import net.minestom.server.instance.WorldBorder;
|
2020-05-06 22:35:32 +02:00
|
|
|
import net.minestom.server.item.ItemStack;
|
2020-08-07 06:36:03 +02:00
|
|
|
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
|
|
|
|
import net.minestom.server.network.packet.server.play.EntityPacket;
|
|
|
|
import net.minestom.server.network.packet.server.play.SpawnLivingEntityPacket;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.network.player.PlayerConnection;
|
|
|
|
import net.minestom.server.utils.Position;
|
|
|
|
import net.minestom.server.utils.Vector;
|
2020-05-25 13:46:48 +02:00
|
|
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
2020-05-23 04:20:01 +02:00
|
|
|
import net.minestom.server.utils.item.ItemStackUtils;
|
2020-05-15 18:03:28 +02:00
|
|
|
import net.minestom.server.utils.time.TimeUnit;
|
2020-07-24 19:31:15 +02:00
|
|
|
import net.minestom.server.utils.validate.Check;
|
2019-08-10 08:44:35 +02:00
|
|
|
|
2020-08-06 11:56:43 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-08-09 05:45:26 +02:00
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
2020-08-06 11:56:43 +02:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2020-04-09 14:25:42 +02:00
|
|
|
public abstract class EntityCreature extends LivingEntity {
|
2020-03-29 20:58:30 +02:00
|
|
|
|
2020-09-24 01:50:25 +02:00
|
|
|
private final PFPathingEntity pathingEntity = new PFPathingEntity(this);
|
2020-07-24 02:31:10 +02:00
|
|
|
private HydrazinePathFinder pathFinder;
|
2020-09-01 21:16:07 +02:00
|
|
|
private IPath path;
|
2020-08-06 13:46:30 +02:00
|
|
|
private Position pathPosition;
|
2020-03-29 20:58:30 +02:00
|
|
|
|
2020-08-06 11:56:43 +02:00
|
|
|
protected List<GoalSelector> goalSelectors = new ArrayList<>();
|
|
|
|
protected List<TargetSelector> targetSelectors = new ArrayList<>();
|
|
|
|
private GoalSelector currentGoalSelector;
|
|
|
|
|
|
|
|
private Entity target;
|
|
|
|
|
2020-05-06 22:35:32 +02:00
|
|
|
// Equipments
|
|
|
|
private ItemStack mainHandItem;
|
|
|
|
private ItemStack offHandItem;
|
|
|
|
|
|
|
|
private ItemStack helmet;
|
|
|
|
private ItemStack chestplate;
|
|
|
|
private ItemStack leggings;
|
|
|
|
private ItemStack boots;
|
|
|
|
|
2020-09-24 01:50:25 +02:00
|
|
|
private final ReentrantLock pathLock = new ReentrantLock();
|
2020-08-09 05:45:26 +02:00
|
|
|
|
2020-05-06 22:35:32 +02:00
|
|
|
|
2020-04-09 14:25:42 +02:00
|
|
|
public EntityCreature(EntityType entityType, Position spawnPosition) {
|
2020-05-25 01:12:12 +02:00
|
|
|
super(entityType, spawnPosition);
|
2020-05-23 04:20:01 +02:00
|
|
|
|
|
|
|
this.mainHandItem = ItemStack.getAirItem();
|
|
|
|
this.offHandItem = ItemStack.getAirItem();
|
|
|
|
|
|
|
|
this.helmet = ItemStack.getAirItem();
|
|
|
|
this.chestplate = ItemStack.getAirItem();
|
|
|
|
this.leggings = ItemStack.getAirItem();
|
|
|
|
this.boots = ItemStack.getAirItem();
|
2020-05-25 19:54:36 +02:00
|
|
|
|
|
|
|
heal();
|
2019-08-10 08:44:35 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:34:01 +02:00
|
|
|
@Override
|
2020-06-01 00:51:31 +02:00
|
|
|
public void update(long time) {
|
2020-08-06 11:56:43 +02:00
|
|
|
|
2020-08-06 18:32:56 +02:00
|
|
|
if (getInstance() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:13:01 +02:00
|
|
|
// Goal selectors
|
2020-08-06 11:56:43 +02:00
|
|
|
{
|
|
|
|
// Supplier used to get the next goal selector which should start
|
|
|
|
// (null if not found)
|
|
|
|
final Supplier<GoalSelector> goalSelectorSupplier = () -> {
|
|
|
|
for (GoalSelector goalSelector : goalSelectors) {
|
|
|
|
final boolean start = goalSelector.shouldStart();
|
|
|
|
if (start) {
|
|
|
|
return goalSelector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// true if the goal selector changed this tick
|
|
|
|
boolean newGoalSelector = false;
|
|
|
|
|
|
|
|
if (currentGoalSelector == null) {
|
|
|
|
// No goal selector, get a new one
|
|
|
|
this.currentGoalSelector = goalSelectorSupplier.get();
|
|
|
|
newGoalSelector = currentGoalSelector != null;
|
|
|
|
} else {
|
|
|
|
final boolean stop = currentGoalSelector.shouldEnd();
|
|
|
|
if (stop) {
|
|
|
|
// The current goal selector stopped, find a new one
|
|
|
|
this.currentGoalSelector.end();
|
|
|
|
this.currentGoalSelector = goalSelectorSupplier.get();
|
|
|
|
newGoalSelector = currentGoalSelector != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the new goal selector
|
|
|
|
if (newGoalSelector) {
|
|
|
|
this.currentGoalSelector.start();
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:13:01 +02:00
|
|
|
// Execute tick for the current goal selector
|
2020-08-06 11:56:43 +02:00
|
|
|
if (currentGoalSelector != null) {
|
2020-08-09 05:13:36 +02:00
|
|
|
currentGoalSelector.tick(time);
|
2020-08-06 11:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:25:42 +02:00
|
|
|
// Path finding
|
2020-08-09 05:45:26 +02:00
|
|
|
{
|
2020-08-12 07:46:07 +02:00
|
|
|
if (pathPosition != null) {
|
|
|
|
this.pathLock.lock();
|
|
|
|
this.path = pathFinder.updatePathFor(pathingEntity);
|
|
|
|
|
|
|
|
if (path != null) {
|
|
|
|
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
|
|
|
|
final Position targetPosition = pathingEntity.getTargetPosition();
|
|
|
|
moveTowards(targetPosition, speed);
|
|
|
|
} else {
|
|
|
|
if (pathPosition != null) {
|
|
|
|
this.pathPosition = null;
|
|
|
|
this.pathFinder.reset();
|
2020-08-09 21:56:01 +02:00
|
|
|
}
|
2020-08-12 07:46:07 +02:00
|
|
|
}
|
2020-08-09 21:56:01 +02:00
|
|
|
|
2020-08-12 07:46:07 +02:00
|
|
|
this.pathLock.unlock();
|
2020-08-07 06:36:03 +02:00
|
|
|
}
|
2020-07-24 02:49:55 +02:00
|
|
|
}
|
2020-07-31 18:55:08 +02:00
|
|
|
|
|
|
|
super.update(time);
|
2020-07-24 02:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInstance(Instance instance) {
|
|
|
|
super.setInstance(instance);
|
|
|
|
this.pathFinder = new HydrazinePathFinder(pathingEntity, instance.getInstanceSpace());
|
2019-08-24 20:34:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 22:53:58 +02:00
|
|
|
@Override
|
|
|
|
public void spawn() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-24 20:34:01 +02:00
|
|
|
@Override
|
2019-08-21 16:50:52 +02:00
|
|
|
public void kill() {
|
2020-04-05 10:15:21 +02:00
|
|
|
super.kill();
|
|
|
|
|
|
|
|
// Needed for proper death animation (wait for it to finish before destroying the entity)
|
2020-05-15 18:03:28 +02:00
|
|
|
scheduleRemove(1000, TimeUnit.MILLISECOND);
|
2019-08-11 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 17:04:19 +02:00
|
|
|
@Override
|
2020-05-23 17:57:56 +02:00
|
|
|
public boolean addViewer(Player player) {
|
2020-07-24 02:31:10 +02:00
|
|
|
final boolean result = super.addViewer(player);
|
2020-05-29 02:11:41 +02:00
|
|
|
|
2020-08-12 07:46:07 +02:00
|
|
|
final PlayerConnection playerConnection = player.getPlayerConnection();
|
2019-08-10 08:44:35 +02:00
|
|
|
|
|
|
|
EntityPacket entityPacket = new EntityPacket();
|
|
|
|
entityPacket.entityId = getEntityId();
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2020-03-29 20:58:30 +02:00
|
|
|
SpawnLivingEntityPacket spawnLivingEntityPacket = new SpawnLivingEntityPacket();
|
|
|
|
spawnLivingEntityPacket.entityId = getEntityId();
|
|
|
|
spawnLivingEntityPacket.entityUuid = getUuid();
|
2020-08-03 18:45:58 +02:00
|
|
|
spawnLivingEntityPacket.entityType = getEntityType().getId();
|
2020-03-29 20:58:30 +02:00
|
|
|
spawnLivingEntityPacket.position = getPosition();
|
|
|
|
spawnLivingEntityPacket.headPitch = 0;
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2019-08-10 08:44:35 +02:00
|
|
|
playerConnection.sendPacket(entityPacket);
|
2020-03-29 20:58:30 +02:00
|
|
|
playerConnection.sendPacket(spawnLivingEntityPacket);
|
2020-05-24 19:22:58 +02:00
|
|
|
playerConnection.sendPacket(getVelocityPacket());
|
2020-02-11 16:48:06 +01:00
|
|
|
playerConnection.sendPacket(getMetadataPacket());
|
2020-05-24 19:22:58 +02:00
|
|
|
|
2020-05-25 01:12:12 +02:00
|
|
|
// Equipments synchronization
|
2020-05-30 21:56:12 +02:00
|
|
|
syncEquipments(playerConnection);
|
2020-05-25 01:12:12 +02:00
|
|
|
|
2020-05-24 19:22:58 +02:00
|
|
|
if (hasPassenger()) {
|
|
|
|
playerConnection.sendPacket(getPassengersPacket());
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:57:56 +02:00
|
|
|
return result;
|
2019-08-10 08:44:35 +02:00
|
|
|
}
|
2020-04-09 14:25:42 +02:00
|
|
|
|
2020-08-06 11:56:43 +02:00
|
|
|
/**
|
|
|
|
* Get the goal selectors of this entity
|
|
|
|
*
|
|
|
|
* @return a modifiable list containing the entity goal selectors
|
|
|
|
*/
|
|
|
|
public List<GoalSelector> getGoalSelectors() {
|
|
|
|
return goalSelectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the target selectors of this entity
|
|
|
|
*
|
|
|
|
* @return a modifiable list containing the entity target selectors
|
|
|
|
*/
|
|
|
|
public List<TargetSelector> getTargetSelectors() {
|
|
|
|
return targetSelectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the entity target
|
|
|
|
*
|
|
|
|
* @return the entity target
|
|
|
|
*/
|
|
|
|
public Entity getTarget() {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the entity target
|
|
|
|
*
|
|
|
|
* @param target the new entity target
|
|
|
|
*/
|
|
|
|
public void setTarget(Entity target) {
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:35:32 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack getItemInMainHand() {
|
|
|
|
return mainHandItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setItemInMainHand(ItemStack itemStack) {
|
2020-05-23 04:20:01 +02:00
|
|
|
this.mainHandItem = ItemStackUtils.notNull(itemStack);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getItemInOffHand() {
|
|
|
|
return offHandItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setItemInOffHand(ItemStack itemStack) {
|
2020-05-23 04:20:01 +02:00
|
|
|
this.offHandItem = ItemStackUtils.notNull(itemStack);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getHelmet() {
|
|
|
|
return helmet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setHelmet(ItemStack itemStack) {
|
2020-05-06 22:42:04 +02:00
|
|
|
this.helmet = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.HELMET);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getChestplate() {
|
|
|
|
return chestplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setChestplate(ItemStack itemStack) {
|
2020-05-06 22:42:04 +02:00
|
|
|
this.chestplate = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.CHESTPLATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getLeggings() {
|
|
|
|
return leggings;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setLeggings(ItemStack itemStack) {
|
2020-05-06 22:42:04 +02:00
|
|
|
this.leggings = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.LEGGINGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getBoots() {
|
|
|
|
return boots;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setBoots(ItemStack itemStack) {
|
2020-05-06 22:42:04 +02:00
|
|
|
this.boots = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
|
2020-05-06 22:35:32 +02:00
|
|
|
syncEquipment(EntityEquipmentPacket.Slot.BOOTS);
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:15:55 +02:00
|
|
|
/**
|
2020-06-03 15:17:34 +02:00
|
|
|
* Call a {@link EntityAttackEvent} with this entity as the source and {@code target} as the target.
|
2020-05-28 19:15:55 +02:00
|
|
|
*
|
2020-06-03 15:17:34 +02:00
|
|
|
* @param target the entity target
|
|
|
|
* @param swingHand true to swing the entity main hand, false otherwise
|
2020-05-28 19:15:55 +02:00
|
|
|
*/
|
2020-06-03 15:17:34 +02:00
|
|
|
public void attack(Entity target, boolean swingHand) {
|
|
|
|
if (swingHand)
|
|
|
|
swingMainHand();
|
2020-05-28 19:15:55 +02:00
|
|
|
EntityAttackEvent attackEvent = new EntityAttackEvent(this, target);
|
|
|
|
callEvent(EntityAttackEvent.class, attackEvent);
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:17:34 +02:00
|
|
|
/**
|
|
|
|
* Call a {@link EntityAttackEvent} with this entity as the source and {@code target} as the target.
|
|
|
|
* <p>
|
|
|
|
* This does not trigger the hand animation
|
|
|
|
*
|
|
|
|
* @param target the entity target
|
|
|
|
*/
|
|
|
|
public void attack(Entity target) {
|
|
|
|
attack(target, false);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:25:42 +02:00
|
|
|
public void jump(float height) {
|
|
|
|
// FIXME magic value
|
2020-07-29 20:13:17 +02:00
|
|
|
final Vector velocity = new Vector(0, height * 2.5f, 0);
|
2020-05-02 23:34:09 +02:00
|
|
|
setVelocity(velocity);
|
2020-04-09 14:25:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 02:49:55 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the path to {@code position} and ask the entity to follow the path
|
|
|
|
* <p>
|
2020-07-24 16:11:48 +02:00
|
|
|
* Can be set to null to reset the pathfinder
|
|
|
|
* <p>
|
2020-07-24 02:49:55 +02:00
|
|
|
* The position is cloned, if you want the entity to continually follow this position object
|
|
|
|
* you need to call this when you want the path to update
|
|
|
|
*
|
2020-07-24 16:11:48 +02:00
|
|
|
* @param position the position to find the path to, null to reset the pathfinder
|
2020-08-06 12:33:45 +02:00
|
|
|
* @return true if a path has been found
|
2020-07-24 02:49:55 +02:00
|
|
|
*/
|
2020-08-09 05:45:26 +02:00
|
|
|
public boolean setPathTo(Position position) {
|
|
|
|
if (position != null && getPathPosition() != null && position.isSimilar(getPathPosition())) {
|
|
|
|
// Tried to set path to the same target position
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 08:55:01 +02:00
|
|
|
if (pathFinder == null) {
|
|
|
|
// Unexpected error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:13:01 +02:00
|
|
|
this.pathLock.lock();
|
2020-08-10 08:55:01 +02:00
|
|
|
|
2020-07-31 22:17:24 +02:00
|
|
|
this.pathFinder.reset();
|
2020-07-24 16:11:48 +02:00
|
|
|
if (position == null) {
|
2020-08-09 21:56:01 +02:00
|
|
|
this.pathLock.unlock();
|
2020-08-06 12:33:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't path outside of the world border
|
|
|
|
final WorldBorder worldBorder = instance.getWorldBorder();
|
|
|
|
if (!worldBorder.isInside(position)) {
|
2020-08-09 21:56:01 +02:00
|
|
|
this.pathLock.unlock();
|
2020-08-06 12:33:45 +02:00
|
|
|
return false;
|
2020-07-24 16:11:48 +02:00
|
|
|
}
|
2020-08-06 12:33:45 +02:00
|
|
|
|
|
|
|
// Can't path in an unloaded chunk
|
|
|
|
final Chunk chunk = instance.getChunkAt(position);
|
2020-08-10 11:32:03 +02:00
|
|
|
if (!ChunkUtils.isLoaded(chunk)) {
|
2020-08-09 21:56:01 +02:00
|
|
|
this.pathLock.unlock();
|
2020-08-06 12:33:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-09 21:56:01 +02:00
|
|
|
final Position targetPosition = position.clone();
|
2020-08-07 06:36:03 +02:00
|
|
|
|
2020-08-09 21:56:01 +02:00
|
|
|
this.path = pathFinder.initiatePathTo(position.getX(), position.getY(), position.getZ());
|
2020-08-09 14:13:01 +02:00
|
|
|
this.pathLock.unlock();
|
2020-08-06 13:46:30 +02:00
|
|
|
final boolean success = path != null;
|
2020-08-09 21:56:01 +02:00
|
|
|
this.pathPosition = success ? targetPosition : null;
|
2020-08-06 13:46:30 +02:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the target pathfinder position
|
|
|
|
*
|
|
|
|
* @return the target pathfinder position, null if there is no one
|
|
|
|
*/
|
|
|
|
public Position getPathPosition() {
|
|
|
|
return pathPosition;
|
2020-04-25 23:51:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 19:59:50 +02:00
|
|
|
/**
|
2020-05-30 19:47:47 +02:00
|
|
|
* Used to move the entity toward {@code direction} in the X and Z axis
|
2020-05-24 19:59:50 +02:00
|
|
|
* Gravity is still applied but the entity will not attempt to jump
|
2020-07-31 21:02:37 +02:00
|
|
|
* Also update the yaw/pitch of the entity to look along 'direction'
|
2020-05-24 19:59:50 +02:00
|
|
|
*
|
|
|
|
* @param direction the targeted position
|
|
|
|
* @param speed define how far the entity will move
|
|
|
|
*/
|
2020-04-09 14:25:42 +02:00
|
|
|
public void moveTowards(Position direction, float speed) {
|
2020-07-24 19:31:15 +02:00
|
|
|
Check.notNull(direction, "The direction cannot be null");
|
2020-07-29 00:31:45 +02:00
|
|
|
final float currentX = position.getX();
|
|
|
|
final float currentZ = position.getZ();
|
|
|
|
final float targetX = direction.getX();
|
|
|
|
final float targetZ = direction.getZ();
|
2020-07-31 18:55:08 +02:00
|
|
|
final float dz = targetZ - currentZ;
|
|
|
|
final float dx = targetX - currentX;
|
|
|
|
|
|
|
|
// the purpose of these few lines is to slow down entities when they reach their destination
|
|
|
|
float distSquared = dx * dx + dz * dz;
|
2020-07-31 21:58:49 +02:00
|
|
|
if (speed > distSquared) {
|
2020-07-31 18:55:08 +02:00
|
|
|
speed = distSquared;
|
|
|
|
}
|
2020-07-27 02:28:03 +02:00
|
|
|
|
2020-07-31 18:55:08 +02:00
|
|
|
final float radians = (float) Math.atan2(dz, dx);
|
2020-07-29 00:31:45 +02:00
|
|
|
final float speedX = (float) (Math.cos(radians) * speed);
|
|
|
|
final float speedZ = (float) (Math.sin(radians) * speed);
|
2020-07-27 02:28:03 +02:00
|
|
|
|
2020-07-31 21:02:37 +02:00
|
|
|
lookAlong(dx, direction.getY(), dz);
|
|
|
|
|
2020-07-31 18:55:08 +02:00
|
|
|
// TODO: is a hard set an issue if there are other external forces at play?
|
|
|
|
final float tps = MinecraftServer.TICK_PER_SECOND;
|
|
|
|
velocity.setX(speedX * tps);
|
|
|
|
velocity.setZ(speedZ * tps);
|
2020-04-09 14:25:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 21:58:49 +02:00
|
|
|
/**
|
|
|
|
* Get the pathing entity
|
|
|
|
* <p>
|
|
|
|
* Used by the pathfinder
|
|
|
|
*
|
|
|
|
* @return the pathing entity
|
|
|
|
*/
|
|
|
|
public PFPathingEntity getPathingEntity() {
|
|
|
|
return pathingEntity;
|
|
|
|
}
|
|
|
|
|
2020-07-31 21:02:37 +02:00
|
|
|
private void lookAlong(float dx, float dy, float dz) {
|
|
|
|
final float horizontalAngle = (float) Math.atan2(dz, dx);
|
|
|
|
final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90;
|
|
|
|
final float pitch = (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
|
|
|
|
|
|
|
|
getPosition().setYaw(yaw);
|
|
|
|
getPosition().setPitch(pitch);
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:42:04 +02:00
|
|
|
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {
|
2020-05-23 04:20:01 +02:00
|
|
|
itemStack = ItemStackUtils.notNull(itemStack);
|
|
|
|
|
2020-05-14 18:59:01 +02:00
|
|
|
ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot);
|
2020-05-06 22:42:04 +02:00
|
|
|
callEvent(ArmorEquipEvent.class, armorEquipEvent);
|
|
|
|
return armorEquipEvent.getArmorItem();
|
|
|
|
}
|
2019-08-10 08:44:35 +02:00
|
|
|
}
|