Rename /npc pose --save to /npc pose --mirror and add /npc pose --save to save the current NPC pose

This commit is contained in:
fullwall 2022-04-24 19:22:55 +08:00
parent 3bd2f7db8c
commit 324d94a21f
17 changed files with 184 additions and 12 deletions

View File

@ -1671,8 +1671,8 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "pose (--save [name] (-d)|--assume [name]|--remove [name]|--default [name]) (-a)",
desc = "Changes/Saves/Lists NPC's head pose(s)",
usage = "pose (--save [name] (-d) | --mirror [name] (-d) | --assume [name] | --remove [name] | --default [name]) (-a)",
desc = "Manage NPC poses",
flags = "ad",
modifiers = { "pose" },
min = 1,
@ -1687,14 +1687,28 @@ public class NPCCommands {
if (args.getSenderLocation() == null)
throw new ServerCommandException();
if (trait.addPose(args.getFlag("save"), args.getSenderLocation(), args.hasFlag('d'))) {
if (trait.addPose(args.getFlag("save"), npc.getStoredLocation(), args.hasFlag('d'))) {
Messaging.sendTr(sender, Messages.POSE_ADDED);
} else
} else {
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("save"));
}
} else if (args.hasValueFlag("mirror")) {
if (args.getFlag("mirror").isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
if (args.getSenderLocation() == null)
throw new ServerCommandException();
if (trait.addPose(args.getFlag("mirror"), npc.getStoredLocation(), args.hasFlag('d'))) {
Messaging.sendTr(sender, Messages.POSE_ADDED);
} else {
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("mirror"));
}
} else if (args.hasValueFlag("default")) {
String pose = args.getFlag("default");
if (!trait.hasPose(pose))
throw new CommandException(Messages.POSE_MISSING, pose);
trait.setDefaultPose(pose);
Messaging.sendTr(sender, Messages.DEFAULT_POSE_SET, pose);
} else if (args.hasValueFlag("assume")) {
@ -1704,6 +1718,7 @@ public class NPCCommands {
if (!trait.hasPose(pose))
throw new CommandException(Messages.POSE_MISSING, pose);
trait.assumePose(pose);
} else if (args.hasValueFlag("remove")) {
if (args.getFlag("remove").isEmpty())
@ -1720,8 +1735,7 @@ public class NPCCommands {
return;
if (args.getSenderLocation() == null)
throw new ServerCommandException();
Location location = args.getSenderLocation();
trait.assumePose(location);
trait.assumePose(args.getSenderLocation());
}
@Command(

View File

@ -102,6 +102,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
min = dist;
lookingAt = player;
}
if (old != lookingAt) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, old, lookingAt);
Bukkit.getPluginManager().callEvent(event);
@ -132,6 +133,10 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
return lookingAt;
}
public boolean isEnabled() {
return enabled;
}
private boolean isInvisible(Player player) {
return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY)
|| isPluginVanished(player) || !canSee(player);
@ -218,6 +223,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
if (lookingAt == null)
return;
Util.faceEntity(npc.getEntity(), lookingAt);
if (npc.getEntity().getType().name().equals("SHULKER")) {
boolean wasSilent = npc.getEntity().isSilent();

View File

@ -62,7 +62,7 @@ public class Poses extends Trait {
private void assumePose(float yaw, float pitch) {
if (!npc.isSpawned()) {
npc.spawn(npc.getOrAddTrait(CurrentLocation.class).getLocation(), SpawnReason.COMMAND);
npc.spawn(npc.getStoredLocation(), SpawnReason.COMMAND);
}
Util.setRotation(npc.getEntity(), yaw, pitch);
}
@ -127,12 +127,14 @@ public class Poses extends Trait {
@Override
public void run() {
if (!hasPose(defaultPose))
if (!hasPose(defaultPose) || npc.getNavigator().isNavigating())
return;
if (npc.hasTrait(LookClose.class)) {
LookClose trait = npc.getOrAddTrait(LookClose.class);
if (trait.isEnabled() && trait.canSeeTarget())
return;
if (!npc.getNavigator().isNavigating()
&& (!npc.hasTrait(LookClose.class) || !npc.getOrAddTrait(LookClose.class).canSeeTarget())) {
assumePose(defaultPose);
}
assumePose(defaultPose);
}
@Override

View File

@ -0,0 +1,95 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
@TraitName("rotationtrait")
public class RotationTrait extends Trait {
protected boolean rotating;
protected double tx;
protected double ty;
protected double tz;
protected float xMaxRotAngle = 10;
protected float yMaxRotSpeed = 40;
public RotationTrait() {
super("rotationtrait");
}
private double getEyeY() {
return NMS.getHeight(npc.getEntity());
}
private double getX() {
return npc.getStoredLocation().getX();
}
protected float getTargetPitchDifference() {
double dx = tx - getX();
double dy = ty - (getY() + getEyeY());
double dz = tz - getZ();
double diag = Math.sqrt((float) (dx * dx + dz * dz));
return (float) -Math.toDegrees(Math.atan2(dy, diag));
}
private double getY() {
return npc.getStoredLocation().getY();
}
protected float getTargetYawDifference() {
return (float) Math.toDegrees(Math.atan2(tz - getZ(), tx - getX())) - 90.0F;
}
private double getZ() {
return npc.getStoredLocation().getZ();
}
protected float rotateTowards(float target, float current, float maxRotPerTick) {
float diff = Util.clamp(current - target);
return target + clamp(diff, -maxRotPerTick, maxRotPerTick);
}
@Override
public void run() {
if (!npc.isSpawned() || npc.getNavigator().isNavigating()) {
// npc.yHeadRot = Mth.rotateIfNecessary(npc.yHeadRot, npc.yBodyRot, 75);
return;
}
if (true) {
// npc.setXRot(0.0F);
}
if (this.rotating) {
this.rotating = false;
NMS.setHeadYaw(npc.getEntity(),
Util.clamp(rotateTowards(NMS.getHeadYaw(npc.getEntity()), getTargetYawDifference(), this.yMaxRotSpeed)));
float d = Util.clamp(NMS.getHeadYaw(npc.getEntity()) - 40);
if (d > NMS.getYaw(npc.getEntity())) {
NMS.setBodyYaw(npc.getEntity(), d);
}
if (d != NMS.getYaw(npc.getEntity())) {
d = NMS.getHeadYaw(npc.getEntity()) + 40;
while (d >= 180F) {
d -= 360F;
}
while (d < -180F) {
d += 360F;
}
if (d < NMS.getYaw(npc.getEntity())) {
NMS.setBodyYaw(npc.getEntity(), d);
}
}
NMS.setPitch(npc.getEntity(),
rotateTowards(npc.getStoredLocation().getPitch(), getTargetPitchDifference(), this.xMaxRotAngle));
}
}
public static float clamp(float var0, float var1, float var2) {
if (var0 < var1) {
return var1;
} else {
return var0 > var2 ? var2 : var0;
}
}
}

View File

@ -553,6 +553,10 @@ public class NMS {
BRIDGE.setPeekShulker(entity, peek);
}
public static void setPitch(Entity entity, float pitch) {
BRIDGE.setPitch(entity, pitch);
}
public static void setPolarBearRearing(Entity entity, boolean rearing) {
BRIDGE.setPolarBearRearing(entity, rearing);
}

View File

@ -155,6 +155,8 @@ public interface NMSBridge {
public void setPeekShulker(Entity entity, int peek);
public void setPitch(Entity entity, float pitch);
public void setPolarBearRearing(Entity entity, boolean rearing);
public void setProfile(SkullMeta meta, GameProfile profile);

View File

@ -1000,6 +1000,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).p(rearing);

View File

@ -1057,6 +1057,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).p(rearing);

View File

@ -1065,6 +1065,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).p(rearing);

View File

@ -1101,6 +1101,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).s(rearing);

View File

@ -1161,6 +1161,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).r(rearing);

View File

@ -1194,6 +1194,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a((byte) peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).r(rearing);

View File

@ -1226,6 +1226,11 @@ public class NMSImpl implements NMSBridge {
((EntityShulker) getHandle(shulker)).a(peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((EntityPolarBear) getHandle(entity)).t(rearing);

View File

@ -1225,6 +1225,11 @@ public class NMSImpl implements NMSBridge {
((Shulker) getHandle(shulker)).setRawPeekAmount(peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).setXRot(pitch);
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((PolarBear) getHandle(entity)).setStanding(rearing);

View File

@ -1232,6 +1232,11 @@ public class NMSImpl implements NMSBridge {
((Shulker) getHandle(shulker)).setRawPeekAmount(peek);
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).setXRot(pitch);
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
((PolarBear) getHandle(entity)).setStanding(rearing);

View File

@ -23,7 +23,6 @@ public class PlayerBodyControl {
}
if (e())
if (Math.abs(this.mob.yHeadRot - this.lastStableYHeadRot) > 15.0F) {
System.out.println("BIG DX");
this.lastStableYHeadRot = 0;
this.lastStableYHeadRot = this.mob.yHeadRot;
rotateBodyIfNecessary();

View File

@ -944,6 +944,11 @@ public class NMSImpl implements NMSBridge {
throw new UnsupportedOperationException();
}
@Override
public void setPitch(org.bukkit.entity.Entity entity, float pitch) {
getHandle(entity).pitch = pitch;
}
@Override
public void setPolarBearRearing(org.bukkit.entity.Entity entity, boolean rearing) {
throw new UnsupportedOperationException();