From c36402b4d92dbd6d01d9d5983a3d7b6374f8d3ca Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 12 Sep 2018 02:26:35 +0200 Subject: [PATCH] Fixed null exceptions when ticking players --- .../com/minelittlepony/unicopia/Unicopia.java | 3 +- .../unicopia/player/DefaultPlayerSpecies.java | 2 +- .../unicopia/player/IPlayer.java | 16 +++++++--- .../unicopia/player/PlayerCapabilities.java | 32 ++++++++----------- .../player/PlayerGravityDelegate.java | 3 +- .../unicopia/player/PlayerSpeciesList.java | 2 +- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index 27ec1d5c..0a3e045e 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -16,7 +16,6 @@ import net.minecraftforge.fml.common.event.FMLServerStartingEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -89,7 +88,7 @@ public class Unicopia { @SubscribeEvent public static void onPlyerTick(TickEvent.PlayerTickEvent event) { if (event.phase == Phase.END) { - PlayerSpeciesList.instance().getPlayer(event.player).onEntityUpdate(); + PlayerSpeciesList.instance().getPlayer(event.player).onUpdate(event.player); } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/DefaultPlayerSpecies.java b/src/main/java/com/minelittlepony/unicopia/player/DefaultPlayerSpecies.java index afad6166..c1ef200c 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/DefaultPlayerSpecies.java +++ b/src/main/java/com/minelittlepony/unicopia/player/DefaultPlayerSpecies.java @@ -53,7 +53,7 @@ final class DefaultPlayerSpecies implements IPlayer, IAbilityReceiver { } @Override - public void onEntityUpdate() { + public void onUpdate(EntityPlayer entity) { } diff --git a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java index b709fdca..df6fcb43 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java +++ b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java @@ -6,10 +6,11 @@ import com.minelittlepony.unicopia.InbtSerialisable; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.spell.ICaster; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraftforge.fml.common.FMLCommonHandler; -public interface IPlayer extends ICaster, InbtSerialisable { +public interface IPlayer extends ICaster, InbtSerialisable, IUpdatable { Race getPlayerSpecies(); void setPlayerSpecies(Race race); @@ -20,13 +21,20 @@ public interface IPlayer extends ICaster, InbtSerialisable { boolean isClientPlayer(); - void onEntityUpdate(); - default void onEntityEat() { } static EntityPlayer getPlayerEntity(UUID playerId) { - return FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUUID(playerId); + EntityPlayer player = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUUID(playerId); + + if (player == null) { + Entity e = FMLCommonHandler.instance().getMinecraftServerInstance().getEntityFromUuid(playerId); + if (e instanceof EntityPlayer) { + return (EntityPlayer)e; + } + } + + return player; } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java index 86d18f7e..41b7547f 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java @@ -18,16 +18,16 @@ class PlayerCapabilities implements IPlayer, ICaster { private Race playerSpecies = Race.HUMAN; - private UUID playerId; - private final PlayerAbilityDelegate powers = new PlayerAbilityDelegate(this); private final PlayerGravityDelegate gravity = new PlayerGravityDelegate(this); private IMagicEffect effect; + private EntityPlayer entity; + PlayerCapabilities(UUID playerId) { - this.playerId = playerId; + } @Override @@ -37,10 +37,6 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public void setPlayerSpecies(Race race) { - if (race == playerSpecies) { - return; - } - playerSpecies = race; EntityPlayer self = getOwner(); @@ -53,7 +49,7 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public void sendCapabilities() { - PlayerSpeciesList.instance().sendCapabilities(playerId); + PlayerSpeciesList.instance().sendCapabilities(getOwner().getGameProfile().getId()); } @Override @@ -64,26 +60,26 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public boolean isClientPlayer() { return UClient.isClientSide() && - Minecraft.getMinecraft().player.getGameProfile().getId().equals(playerId); + Minecraft.getMinecraft().player.getGameProfile().getId().equals(getOwner().getGameProfile().getId()); } @Override - public void onEntityUpdate() { - EntityPlayer player = getOwner(); + public void onUpdate(EntityPlayer entity) { + this.entity = entity; - powers.onUpdate(player); - gravity.onUpdate(player); + powers.onUpdate(entity); + gravity.onUpdate(entity); if (!getPlayerSpecies().canCast()) { effect = null; } if (effect != null) { - if (player.getEntityWorld().isRemote && player.getEntityWorld().getWorldTime() % 10 == 0) { - effect.render(player); + if (entity.getEntityWorld().isRemote && entity.getEntityWorld().getWorldTime() % 10 == 0) { + effect.render(entity); } - if (!effect.update(player)) { + if (!effect.update(entity)) { effect = null; } } @@ -100,7 +96,6 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public void writeToNBT(NBTTagCompound compound) { - compound.setUniqueId("playerId", playerId); compound.setString("playerSpecies", playerSpecies.name()); compound.setTag("powers", powers.toNBT()); compound.setTag("gravity", gravity.toNBT()); @@ -113,7 +108,6 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public void readFromNBT(NBTTagCompound compound) { - playerId = compound.getUniqueId("playerId"); playerSpecies = Race.fromName(compound.getString("playerSpecies")); powers.readFromNBT(compound.getCompoundTag("powers")); gravity.readFromNBT(compound.getCompoundTag("gravity")); @@ -139,6 +133,6 @@ class PlayerCapabilities implements IPlayer, ICaster { @Override public EntityPlayer getOwner() { - return IPlayer.getPlayerEntity(playerId); + return entity; } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java index c80d5c25..d0b33cbe 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java @@ -25,7 +25,8 @@ class PlayerGravityDelegate implements IUpdatable, InbtSerialisable { ticksSinceLanding++; } - entity.capabilities.allowFlying = entity.capabilities.isFlying = false; + entity.capabilities.allowFlying = player.getPlayerSpecies().canFly(); + entity.capabilities.isFlying = false; } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java index 396e1397..e643a76f 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerSpeciesList.java @@ -28,7 +28,7 @@ public class PlayerSpeciesList { } public void sendCapabilities(UUID playerId) { - Unicopia.channel.send(new MsgPlayerCapabilities(getPlayer(playerId).getPlayerSpecies(), playerId), Target.SERVER_AND_CLIENTS); + Unicopia.channel.send(new MsgPlayerCapabilities(getPlayer(playerId).getPlayerSpecies(), playerId), playerId); } public void handleSpeciesChange(UUID playerId, Race race) {