Gui Handler ported from MC forge

This commit is contained in:
Christian 2012-08-08 00:31:24 -04:00
parent 8998d4c5e0
commit 77f4cc5bea
14 changed files with 239 additions and 15 deletions

View file

@ -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);
}
}

View file

@ -337,4 +337,9 @@ public class FMLCommonHandler
//TODO
return null;
}
public void showGuiScreen(Object clientGuiElement)
{
sidedDelegate.showGuiScreen(clientGuiElement);
}
}

View file

@ -20,4 +20,6 @@ public interface IFMLSidedHandler
Side getSide();
void haltGame(String message, Throwable exception);
void showGuiScreen(Object clientGuiElement);
}

View file

@ -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
}
}

View file

@ -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);
}
}
}

View file

@ -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<? extends FMLPacket> packetType;

View file

@ -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

View file

@ -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);
}

View file

@ -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<IConnectionHandler> connectionHandlers = Sets.newLinkedHashSet();
private Map<ModContainer, IGuiHandler> serverGuiHandlers = Maps.newHashMap();
private Map<ModContainer, IGuiHandler> 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<String> 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));
}
}

View file

@ -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;
}
}

View file

@ -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

View file

@ -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);
+ }
+
}

View file

@ -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);
+ }
}

View file

@ -167,4 +167,10 @@ public class FMLServerHandler implements IFMLSidedHandler
{
return Side.SERVER;
}
@Override
public void showGuiScreen(Object clientGuiElement)
{
}
}