Avoid copying
This commit is contained in:
parent
145d6ce616
commit
8693de6b5f
|
@ -1,15 +1,18 @@
|
||||||
package net.citizensnpcs.util;
|
package net.citizensnpcs.util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.util.Messaging;
|
import net.citizensnpcs.api.util.Messaging;
|
||||||
import net.citizensnpcs.npc.CitizensNPC;
|
import net.citizensnpcs.npc.CitizensNPC;
|
||||||
|
@ -17,42 +20,55 @@ import net.citizensnpcs.npc.ai.NPCHolder;
|
||||||
import net.citizensnpcs.trait.PacketNPC;
|
import net.citizensnpcs.trait.PacketNPC;
|
||||||
|
|
||||||
public class PlayerUpdateTask extends BukkitRunnable {
|
public class PlayerUpdateTask extends BukkitRunnable {
|
||||||
private PlayerTick[] players = new PlayerTick[0];
|
private final List<PlayerTick> players = Lists.newArrayList();
|
||||||
|
private final Set<UUID> uuids = Sets.newHashSet();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
super.cancel();
|
super.cancel();
|
||||||
PLAYERS.clear();
|
uuids.clear();
|
||||||
|
players.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (Entity entity : PLAYERS_PENDING_REMOVE) {
|
if (PLAYERS_PENDING_REMOVE.size() > 0) {
|
||||||
PLAYERS.remove(entity.getUniqueId());
|
players.removeIf(pt -> PLAYERS_PENDING_REMOVE.contains(pt.entity));
|
||||||
|
for (Entity entity : PLAYERS_PENDING_REMOVE) {
|
||||||
|
uuids.remove(entity.getUniqueId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (Entity entity : PLAYERS_PENDING_ADD) {
|
for (Entity entity : PLAYERS_PENDING_ADD) {
|
||||||
PlayerTick rm = PLAYERS.remove(entity.getUniqueId());
|
|
||||||
NPC next = ((NPCHolder) entity).getNPC();
|
NPC next = ((NPCHolder) entity).getNPC();
|
||||||
if (rm != null) {
|
if (uuids.contains(entity.getUniqueId())) {
|
||||||
|
// XXX: how often does this case get hit? can simplify implementation
|
||||||
|
PlayerTick rm = null;
|
||||||
|
for (Iterator<PlayerTick> itr = players.iterator(); itr.hasNext();) {
|
||||||
|
PlayerTick pt = itr.next();
|
||||||
|
if (pt.entity.getUniqueId().equals(entity.getUniqueId())) {
|
||||||
|
rm = pt;
|
||||||
|
itr.remove();
|
||||||
|
uuids.remove(pt.entity.getUniqueId());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
NPC old = ((NPCHolder) rm.entity).getNPC();
|
NPC old = ((NPCHolder) rm.entity).getNPC();
|
||||||
Messaging.severe(old == next ? "Player registered twice"
|
Messaging.severe(old == next ? "Player registered twice"
|
||||||
: "Player registered twice with different NPC instances", rm.entity.getUniqueId());
|
: "Player registered twice with different NPC instances", rm.entity.getUniqueId());
|
||||||
rm.entity.remove();
|
rm.entity.remove();
|
||||||
}
|
}
|
||||||
if (next.hasTrait(PacketNPC.class)) {
|
if (next.hasTrait(PacketNPC.class)) {
|
||||||
PLAYERS.put(entity.getUniqueId(), new PlayerTick(entity, () -> ((CitizensNPC) next).update()));
|
players.add(new PlayerTick(entity, () -> ((CitizensNPC) next).update()));
|
||||||
} else {
|
} else {
|
||||||
PLAYERS.put(entity.getUniqueId(), new PlayerTick((Player) entity));
|
players.add(new PlayerTick(entity, NMS.playerTicker((Player) entity)));
|
||||||
}
|
}
|
||||||
}
|
uuids.add(entity.getUniqueId());
|
||||||
if (PLAYERS_PENDING_ADD.size() + PLAYERS_PENDING_REMOVE.size() > 0) {
|
|
||||||
players = PLAYERS.values().toArray(new PlayerTick[PLAYERS.values().size()]);
|
|
||||||
}
|
}
|
||||||
PLAYERS_PENDING_ADD.clear();
|
PLAYERS_PENDING_ADD.clear();
|
||||||
PLAYERS_PENDING_REMOVE.clear();
|
PLAYERS_PENDING_REMOVE.clear();
|
||||||
|
|
||||||
for (int i = 0; i < players.length; i++) {
|
for (PlayerTick player : players) {
|
||||||
players[i].run();
|
player.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,10 +81,6 @@ public class PlayerUpdateTask extends BukkitRunnable {
|
||||||
this.tick = tick;
|
this.tick = tick;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerTick(Player player) {
|
|
||||||
this(player, NMS.playerTicker(player));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
tick.run();
|
tick.run();
|
||||||
|
@ -85,7 +97,6 @@ public class PlayerUpdateTask extends BukkitRunnable {
|
||||||
PLAYERS_PENDING_ADD.add(entity);
|
PLAYERS_PENDING_ADD.add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<UUID, PlayerTick> PLAYERS = new HashMap<>();
|
|
||||||
private static List<Entity> PLAYERS_PENDING_ADD = new ArrayList<>();
|
private static List<Entity> PLAYERS_PENDING_ADD = new ArrayList<>();
|
||||||
private static List<Entity> PLAYERS_PENDING_REMOVE = new ArrayList<>();
|
private static List<Entity> PLAYERS_PENDING_REMOVE = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue