From 8e85dde59bb8953e2c61d404c75612a8086c1aa5 Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 2 Mar 2016 13:53:18 +0800 Subject: [PATCH] Add new player update task for players not on the playerlist --- src/main/java/net/citizensnpcs/Citizens.java | 2 + src/main/java/net/citizensnpcs/util/NMS.java | 1 + .../citizensnpcs/util/PlayerUpdateTask.java | 60 +++++++++++++++++++ .../util/nms/PlayerPathfinderAbstract.java | 14 ++++- .../util/nms/PlayerPathfinderNormal.java | 2 +- 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/citizensnpcs/util/PlayerUpdateTask.java diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 4d408bb93..7dd636f0f 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -58,6 +58,7 @@ import net.citizensnpcs.npc.profile.ProfileFetcher; import net.citizensnpcs.npc.skin.Skin; import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.NMS; +import net.citizensnpcs.util.PlayerUpdateTask; import net.citizensnpcs.util.StringHelper; import net.citizensnpcs.util.Util; import net.milkbowl.vault.economy.Economy; @@ -297,6 +298,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { startMetrics(); scheduleSaveTask(Setting.SAVE_TASK_DELAY.asInt()); Bukkit.getPluginManager().callEvent(new CitizensEnableEvent()); + new PlayerUpdateTask().runTaskTimer(Citizens.this, 0, 1); } }, 1) == -1) { Messaging.severeTr(Messages.LOAD_TASK_NOT_SCHEDULED); diff --git a/src/main/java/net/citizensnpcs/util/NMS.java b/src/main/java/net/citizensnpcs/util/NMS.java index 6fddb496d..901e6e2e6 100644 --- a/src/main/java/net/citizensnpcs/util/NMS.java +++ b/src/main/java/net/citizensnpcs/util/NMS.java @@ -101,6 +101,7 @@ public class NMS { } else if (!handle.world.players.contains(handle)) { handle.world.players.add(handle); } + PlayerUpdateTask.addOrRemove(entity, remove); } public static boolean addToWorld(org.bukkit.World world, org.bukkit.entity.Entity entity, diff --git a/src/main/java/net/citizensnpcs/util/PlayerUpdateTask.java b/src/main/java/net/citizensnpcs/util/PlayerUpdateTask.java new file mode 100644 index 000000000..22477857d --- /dev/null +++ b/src/main/java/net/citizensnpcs/util/PlayerUpdateTask.java @@ -0,0 +1,60 @@ +package net.citizensnpcs.util; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.bukkit.scheduler.BukkitRunnable; + +import net.minecraft.server.v1_9_R1.CrashReport; +import net.minecraft.server.v1_9_R1.CrashReportSystemDetails; +import net.minecraft.server.v1_9_R1.Entity; +import net.minecraft.server.v1_9_R1.ReportedException; + +public class PlayerUpdateTask extends BukkitRunnable { + @Override + public void cancel() { + super.cancel(); + TICKERS.clear(); + } + + @Override + public void run() { + Iterator itr = TICKERS.iterator(); + while (itr.hasNext()) { + Entity entity = NMS.getHandle(itr.next()); + Entity entity1 = entity.by(); + if (entity1 != null) { + if ((entity1.dead) || (!entity1.w(entity))) { + entity.stopRiding(); + } + } else { + if (!entity.dead) { + try { + entity.world.g(entity); + } catch (Throwable throwable) { + CrashReport crashreport = CrashReport.a(throwable, "Ticking player"); + CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Player being ticked"); + + entity.appendEntityCrashDetails(crashreportsystemdetails); + throw new ReportedException(crashreport); + } + } + if (entity.dead) { + entity.world.removeEntity(entity); + itr.remove(); + } + } + } + } + + public static void addOrRemove(org.bukkit.entity.Entity entity, boolean remove) { + if (remove) { + TICKERS.remove(entity); + } else { + TICKERS.add(entity); + } + } + + private static List TICKERS = new ArrayList(); +} diff --git a/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderAbstract.java b/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderAbstract.java index e92c756f9..a39d56b6c 100644 --- a/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderAbstract.java +++ b/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderAbstract.java @@ -3,7 +3,6 @@ package net.citizensnpcs.util.nms; import net.citizensnpcs.npc.entity.EntityHumanNPC; import net.minecraft.server.v1_9_R1.EntityInsentient; import net.minecraft.server.v1_9_R1.IBlockAccess; -import net.minecraft.server.v1_9_R1.IntHashMap; import net.minecraft.server.v1_9_R1.MathHelper; import net.minecraft.server.v1_9_R1.PathPoint; import net.minecraft.server.v1_9_R1.PathType; @@ -12,7 +11,6 @@ import net.minecraft.server.v1_9_R1.PathfinderAbstract; public abstract class PlayerPathfinderAbstract extends PathfinderAbstract { protected IBlockAccess a; protected EntityHumanNPC b; - protected final IntHashMap c = new IntHashMap(); protected int d; protected int e; protected int f; @@ -20,13 +18,16 @@ public abstract class PlayerPathfinderAbstract extends PathfinderAbstract { protected boolean h; protected boolean i; + @Override public void a() { } + @Override public void a(boolean paramBoolean) { this.g = paramBoolean; } + @Override public abstract PathPoint a(double paramDouble1, double paramDouble2, double paramDouble3); public void a(IBlockAccess paramIBlockAccess, EntityHumanNPC paramEntityInsentient) { @@ -43,10 +44,12 @@ public abstract class PlayerPathfinderAbstract extends PathfinderAbstract { EntityHumanNPC paramEntityInsentient, int paramInt4, int paramInt5, int paramInt6, boolean paramBoolean1, boolean paramBoolean2); + @Override public abstract PathType a(IBlockAccess paramIBlockAccess, int paramInt1, int paramInt2, int paramInt3, EntityInsentient paramEntityInsentient, int paramInt4, int paramInt5, int paramInt6, boolean paramBoolean1, boolean paramBoolean2); + @Override protected PathPoint a(int paramInt1, int paramInt2, int paramInt3) { int j = PathPoint.b(paramInt1, paramInt2, paramInt3); PathPoint localPathPoint = this.c.get(j); @@ -57,27 +60,34 @@ public abstract class PlayerPathfinderAbstract extends PathfinderAbstract { return localPathPoint; } + @Override public abstract int a(PathPoint[] paramArrayOfPathPoint, PathPoint paramPathPoint1, PathPoint paramPathPoint2, float paramFloat); + @Override public abstract PathPoint b(); + @Override public void b(boolean paramBoolean) { this.h = paramBoolean; } + @Override public boolean c() { return this.g; } + @Override public void c(boolean paramBoolean) { this.i = paramBoolean; } + @Override public boolean d() { return this.h; } + @Override public boolean e() { return this.i; } diff --git a/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderNormal.java b/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderNormal.java index 3c904109d..78ed5af49 100644 --- a/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderNormal.java +++ b/src/main/java/net/citizensnpcs/util/nms/PlayerPathfinderNormal.java @@ -341,7 +341,7 @@ public class PlayerPathfinderNormal extends PlayerPathfinderAbstract { localObject1 = new BlockPosition(this.b); Object localObject2 = a(this.b, localObject1.getX(), i, localObject1.getZ()); if (this.b.a((PathType) localObject2) < 0.0F) { - HashSet localHashSet = new HashSet(); + HashSet localHashSet = new HashSet(); localHashSet.add(new BlockPosition(this.b.getBoundingBox().a, i, this.b.getBoundingBox().c)); localHashSet.add(new BlockPosition(this.b.getBoundingBox().a, i, this.b.getBoundingBox().f)); localHashSet.add(new BlockPosition(this.b.getBoundingBox().d, i, this.b.getBoundingBox().c));