Change how NPC looking works so that body yaw matches head yaw after a tick

This commit is contained in:
fullwall 2020-04-21 22:24:13 +08:00
parent 06d8e92ff2
commit 655bdf0001
16 changed files with 137 additions and 33 deletions

View File

@ -159,41 +159,46 @@ public class EventListen implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChunkUnload(final ChunkUnloadEvent event) {
ChunkCoord coord = new ChunkCoord(event.getChunk());
boolean loadChunk = false;
for (Entity entity : event.getChunk().getEntities()) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc == null || !npc.isSpawned())
continue;
if (!npc.despawn(DespawnReason.CHUNK_UNLOAD)) {
if (!(event instanceof Cancellable)) {
loadChunk = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
ChunkCoord coord = new ChunkCoord(event.getChunk());
boolean loadChunk = false;
for (Entity entity : event.getChunk().getEntities()) {
NPC npc = CitizensAPI.getNPCRegistry().getNPC(entity);
if (npc == null || !npc.isSpawned())
continue;
if (!npc.despawn(DespawnReason.CHUNK_UNLOAD)) {
if (!(event instanceof Cancellable)) {
loadChunk = true;
toRespawn.put(coord, npc);
continue;
}
((Cancellable) event).setCancelled(true);
if (Messaging.isDebugging()) {
Messaging.debug("Cancelled chunk unload at [" + coord.x + "," + coord.z + "]");
}
respawnAllFromCoord(coord);
return;
}
toRespawn.put(coord, npc);
continue;
}
((Cancellable) event).setCancelled(true);
if (Messaging.isDebugging()) {
Messaging.debug("Cancelled chunk unload at [" + coord.x + "," + coord.z + "]");
}
respawnAllFromCoord(coord);
return;
}
toRespawn.put(coord, npc);
if (Messaging.isDebugging()) {
Messaging.debug("Despawned id", npc.getId(),
"due to chunk unload at [" + coord.x + "," + coord.z + "]");
}
}
if (loadChunk) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (!event.getChunk().isLoaded()) {
event.getChunk().load();
if (Messaging.isDebugging()) {
Messaging.debug("Despawned id", npc.getId(),
"due to chunk unload at [" + coord.x + "," + coord.z + "]");
}
}
}, 10);
}
if (loadChunk) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (!event.getChunk().isLoaded()) {
event.getChunk().load();
}
}
}, 10);
}
}
});
}
@EventHandler(priority = EventPriority.MONITOR)

View File

@ -228,7 +228,8 @@ public class CommandTrait extends Trait {
}
long currentTimeSec = System.currentTimeMillis() / 1000;
if (lastUsed.containsKey(command.command)) {
if (currentTimeSec < ((Number) (lastUsed.get(command.command) + command.cooldown)).longValue()) {
if (currentTimeSec < ((Number) (lastUsed.get(command.command)
+ ((Number) command.cooldown).longValue())).longValue()) {
return false;
}
lastUsed.remove(command.command);

View File

@ -30,6 +30,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -69,6 +70,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -92,6 +100,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -42,6 +42,7 @@ public class PlayerControllerLook {
this.a.aQ += 360F;
}
} else {
this.a.yaw = this.a.aQ + 40;
// this.a.aQ = a(this.a.aQ, this.a.aO, 10.0F);
}
float f3 = MathHelper.g(this.a.aQ - this.a.aO);
@ -56,6 +57,10 @@ public class PlayerControllerLook {
}
public void a(double d0, double d1, double d2, float f, float f1) {
double d = Math.pow(this.e - d0, 2) + Math.pow(this.f - d1, 2) + Math.pow(this.g - d2, 2);
if (d < 0.01) {
return;
}
this.e = d0;
this.f = d1;
this.g = d2;

View File

@ -30,6 +30,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -69,6 +70,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -92,6 +100,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -42,6 +42,7 @@ public class PlayerControllerLook {
this.a.aP += 360F;
}
} else {
this.a.yaw = this.a.aP + 40;
// this.a.aP = a(this.a.aP, this.a.aN, 10.0F);
}
float f3 = MathHelper.g(this.a.aP - this.a.aN);
@ -56,6 +57,10 @@ public class PlayerControllerLook {
}
public void a(double d0, double d1, double d2, float f, float f1) {
double d = Math.pow(this.e - d0, 2) + Math.pow(this.f - d1, 2) + Math.pow(this.g - d2, 2);
if (d < 0.01) {
return;
}
this.e = d0;
this.f = d1;
this.g = d2;

View File

@ -30,6 +30,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -69,6 +70,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -92,6 +100,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -42,6 +42,7 @@ public class PlayerControllerLook {
this.a.aP += 360F;
}
} else {
this.a.yaw = this.a.aP + 40;
// this.a.aP = a(this.a.aP, this.a.aN, 10.0F);
}
float f3 = MathHelper.g(this.a.aP - this.a.aN);
@ -56,6 +57,10 @@ public class PlayerControllerLook {
}
public void a(double d0, double d1, double d2, float f, float f1) {
double d = Math.pow(this.e - d0, 2) + Math.pow(this.f - d1, 2) + Math.pow(this.g - d2, 2);
if (d < 0.01) {
return;
}
this.e = d0;
this.f = d1;
this.g = d2;

View File

@ -30,6 +30,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -69,6 +70,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -92,6 +100,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -43,6 +43,7 @@ public class PlayerControllerLook {
this.a.aS += 360F;
}
} else {
this.a.yaw = this.a.aS + 40;
// this.a.aP = a(this.a.aS, this.a.aQ, 10.0F);
}
float f3 = MathHelper.g(this.a.aS - this.a.aQ);
@ -57,6 +58,10 @@ public class PlayerControllerLook {
}
public void a(double d0, double d1, double d2, float f, float f1) {
double d = Math.pow(this.e - d0, 2) + Math.pow(this.f - d1, 2) + Math.pow(this.g - d2, 2);
if (d < 0.01) {
return;
}
this.e = d0;
this.f = d1;
this.g = d2;

View File

@ -31,6 +31,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -70,6 +71,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -93,6 +101,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -31,6 +31,7 @@ public class PlayerControllerLook {
this.a.yaw = this.a.aM;
this.a.pitch = this.a(this.a.pitch, this.g(), this.c);
} else {
this.a.yaw = this.a.aM + 40;
// this.a.aM = this.a(this.a.aM, this.a.aK, 10.0F);
}
@ -44,6 +45,10 @@ public class PlayerControllerLook {
}
public void a(double var0, double var2, double var4, float var6, float var7) {
double d = Math.pow(this.e - var0, 2) + Math.pow(this.f - var2, 2) + Math.pow(this.g - var4, 2);
if (d < 0.01) {
return;
}
this.e = var0;
this.f = var2;
this.g = var4;

View File

@ -31,6 +31,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -70,6 +71,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -93,6 +101,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -31,6 +31,7 @@ public class PlayerControllerLook {
this.a.yaw = this.a.aK;
this.a.pitch = this.a(this.a.pitch, this.g(), this.c);
} else {
this.a.yaw = this.a.aK + 40;
// this.a.aK = this.a(this.a.aK, this.a.aI, 10.0F);
}
@ -44,6 +45,10 @@ public class PlayerControllerLook {
}
public void a(double var0, double var2, double var4, float var6, float var7) {
double d = Math.pow(this.e - var0, 2) + Math.pow(this.f - var2, 2) + Math.pow(this.g - var4, 2);
if (d < 0.01) {
return;
}
this.e = var0;
this.f = var2;
this.g = var4;

View File

@ -30,6 +30,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
@ -69,6 +70,13 @@ public class CitizensBlockBreaker extends BlockBreaker {
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
@ -92,6 +100,7 @@ public class CitizensBlockBreaker extends BlockBreaker {
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;

View File

@ -43,6 +43,7 @@ public class PlayerControllerLook {
this.a.aK += 360F;
}
} else {
this.a.yaw = this.a.aK + 40;
// this.a.aK = a(this.a.aK, this.a.aI, 10.0F);
}
float f3 = MathHelper.g(this.a.aK - this.a.aI);
@ -57,6 +58,10 @@ public class PlayerControllerLook {
}
public void a(double d0, double d1, double d2, float f, float f1) {
double d = Math.pow(this.e - d0, 2) + Math.pow(this.f - d1, 2) + Math.pow(this.g - d2, 2);
if (d < 0.01) {
return;
}
this.e = d0;
this.f = d1;
this.g = d2;