From fe8d55f38f6cd10f4baa67eb665076ca88a25639 Mon Sep 17 00:00:00 2001 From: mezz Date: Thu, 18 Sep 2014 22:41:48 -0700 Subject: [PATCH] Created Server-safe method to get EntityPlayer from GameProfile --- api | 2 +- .../forestry/core/proxy/ClientProxyCommon.java | 7 +++++++ .../java/forestry/core/proxy/ProxyCommon.java | 17 +++++++++++++++++ .../factory/gadgets/MachineCarpenter.java | 4 ++-- .../factory/gadgets/MachineFabricator.java | 3 ++- .../factory/gadgets/MachineSqueezer.java | 3 ++- .../forestry/farming/gadgets/TileFarmPlain.java | 2 +- src/main/java/forestry/mail/MailAddress.java | 9 +-------- .../java/forestry/mail/proxy/ProxyMail.java | 2 +- 9 files changed, 34 insertions(+), 15 deletions(-) diff --git a/api b/api index 3d25676cc..c3641536b 160000 --- a/api +++ b/api @@ -1 +1 @@ -Subproject commit 3d25676cc26f216836baec287624aa12eff92fc9 +Subproject commit c3641536b984fada6fd7b5d7b9b757a7f40dd3d7 diff --git a/src/main/java/forestry/core/proxy/ClientProxyCommon.java b/src/main/java/forestry/core/proxy/ClientProxyCommon.java index 7dbe1a2f6..f91e2179e 100755 --- a/src/main/java/forestry/core/proxy/ClientProxyCommon.java +++ b/src/main/java/forestry/core/proxy/ClientProxyCommon.java @@ -12,6 +12,8 @@ package forestry.core.proxy; import java.io.File; +import com.mojang.authlib.GameProfile; + import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; @@ -90,6 +92,11 @@ public class ClientProxyCommon extends ProxyCommon { return true; } + @Override + public EntityPlayer getPlayer(World world, GameProfile profile) { + return world.getPlayerEntityByName(profile.getName()); + } + @Override public double getBlockReachDistance(EntityPlayer entityplayer) { if (entityplayer instanceof EntityPlayerSP) diff --git a/src/main/java/forestry/core/proxy/ProxyCommon.java b/src/main/java/forestry/core/proxy/ProxyCommon.java index 009c53b9b..61ee87411 100755 --- a/src/main/java/forestry/core/proxy/ProxyCommon.java +++ b/src/main/java/forestry/core/proxy/ProxyCommon.java @@ -12,6 +12,8 @@ package forestry.core.proxy; import java.io.File; +import com.mojang.authlib.GameProfile; + import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.IResourceManager; @@ -37,6 +39,8 @@ import cpw.mods.fml.common.versioning.DefaultArtifactVersion; import cpw.mods.fml.common.versioning.VersionParser; import cpw.mods.fml.common.versioning.VersionRange; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; @@ -153,6 +157,19 @@ public class ProxyCommon { return server.getConfigurationManager().func_152596_g(player.getGameProfile()); } + /** + * Get a player for a given World and GameProfile. + * If they are not in the World, returns a FakePlayer. + * Do not store references to the return value, to prevent worlds staying in memory. + */ + public EntityPlayer getPlayer(World world, GameProfile profile) { + EntityPlayer player = world.getPlayerEntityByName(profile.getName()); + if (player != null) + return player; + else + return FakePlayerFactory.get((WorldServer)world, profile); + } + public double getBlockReachDistance(EntityPlayer entityplayer) { return 4f; } diff --git a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java index c9d67863e..3175f0021 100755 --- a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java +++ b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java @@ -43,7 +43,7 @@ import forestry.core.EnumErrorCode; import forestry.core.config.Config; import forestry.core.config.Defaults; import forestry.core.config.ForestryItem; -import forestry.core.fluids.tanks.StandardTank; +import forestry.core.proxy.Proxies; import forestry.core.fluids.tanks.FilteredTank; import forestry.core.fluids.TankManager; import forestry.core.gadgets.TileBase; @@ -456,7 +456,7 @@ public class MachineCarpenter extends TilePowered implements ISidedInventory, IL } private boolean removeSets(int count, ItemStack[] set) { - EntityPlayer player = worldObj.getPlayerEntityByName(owner.getName()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, owner); return accessibleInventory.removeSets(count, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true, true); } diff --git a/src/main/java/forestry/factory/gadgets/MachineFabricator.java b/src/main/java/forestry/factory/gadgets/MachineFabricator.java index aeea17996..042f87da1 100755 --- a/src/main/java/forestry/factory/gadgets/MachineFabricator.java +++ b/src/main/java/forestry/factory/gadgets/MachineFabricator.java @@ -39,6 +39,7 @@ import forestry.core.interfaces.ICrafter; import forestry.core.interfaces.ICraftingPlan; import forestry.core.interfaces.ILiquidTankContainer; import forestry.core.network.GuiId; +import forestry.core.proxy.Proxies; import forestry.core.fluids.tanks.StandardTank; import forestry.core.fluids.TankManager; import forestry.core.fluids.tanks.FilteredTank; @@ -505,7 +506,7 @@ public class MachineFabricator extends TilePowered implements ICrafter, ISpecial public ItemStack[] extractItem(boolean doRemove, ForgeDirection from, int maxItemCount) { ItemStack taken; if (doRemove) { - EntityPlayer player = worldObj.getPlayerEntityByName(owner.getName()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, owner); taken = this.takenFromSlot(SLOT_RESULT, false, player); } else { taken = this.getResult(); diff --git a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java index 89c3f3214..2712f9b2b 100755 --- a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java +++ b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java @@ -49,6 +49,7 @@ import forestry.core.utils.EnumTankLevel; import forestry.core.fluids.tanks.StandardTank; import forestry.core.fluids.tanks.FilteredTank; import forestry.core.fluids.TankManager; +import forestry.core.proxy.Proxies; import forestry.core.utils.InventoryAdapter; import forestry.core.utils.LiquidHelper; import forestry.core.utils.StackUtils; @@ -351,7 +352,7 @@ public class MachineSqueezer extends TilePowered implements ISpecialInventory, I } private boolean removeResources(ItemStack[] stacks) { - EntityPlayer player = worldObj.getPlayerEntityByName(owner.getName()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, owner); return inventory.removeSets(1, stacks, SLOT_RESOURCE_1, SLOTS_RESOURCE_COUNT, player, false, true, true); } diff --git a/src/main/java/forestry/farming/gadgets/TileFarmPlain.java b/src/main/java/forestry/farming/gadgets/TileFarmPlain.java index b5fe62890..14cfe796b 100755 --- a/src/main/java/forestry/farming/gadgets/TileFarmPlain.java +++ b/src/main/java/forestry/farming/gadgets/TileFarmPlain.java @@ -763,7 +763,7 @@ public class TileFarmPlain extends TileFarm implements IFarmHousing, ISocketable @Override public void removeResources(ItemStack[] resources) { - EntityPlayer player = worldObj.getPlayerEntityByName(owner.getName()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, owner); inventory.removeSets(1, resources, SLOT_RESOURCES_1, SLOT_RESOURCES_1, player, false, true, true); } diff --git a/src/main/java/forestry/mail/MailAddress.java b/src/main/java/forestry/mail/MailAddress.java index 5fbc07ab1..2e9563d6e 100644 --- a/src/main/java/forestry/mail/MailAddress.java +++ b/src/main/java/forestry/mail/MailAddress.java @@ -143,18 +143,11 @@ public class MailAddress implements INBTTagable, IMailAddress { return address; } - public EntityPlayer getPlayer(World world) { - if (!this.isPlayer()) - return null; - - return world.getPlayerEntityByName(this.gameProfile.getName()); - } - public boolean isClientPlayer(World world) { if (!this.isPlayer()) return false; - EntityPlayer addressPlayer = this.getPlayer(world); + EntityPlayer addressPlayer = Proxies.common.getPlayer(world, gameProfile); EntityPlayer clientPlayer = Proxies.common.getPlayer(); return addressPlayer != null && clientPlayer != null && clientPlayer.equals(addressPlayer); diff --git a/src/main/java/forestry/mail/proxy/ProxyMail.java b/src/main/java/forestry/mail/proxy/ProxyMail.java index 36fb92b10..acb3221c5 100755 --- a/src/main/java/forestry/mail/proxy/ProxyMail.java +++ b/src/main/java/forestry/mail/proxy/ProxyMail.java @@ -28,7 +28,7 @@ public class ProxyMail { } public void setPOBoxInfo(World world, IMailAddress address, POBoxInfo info) { - EntityPlayer player = address.getPlayer(world); + EntityPlayer player = Proxies.common.getPlayer(world, address.getPlayerProfile()); if (player != null) Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketIds.POBOX_INFO, info), player); }