From e9809238de435fed036c8b0f3234745b9e7b3c73 Mon Sep 17 00:00:00 2001 From: mezz Date: Sat, 18 Jun 2016 16:14:13 -0700 Subject: [PATCH] Fix #1325 Null Pointer Exception from broken mailbox --- gradle.properties | 2 +- .../java/forestry/api/mail/IMailAddress.java | 4 ---- .../java/forestry/api/mail/IPostRegistry.java | 8 +++++--- .../arboriculture/tiles/TileTreeContainer.java | 2 +- .../java/forestry/core/access/AccessHandler.java | 3 ++- .../forestry/core/access/FakeAccessHandler.java | 3 ++- src/main/java/forestry/core/access/IOwnable.java | 10 +++++++--- .../lepidopterology/tiles/TileCocoon.java | 2 +- src/main/java/forestry/mail/MailAddress.java | 16 +++------------- src/main/java/forestry/mail/POBox.java | 5 +++-- src/main/java/forestry/mail/PostRegistry.java | 10 ++++++---- src/main/java/forestry/mail/TradeStation.java | 3 ++- .../java/forestry/mail/TradeStationInfo.java | 3 ++- .../java/forestry/mail/gui/ContainerLetter.java | 7 ++++--- .../java/forestry/mail/tiles/TileMailbox.java | 16 ++++++++++++---- 15 files changed, 51 insertions(+), 43 deletions(-) diff --git a/gradle.properties b/gradle.properties index 1f061de5e..e4942fe00 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,4 +7,4 @@ ic2_version=2.5.5-ex19 version_major=5 version_minor=0 -version_patch=18 +version_patch=19 diff --git a/src/main/java/forestry/api/mail/IMailAddress.java b/src/main/java/forestry/api/mail/IMailAddress.java index 5c601972f..956245ca6 100644 --- a/src/main/java/forestry/api/mail/IMailAddress.java +++ b/src/main/java/forestry/api/mail/IMailAddress.java @@ -17,9 +17,5 @@ public interface IMailAddress extends INbtWritable { boolean isValid(); - boolean isPlayer(); - - boolean isTrader(); - GameProfile getPlayerProfile(); } diff --git a/src/main/java/forestry/api/mail/IPostRegistry.java b/src/main/java/forestry/api/mail/IPostRegistry.java index dadb155d4..a5561d25c 100644 --- a/src/main/java/forestry/api/mail/IPostRegistry.java +++ b/src/main/java/forestry/api/mail/IPostRegistry.java @@ -5,6 +5,7 @@ ******************************************************************************/ package forestry.api.mail; +import javax.annotation.Nonnull; import java.util.Map; import net.minecraft.item.ItemStack; @@ -18,9 +19,10 @@ public interface IPostRegistry { IPostOffice getPostOffice(World world); /* MAIL ADDRESSES */ - IMailAddress getMailAddress(GameProfile gameProfile); - - IMailAddress getMailAddress(String traderName); + @Nonnull + IMailAddress getMailAddress(@Nonnull GameProfile gameProfile); + + IMailAddress getMailAddress(@Nonnull String traderName); /* LETTERS */ boolean isLetter(ItemStack itemstack); diff --git a/src/main/java/forestry/arboriculture/tiles/TileTreeContainer.java b/src/main/java/forestry/arboriculture/tiles/TileTreeContainer.java index c8c7b0859..948292aaf 100644 --- a/src/main/java/forestry/arboriculture/tiles/TileTreeContainer.java +++ b/src/main/java/forestry/arboriculture/tiles/TileTreeContainer.java @@ -130,7 +130,7 @@ public abstract class TileTreeContainer extends TileEntity implements IStreamabl } @Override - public void setOwner(@Nullable GameProfile owner) { + public void setOwner(@Nonnull GameProfile owner) { this.owner = owner; } diff --git a/src/main/java/forestry/core/access/AccessHandler.java b/src/main/java/forestry/core/access/AccessHandler.java index 0ded4908a..c7c114702 100644 --- a/src/main/java/forestry/core/access/AccessHandler.java +++ b/src/main/java/forestry/core/access/AccessHandler.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.access; +import javax.annotation.Nonnull; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -100,7 +101,7 @@ public final class AccessHandler implements IAccessHandler { } @Override - public void setOwner(GameProfile owner) { + public void setOwner(@Nonnull GameProfile owner) { this.owner = owner; for (IAccessOwnerListener listener : accessOwnerListeners) { listener.onOwnerSet(owner); diff --git a/src/main/java/forestry/core/access/FakeAccessHandler.java b/src/main/java/forestry/core/access/FakeAccessHandler.java index 997ba7ca0..57470ae58 100644 --- a/src/main/java/forestry/core/access/FakeAccessHandler.java +++ b/src/main/java/forestry/core/access/FakeAccessHandler.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.access; +import javax.annotation.Nonnull; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; @@ -90,7 +91,7 @@ public class FakeAccessHandler implements IAccessHandler { } @Override - public void setOwner(GameProfile owner) { + public void setOwner(@Nonnull GameProfile owner) { } diff --git a/src/main/java/forestry/core/access/IOwnable.java b/src/main/java/forestry/core/access/IOwnable.java index f801b60c4..308f0095c 100644 --- a/src/main/java/forestry/core/access/IOwnable.java +++ b/src/main/java/forestry/core/access/IOwnable.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.core.access; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import net.minecraft.entity.player.EntityPlayer; import com.mojang.authlib.GameProfile; @@ -17,10 +20,11 @@ import com.mojang.authlib.GameProfile; public interface IOwnable { boolean isOwned(); - + + @Nullable GameProfile getOwner(); - - void setOwner(GameProfile owner); + + void setOwner(@Nonnull GameProfile owner); boolean isOwner(EntityPlayer player); diff --git a/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java b/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java index b76f7c775..ca5b8ce29 100644 --- a/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java +++ b/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java @@ -152,7 +152,7 @@ public class TileCocoon extends TileEntity implements IStreamable, IOwnable, IBu } @Override - public void setOwner(GameProfile owner) { + public void setOwner(@Nonnull GameProfile owner) { this.owner = owner; } diff --git a/src/main/java/forestry/mail/MailAddress.java b/src/main/java/forestry/mail/MailAddress.java index 5f30d1e48..11d878d15 100644 --- a/src/main/java/forestry/mail/MailAddress.java +++ b/src/main/java/forestry/mail/MailAddress.java @@ -90,20 +90,10 @@ public class MailAddress implements INbtWritable, IMailAddress { public boolean isValid() { return gameProfile.getName() != null && !PlayerUtil.isSameGameProfile(gameProfile, invalidGameProfile); } - - @Override - public boolean isPlayer() { - return type == EnumAddressee.PLAYER; - } - - @Override - public boolean isTrader() { - return type == EnumAddressee.TRADER; - } - + @Override public GameProfile getPlayerProfile() { - if (!this.isPlayer()) { + if (this.type != EnumAddressee.PLAYER) { return null; } return gameProfile; @@ -127,7 +117,7 @@ public class MailAddress implements INbtWritable, IMailAddress { @Override public String toString() { String name = getName().toLowerCase(Locale.ENGLISH); - if (isPlayer()) { + if (getType() == EnumAddressee.PLAYER) { return type + "-" + name + '-' + gameProfile.getId(); } else { return type + "-" + name; diff --git a/src/main/java/forestry/mail/POBox.java b/src/main/java/forestry/mail/POBox.java index 121bf7b0a..4edd957d2 100755 --- a/src/main/java/forestry/mail/POBox.java +++ b/src/main/java/forestry/mail/POBox.java @@ -17,6 +17,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.WorldSavedData; +import forestry.api.mail.EnumAddressee; import forestry.api.mail.ILetter; import forestry.api.mail.IMailAddress; import forestry.api.mail.PostManager; @@ -33,7 +34,7 @@ public class POBox extends WorldSavedData implements IInventory { public POBox(IMailAddress address) { super(SAVE_NAME + address); - if (!address.isPlayer()) { + if (address.getType() != EnumAddressee.PLAYER) { throw new IllegalArgumentException("POBox address must be a player"); } this.address = address; @@ -85,7 +86,7 @@ public class POBox extends WorldSavedData implements IInventory { continue; } ILetter letter = new Letter(letters.getStackInSlot(i).getTagCompound()); - if (letter.getSender().isPlayer()) { + if (letter.getSender().getType() == EnumAddressee.PLAYER) { playerLetters++; } else { tradeLetters++; diff --git a/src/main/java/forestry/mail/PostRegistry.java b/src/main/java/forestry/mail/PostRegistry.java index cf61defed..917c7f8bb 100755 --- a/src/main/java/forestry/mail/PostRegistry.java +++ b/src/main/java/forestry/mail/PostRegistry.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.mail; +import javax.annotation.Nonnull; import java.io.File; import java.util.EnumMap; import java.util.HashMap; @@ -47,7 +48,7 @@ public class PostRegistry implements IPostRegistry { */ @Override public boolean isValidPOBox(World world, IMailAddress address) { - return address.isPlayer() && address.getName().matches("^[a-zA-Z0-9]+$"); + return address.getType() == EnumAddressee.PLAYER && address.getName().matches("^[a-zA-Z0-9]+$"); } public static POBox getPOBox(World world, IMailAddress address) { @@ -88,7 +89,7 @@ public class PostRegistry implements IPostRegistry { */ @Override public boolean isValidTradeAddress(World world, IMailAddress address) { - return address != null && address.isTrader() && address.getName().matches("^[a-zA-Z0-9]+$"); + return address != null && address.getType() == EnumAddressee.TRADER && address.getName().matches("^[a-zA-Z0-9]+$"); } /** @@ -173,13 +174,14 @@ public class PostRegistry implements IPostRegistry { return office; } + @Nonnull @Override - public IMailAddress getMailAddress(GameProfile gameProfile) { + public IMailAddress getMailAddress(@Nonnull GameProfile gameProfile) { return new MailAddress(gameProfile); } @Override - public IMailAddress getMailAddress(String traderName) { + public IMailAddress getMailAddress(@Nonnull String traderName) { return new MailAddress(traderName); } diff --git a/src/main/java/forestry/mail/TradeStation.java b/src/main/java/forestry/mail/TradeStation.java index cbf891639..6101235af 100755 --- a/src/main/java/forestry/mail/TradeStation.java +++ b/src/main/java/forestry/mail/TradeStation.java @@ -23,6 +23,7 @@ import net.minecraft.world.WorldSavedData; import com.mojang.authlib.GameProfile; +import forestry.api.mail.EnumAddressee; import forestry.api.mail.EnumPostage; import forestry.api.mail.EnumTradeStationState; import forestry.api.mail.ILetter; @@ -64,7 +65,7 @@ public class TradeStation extends WorldSavedData implements ITradeStation, IInve public TradeStation(GameProfile owner, IMailAddress address) { super(SAVE_NAME + address); - if (!address.isTrader()) { + if (address.getType() != EnumAddressee.TRADER) { throw new IllegalArgumentException("TradeStation address must be a trader"); } this.owner = owner; diff --git a/src/main/java/forestry/mail/TradeStationInfo.java b/src/main/java/forestry/mail/TradeStationInfo.java index f3c70ce78..25573fdf3 100644 --- a/src/main/java/forestry/mail/TradeStationInfo.java +++ b/src/main/java/forestry/mail/TradeStationInfo.java @@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack; import com.mojang.authlib.GameProfile; +import forestry.api.mail.EnumAddressee; import forestry.api.mail.EnumTradeStationState; import forestry.api.mail.IMailAddress; import forestry.api.mail.ITradeStationInfo; @@ -26,7 +27,7 @@ public class TradeStationInfo implements ITradeStationInfo { private final EnumTradeStationState state; public TradeStationInfo(IMailAddress address, GameProfile owner, ItemStack tradegood, ItemStack[] required, EnumTradeStationState state) { - if (!address.isTrader()) { + if (address.getType() != EnumAddressee.TRADER) { throw new IllegalArgumentException("TradeStation address must be a trader"); } this.address = address; diff --git a/src/main/java/forestry/mail/gui/ContainerLetter.java b/src/main/java/forestry/mail/gui/ContainerLetter.java index c6862c849..4cc7be3ed 100755 --- a/src/main/java/forestry/mail/gui/ContainerLetter.java +++ b/src/main/java/forestry/mail/gui/ContainerLetter.java @@ -10,8 +10,8 @@ ******************************************************************************/ package forestry.mail.gui; +import javax.annotation.Nullable; import java.util.Iterator; -import java.util.UUID; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; @@ -126,7 +126,7 @@ public class ContainerLetter extends ContainerItemInventory getLetter().setRecipient(recipient); // Update the trading info - if (recipient == null || recipient.isTrader()) { + if (recipient == null || recipient.getType() == EnumAddressee.TRADER) { updateTradeInfo(player.worldObj, recipient); } @@ -134,12 +134,13 @@ public class ContainerLetter extends ContainerItemInventory Proxies.net.sendToPlayer(new PacketLetterInfoResponse(type, tradeInfo, recipient), player); } + @Nullable private static IMailAddress getRecipient(MinecraftServer minecraftServer, String recipientName, EnumAddressee type) { switch (type) { case PLAYER: { GameProfile gameProfile = minecraftServer.getPlayerProfileCache().getGameProfileForUsername(recipientName); if (gameProfile == null) { - gameProfile = new GameProfile(new UUID(0, 0), recipientName); + return null; } return PostManager.postRegistry.getMailAddress(gameProfile); } diff --git a/src/main/java/forestry/mail/tiles/TileMailbox.java b/src/main/java/forestry/mail/tiles/TileMailbox.java index 6dd02d5a1..217379b33 100644 --- a/src/main/java/forestry/mail/tiles/TileMailbox.java +++ b/src/main/java/forestry/mail/tiles/TileMailbox.java @@ -10,6 +10,8 @@ ******************************************************************************/ package forestry.mail.tiles; +import javax.annotation.Nonnull; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -63,13 +65,16 @@ public class TileMailbox extends TileBase implements IMailContainer { @Override public void updateServerSide() { if (!isLinked) { - getOrCreateMailInventory(worldObj, getAccessHandler().getOwner()); + GameProfile owner = getAccessHandler().getOwner(); + if (owner != null) { + getOrCreateMailInventory(worldObj, owner); + } isLinked = true; } } /* MAIL HANDLING */ - public IInventory getOrCreateMailInventory(World world, GameProfile playerProfile) { + public IInventory getOrCreateMailInventory(World world, @Nonnull GameProfile playerProfile) { if (world.isRemote) { return getInternalInventory(); } @@ -93,8 +98,11 @@ public class TileMailbox extends TileBase implements IMailContainer { @Override public boolean hasMail() { - - IInventory mailInventory = getOrCreateMailInventory(worldObj, getAccessHandler().getOwner()); + GameProfile owner = getAccessHandler().getOwner(); + if (owner == null) { + return false; + } + IInventory mailInventory = getOrCreateMailInventory(worldObj, owner); for (int i = 0; i < mailInventory.getSizeInventory(); i++) { if (mailInventory.getStackInSlot(i) != null) { return true;