diff --git a/src/main/java/forestry/core/network/PacketBufferForestry.java b/src/main/java/forestry/core/network/PacketBufferForestry.java index 66eb010c8..0eab65d6f 100644 --- a/src/main/java/forestry/core/network/PacketBufferForestry.java +++ b/src/main/java/forestry/core/network/PacketBufferForestry.java @@ -2,7 +2,6 @@ package forestry.core.network; import javax.annotation.Nullable; import java.io.IOException; -import java.io.InvalidObjectException; import java.util.List; import io.netty.buffer.ByteBuf; @@ -122,15 +121,9 @@ public class PacketBufferForestry extends PacketBuffer { } @Nullable - public T readStreamable(Class streamableClass) throws IOException { + public T readStreamable(IStreamableFactory factory) throws IOException { if (readBoolean()) { - try { - T streamable = streamableClass.newInstance(); - streamable.readData(this); - return streamable; - } catch (IllegalAccessException | InstantiationException | IOException e) { - throw new InvalidObjectException("Failed to read Streamable for class " + streamableClass + " with error " + e); - } + return factory.create(this); } return null; } @@ -146,14 +139,18 @@ public class PacketBufferForestry extends PacketBuffer { } } - public void readStreamables(List outputList, Class streamableClass) throws IOException { + public void readStreamables(List outputList, IStreamableFactory factory) throws IOException { outputList.clear(); int length = readVarInt(); if (length > 0) { for (int i = 0; i < length; i++) { - T streamable = readStreamable(streamableClass); + T streamable = readStreamable(factory); outputList.add(streamable); } } } + + public interface IStreamableFactory { + T create(PacketBufferForestry data) throws IOException; + } } diff --git a/src/main/java/forestry/core/tiles/EscritoireGameBoard.java b/src/main/java/forestry/core/tiles/EscritoireGameBoard.java index 018f2bc26..7c5f36447 100644 --- a/src/main/java/forestry/core/tiles/EscritoireGameBoard.java +++ b/src/main/java/forestry/core/tiles/EscritoireGameBoard.java @@ -225,6 +225,6 @@ public class EscritoireGameBoard implements INbtWritable, IStreamable { @Override public void readData(PacketBufferForestry data) throws IOException { tokenCount = data.readVarInt(); - data.readStreamables(gameTokens, EscritoireGameToken.class); + data.readStreamables(gameTokens, EscritoireGameToken::new); } } diff --git a/src/main/java/forestry/core/tiles/EscritoireGameToken.java b/src/main/java/forestry/core/tiles/EscritoireGameToken.java index 5e33218e0..adfcff2ca 100644 --- a/src/main/java/forestry/core/tiles/EscritoireGameToken.java +++ b/src/main/java/forestry/core/tiles/EscritoireGameToken.java @@ -47,9 +47,8 @@ public class EscritoireGameToken implements INbtWritable, IStreamable { private State state = State.UNREVEALED; - @SuppressWarnings("unused") - public EscritoireGameToken() { - // required for IStreamable serialization + public EscritoireGameToken(PacketBufferForestry data) throws IOException { + readData(data); } public EscritoireGameToken(String speciesUid) { diff --git a/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java index 26cf8f4cd..218728760 100644 --- a/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java +++ b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java @@ -53,8 +53,7 @@ public class PacketWorktableRecipeRequest extends ForestryPacket implements IFor @Override public void onPacketData(PacketBufferForestry data, EntityPlayerMP player) throws IOException { BlockPos pos = data.readBlockPos(); - MemorizedRecipe recipe = new MemorizedRecipe(); - recipe.readData(data); + MemorizedRecipe recipe = new MemorizedRecipe(data); TileEntity tile = player.world.getTileEntity(pos); if (tile instanceof TileWorktable) { diff --git a/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeUpdate.java b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeUpdate.java index 4b24d1d4b..bd1ee058f 100644 --- a/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeUpdate.java +++ b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeUpdate.java @@ -52,8 +52,7 @@ public class PacketWorktableRecipeUpdate extends ForestryPacket implements IFore @Override public void onPacketData(PacketBufferForestry data, EntityPlayer player) throws IOException { BlockPos pos = data.readBlockPos(); - MemorizedRecipe recipe = new MemorizedRecipe(); - recipe.readData(data); + MemorizedRecipe recipe = data.readStreamable(MemorizedRecipe::new); TileEntity tile = player.world.getTileEntity(pos); if (tile instanceof TileWorktable) { diff --git a/src/main/java/forestry/factory/recipes/MemorizedRecipe.java b/src/main/java/forestry/factory/recipes/MemorizedRecipe.java index b700bfeae..ec09648bf 100644 --- a/src/main/java/forestry/factory/recipes/MemorizedRecipe.java +++ b/src/main/java/forestry/factory/recipes/MemorizedRecipe.java @@ -32,8 +32,12 @@ public final class MemorizedRecipe implements INbtWritable, INbtReadable, IStrea private long lastUsed; private boolean locked; - public MemorizedRecipe() { - // required for IStreamable serialization + public MemorizedRecipe(PacketBufferForestry data) throws IOException { + readData(data); + } + + public MemorizedRecipe(NBTTagCompound nbt) { + readFromNBT(nbt); } public MemorizedRecipe(InventoryCraftingForestry craftMatrix, NonNullList recipeOutputs) { diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java index 9275b87cd..3f8fe6db2 100644 --- a/src/main/java/forestry/factory/recipes/RecipeMemory.java +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -53,8 +53,7 @@ public class RecipeMemory implements INbtWritable, IStreamable { NBTTagList nbttaglist = nbt.getTagList("RecipeMemory", 10); for (int j = 0; j < nbttaglist.tagCount(); ++j) { NBTTagCompound recipeNbt = nbttaglist.getCompoundTagAt(j); - MemorizedRecipe recipe = new MemorizedRecipe(); - recipe.readFromNBT(recipeNbt); + MemorizedRecipe recipe = new MemorizedRecipe(recipeNbt); memorizedRecipes.add(recipe); } } @@ -202,6 +201,6 @@ public class RecipeMemory implements INbtWritable, IStreamable { @Override public void readData(PacketBufferForestry data) throws IOException { - data.readStreamables(memorizedRecipes, MemorizedRecipe.class); + data.readStreamables(memorizedRecipes, MemorizedRecipe::new); } }