Potentially make PlayerUpdateTask faster

This commit is contained in:
fullwall 2016-03-08 21:42:34 +08:00
parent 764b3c45a3
commit 62c38bc359

View File

@ -1,10 +1,11 @@
package net.citizensnpcs.util; package net.citizensnpcs.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Map;
import java.util.UUID;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -24,11 +25,17 @@ public class PlayerUpdateTask extends BukkitRunnable {
@Override @Override
public void run() { public void run() {
TICKERS.removeAll(TICKERS_PENDING_REMOVE); for (int i = 0; i < TICKERS_PENDING_ADD.size(); i++) {
TICKERS.addAll(TICKERS_PENDING_ADD); org.bukkit.entity.Entity ent = TICKERS_PENDING_ADD.get(i);
TICKERS.put(ent.getUniqueId(), ent);
}
for (int i = 0; i < TICKERS_PENDING_REMOVE.size(); i++) {
org.bukkit.entity.Entity ent = TICKERS_PENDING_REMOVE.get(i);
TICKERS.remove(ent.getUniqueId());
}
TICKERS_PENDING_ADD.clear(); TICKERS_PENDING_ADD.clear();
TICKERS_PENDING_REMOVE.clear(); TICKERS_PENDING_REMOVE.clear();
Iterator<org.bukkit.entity.Entity> itr = TICKERS.iterator(); Iterator<org.bukkit.entity.Entity> itr = TICKERS.values().iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
Entity entity = NMS.getHandle(itr.next()); Entity entity = NMS.getHandle(itr.next());
Entity entity1 = entity.by(); Entity entity1 = entity.by();
@ -57,14 +64,17 @@ public class PlayerUpdateTask extends BukkitRunnable {
} }
public static void addOrRemove(org.bukkit.entity.Entity entity, boolean remove) { public static void addOrRemove(org.bukkit.entity.Entity entity, boolean remove) {
boolean contains = TICKERS.containsKey(entity.getUniqueId());
if (!remove) { if (!remove) {
TICKERS_PENDING_REMOVE.add(entity); if (contains) {
} else { TICKERS_PENDING_REMOVE.add(entity);
}
} else if (!contains) {
TICKERS_PENDING_ADD.add(entity); TICKERS_PENDING_ADD.add(entity);
} }
} }
private static Set<org.bukkit.entity.Entity> TICKERS = new HashSet<org.bukkit.entity.Entity>(); private static Map<UUID, org.bukkit.entity.Entity> TICKERS = new HashMap<UUID, org.bukkit.entity.Entity>();
private static List<org.bukkit.entity.Entity> TICKERS_PENDING_ADD = new ArrayList<org.bukkit.entity.Entity>(); private static List<org.bukkit.entity.Entity> TICKERS_PENDING_ADD = new ArrayList<org.bukkit.entity.Entity>();
private static List<org.bukkit.entity.Entity> TICKERS_PENDING_REMOVE = new ArrayList<org.bukkit.entity.Entity>(); private static List<org.bukkit.entity.Entity> TICKERS_PENDING_REMOVE = new ArrayList<org.bukkit.entity.Entity>();
} }