Create and use EntityPoseTrait

This commit is contained in:
fullwall 2024-04-29 23:13:17 +08:00
parent efd3ec54f5
commit 66c06fd641
19 changed files with 146 additions and 34 deletions

View File

@ -19,6 +19,7 @@ import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.PlayerFilter;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.model.ModelTrait;
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
@ -30,6 +31,7 @@ import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.DropsTrait;
import net.citizensnpcs.trait.EnderCrystalTrait;
import net.citizensnpcs.trait.EndermanTrait;
import net.citizensnpcs.trait.EntityPoseTrait;
import net.citizensnpcs.trait.FollowTrait;
import net.citizensnpcs.trait.GameModeTrait;
import net.citizensnpcs.trait.Gravity;
@ -82,6 +84,7 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(DropsTrait.class).optInToStats());
registerTrait(TraitInfo.create(EnderCrystalTrait.class));
registerTrait(TraitInfo.create(EndermanTrait.class));
registerTrait(TraitInfo.create(EntityPoseTrait.class));
registerTrait(TraitInfo.create(Equipment.class));
registerTrait(TraitInfo.create(FollowTrait.class).optInToStats());
registerTrait(TraitInfo.create(GameModeTrait.class));
@ -93,7 +96,7 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(ItemFrameTrait.class));
registerTrait(TraitInfo.create(LookClose.class));
registerTrait(TraitInfo.create(PaintingTrait.class));
registerTrait(TraitInfo.create(MirrorTrait.class).optInToStats());
registerTrait(TraitInfo.create(MirrorTrait.class).optInToStats());
registerTrait(TraitInfo.create(MountTrait.class));
registerTrait(TraitInfo.create(MobType.class).asDefaultTrait());
registerTrait(TraitInfo.create(OcelotModifiers.class));

View File

@ -0,0 +1,67 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
@TraitName("entitypose")
public class EntityPoseTrait extends Trait {
@Persist
private EntityPose pose;
public EntityPoseTrait() {
super("entitypose");
}
public EntityPose getPose() {
return pose;
}
@Override
public void run() {
if (pose == null || !npc.isSpawned())
return;
NMS.setPose(npc.getEntity(), pose);
}
public void setPose(EntityPose pose) {
this.pose = pose;
}
public enum EntityPose {
CROAKING(8),
CROUCHING(5),
DIGGING(14),
DYING(7),
EMERGING(13),
FALL_FLYING(1),
INHALING(17),
LONG_JUMPING(6),
ROARING(11),
SHOOTING(16),
SITTING(10),
SLEEPING(2),
SLIDING(15),
SNIFFING(12),
SPIN_ATTACK(4),
STANDING(0),
SWIMMING(3),
USING_TONGUE(9);
private final int id;
private EntityPose(int id) {
this.id = id;
}
@Deprecated
/**
* Internal minecraft ID
*/
public int id() {
return id;
}
}
}

View File

@ -6,10 +6,10 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.util.NMS;
@TraitName("sleeptrait")
@ -40,7 +40,7 @@ public class SleepTrait extends Trait {
}
if (npc.getEntity() instanceof Player) {
Player player = (Player) npc.getEntity();
npc.data().set(NPC.Metadata.ENTITY_POSE, "SLEEPING");
npc.getOrAddTrait(EntityPoseTrait.class).setPose(EntityPose.SLEEPING);
if (SUPPORT_BLOCKDATA) {
try {
if (at.getBlock().getBlockData() instanceof Bed || at.getBlock().getState() instanceof Bed) {
@ -67,7 +67,7 @@ public class SleepTrait extends Trait {
}
private void wakeup() {
npc.data().remove(NPC.Metadata.ENTITY_POSE);
npc.getOrAddTrait(EntityPoseTrait.class).setPose(null);
if (npc.getEntity() instanceof Player) {
NMS.sleep((Player) npc.getEntity(), false);
} else if (npc.getEntity() instanceof Villager) {

View File

@ -59,6 +59,7 @@ import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.PacketNPC;
import net.citizensnpcs.trait.versioned.ArmadilloTrait.ArmadilloState;
@ -879,6 +880,10 @@ public class NMS {
BRIDGE.setPolarBearRearing(entity, rearing);
}
public static void setPose(Entity entity, EntityPose pose) {
BRIDGE.setPose(entity, pose);
}
public static void setProfile(SkullMeta meta, GameProfile profile) {
BRIDGE.setProfile(meta, profile);
}
@ -978,6 +983,7 @@ public class NMS {
private static MethodHandle UNSAFE_PUT_INT;
private static MethodHandle UNSAFE_PUT_LONG;
private static MethodHandle UNSAFE_PUT_OBJECT;
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;
static {

View File

@ -41,6 +41,7 @@ import net.citizensnpcs.api.util.EntityDim;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.trait.versioned.ArmadilloTrait.ArmadilloState;
@ -255,6 +256,9 @@ public interface NMSBridge {
throw new UnsupportedOperationException();
}
public default void setPose(Entity entity, EntityPose pose) {
}
public void setProfile(SkullMeta meta, GameProfile profile);
public void setShouldJump(Entity entity);

View File

@ -35,6 +35,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -47,7 +48,6 @@ import net.minecraft.server.v1_14_R1.DamageSource;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityHuman;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EntityPose;
import net.minecraft.server.v1_14_R1.EnumGamemode;
import net.minecraft.server.v1_14_R1.EnumItemSlot;
import net.minecraft.server.v1_14_R1.EnumProtocolDirection;
@ -358,9 +358,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
dB();
}
}

View File

@ -1408,6 +1408,15 @@ public class NMSImpl implements NMSBridge {
((EntityPolarBear) getHandle(entity)).r(rearing);
}
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
try {
ENTITY_SETPOSE_METHOD.invoke(getHandle(entity),
net.minecraft.server.v1_14_R1.EntityPose.valueOf(pose.name()));
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SKULL_PROFILE_FIELD == null) {

View File

@ -34,6 +34,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -46,7 +47,6 @@ import net.minecraft.server.v1_15_R1.DamageSource;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityHuman;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.EntityPose;
import net.minecraft.server.v1_15_R1.EnumGamemode;
import net.minecraft.server.v1_15_R1.EnumItemSlot;
import net.minecraft.server.v1_15_R1.EnumProtocolDirection;
@ -404,9 +404,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
dX();
}
}

View File

@ -1450,6 +1450,15 @@ public class NMSImpl implements NMSBridge {
((EntityPolarBear) getHandle(entity)).r(rearing);
}
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
try {
ENTITY_SETPOSE_METHOD.invoke(getHandle(entity),
net.minecraft.server.v1_15_R1.EntityPose.valueOf(pose.name()));
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {

View File

@ -34,6 +34,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -45,7 +46,6 @@ import net.minecraft.server.v1_16_R3.DamageSource;
import net.minecraft.server.v1_16_R3.Entity;
import net.minecraft.server.v1_16_R3.EntityHuman;
import net.minecraft.server.v1_16_R3.EntityPlayer;
import net.minecraft.server.v1_16_R3.EntityPose;
import net.minecraft.server.v1_16_R3.EnumGamemode;
import net.minecraft.server.v1_16_R3.EnumItemSlot;
import net.minecraft.server.v1_16_R3.EnumProtocolDirection;
@ -369,9 +369,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
eu();
}
}

View File

@ -1484,6 +1484,10 @@ public class NMSImpl implements NMSBridge {
((EntityPolarBear) getHandle(entity)).t(rearing);
}
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
getHandle(entity).setPose(net.minecraft.server.v1_16_R3.EntityPose.valueOf(pose.name()));
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {

View File

@ -32,6 +32,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -48,7 +49,6 @@ import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
@ -162,9 +162,7 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
updatePlayerPose();
}
}

View File

@ -195,6 +195,7 @@ import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.AxolotlTrait;
import net.citizensnpcs.trait.versioned.BeeTrait;
@ -1481,6 +1482,11 @@ public class NMSImpl implements NMSBridge {
((PolarBear) getHandle(entity)).setStanding(rearing);
}
@Override
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
getHandle(entity).setPose(Pose.valueOf(pose.name()));
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {

View File

@ -33,6 +33,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -49,7 +50,6 @@ import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
@ -163,9 +163,7 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
updatePlayerPose();
}
}

View File

@ -196,6 +196,7 @@ import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.AxolotlTrait;
import net.citizensnpcs.trait.versioned.BeeTrait;
@ -1491,6 +1492,11 @@ public class NMSImpl implements NMSBridge {
((PolarBear) getHandle(entity)).setStanding(rearing);
}
@Override
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
getHandle(entity).setPose(Pose.valueOf(pose.name()));
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {

View File

@ -31,6 +31,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -49,7 +50,6 @@ import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.tags.TagKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
@ -156,9 +156,7 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
updatePlayerPose();
}
}

View File

@ -213,6 +213,7 @@ import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.AllayTrait;
@ -1634,6 +1635,11 @@ public class NMSImpl implements NMSBridge {
((PolarBear) getHandle(entity)).setStanding(rearing);
}
@Override
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
getHandle(entity).setPose(Pose.valueOf(pose.name()));
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {

View File

@ -29,6 +29,7 @@ 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.EntityPoseTrait;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.util.NMS;
@ -49,7 +50,6 @@ import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.tags.TagKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
@ -158,9 +158,7 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
if (!npc.hasTrait(EntityPoseTrait.class) || npc.getTraitNullable(EntityPoseTrait.class).getPose() == null) {
updatePlayerPose();
}
}

View File

@ -209,6 +209,7 @@ import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.EntityPoseTrait.EntityPose;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.AllayTrait;
@ -888,9 +889,9 @@ public class NMSImpl implements NMSBridge {
@Override
public boolean isSneaking(org.bukkit.entity.Entity entity) {
if (entity instanceof Player) {
if (entity instanceof Player)
return ((Player) entity).isSneaking();
}
return getHandle(entity).getPose() == Pose.CROUCHING;
}
@ -1633,6 +1634,11 @@ public class NMSImpl implements NMSBridge {
((PolarBear) getHandle(entity)).setStanding(rearing);
}
@Override
public void setPose(org.bukkit.entity.Entity entity, EntityPose pose) {
getHandle(entity).setPose(Pose.BY_ID.apply(pose.id()));
}
@Override
public void setProfile(SkullMeta meta, GameProfile profile) {
if (SET_PROFILE_METHOD == null) {