Skip to content

Commit

Permalink
better EntityPlayerMP search
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed May 23, 2017
1 parent 5545bbd commit 78e1f39
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
1 change: 1 addition & 0 deletions 111/fix.sed
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 35 additions & 41 deletions 19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public class APIHandler {
private String authenticatedUsername = null;
private boolean stopChanges = false;
private boolean verifiedUser = false;
private Map<Integer,UUID> idToUUID = new HashMap<Integer,UUID>();

public APIHandler(MCEventHandler eventHandler, PrintWriter writer) throws IOException {
this(eventHandler, writer, true);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -885,18 +876,14 @@ else if (arg.startsWith(EVENTSSETTING+".")) {
}

protected EntityPlayer getPlayerByNameOrUUID(String name) {
for (World w : serverWorlds) {
for (EntityPlayer p : (List<EntityPlayer>)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<EntityPlayer>)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;
Expand Down Expand Up @@ -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<EntityPlayer> done = new HashSet<EntityPlayer>();
for (World w : RaspberryJamMod.minecraftServer.worldServers) {
for (EntityPlayer p : (List<EntityPlayer>)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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -51,6 +53,22 @@ protected boolean setup() {
return true;
}

@Override
protected EntityPlayer getPlayerByNameOrUUID(String name) {
for (EntityPlayer p : (List<EntityPlayer>)mc.theWorld.playerEntities) {
if (p.getName().equals(name)) {
return p;
}
}
for (EntityPlayer p : (List<EntityPlayer>)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);
Expand Down

0 comments on commit 78e1f39

Please sign in to comment.