2022-03-01 13:54:08 +01:00
|
|
|
package net.citizensnpcs.nms.v1_18_R2.entity;
|
2021-12-01 15:56:22 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Consumer;
|
2023-02-22 17:06:13 +01:00
|
|
|
|
2021-12-01 15:56:22 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
2022-03-01 13:54:08 +01:00
|
|
|
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
|
|
|
|
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer;
|
2022-01-01 18:54:27 +01:00
|
|
|
import org.bukkit.entity.EntityType;
|
2021-12-01 15:56:22 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.metadata.MetadataValue;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.util.Vector;
|
2023-02-22 17:06:13 +01:00
|
|
|
|
2021-12-01 15:56:22 +01:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
import com.mojang.datafixers.util.Pair;
|
2023-02-22 17:06:13 +01:00
|
|
|
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
|
|
import net.citizensnpcs.api.event.NPCEnderTeleportEvent;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
2023-01-01 08:26:35 +01:00
|
|
|
import net.citizensnpcs.api.npc.NPC.NPCUpdate;
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.citizensnpcs.api.trait.trait.Inventory;
|
2022-07-07 17:34:15 +02:00
|
|
|
import net.citizensnpcs.api.util.SpigotUtil;
|
2022-03-01 13:54:08 +01:00
|
|
|
import net.citizensnpcs.nms.v1_18_R2.network.EmptyNetHandler;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.network.EmptyNetworkManager;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.EmptyAdvancementDataPlayer;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.EmptyServerStatsCounter;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.NMSImpl;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.PlayerControllerJump;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.PlayerMoveControl;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.PlayerNavigation;
|
|
|
|
import net.citizensnpcs.nms.v1_18_R2.util.PlayerlistTracker;
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.citizensnpcs.npc.CitizensNPC;
|
|
|
|
import net.citizensnpcs.npc.ai.NPCHolder;
|
|
|
|
import net.citizensnpcs.npc.skin.SkinPacketTracker;
|
|
|
|
import net.citizensnpcs.npc.skin.SkinnableEntity;
|
|
|
|
import net.citizensnpcs.trait.Gravity;
|
|
|
|
import net.citizensnpcs.trait.SkinTrait;
|
2022-11-28 16:10:50 +01:00
|
|
|
import net.citizensnpcs.util.EmptySocket;
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.citizensnpcs.util.NMS;
|
|
|
|
import net.citizensnpcs.util.Util;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.network.chat.TextComponent;
|
|
|
|
import net.minecraft.network.protocol.Packet;
|
|
|
|
import net.minecraft.network.protocol.PacketFlow;
|
|
|
|
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
|
|
import net.minecraft.server.level.ServerPlayerGameMode;
|
2022-06-24 06:43:23 +02:00
|
|
|
import net.minecraft.sounds.SoundEvent;
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.minecraft.stats.ServerStatsCounter;
|
|
|
|
import net.minecraft.world.damagesource.DamageSource;
|
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
import net.minecraft.world.entity.EquipmentSlot;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.Attribute;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeMap;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
|
|
|
import net.minecraft.world.entity.ai.attributes.Attributes;
|
|
|
|
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.GameType;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.pathfinder.BlockPathTypes;
|
2022-11-10 14:31:32 +01:00
|
|
|
import net.minecraft.world.phys.AABB;
|
2021-12-01 15:56:22 +01:00
|
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
|
|
|
|
public class EntityHumanNPC extends ServerPlayer implements NPCHolder, SkinnableEntity {
|
|
|
|
private PlayerControllerJump controllerJump;
|
|
|
|
private PlayerMoveControl controllerMove;
|
|
|
|
private final Map<EquipmentSlot, ItemStack> equipmentCache = Maps.newEnumMap(EquipmentSlot.class);
|
|
|
|
private int jumpTicks = 0;
|
|
|
|
private final Map<BlockPathTypes, Float> malus = Maps.newEnumMap(BlockPathTypes.class);
|
|
|
|
private PlayerNavigation navigation;
|
|
|
|
private final CitizensNPC npc;
|
|
|
|
private final Location packetLocationCache = new Location(null, 0, 0, 0);
|
|
|
|
private PlayerlistTracker playerlistTracker;
|
|
|
|
private final SkinPacketTracker skinTracker;
|
|
|
|
private EmptyServerStatsCounter statsCache;
|
|
|
|
|
|
|
|
public EntityHumanNPC(MinecraftServer minecraftServer, ServerLevel world, GameProfile gameProfile, NPC npc) {
|
|
|
|
super(minecraftServer, world, gameProfile);
|
|
|
|
this.npc = (CitizensNPC) npc;
|
|
|
|
if (npc != null) {
|
|
|
|
skinTracker = new SkinPacketTracker(this);
|
|
|
|
try {
|
|
|
|
GAMEMODE_SETTING.invoke(gameMode, GameType.SURVIVAL, null);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
initialise(minecraftServer);
|
|
|
|
} else {
|
|
|
|
skinTracker = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean broadcastToPlayer(ServerPlayer entityplayer) {
|
|
|
|
if (npc != null && playerlistTracker == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return super.broadcastToPlayer(entityplayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCutCorner(BlockPathTypes pathtype) {
|
|
|
|
return (pathtype != BlockPathTypes.DANGER_FIRE && pathtype != BlockPathTypes.DANGER_CACTUS
|
|
|
|
&& pathtype != BlockPathTypes.DANGER_OTHER && pathtype != BlockPathTypes.WALKABLE_DOOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean causeFallDamage(float f, float f1, DamageSource damagesource) {
|
|
|
|
if (npc == null || !npc.isFlyable()) {
|
|
|
|
return super.causeFallDamage(f, f1, damagesource);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void checkFallDamage(double d0, boolean flag, BlockState iblockdata, BlockPos blockposition) {
|
|
|
|
if (npc == null || !npc.isFlyable()) {
|
|
|
|
super.checkFallDamage(d0, flag, iblockdata, blockposition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void die(DamageSource damagesource) {
|
|
|
|
// players that die are not normally removed from the world. when the
|
|
|
|
// NPC dies, we are done with the instance and it should be removed.
|
|
|
|
if (dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.die(damagesource);
|
|
|
|
Bukkit.getScheduler().runTaskLater(CitizensAPI.getPlugin(), new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
EntityHumanNPC.this.getLevel().removePlayerImmediately(EntityHumanNPC.this, RemovalReason.KILLED);
|
|
|
|
((ServerLevel) level).getChunkSource().removeEntity(EntityHumanNPC.this);
|
|
|
|
}
|
|
|
|
}, 15); // give enough time for death and smoke animation
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dismountTo(double d0, double d1, double d2) {
|
|
|
|
if (npc == null) {
|
|
|
|
super.dismountTo(d0, d1, d2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NPCEnderTeleportEvent event = new NPCEnderTeleportEvent(npc);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
|
|
|
super.dismountTo(d0, d1, d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doTick() {
|
|
|
|
if (npc == null) {
|
|
|
|
super.doTick();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.baseTick();
|
2022-11-07 16:30:39 +01:00
|
|
|
boolean navigating = npc.getNavigator().isNavigating() || controllerMove.hasWanted();
|
2021-12-01 15:56:22 +01:00
|
|
|
if (!navigating && getBukkitEntity() != null
|
|
|
|
&& (!npc.hasTrait(Gravity.class) || npc.getOrAddTrait(Gravity.class).hasGravity())
|
2022-07-07 17:34:15 +02:00
|
|
|
&& Util.isLoaded(getBukkitEntity().getLocation(LOADED_LOCATION))
|
|
|
|
&& SpigotUtil.checkYSafe(getY(), getBukkitEntity().getWorld())) {
|
2021-12-01 15:56:22 +01:00
|
|
|
moveWithFallDamage(Vec3.ZERO);
|
|
|
|
}
|
|
|
|
Vec3 mot = getDeltaMovement();
|
|
|
|
if (Math.abs(mot.x) < EPSILON && Math.abs(mot.y) < EPSILON && Math.abs(mot.z) < EPSILON) {
|
|
|
|
setDeltaMovement(Vec3.ZERO);
|
|
|
|
}
|
|
|
|
if (navigating) {
|
2022-02-19 06:35:16 +01:00
|
|
|
if (!navigation.isDone()) {
|
|
|
|
navigation.tick();
|
2021-12-01 15:56:22 +01:00
|
|
|
}
|
|
|
|
moveOnCurrentHeading();
|
|
|
|
}
|
2023-01-05 15:51:42 +01:00
|
|
|
updateAI();
|
2021-12-01 15:56:22 +01:00
|
|
|
if (isSpectator()) {
|
|
|
|
this.noPhysics = true;
|
|
|
|
this.onGround = false;
|
|
|
|
}
|
2022-02-20 14:21:50 +01:00
|
|
|
if (npc.data().get(NPC.Metadata.COLLIDABLE, !npc.isProtected())) {
|
|
|
|
pushEntities();
|
|
|
|
}
|
2022-11-10 14:31:32 +01:00
|
|
|
if (npc.data().get(NPC.Metadata.PICKUP_ITEMS, !npc.isProtected())) {
|
|
|
|
AABB axisalignedbb;
|
|
|
|
if (this.isPassenger() && !this.getVehicle().isRemoved()) {
|
|
|
|
axisalignedbb = this.getBoundingBox().minmax(this.getVehicle().getBoundingBox()).inflate(1.0, 0.0, 1.0);
|
|
|
|
} else {
|
|
|
|
axisalignedbb = this.getBoundingBox().inflate(1.0, 0.5, 1.0);
|
|
|
|
}
|
|
|
|
for (Entity entity : this.level.getEntities(this, axisalignedbb)) {
|
|
|
|
entity.playerTouch(this);
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 15:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet<?> getAddEntityPacket() {
|
|
|
|
if (playerlistTracker != null) {
|
|
|
|
playerlistTracker.updateLastPlayer();
|
|
|
|
}
|
|
|
|
return super.getAddEntityPacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CraftPlayer getBukkitEntity() {
|
|
|
|
if (npc != null && !(super.getBukkitEntity() instanceof NPCHolder)) {
|
|
|
|
NMSImpl.setBukkitEntity(this, new PlayerNPC(this));
|
|
|
|
}
|
|
|
|
return super.getBukkitEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerControllerJump getControllerJump() {
|
|
|
|
return controllerJump;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:43:23 +02:00
|
|
|
@Override
|
|
|
|
protected SoundEvent getDeathSound() {
|
2023-01-01 08:26:35 +01:00
|
|
|
return NMSImpl.getSoundEffect(npc, super.getDeathSound(), NPC.Metadata.DEATH_SOUND);
|
2022-06-24 06:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SoundEvent getHurtSound(DamageSource damagesource) {
|
2023-01-01 08:26:35 +01:00
|
|
|
return NMSImpl.getSoundEffect(npc, super.getHurtSound(damagesource), NPC.Metadata.HURT_SOUND);
|
2022-06-24 06:43:23 +02:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:56:22 +01:00
|
|
|
public PlayerMoveControl getMoveControl() {
|
|
|
|
return controllerMove;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PathNavigation getNavigation() {
|
|
|
|
return navigation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPC getNPC() {
|
|
|
|
return npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getPathfindingMalus(BlockPathTypes pathtype) {
|
|
|
|
return this.malus.containsKey(pathtype) ? this.malus.get(pathtype) : pathtype.getMalus();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GameProfile getProfile() {
|
|
|
|
return super.getGameProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSkinName() {
|
|
|
|
String skinName = npc.getOrAddTrait(SkinTrait.class).getSkinName();
|
|
|
|
if (skinName == null) {
|
|
|
|
skinName = npc.getName();
|
|
|
|
}
|
|
|
|
return skinName.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SkinPacketTracker getSkinTracker() {
|
|
|
|
return skinTracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ServerStatsCounter getStats() {
|
|
|
|
return this.statsCache == null ? statsCache = new EmptyServerStatsCounter() : statsCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Component getTabListDisplayName() {
|
2023-01-01 08:26:35 +01:00
|
|
|
if (Setting.DISABLE_TABLIST.asBoolean()) {
|
2021-12-01 15:56:22 +01:00
|
|
|
return new TextComponent("");
|
|
|
|
}
|
|
|
|
return super.getTabListDisplayName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hurt(DamageSource damagesource, float f) {
|
|
|
|
// knock back velocity is cancelled and sent to client for handling when
|
|
|
|
// the entity is a player. there is no client so make this happen
|
|
|
|
// manually.
|
|
|
|
boolean damaged = super.hurt(damagesource, f);
|
|
|
|
if (damaged && hurtMarked) {
|
|
|
|
hurtMarked = false;
|
|
|
|
Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
EntityHumanNPC.this.hurtMarked = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return damaged;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialise(MinecraftServer minecraftServer) {
|
|
|
|
Socket socket = new EmptySocket();
|
|
|
|
EmptyNetworkManager conn = null;
|
|
|
|
try {
|
|
|
|
conn = new EmptyNetworkManager(PacketFlow.CLIENTBOUND);
|
|
|
|
connection = new EmptyNetHandler(minecraftServer, conn, this);
|
|
|
|
conn.setListener(connection);
|
|
|
|
socket.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// swallow
|
|
|
|
}
|
|
|
|
AttributeInstance range = getAttribute(Attributes.FOLLOW_RANGE);
|
|
|
|
if (range == null) {
|
|
|
|
try {
|
|
|
|
AttributeSupplier provider = (AttributeSupplier) ATTRIBUTE_SUPPLIER.invoke(getAttributes());
|
|
|
|
Map<Attribute, AttributeInstance> all = Maps
|
|
|
|
.newHashMap((Map<Attribute, AttributeInstance>) ATTRIBUTE_PROVIDER_MAP.invoke(provider));
|
|
|
|
all.put(Attributes.FOLLOW_RANGE,
|
|
|
|
new AttributeInstance(Attributes.FOLLOW_RANGE, new Consumer<AttributeInstance>() {
|
|
|
|
@Override
|
|
|
|
public void accept(AttributeInstance att) {
|
|
|
|
throw new UnsupportedOperationException(
|
|
|
|
"Tried to change value for default attribute instance FOLLOW_RANGE");
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
ATTRIBUTE_PROVIDER_MAP_SETTER.invoke(provider, ImmutableMap.copyOf(all));
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
range = getAttribute(Attributes.FOLLOW_RANGE);
|
|
|
|
}
|
2022-03-10 12:27:00 +01:00
|
|
|
getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.3D);
|
2021-12-01 15:56:22 +01:00
|
|
|
range.setBaseValue(Setting.DEFAULT_PATHFINDING_RANGE.asDouble());
|
|
|
|
controllerJump = new PlayerControllerJump(this);
|
|
|
|
controllerMove = new PlayerMoveControl(this);
|
|
|
|
navigation = new PlayerNavigation(this, level);
|
|
|
|
this.invulnerableTime = 0;
|
|
|
|
NMS.setStepHeight(getBukkitEntity(), 1); // the default (0) breaks step climbing
|
|
|
|
setSkinFlags((byte) 0xFF);
|
|
|
|
EmptyAdvancementDataPlayer.clear(this.getAdvancements());
|
|
|
|
NMSImpl.setAdvancement(this.getBukkitEntity(),
|
|
|
|
new EmptyAdvancementDataPlayer(minecraftServer.getFixerUpper(), minecraftServer.getPlayerList(),
|
|
|
|
minecraftServer.getAdvancements(), CitizensAPI.getDataFolder().getParentFile(), this));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isInWall() {
|
|
|
|
if (npc == null || noPhysics || isSleeping()) {
|
|
|
|
return super.isInWall();
|
|
|
|
}
|
|
|
|
return Util.inBlock(getBukkitEntity());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isNavigating() {
|
|
|
|
return npc.getNavigator().isNavigating();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPushable() {
|
2023-01-01 08:26:35 +01:00
|
|
|
return npc == null ? super.isPushable() : npc.data().<Boolean> get(NPC.Metadata.COLLIDABLE, !npc.isProtected());
|
2021-12-01 15:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void moveOnCurrentHeading() {
|
|
|
|
if (jumping) {
|
|
|
|
if (onGround && jumpTicks == 0) {
|
|
|
|
jumpFromGround();
|
|
|
|
jumpTicks = 10;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
jumpTicks = 0;
|
|
|
|
}
|
|
|
|
xxa *= 0.98F;
|
|
|
|
zza *= 0.98F;
|
|
|
|
moveWithFallDamage(new Vec3(this.xxa, this.yya, this.zza));
|
|
|
|
NMS.setHeadYaw(getBukkitEntity(), getYRot());
|
|
|
|
if (jumpTicks > 0) {
|
|
|
|
jumpTicks--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void moveWithFallDamage(Vec3 vec) {
|
|
|
|
double y = getY();
|
|
|
|
travel(vec);
|
|
|
|
if (!npc.isProtected()) {
|
|
|
|
doCheckFallDamage(getY() - y, onGround);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onClimbable() {
|
|
|
|
if (npc == null || !npc.isFlyable()) {
|
|
|
|
return super.onClimbable();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void push(double x, double y, double z) {
|
|
|
|
Vector vector = Util.callPushEvent(npc, x, y, z);
|
|
|
|
if (vector != null) {
|
|
|
|
super.push(vector.getX(), vector.getY(), vector.getZ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void push(Entity entity) {
|
|
|
|
// this method is called by both the entities involved - cancelling
|
|
|
|
// it will not stop the NPC from moving.
|
|
|
|
super.push(entity);
|
|
|
|
if (npc != null) {
|
|
|
|
Util.callCollisionEvent(npc, entity.getBukkitEntity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove(RemovalReason reason) {
|
|
|
|
super.remove(reason);
|
|
|
|
getAdvancements().save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMoveDestination(double x, double y, double z, double speed) {
|
|
|
|
controllerMove.setWantedPosition(x, y, z, speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPathfindingMalus(BlockPathTypes pathtype, float f) {
|
|
|
|
this.malus.put(pathtype, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setShouldJump() {
|
|
|
|
controllerJump.jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinFlags(byte flags) {
|
|
|
|
this.getEntityData().set(net.minecraft.world.entity.player.Player.DATA_PLAYER_MODE_CUSTOMISATION, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinName(String name) {
|
|
|
|
npc.getOrAddTrait(SkinTrait.class).setSkinName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinName(String name, boolean forceUpdate) {
|
|
|
|
npc.getOrAddTrait(SkinTrait.class).setSkinName(name, forceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinPersistent(String skinName, String signature, String data) {
|
|
|
|
npc.getOrAddTrait(SkinTrait.class).setSkinPersistent(skinName, signature, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTracked(PlayerlistTracker tracker) {
|
|
|
|
this.playerlistTracker = tracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
super.tick();
|
|
|
|
if (npc == null)
|
|
|
|
return;
|
|
|
|
noPhysics = isSpectator();
|
|
|
|
Bukkit.getServer().getPluginManager().unsubscribeFromPermission("bukkit.broadcast.user", getBukkitEntity());
|
|
|
|
updatePackets(npc.getNavigator().isNavigating());
|
|
|
|
npc.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void travel(Vec3 vec3d) {
|
|
|
|
if (npc == null || !npc.isFlyable()) {
|
|
|
|
super.travel(vec3d);
|
|
|
|
} else {
|
|
|
|
NMSImpl.flyingMoveLogic(this, vec3d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateAI() {
|
|
|
|
controllerMove.tick();
|
2022-02-20 14:21:50 +01:00
|
|
|
controllerJump.tick();
|
2021-12-01 15:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePackets(boolean navigating) {
|
2023-01-01 08:26:35 +01:00
|
|
|
if (!npc.isUpdating(NPCUpdate.PACKET))
|
2022-04-23 07:42:48 +02:00
|
|
|
return;
|
2023-01-01 08:26:35 +01:00
|
|
|
effectsDirty = true;
|
2021-12-01 15:56:22 +01:00
|
|
|
boolean itemChanged = false;
|
|
|
|
for (EquipmentSlot slot : EquipmentSlot.values()) {
|
|
|
|
ItemStack equipment = getItemBySlot(slot);
|
|
|
|
ItemStack cache = equipmentCache.get(slot);
|
|
|
|
if (!(cache == null && equipment == null)
|
|
|
|
&& (cache == null ^ equipment == null || !ItemStack.isSame(cache, equipment))) {
|
|
|
|
itemChanged = true;
|
|
|
|
}
|
|
|
|
equipmentCache.put(slot, equipment);
|
|
|
|
}
|
2022-04-23 07:42:48 +02:00
|
|
|
if (!itemChanged)
|
2021-12-01 15:56:22 +01:00
|
|
|
return;
|
|
|
|
Location current = getBukkitEntity().getLocation(packetLocationCache);
|
|
|
|
List<Pair<EquipmentSlot, ItemStack>> vals = Lists.newArrayList();
|
|
|
|
for (EquipmentSlot slot : EquipmentSlot.values()) {
|
|
|
|
vals.add(new Pair<EquipmentSlot, ItemStack>(slot, getItemBySlot(slot)));
|
|
|
|
}
|
|
|
|
Packet<?>[] packets = { new ClientboundSetEquipmentPacket(getId(), vals) };
|
|
|
|
NMSImpl.sendPacketsNearby(getBukkitEntity(), current, packets);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updatePathfindingRange(float pathfindingRange) {
|
|
|
|
this.navigation.setRange(pathfindingRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class PlayerNPC extends CraftPlayer implements NPCHolder, SkinnableEntity {
|
|
|
|
private final CraftServer cserver;
|
|
|
|
private final CitizensNPC npc;
|
|
|
|
|
|
|
|
private PlayerNPC(EntityHumanNPC entity) {
|
|
|
|
super((CraftServer) Bukkit.getServer(), entity);
|
|
|
|
this.npc = entity.npc;
|
|
|
|
this.cserver = (CraftServer) Bukkit.getServer();
|
|
|
|
npc.getOrAddTrait(Inventory.class);
|
|
|
|
}
|
|
|
|
|
2022-01-01 18:54:27 +01:00
|
|
|
@Override
|
|
|
|
public boolean canSee(org.bukkit.entity.Entity entity) {
|
2022-01-01 18:56:20 +01:00
|
|
|
if (entity != null && entity.getType() == EntityType.ITEM_FRAME) {
|
2022-01-01 18:54:27 +01:00
|
|
|
return false; // optimise for large maps in item frames
|
|
|
|
}
|
|
|
|
return super.canSee(entity);
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:56:22 +01:00
|
|
|
@Override
|
|
|
|
public Player getBukkitEntity() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EntityHumanNPC getHandle() {
|
|
|
|
return (EntityHumanNPC) this.entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<MetadataValue> getMetadata(String metadataKey) {
|
|
|
|
return cserver.getEntityMetadata().getMetadata(this, metadataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPC getNPC() {
|
|
|
|
return npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSkinName() {
|
|
|
|
return ((SkinnableEntity) this.entity).getSkinName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SkinPacketTracker getSkinTracker() {
|
|
|
|
return ((SkinnableEntity) this.entity).getSkinTracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasMetadata(String metadataKey) {
|
|
|
|
return cserver.getEntityMetadata().hasMetadata(this, metadataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeMetadata(String metadataKey, Plugin owningPlugin) {
|
|
|
|
cserver.getEntityMetadata().removeMetadata(this, metadataKey, owningPlugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setMetadata(String metadataKey, MetadataValue newMetadataValue) {
|
|
|
|
cserver.getEntityMetadata().setMetadata(this, metadataKey, newMetadataValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinFlags(byte flags) {
|
|
|
|
((SkinnableEntity) this.entity).setSkinFlags(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinName(String name) {
|
|
|
|
((SkinnableEntity) this.entity).setSkinName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinName(String skinName, boolean forceUpdate) {
|
|
|
|
((SkinnableEntity) this.entity).setSkinName(skinName, forceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setSkinPersistent(String skinName, String signature, String data) {
|
|
|
|
((SkinnableEntity) this.entity).setSkinPersistent(skinName, signature, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final MethodHandle ATTRIBUTE_PROVIDER_MAP = NMS.getFirstGetter(AttributeSupplier.class, Map.class);
|
|
|
|
private static final MethodHandle ATTRIBUTE_PROVIDER_MAP_SETTER = NMS.getFinalSetter(AttributeSupplier.class, "a");
|
|
|
|
private static final MethodHandle ATTRIBUTE_SUPPLIER = NMS.getFirstGetter(AttributeMap.class,
|
|
|
|
AttributeSupplier.class);
|
|
|
|
private static final float EPSILON = 0.003F;
|
|
|
|
private static final MethodHandle GAMEMODE_SETTING = NMS.getFirstMethodHandle(ServerPlayerGameMode.class, true,
|
|
|
|
GameType.class, GameType.class);
|
|
|
|
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
|
|
|
|
}
|