From 78e1f39d35c6fb7ad7f2ddb3c6eea4bd6308c2b6 Mon Sep 17 00:00:00 2001 From: arpruss Date: Tue, 23 May 2017 08:35:23 -0500 Subject: [PATCH] better EntityPlayerMP search --- 111/fix.sed | 1 + .../raspberryjammod/APIHandler.java | 76 +++++++++---------- .../raspberryjammod/APIHandlerClientOnly.java | 18 +++++ 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/111/fix.sed b/111/fix.sed index 5487114..54f026d 100644 --- a/111/fix.sed +++ b/111/fix.sed @@ -17,3 +17,4 @@ s/worldServers/worlds/g s/DamageSource\.inWall/DamageSource.IN_WALL/g s/DamageSource\.fall/DamageSource.FALL/g s/getUnformattedTextForChat/getUnformattedComponentText/g +s/getPlayerList()\.getPlayerList()/getPlayerList().getPlayers()/g \ No newline at end of file diff --git a/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java b/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java index 4190c29..169b37a 100644 --- a/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java +++ b/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java @@ -266,6 +266,7 @@ public class APIHandler { private String authenticatedUsername = null; private boolean stopChanges = false; private boolean verifiedUser = false; + private Map idToUUID = new HashMap(); public APIHandler(MCEventHandler eventHandler, PrintWriter writer) throws IOException { this(eventHandler, writer, true); @@ -342,18 +343,7 @@ synchronized private void updatePlayerMP() { if (playerMP != null) return; - if (RaspberryJamMod.integrated) { - playerMP = mc.getIntegratedServer().getPlayerList().getPlayerByUUID(playerUniqueId); - playerId = playerMP.getEntityId(); - } - - for (World w : serverWorlds) { - Entity e = w.getEntityByID(playerId); - if (e != null) { - playerMP = (EntityPlayerMP)e; - return; - } - } + playerMP = RaspberryJamMod.minecraftServer.getPlayerList().getPlayerByUUID(playerUniqueId); } public static String tohex(byte[] array) { @@ -385,7 +375,7 @@ protected boolean setup() { return false; } playerUniqueId = mc.thePlayer.getUniqueID(); - playerMP = mc.getIntegratedServer().getPlayerList().getPlayerByUUID(playerUniqueId); + playerMP = RaspberryJamMod.minecraftServer.getPlayerList().getPlayerByUUID(playerUniqueId); playerId = playerMP.getEntityId(); } else { @@ -396,22 +386,23 @@ protected boolean setup() { if (playerMP != null) { verifiedUser = true; - playerId = playerMP.getEntityId(); } else { int firstId = 0; - for (World w : serverWorlds) { - for (EntityPlayer p : w.playerEntities) { - int id = p.getEntityId(); - if (playerMP == null || id < firstId) { - firstId = id; - playerMP = (EntityPlayerMP)p; - playerId = id; - } + for (EntityPlayer p : RaspberryJamMod.minecraftServer.getPlayerList().getPlayerList()) { + int id = p.getEntityId(); + if (playerMP == null || id < firstId) { + firstId = id; + playerMP = (EntityPlayerMP)p; } } } + + if (playerMP != null) { + playerId = playerMP.getEntityId(); + playerUniqueId = playerMP.getUniqueID(); + } } if (playerMP == null) { @@ -885,18 +876,14 @@ else if (arg.startsWith(EVENTSSETTING+".")) { } protected EntityPlayer getPlayerByNameOrUUID(String name) { - for (World w : serverWorlds) { - for (EntityPlayer p : (List)w.playerEntities) { - if (p.getName().equals(name)) { - return p; - } + for (EntityPlayer p : RaspberryJamMod.minecraftServer.getPlayerList().getPlayerList()) { + if (p.getName().equals(name)) { + return p; } } - for (World w : serverWorlds) { - for (EntityPlayer p : (List)w.playerEntities) { - if (p.getUniqueID().toString().equals(name)) { - return p; - } + for (EntityPlayer p : RaspberryJamMod.minecraftServer.getPlayerList().getPlayerList()) { + if (p.getUniqueID().toString().equals(name)) { + return p; } } return null; @@ -1437,26 +1424,33 @@ protected Location getBlockLocation(Scanner scan) { protected Entity getServerEntityByID(int id) { if (id == playerId) { - //updatePlayerMP(); return playerMP; } + + UUID uuid = idToUUID.get(id); + + if (uuid != null) { + return RaspberryJamMod.minecraftServer.getPlayerList().getPlayerByUUID(uuid); + } + + for (EntityPlayerMP player : RaspberryJamMod.minecraftServer.getPlayerList().getPlayerList()) + if (player.getEntityId() == id) { + idToUUID.put(id, player.getUniqueID()); + return player; + } + for (World w : serverWorlds) { Entity e = w.getEntityByID(id); if (e != null) return e; } + return null; } static void globalMessage(String message) { - Set done = new HashSet(); - for (World w : RaspberryJamMod.minecraftServer.worldServers) { - for (EntityPlayer p : (List)w.playerEntities ) { - if (!done.contains(p)) { - p.addChatComponentMessage(new TextComponentString(message)); - done.add(p); - } - } + for (EntityPlayerMP player : RaspberryJamMod.minecraftServer.getPlayerList().getPlayerList()) { + player.addChatComponentMessage(new TextComponentString(message)); } } diff --git a/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandlerClientOnly.java b/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandlerClientOnly.java index 642e4ce..9a20b9b 100644 --- a/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandlerClientOnly.java +++ b/19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandlerClientOnly.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; +import java.util.List; import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; @@ -12,6 +13,7 @@ import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; import net.minecraft.world.WorldServer; +import net.minecraft.entity.player.EntityPlayer; // This class is meant to provide most of the APIHandler facility while one is connected to a // server. Of course, any block changes won't get written back to the server. @@ -51,6 +53,22 @@ protected boolean setup() { return true; } + @Override + protected EntityPlayer getPlayerByNameOrUUID(String name) { + for (EntityPlayer p : (List)mc.theWorld.playerEntities) { + if (p.getName().equals(name)) { + return p; + } + } + for (EntityPlayer p : (List)mc.theWorld.playerEntities) { + if (p.getUniqueID().toString().equals(name)) { + return p; + } + } + return null; + } + + @Override protected Entity getServerEntityByID(int id) { Entity e = mc.theWorld.getEntityByID(id);