From e050c7807d12990f7903af4e91568dcc6ce1a1e3 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 8 Aug 2012 00:31:24 -0400 Subject: [PATCH] Gui Handler ported from MC forge --- .../cpw/mods/fml/client/FMLClientHandler.java | 7 +++ .../cpw/mods/fml/common/FMLCommonHandler.java | 5 ++ .../cpw/mods/fml/common/IFMLSidedHandler.java | 2 + .../modloader/ModLoaderConnectionHandler.java | 16 ++--- .../fml/common/network/FMLNetworkHandler.java | 16 ++++- .../mods/fml/common/network/FMLPacket.java | 6 +- .../common/network/IConnectionHandler.java | 4 +- .../mods/fml/common/network/IGuiHandler.java | 46 ++++++++++++++ .../fml/common/network/NetworkRegistry.java | 38 +++++++++++- .../fml/common/network/OpenGuiPacket.java | 61 +++++++++++++++++++ common/fml_at.cfg | 4 ++ .../net/minecraft/src/EntityPlayer.java.patch | 22 +++++++ .../net/minecraft/src/EntityPlayer.java.patch | 21 +++++++ .../cpw/mods/fml/server/FMLServerHandler.java | 6 ++ 14 files changed, 239 insertions(+), 15 deletions(-) create mode 100644 common/cpw/mods/fml/common/network/IGuiHandler.java create mode 100644 common/cpw/mods/fml/common/network/OpenGuiPacket.java create mode 100644 patches/minecraft/net/minecraft/src/EntityPlayer.java.patch create mode 100644 patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch diff --git a/client/cpw/mods/fml/client/FMLClientHandler.java b/client/cpw/mods/fml/client/FMLClientHandler.java index 488270e587..fe11444662 100644 --- a/client/cpw/mods/fml/client/FMLClientHandler.java +++ b/client/cpw/mods/fml/client/FMLClientHandler.java @@ -325,4 +325,11 @@ public class FMLClientHandler implements IFMLSidedHandler { return optifineContainer!=null; } + + @Override + public void showGuiScreen(Object clientGuiElement) + { + GuiScreen gui = (GuiScreen) clientGuiElement; + client.func_71373_a(gui); + } } diff --git a/common/cpw/mods/fml/common/FMLCommonHandler.java b/common/cpw/mods/fml/common/FMLCommonHandler.java index 0e78b2bb39..eed28e7368 100644 --- a/common/cpw/mods/fml/common/FMLCommonHandler.java +++ b/common/cpw/mods/fml/common/FMLCommonHandler.java @@ -337,4 +337,9 @@ public class FMLCommonHandler //TODO return null; } + + public void showGuiScreen(Object clientGuiElement) + { + sidedDelegate.showGuiScreen(clientGuiElement); + } } diff --git a/common/cpw/mods/fml/common/IFMLSidedHandler.java b/common/cpw/mods/fml/common/IFMLSidedHandler.java index 878edda0f6..2f8c4bad77 100644 --- a/common/cpw/mods/fml/common/IFMLSidedHandler.java +++ b/common/cpw/mods/fml/common/IFMLSidedHandler.java @@ -20,4 +20,6 @@ public interface IFMLSidedHandler Side getSide(); void haltGame(String message, Throwable exception); + + void showGuiScreen(Object clientGuiElement); } diff --git a/common/cpw/mods/fml/common/modloader/ModLoaderConnectionHandler.java b/common/cpw/mods/fml/common/modloader/ModLoaderConnectionHandler.java index 987bbccec6..60161d1f6b 100644 --- a/common/cpw/mods/fml/common/modloader/ModLoaderConnectionHandler.java +++ b/common/cpw/mods/fml/common/modloader/ModLoaderConnectionHandler.java @@ -1,6 +1,6 @@ package cpw.mods.fml.common.modloader; -import net.minecraft.src.IntegratedServer; +import net.minecraft.server.MinecraftServer; import net.minecraft.src.NetHandler; import net.minecraft.src.NetLoginHandler; import net.minecraft.src.NetworkManager; @@ -30,13 +30,6 @@ public class ModLoaderConnectionHandler implements IConnectionHandler } - @Override - public void connectionOpened(NetHandler netClientHandler, IntegratedServer server, NetworkManager manager) - { - // TODO Auto-generated method stub - - } - @Override public void connectionClosed(NetworkManager manager) { @@ -51,4 +44,11 @@ public class ModLoaderConnectionHandler implements IConnectionHandler } + @Override + public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, NetworkManager manager) + { + // TODO Auto-generated method stub + + } + } diff --git a/common/cpw/mods/fml/common/network/FMLNetworkHandler.java b/common/cpw/mods/fml/common/network/FMLNetworkHandler.java index e12dd80880..160325dd07 100644 --- a/common/cpw/mods/fml/common/network/FMLNetworkHandler.java +++ b/common/cpw/mods/fml/common/network/FMLNetworkHandler.java @@ -10,7 +10,6 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.src.EntityPlayer; import net.minecraft.src.EntityPlayerMP; import net.minecraft.src.EnumGameType; -import net.minecraft.src.IntegratedServer; import net.minecraft.src.NetHandler; import net.minecraft.src.NetLoginHandler; import net.minecraft.src.NetServerHandler; @@ -19,6 +18,7 @@ import net.minecraft.src.Packet; import net.minecraft.src.Packet1Login; import net.minecraft.src.Packet250CustomPayload; import net.minecraft.src.ServerConfigurationManager; +import net.minecraft.src.World; import net.minecraft.src.WorldType; import com.google.common.collect.Maps; @@ -258,7 +258,7 @@ public class FMLNetworkHandler NetworkRegistry.instance().connectionOpened(netClientHandler, server, port, networkManager); } - public static void onClientConnectionToIntegratedServer(NetHandler netClientHandler, IntegratedServer server, NetworkManager networkManager) + public static void onClientConnectionToIntegratedServer(NetHandler netClientHandler, MinecraftServer server, NetworkManager networkManager) { NetworkRegistry.instance().connectionOpened(netClientHandler, server, networkManager); } @@ -272,4 +272,16 @@ public class FMLNetworkHandler public static void sendPacket(Player player, Packet packet) { } + + public static void openGui(EntityPlayer player, Object mod, int modGuiId, World world, int x, int y, int z) + { + if (player instanceof EntityPlayerMP) + { + NetworkRegistry.instance().openRemoteGui(instance().findNetworkModHandler(mod), (EntityPlayerMP) player, modGuiId, world, x, y, z); + } + else + { + NetworkRegistry.instance().openLocalGui(instance().findNetworkModHandler(mod), (EntityPlayerMP) player, modGuiId, world, x, y, z); + } + } } \ No newline at end of file diff --git a/common/cpw/mods/fml/common/network/FMLPacket.java b/common/cpw/mods/fml/common/network/FMLPacket.java index a7e975ec60..be83efb34e 100644 --- a/common/cpw/mods/fml/common/network/FMLPacket.java +++ b/common/cpw/mods/fml/common/network/FMLPacket.java @@ -35,7 +35,11 @@ public abstract class FMLPacket /** * Or, if there is missing stuff, the server tells the client what's missing and drops the connection. */ - MOD_MISSING(ModMissingPacket.class); + MOD_MISSING(ModMissingPacket.class), + /** + * Open a GUI on the client from the server + */ + GUIOPEN(OpenGuiPacket.class); private Class packetType; diff --git a/common/cpw/mods/fml/common/network/IConnectionHandler.java b/common/cpw/mods/fml/common/network/IConnectionHandler.java index c690db7108..acecc8f661 100644 --- a/common/cpw/mods/fml/common/network/IConnectionHandler.java +++ b/common/cpw/mods/fml/common/network/IConnectionHandler.java @@ -1,8 +1,8 @@ package cpw.mods.fml.common.network; +import net.minecraft.server.MinecraftServer; import net.minecraft.src.EntityPlayer; import net.minecraft.src.EntityPlayerMP; -import net.minecraft.src.IntegratedServer; import net.minecraft.src.NetHandler; import net.minecraft.src.NetLoginHandler; import net.minecraft.src.NetServerHandler; @@ -52,7 +52,7 @@ public interface IConnectionHandler * @param netClientHandler * @param server */ - void connectionOpened(NetHandler netClientHandler, IntegratedServer server, NetworkManager manager); + void connectionOpened(NetHandler netClientHandler, MinecraftServer server, NetworkManager manager); /** * Fired when a connection closes diff --git a/common/cpw/mods/fml/common/network/IGuiHandler.java b/common/cpw/mods/fml/common/network/IGuiHandler.java new file mode 100644 index 0000000000..73f01aa438 --- /dev/null +++ b/common/cpw/mods/fml/common/network/IGuiHandler.java @@ -0,0 +1,46 @@ +package cpw.mods.fml.common.network; + +import net.minecraft.src.EntityPlayer; +import net.minecraft.src.World; + +public interface IGuiHandler +{ + /** + * 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 + * @return A GuiScreen/Container to be displayed to the user, null if none. + */ + public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z); + /** + * 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 + * @return A GuiScreen/Container to be displayed to the user, null if none. + */ + public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z); +} diff --git a/common/cpw/mods/fml/common/network/NetworkRegistry.java b/common/cpw/mods/fml/common/network/NetworkRegistry.java index a71bb9bd6e..a3e0a0429b 100644 --- a/common/cpw/mods/fml/common/network/NetworkRegistry.java +++ b/common/cpw/mods/fml/common/network/NetworkRegistry.java @@ -8,15 +8,18 @@ import java.util.Map; import java.util.Set; import java.util.logging.Level; +import net.minecraft.server.MinecraftServer; +import net.minecraft.src.Container; import net.minecraft.src.EntityPlayer; import net.minecraft.src.EntityPlayerMP; -import net.minecraft.src.IntegratedServer; import net.minecraft.src.NetHandler; import net.minecraft.src.NetLoginHandler; import net.minecraft.src.NetServerHandler; import net.minecraft.src.NetworkManager; import net.minecraft.src.Packet1Login; import net.minecraft.src.Packet250CustomPayload; +import net.minecraft.src.TcpConnection; +import net.minecraft.src.World; import com.google.common.base.Charsets; import com.google.common.base.Joiner; @@ -31,8 +34,10 @@ import com.google.common.collect.Multimaps; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.ModContainer; +import cpw.mods.fml.common.network.FMLPacket.Type; public class NetworkRegistry { @@ -50,6 +55,8 @@ public class NetworkRegistry * A linked set of registered connection handlers */ private Set connectionHandlers = Sets.newLinkedHashSet(); + private Map serverGuiHandlers = Maps.newHashMap(); + private Map clientGuiHandlers = Maps.newHashMap(); public static NetworkRegistry instance() { @@ -139,7 +146,7 @@ public class NetworkRegistry } } - void connectionOpened(NetHandler netClientHandler, IntegratedServer server, NetworkManager networkManager) + void connectionOpened(NetHandler netClientHandler, MinecraftServer server, NetworkManager networkManager) { for (IConnectionHandler handler : connectionHandlers) { @@ -226,4 +233,31 @@ public class NetworkRegistry List channels = Lists.newArrayList(Splitter.on('\0').split(request)); return channels; } + public void openRemoteGui(NetworkModHandler networkModHandler, EntityPlayerMP player, int modGuiId, World world, int x, int y, int z) + { + IGuiHandler handler = serverGuiHandlers.get(networkModHandler.getContainer()); + if (handler != null) + { + Container container = (Container)handler.getServerGuiElement(modGuiId, player, world, x, y, z); + if (container != null) + { + player.func_71117_bO(); + player.func_71128_l(); + int windowId = player.field_71139_cq; + Packet250CustomPayload pkt = new Packet250CustomPayload(); + pkt.field_73630_a = "FML"; + pkt.field_73629_c = FMLPacket.makePacket(Type.GUIOPEN, windowId, networkModHandler.getNetworkId(), modGuiId, x, y, z); + pkt.field_73628_b = pkt.field_73629_c.length; + player.field_71135_a.func_72567_b(pkt); + player.field_71070_bA = container; + player.field_71070_bA.field_75152_c = windowId; + player.field_71070_bA.func_75132_a(player); + } + } + } + public void openLocalGui(NetworkModHandler networkModHandler, EntityPlayer player, int modGuiId, World world, int x, int y, int z) + { + IGuiHandler handler = clientGuiHandlers.get(networkModHandler.getContainer()); + FMLCommonHandler.instance().showGuiScreen(handler.getClientGuiElement(modGuiId, player, world, x, y, z)); + } } diff --git a/common/cpw/mods/fml/common/network/OpenGuiPacket.java b/common/cpw/mods/fml/common/network/OpenGuiPacket.java new file mode 100644 index 0000000000..e7511a7c9a --- /dev/null +++ b/common/cpw/mods/fml/common/network/OpenGuiPacket.java @@ -0,0 +1,61 @@ +package cpw.mods.fml.common.network; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; + +import cpw.mods.fml.common.FMLCommonHandler; + +import net.minecraft.src.EntityPlayer; +import net.minecraft.src.NetHandler; +import net.minecraft.src.NetworkManager; + +public class OpenGuiPacket extends FMLPacket +{ + private int windowId; + private int networkId; + private int modGuiId; + private int x; + private int y; + private int z; + + public OpenGuiPacket() + { + super(Type.GUIOPEN); + } + + @Override + public byte[] generatePacket(Object... data) + { + ByteArrayDataOutput dat = ByteStreams.newDataOutput(); + dat.writeInt((Integer) data[0]); // windowId + dat.writeInt((Integer) data[1]); // networkId + dat.writeInt((Integer) data[2]); // modGuiId + dat.writeInt((Integer) data[3]); // x + dat.writeInt((Integer) data[4]); // y + dat.writeInt((Integer) data[5]); // z + return dat.toByteArray(); + } + + @Override + public FMLPacket consumePacket(byte[] data) + { + ByteArrayDataInput dat = ByteStreams.newDataInput(data); + windowId = dat.readInt(); + networkId = dat.readInt(); + modGuiId = dat.readInt(); + x = dat.readInt(); + y = dat.readInt(); + z = dat.readInt(); + return this; + } + + @Override + public void execute(NetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName) + { + EntityPlayer player = netHandler.getPlayer(); + player.openGui(networkId, modGuiId, player.field_70170_p, x, y, z); + player.field_71070_bA.field_75152_c = windowId; + } + +} diff --git a/common/fml_at.cfg b/common/fml_at.cfg index 1057cb27d3..89a157038a 100644 --- a/common/fml_at.cfg +++ b/common/fml_at.cfg @@ -179,4 +179,8 @@ public ir.a public gw.a(Lgw;Z)Z # NetLoginHandler userName field public gw.h +# EntityPlayerMP getNextWindowId +public gt.bO()V +public gt.cq + diff --git a/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch b/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch new file mode 100644 index 0000000000..a4e1a11989 --- /dev/null +++ b/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch @@ -0,0 +1,22 @@ +--- ../src-base/minecraft/net/minecraft/src/EntityPlayer.java ++++ ../src-work/minecraft/net/minecraft/src/EntityPlayer.java +@@ -2,6 +2,8 @@ + + import java.util.Iterator; + import java.util.List; ++ ++import cpw.mods.fml.common.network.FMLNetworkHandler; + + public abstract class EntityPlayer extends EntityLiving implements ICommandSender + { +@@ -1593,4 +1595,10 @@ + { + return this.field_71078_a; + } ++ ++ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z) ++ { ++ FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z); ++ } ++ + } diff --git a/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch b/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch new file mode 100644 index 0000000000..e061a9f510 --- /dev/null +++ b/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch @@ -0,0 +1,21 @@ +--- ../src-base/minecraft_server/net/minecraft/src/EntityPlayer.java ++++ ../src-work/minecraft_server/net/minecraft/src/EntityPlayer.java +@@ -2,6 +2,8 @@ + + import java.util.Iterator; + import java.util.List; ++ ++import cpw.mods.fml.common.network.FMLNetworkHandler; + + public abstract class EntityPlayer extends EntityLiving implements ICommandSender + { +@@ -1480,4 +1482,9 @@ + { + return this.field_71078_a; + } ++ ++ public void openGui(Object mod, int modGuiId, World world, int x, int y, int z) ++ { ++ FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z); ++ } + } diff --git a/server/cpw/mods/fml/server/FMLServerHandler.java b/server/cpw/mods/fml/server/FMLServerHandler.java index 3151fc245a..f782ed0b59 100644 --- a/server/cpw/mods/fml/server/FMLServerHandler.java +++ b/server/cpw/mods/fml/server/FMLServerHandler.java @@ -167,4 +167,10 @@ public class FMLServerHandler implements IFMLSidedHandler { return Side.SERVER; } + + @Override + public void showGuiScreen(Object clientGuiElement) + { + + } }