Add rough impl of SneakTrait

This commit is contained in:
fullwall 2021-09-20 20:59:54 +08:00
parent 004c46ab76
commit db825cce72
16 changed files with 137 additions and 15 deletions

View File

@ -45,6 +45,7 @@ import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.util.ChunkCoord;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
@ -316,6 +317,9 @@ public class CitizensNPC extends AbstractNPC {
if (getEntity() instanceof Player) {
NMS.replaceTrackerEntry((Player) getEntity());
PlayerUpdateTask.registerPlayer(getEntity());
if (!hasTrait(SneakTrait.class)) {
addTrait(SneakTrait.class);
}
}
}

View File

@ -50,6 +50,7 @@ import net.citizensnpcs.trait.ShopTrait;
import net.citizensnpcs.trait.SkinLayers;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.trait.SlimeSize;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.WitherTrait;
import net.citizensnpcs.trait.WolfModifiers;
@ -80,6 +81,8 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(HologramTrait.class));
registerTrait(TraitInfo.create(Inventory.class));
registerTrait(TraitInfo.create(LookClose.class));
registerTrait(TraitInfo.create(MountTrait.class));
registerTrait(TraitInfo.create(MobType.class).asDefaultTrait());
registerTrait(TraitInfo.create(OcelotModifiers.class));
registerTrait(TraitInfo.create(Owner.class));
registerTrait(TraitInfo.create(Poses.class));
@ -91,13 +94,12 @@ public class CitizensTraitFactory implements TraitFactory {
registerTrait(TraitInfo.create(SheepTrait.class));
registerTrait(TraitInfo.create(SkinLayers.class));
registerTrait(TraitInfo.create(SkinTrait.class));
registerTrait(TraitInfo.create(MountTrait.class));
registerTrait(TraitInfo.create(SneakTrait.class));
registerTrait(TraitInfo.create(SlimeSize.class));
registerTrait(TraitInfo.create(ShopTrait.class));
registerTrait(TraitInfo.create(Spawned.class));
registerTrait(TraitInfo.create(Speech.class));
registerTrait(TraitInfo.create(Text.class));
registerTrait(TraitInfo.create(MobType.class).asDefaultTrait());
registerTrait(TraitInfo.create(Waypoints.class));
registerTrait(TraitInfo.create(WitherTrait.class));
registerTrait(TraitInfo.create(WoolColor.class));

View File

@ -0,0 +1,41 @@
package net.citizensnpcs.trait;
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.util.NMS;
@TraitName("sneak")
public class SneakTrait extends Trait {
@Persist
private boolean sneaking = false;
public SneakTrait() {
super("sneak");
}
private void apply() {
NMS.setSneaking(npc.getEntity(), sneaking);
}
@Override
public void onSpawn() {
apply();
}
@Override
public void run() {
if (npc.data().has(NPC.SNEAKING_METADATA)) {
setSneaking(npc.data().get(NPC.SNEAKING_METADATA));
npc.data().remove(NPC.SNEAKING_METADATA);
}
}
public void setSneaking(boolean sneak) {
this.sneaking = sneak;
if (npc.isSpawned()) {
apply();
}
}
}

View File

@ -554,6 +554,10 @@ public class NMS {
BRIDGE.setSitting(tameable, sitting);
}
public static void setSneaking(Entity entity, boolean sneaking) {
BRIDGE.setSneaking(entity, sneaking);
}
public static void setStepHeight(org.bukkit.entity.Entity entity, float height) {
BRIDGE.setStepHeight(entity, height);
}

View File

@ -161,6 +161,8 @@ public interface NMSBridge {
public void setSitting(Tameable tameable, boolean sitting);
public void setSneaking(Entity entity, boolean sneaking);
public void setStepHeight(Entity entity, float height);
public void setTeamNameTagVisible(Team team, boolean visible);

View File

@ -200,7 +200,6 @@ import net.minecraft.server.v1_10_R1.PathType;
import net.minecraft.server.v1_10_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_10_R1.ReportedException;
import net.minecraft.server.v1_10_R1.ScoreboardTeam;
import net.minecraft.server.v1_10_R1.ScoreboardTeamBase.EnumNameTagVisibility;
import net.minecraft.server.v1_10_R1.SoundEffect;
import net.minecraft.server.v1_10_R1.Vec3D;
import net.minecraft.server.v1_10_R1.WorldServer;
@ -1030,6 +1029,13 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).P = height;

View File

@ -1086,6 +1086,13 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).P = height;

View File

@ -1094,6 +1094,13 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).P = height;

View File

@ -1132,6 +1132,13 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).Q = height;

View File

@ -1183,6 +1183,18 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
try {
ENTITY_SETPOSE.invoke(getHandle(entity), sneaking ? EntityPose.SNEAKING : EntityPose.STANDING);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).K = height;
@ -1819,6 +1831,7 @@ public class NMSImpl implements NMSBridge {
private static final MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class,
"advancementDataPlayer");
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.SILVERFISH,
EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT, EntityType.SLIME,
EntityType.DOLPHIN, EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST, EntityType.SHULKER,
@ -1837,6 +1850,8 @@ public class NMSImpl implements NMSBridge {
true, int.class);
private static final MethodHandle ENTITY_R = NMS.getMethodHandle(EntityLiving.class, "r", true, float.class);
private static CustomEntityRegistry ENTITY_REGISTRY;
private static final MethodHandle ENTITY_SETPOSE = NMS.getMethodHandle(Entity.class, "setPose", false,
EntityPose.class);
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
private static final MethodHandle GOAL_FIELD = NMS.getGetter(PathfinderGoalSelector.class, "d");
private static final MethodHandle HEAD_HEIGHT = NMS.getSetter(Entity.class, "headHeight");

View File

@ -76,7 +76,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
private final Location packetLocationCache = new Location(null, 0, 0, 0);
private PlayerlistTracker playerlistTracker;
private final SkinPacketTracker skinTracker;
private int updateCounter = 0;
public EntityHumanNPC(MinecraftServer minecraftServer, WorldServer world, GameProfile gameProfile,

View File

@ -1201,6 +1201,18 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle(tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
try {
ENTITY_SETPOSE.invoke(getHandle(entity), sneaking ? EntityPose.CROUCHING : EntityPose.STANDING);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).H = height;
@ -1873,6 +1885,7 @@ public class NMSImpl implements NMSBridge {
private static final MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class,
"advancementDataPlayer");
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.BEE,
EntityType.SILVERFISH, EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT,
EntityType.SLIME, EntityType.DOLPHIN, EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST,
@ -1889,6 +1902,8 @@ public class NMSImpl implements NMSBridge {
true, int.class);
private static final MethodHandle ENTITY_R = NMS.getMethodHandle(EntityLiving.class, "r", true, float.class);
private static CustomEntityRegistry ENTITY_REGISTRY;
private static final MethodHandle ENTITY_SETPOSE = NMS.getMethodHandle(Entity.class, "setPose", false,
EntityPose.class);
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
private static final MethodHandle GOAL_SET_FIELD = NMS.getGetter(PathfinderGoalSelector.class, "d");
private static final MethodHandle HEAD_HEIGHT = NMS.getSetter(Entity.class, "headHeight");

View File

@ -1228,6 +1228,14 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle(tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
getHandle(entity).setPose(sneaking ? EntityPose.CROUCHING : EntityPose.STANDING);
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).G = height;

View File

@ -487,16 +487,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
}
npc.update();
/*
double diff = this.yaw - this.aK;
if (diff != 40 && diff != -40) {
++this.yawUpdateRequiredTicks;
}
if (this.yawUpdateRequiredTicks > 5) {
this.yaw = (diff > -40 && diff < 0) || (diff > 0 && diff > 40) ? this.aK - 40 : this.aK + 40;
this.yawUpdateRequiredTicks = 0;
}
*/
}
@Override

View File

@ -229,6 +229,7 @@ import net.minecraft.server.level.ServerBossEvent;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.v1_16_R3.EntityPose;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
@ -1221,6 +1222,14 @@ public class NMSImpl implements NMSBridge {
((TamableAnimal) NMSImpl.getHandle(tameable)).setInSittingPose(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
getHandle(entity).setPose(sneaking ? EntityPose.CROUCHING : EntityPose.STANDING);
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).maxUpStep = height;

View File

@ -964,6 +964,13 @@ public class NMSImpl implements NMSBridge {
((EntityTameableAnimal) NMSImpl.getHandle((LivingEntity) tameable)).setSitting(sitting);
}
@Override
public void setSneaking(org.bukkit.entity.Entity entity, boolean sneaking) {
if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}
@Override
public void setStepHeight(org.bukkit.entity.Entity entity, float height) {
NMSImpl.getHandle(entity).S = height;
@ -1427,7 +1434,6 @@ public class NMSImpl implements NMSBridge {
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.SILVERFISH, EntityType.ENDERMITE,
EntityType.ENDER_DRAGON, EntityType.BAT, EntityType.SLIME, EntityType.MAGMA_CUBE, EntityType.HORSE,
EntityType.GHAST);
private static final float DEFAULT_SPEED = 1F;
private static Map<Class<?>, Integer> ENTITY_CLASS_TO_INT;
private static Map<Class<?>, String> ENTITY_CLASS_TO_NAME;