From 8165d08cc666bc9291328c128c83a8e0e20016b5 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Fri, 16 Feb 2024 11:23:20 +0800 Subject: [PATCH 1/3] Enable the openGui data longer than 128 bits warning! break the network compat with old forge server/client. https://github.com/MinecraftForge/MinecraftForge/issues/3228 --- .../entity/player/EntityPlayer.java.patch | 26 ++++++++++- .../common/util/FakePlayer.java | 5 ++ .../fml/common/network/IGuiHandler.java | 38 +++++++++++++++ .../fml/common/network/NetworkRegistry.java | 46 +++++++++++++++++++ .../common/network/internal/FMLMessage.java | 25 ++++++++++ .../network/internal/FMLNetworkHandler.java | 41 +++++++++++++++++ .../network/internal/OpenGuiHandler.java | 3 +- 7 files changed, 182 insertions(+), 2 deletions(-) diff --git a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch index 5f8f08b43..980934948 100644 --- a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch +++ b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch @@ -1625,7 +1625,7 @@ public void func_70108_f(Entity p_70108_1_) { if (!this.func_70608_bn()) -@@ -2604,6 +2533,168 @@ +@@ -2604,6 +2533,192 @@ { return this.field_71075_bZ.field_75098_d && this.func_70003_b(2, ""); } @@ -1646,6 +1646,30 @@ + net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z); + } + ++ /** ++ * Opens a GUI with this player, uses FML's IGuiHandler system. ++ * Allows for extension by modders. ++ * ++ * @param mod The mod trying to open a GUI ++ * @param modGuiId GUI ID ++ * @param world Current World ++ * @param x Passed directly to IGuiHandler, data meaningless Typically world X position ++ * @param y Passed directly to IGuiHandler, data meaningless Typically world Y position ++ * @param z Passed directly to IGuiHandler, data meaningless Typically world Z position ++ * @param customData The custom data to IGuiHandler. For the information longer than 128 bits. ++ */ ++ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, io.netty.buffer.ByteBuf customData) ++ { ++ net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z, customData); ++ } ++ ++ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, java.util.function.Consumer encoder) ++ { ++ io.netty.buffer.ByteBuf buf = io.netty.buffer.Unpooled.buffer(); ++ encoder.accept(buf); ++ this.openGui(mod, modGuiId, world, x, y, z, buf); ++ } ++ + + /* ======================================== FORGE START =====================================*/ + /** diff --git a/src/main/java/net/minecraftforge/common/util/FakePlayer.java b/src/main/java/net/minecraftforge/common/util/FakePlayer.java index 652072fda..e331a53e7 100644 --- a/src/main/java/net/minecraftforge/common/util/FakePlayer.java +++ b/src/main/java/net/minecraftforge/common/util/FakePlayer.java @@ -23,6 +23,7 @@ import com.mojang.authlib.GameProfile; +import io.netty.buffer.ByteBuf; import net.minecraft.entity.Entity; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraft.entity.player.EntityPlayer; @@ -37,6 +38,8 @@ import net.minecraft.world.World; import net.minecraft.world.WorldServer; +import java.util.function.Consumer; + //Preliminary, simple Fake Player class public class FakePlayer extends EntityPlayerMP { @@ -51,6 +54,8 @@ public FakePlayer(WorldServer world, GameProfile name) @Override public void sendMessage(ITextComponent component) {} @Override public void addStat(StatBase par1StatBase, int par2){} @Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z){} + @Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, Consumer encoder) {} + @Override public void openGui(Object mod, int modGuiId, World world, int x, int y, int z, ByteBuf customData) {} @Override public boolean isEntityInvulnerable(DamageSource source){ return true; } @Override public boolean canAttackPlayer(EntityPlayer player){ return false; } @Override public void onDeath(DamageSource source){ return; } diff --git a/src/main/java/net/minecraftforge/fml/common/network/IGuiHandler.java b/src/main/java/net/minecraftforge/fml/common/network/IGuiHandler.java index f841f66a2..9afe036b0 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/IGuiHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/IGuiHandler.java @@ -19,6 +19,7 @@ package net.minecraftforge.fml.common.network; +import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; @@ -55,4 +56,41 @@ public interface IGuiHandler */ @Nullable Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z); + + + /** + * Returns a Server side Container to be displayed to the user. + * + * @param ID The Gui ID Number + * @param player The player viewing the Gui + * @param world The current world + * @param x X Position + * @param y Y Position + * @param z Z Position + * @param customData The customData + * @return A GuiScreen/Container to be displayed to the user, null if none. + */ + @Nullable + default Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z, ByteBuf customData){ + return null; + } + + /** + * Returns a Container to be displayed to the user. On the client side, this + * needs to return a instance of GuiScreen On the server side, this needs to + * return a instance of Container + * + * @param ID The Gui ID Number + * @param player The player viewing the Gui + * @param world The current world + * @param x X Position + * @param y Y Position + * @param z Z Position + * @param customData The customData + * @return A GuiScreen/Container to be displayed to the user, null if none. + */ + @Nullable + default Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z, ByteBuf customData){ + return null; + } } diff --git a/src/main/java/net/minecraftforge/fml/common/network/NetworkRegistry.java b/src/main/java/net/minecraftforge/fml/common/network/NetworkRegistry.java index d0c2499bc..b28a2586b 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/NetworkRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/network/NetworkRegistry.java @@ -19,6 +19,7 @@ package net.minecraftforge.fml.common.network; +import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageCodec; @@ -258,6 +259,32 @@ public Container getRemoteGuiContainer(ModContainer mc, EntityPlayerMP player, i } } + /** + * INTERNAL method for accessing the Gui registry + * @param mc Mod Container + * @param player Player + * @param modGuiId guiId + * @param world World + * @param x X coord + * @param y Y coord + * @param z Z coord + * @return The server side GUI object (An instance of {@link Container}) + */ + @Nullable + public Container getRemoteGuiContainer(ModContainer mc, EntityPlayerMP player, int modGuiId, World world, int x, int y, int z, ByteBuf customData) + { + IGuiHandler handler = serverGuiHandlers.get(mc); + + if (handler != null) + { + return (Container)handler.getServerGuiElement(modGuiId, player, world, x, y, z ,customData); + } + else + { + return null; + } + } + /** * INTERNAL method for accessing the Gui registry * @param mc Mod Container @@ -276,6 +303,25 @@ public Object getLocalGuiContainer(ModContainer mc, EntityPlayer player, int mod return handler.getClientGuiElement(modGuiId, player, world, x, y, z); } + /** + * INTERNAL method for accessing the Gui registry + * @param mc Mod Container + * @param player Player + * @param modGuiId guiId + * @param world World + * @param x X coord + * @param y Y coord + * @param z Z coord + * @param customData Custom Data + * @return The client side GUI object (An instance of {@link net.minecraft.client.gui.Gui}) + */ + @Nullable + public Object getLocalGuiContainer(ModContainer mc, EntityPlayer player, int modGuiId, World world, int x, int y, int z, ByteBuf customData) + { + IGuiHandler handler = clientGuiHandlers.get(mc); + return handler.getClientGuiElement(modGuiId, player, world, x, y, z, customData); + } + /** * Is there a channel with this name on this side? * @param channelName The name diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java index 12bc508b1..ed4e256f2 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.List; import java.util.UUID; +import java.util.function.Consumer; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; @@ -65,6 +66,8 @@ public static class OpenGui extends FMLMessage { int x; int y; int z; + boolean isLegacy; + ByteBuf customData; public OpenGui() {} OpenGui(int windowId, String modId, int modGuiId, int x, int y, int z) @@ -75,6 +78,19 @@ public OpenGui() {} this.x = x; this.y = y; this.z = z; + isLegacy=true; + customData=null; + } + OpenGui(int windowId, String modId, int modGuiId, int x, int y, int z, ByteBuf customData) + { + this.windowId = windowId; + this.modId = modId; + this.modGuiId = modGuiId; + this.x = x; + this.y = y; + this.z = z; + this.isLegacy = false; + this.customData = customData; } @Override @@ -86,6 +102,10 @@ void toBytes(ByteBuf buf) buf.writeInt(x); buf.writeInt(y); buf.writeInt(z); + buf.writeBoolean(isLegacy); + if (!isLegacy){ + buf.writeBytes(customData); + } } @Override @@ -97,6 +117,11 @@ void fromBytes(ByteBuf buf) x = buf.readInt(); y = buf.readInt(); z = buf.readInt(); + isLegacy= buf.readBoolean(); + if (!isLegacy){ + customData=Unpooled.buffer(); + buf.readBytes(customData); + } } } diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java index 691d59695..8bccb617b 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java @@ -19,6 +19,8 @@ package net.minecraftforge.fml.common.network.internal; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelPipeline; import io.netty.channel.embedded.EmbeddedChannel; @@ -27,6 +29,7 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import java.util.stream.Collectors; import net.minecraft.entity.Entity; @@ -116,6 +119,44 @@ else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT)) FMLLog.log.debug("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: {},{}", mc.getModId(), modGuiId); } + } + public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, World world, int x, int y, int z, ByteBuf customData) + { + ModContainer mc = FMLCommonHandler.instance().findContainerFor(mod); + if (entityPlayer instanceof EntityPlayerMP && !(entityPlayer instanceof FakePlayer)) + { + EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entityPlayer; + Container remoteGuiContainer = NetworkRegistry.INSTANCE.getRemoteGuiContainer(mc, entityPlayerMP, modGuiId, world, x, y, z, customData); + if (remoteGuiContainer != null) + { + entityPlayerMP.getNextWindowId(); + entityPlayerMP.closeContainer(); + int windowId = entityPlayerMP.currentWindowId; + FMLMessage.OpenGui openGui = new FMLMessage.OpenGui(windowId, mc.getModId(), modGuiId, x, y, z, customData); + EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER); + embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.PLAYER); + embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entityPlayerMP); + embeddedChannel.writeOutbound(openGui); + entityPlayerMP.openContainer = remoteGuiContainer; + entityPlayerMP.openContainer.windowId = windowId; + entityPlayerMP.openContainer.addListener(entityPlayerMP); + net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer)); + } + } + else if (entityPlayer instanceof FakePlayer) + { + // NO OP - I won't even log a message! + } + else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT)) + { + Object guiContainer = NetworkRegistry.INSTANCE.getLocalGuiContainer(mc, entityPlayer, modGuiId, world, x, y, z, customData); + FMLCommonHandler.instance().showGuiScreen(guiContainer); + } + else + { + FMLLog.log.debug("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: {},{}", mc.getModId(), modGuiId); + } + } @Nullable diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java index 7e8c3a713..c4b063443 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java @@ -47,7 +47,8 @@ protected void channelRead0(ChannelHandlerContext ctx, final OpenGui msg) throws private void process(OpenGui msg) { EntityPlayer player = FMLClientHandler.instance().getClient().player; - player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z); + if (msg.isLegacy) player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z); + else player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z, msg.customData); player.openContainer.windowId = msg.windowId; } From 78ac4537f62055d3814c5021944cb8a7a07a3e9c Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Sat, 17 Feb 2024 14:00:47 +0800 Subject: [PATCH 2/3] Update FMLNetworkHandler import --- .../network/internal/FMLNetworkHandler.java | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java index 8bccb617b..7a8675ee5 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java @@ -19,19 +19,11 @@ package net.minecraftforge.fml.common.network.internal; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import io.netty.channel.ChannelPipeline; import io.netty.channel.embedded.EmbeddedChannel; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; -import java.util.stream.Collectors; - import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -40,16 +32,14 @@ import net.minecraft.network.Packet; import net.minecraft.server.management.PlayerList; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.fml.common.FMLCommonHandler; -import net.minecraftforge.fml.common.FMLContainer; -import net.minecraftforge.fml.common.FMLLog; -import net.minecraftforge.fml.common.Loader; -import net.minecraftforge.fml.common.ModContainer; +import net.minecraftforge.event.entity.player.PlayerContainerEvent; +import net.minecraftforge.fml.common.*; import net.minecraftforge.fml.common.network.FMLEmbeddedChannel; import net.minecraftforge.fml.common.network.FMLOutboundHandler; -import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget; +import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.handshake.FMLHandshakeMessage; import net.minecraftforge.fml.common.network.handshake.NetworkDispatcher; import net.minecraftforge.fml.common.network.internal.FMLMessage.CompleteHandshake; @@ -57,12 +47,11 @@ import net.minecraftforge.fml.common.registry.EntityRegistry.EntityRegistration; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; import org.apache.commons.lang3.tuple.Pair; import javax.annotation.Nullable; +import java.util.*; +import java.util.stream.Collectors; public class FMLNetworkHandler { @@ -102,7 +91,7 @@ public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, entityPlayerMP.openContainer = remoteGuiContainer; entityPlayerMP.openContainer.windowId = windowId; entityPlayerMP.openContainer.addListener(entityPlayerMP); - net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer)); + MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer)); } } else if (entityPlayer instanceof FakePlayer) @@ -140,7 +129,7 @@ public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, entityPlayerMP.openContainer = remoteGuiContainer; entityPlayerMP.openContainer.windowId = windowId; entityPlayerMP.openContainer.addListener(entityPlayerMP); - net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer)); + MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer)); } } else if (entityPlayer instanceof FakePlayer) From 75c6f323cf03e2a6c55b93541fc2e06c2de89a12 Mon Sep 17 00:00:00 2001 From: Hileb <107909747+Ecdcaeb@users.noreply.github.com> Date: Sat, 17 Feb 2024 15:36:26 +0800 Subject: [PATCH 3/3] new OpenGuiMessage for the expanded openGui() --- .../common/network/internal/FMLMessage.java | 51 +++++++++++++------ .../network/internal/FMLNetworkHandler.java | 3 +- .../network/internal/FMLRuntimeCodec.java | 1 + .../internal/OpenGuiExpandHandler.java | 38 ++++++++++++++ .../network/internal/OpenGuiHandler.java | 3 +- 5 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiExpandHandler.java diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java index ed4e256f2..e0a641cf6 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLMessage.java @@ -66,9 +66,6 @@ public static class OpenGui extends FMLMessage { int x; int y; int z; - boolean isLegacy; - ByteBuf customData; - public OpenGui() {} OpenGui(int windowId, String modId, int modGuiId, int x, int y, int z) { @@ -78,10 +75,39 @@ public OpenGui() {} this.x = x; this.y = y; this.z = z; - isLegacy=true; - customData=null; } - OpenGui(int windowId, String modId, int modGuiId, int x, int y, int z, ByteBuf customData) + @Override + void toBytes(ByteBuf buf) + { + buf.writeInt(windowId); + ByteBufUtils.writeUTF8String(buf, modId); + buf.writeInt(modGuiId); + buf.writeInt(x); + buf.writeInt(y); + buf.writeInt(z); + } + @Override + void fromBytes(ByteBuf buf) + { + windowId = buf.readInt(); + modId = ByteBufUtils.readUTF8String(buf); + modGuiId = buf.readInt(); + x = buf.readInt(); + y = buf.readInt(); + z = buf.readInt(); + } + } + public static class OpenGuiExpand extends FMLMessage { + int windowId; + String modId; + int modGuiId; + int x; + int y; + int z; + ByteBuf customData; + + public OpenGuiExpand() {} + OpenGuiExpand(int windowId, String modId, int modGuiId, int x, int y, int z, ByteBuf customData) { this.windowId = windowId; this.modId = modId; @@ -89,7 +115,6 @@ public OpenGui() {} this.x = x; this.y = y; this.z = z; - this.isLegacy = false; this.customData = customData; } @@ -102,10 +127,7 @@ void toBytes(ByteBuf buf) buf.writeInt(x); buf.writeInt(y); buf.writeInt(z); - buf.writeBoolean(isLegacy); - if (!isLegacy){ - buf.writeBytes(customData); - } + buf.writeBytes(customData); } @Override @@ -117,11 +139,8 @@ void fromBytes(ByteBuf buf) x = buf.readInt(); y = buf.readInt(); z = buf.readInt(); - isLegacy= buf.readBoolean(); - if (!isLegacy){ - customData=Unpooled.buffer(); - buf.readBytes(customData); - } + customData=Unpooled.buffer(); + buf.readBytes(customData); } } diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java index 7a8675ee5..558e78a4c 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLNetworkHandler.java @@ -121,7 +121,7 @@ public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, entityPlayerMP.getNextWindowId(); entityPlayerMP.closeContainer(); int windowId = entityPlayerMP.currentWindowId; - FMLMessage.OpenGui openGui = new FMLMessage.OpenGui(windowId, mc.getModId(), modGuiId, x, y, z, customData); + FMLMessage.OpenGuiExpand openGui = new FMLMessage.OpenGuiExpand(windowId, mc.getModId(), modGuiId, x, y, z, customData); EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER); embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.PLAYER); embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entityPlayerMP); @@ -209,6 +209,7 @@ private static void addClientHandlers() String targetName = channelPair.get(Side.CLIENT).findChannelHandlerNameForType(FMLRuntimeCodec.class); pipeline.addAfter(targetName, "GuiHandler", new OpenGuiHandler()); pipeline.addAfter(targetName, "EntitySpawnHandler", new EntitySpawnHandler()); + pipeline.addAfter(targetName, "GuiExpandHandler", new OpenGuiExpandHandler()); } public static void registerChannel(FMLContainer container, Side side) { diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLRuntimeCodec.java b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLRuntimeCodec.java index fb17e7da9..80e0e6bd8 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/FMLRuntimeCodec.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/FMLRuntimeCodec.java @@ -35,6 +35,7 @@ public FMLRuntimeCodec() addDiscriminator(0,FMLMessage.CompleteHandshake.class); addDiscriminator(1,FMLMessage.OpenGui.class); addDiscriminator(2,FMLMessage.EntitySpawnMessage.class); + addDiscriminator(3,FMLMessage.OpenGuiExpand.class); } @Override public void encodeInto(ChannelHandlerContext ctx, FMLMessage msg, ByteBuf target) throws Exception diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiExpandHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiExpandHandler.java new file mode 100644 index 000000000..5e1e052ea --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiExpandHandler.java @@ -0,0 +1,38 @@ +package net.minecraftforge.fml.common.network.internal; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.IThreadListener; +import net.minecraftforge.fml.client.FMLClientHandler; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.FMLLog; +import net.minecraftforge.fml.common.network.NetworkRegistry; + +public class OpenGuiExpandHandler extends SimpleChannelInboundHandler { + @Override + protected void channelRead0(ChannelHandlerContext ctx, FMLMessage.OpenGuiExpand msg) throws Exception { + IThreadListener thread = FMLCommonHandler.instance().getWorldThread(ctx.channel().attr(NetworkRegistry.NET_HANDLER).get()); + if (thread.isCallingFromMinecraftThread()) + { + process(msg); + } + else + { + thread.addScheduledTask(() -> OpenGuiExpandHandler.this.process(msg)); + } + } + private void process(FMLMessage.OpenGuiExpand msg) + { + EntityPlayer player = FMLClientHandler.instance().getClient().player; + player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z, msg.customData); + player.openContainer.windowId = msg.windowId; + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception + { + FMLLog.log.error("OpenGuiHandler exception", cause); + super.exceptionCaught(ctx, cause); + } +} diff --git a/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java index c4b063443..7e8c3a713 100644 --- a/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java +++ b/src/main/java/net/minecraftforge/fml/common/network/internal/OpenGuiHandler.java @@ -47,8 +47,7 @@ protected void channelRead0(ChannelHandlerContext ctx, final OpenGui msg) throws private void process(OpenGui msg) { EntityPlayer player = FMLClientHandler.instance().getClient().player; - if (msg.isLegacy) player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z); - else player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z, msg.customData); + player.openGui(msg.modId, msg.modGuiId, player.world, msg.x, msg.y, msg.z); player.openContainer.windowId = msg.windowId; }