Implemented a generic EntityPlayer.openGui system, and the network backend for it to work on server and client.

This commit is contained in:
LexManos 2012-02-20 23:56:01 -08:00
parent a6f93ed528
commit 54ec1567b2
10 changed files with 271 additions and 6 deletions

View File

@ -0,0 +1,21 @@
package net.minecraft.src.forge;
import net.minecraft.src.EntityPlayerSP;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.World;
public interface IGuiHandler
{
/**
* Returns a GuiScreen to be displayed to the user. This is client side
*
* @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 to be displayed to the user, null if none.
*/
public GuiScreen getGuiScreen(int ID, EntityPlayerSP player, World world, int X, int Y, int Z);
}

View File

@ -53,6 +53,12 @@ public class PacketHandlerClient implements IPacketHandler
pkt.readData(data);
onModIDs((PacketModIDs)pkt);
break;
case ForgePacket.OPEN_GUI:
pkt = new PacketOpenGUI();
pkt.readData(data);
onOpenGui((PacketOpenGUI)pkt);
break;
}
}
catch(IOException e)
@ -192,4 +198,20 @@ public class PacketHandlerClient implements IPacketHandler
}
//TODO: Display error/confirmation screen
}
/**
* Handles opening the Gui for the player.
*
* @param pkt The Open Gui Packet
*/
private void onOpenGui(PacketOpenGUI pkt)
{
NetworkMod mod = MinecraftForge.getModByID(pkt.ModID);
if (mod != null)
{
EntityPlayerSP player = (EntityPlayerSP)ModLoader.getMinecraftInstance().thePlayer;
player.openGui(mod, pkt.GuiID, player.worldObj, pkt.X, pkt.Y, pkt.Z);
player.craftingInventory.windowId = pkt.WindowID;
}
}
}

View File

@ -368,6 +368,7 @@ public class ForgeHooks {
}
public static Hashtable<Integer, NetworkMod> networkMods = new Hashtable<Integer, NetworkMod>();
public static Hashtable<BaseMod, IGuiHandler> guiHandlers = new Hashtable<BaseMod, IGuiHandler>();
public static final int majorVersion=0;
public static final int minorVersion=0;

View File

@ -906,6 +906,28 @@ public class MinecraftForge {
}
return ret.toArray(new NetworkMod[0]);
}
/**
* Sets the GuiHandler associated with a mod.
*
* @param mod The mod
* @param handler The Gui Handler
*/
public static void setGuiHandler(BaseMod mod, IGuiHandler handler)
{
ForgeHooks.guiHandlers.put(mod, handler);
}
/**
* Gets the GuiHandler associated with a mod
*
* @param mod The mod
* @return The handler, or null if none associated.
*/
public static IGuiHandler getGuiHandler(BaseMod mod)
{
return ForgeHooks.guiHandlers.get(mod);
}
static

View File

@ -0,0 +1,54 @@
package net.minecraft.src.forge.packets;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketOpenGUI extends ForgePacket
{
public int WindowID;
public int ModID;
public int GuiID;
public int X;
public int Y;
public int Z;
public PacketOpenGUI(){}
public PacketOpenGUI(int window, int mod, int id, int x, int y, int z)
{
WindowID = window;
ModID = mod;
GuiID = id;
X = x;
Y = y;
Z = z;
}
@Override
public void writeData(DataOutputStream data) throws IOException
{
data.writeInt(WindowID);
data.writeInt(ModID);
data.writeInt(GuiID);
data.writeInt(X);
data.writeInt(Y);
data.writeInt(Z);
}
@Override
public void readData(DataInputStream data) throws IOException
{
WindowID = data.readInt();
ModID = data.readInt();
GuiID = data.readInt();
X = data.readInt();
Y = data.readInt();
Z = data.readInt();
}
@Override
public int getID()
{
return ForgePacket.OPEN_GUI;
}
}

View File

@ -0,0 +1,21 @@
package net.minecraft.src.forge;
import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayerMP;
import net.minecraft.src.World;
public interface IGuiHandler
{
/**
* Returns a Container to be displayed to the user. This is server side
*
* @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 to be displayed to the user, null if none.
*/
public Container getGuiContainer(int ID, EntityPlayerMP player, World world, int X, int Y, int Z);
}

View File

@ -140,3 +140,20 @@
if (!worldObj.isRemote)
{
if (isPlayerSleeping() || !isEntityAlive())
@@ -1473,4 +1545,16 @@
experience = entityplayer.experience;
score = entityplayer.score;
}
+
+ /**
+ * Opens a Gui for the player.
+ *
+ * @param mod The mod associated with the gui
+ * @param ID The ID number for the Gui
+ * @param world The World
+ * @param X X Position
+ * @param Y Y Position
+ * @param Z Z Position
+ */
+ public void openGui(BaseMod mod, int ID, World world, int X, int Y, int Z){}
}

View File

@ -0,0 +1,40 @@
--- ../src_base/minecraft/net/minecraft/src/EntityPlayerSP.java 0000-00-00 00:00:00.000000000 -0000
+++ ../src_work/minecraft/net/minecraft/src/EntityPlayerSP.java 0000-00-00 00:00:00.000000000 -0000
@@ -2,6 +2,8 @@
import java.util.Random;
import net.minecraft.client.Minecraft;
+import net.minecraft.src.forge.IGuiHandler;
+import net.minecraft.src.forge.MinecraftForge;
public class EntityPlayerSP extends EntityPlayer
{
@@ -474,4 +476,28 @@
experienceTotal = i;
experienceLevel = j;
}
+
+ /**
+ * Opens a Gui for the player.
+ *
+ * @param mod The mod associated with the gui
+ * @param ID The ID number for the Gui
+ * @param world The World
+ * @param X X Position
+ * @param Y Y Position
+ * @param Z Z Position
+ */
+ @Override
+ public void openGui(BaseMod mod, int ID, World world, int X, int Y, int Z)
+ {
+ IGuiHandler handler = MinecraftForge.getGuiHandler(mod);
+ if (handler != null)
+ {
+ GuiScreen screen = handler.getGuiScreen(ID, this, world, X, Y, Z);
+ if (screen != null)
+ {
+ mc.displayGuiScreen(screen);
+ }
+ }
+ }
}

View File

@ -150,3 +150,20 @@
if (!worldObj.isRemote)
{
if (isPlayerSleeping() || !isEntityAlive())
@@ -1353,4 +1428,16 @@
experience = entityplayer.experience;
score = entityplayer.score;
}
+
+ /**
+ * Opens a Gui for the player.
+ *
+ * @param mod The mod associated with the gui
+ * @param ID The ID number for the Gui
+ * @param world The World
+ * @param X X Position
+ * @param Y Y Position
+ * @param Z Z Position
+ */
+ public void openGui(BaseMod mod, int ID, World world, int X, int Y, int Z){}
}

View File

@ -1,6 +1,17 @@
--- ../src_base/minecraft_server/net/minecraft/src/EntityPlayerMP.java 0000-00-00 00:00:00.000000000 -0000
+++ ../src_work/minecraft_server/net/minecraft/src/EntityPlayerMP.java 0000-00-00 00:00:00.000000000 -0000
@@ -405,6 +405,7 @@
@@ -2,6 +2,10 @@
import java.util.*;
import net.minecraft.server.MinecraftServer;
+import net.minecraft.src.forge.IGuiHandler;
+import net.minecraft.src.forge.MinecraftForge;
+import net.minecraft.src.forge.NetworkMod;
+import net.minecraft.src.forge.packets.PacketOpenGUI;
public class EntityPlayerMP extends EntityPlayer
implements ICrafting
@@ -405,6 +409,7 @@
public void displayWorkbenchGUI(int i, int j, int k)
{
getNextWidowId();
@ -8,7 +19,7 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 1, "Crafting", 9));
craftingInventory = new ContainerWorkbench(inventory, worldObj, i, j, k);
craftingInventory.windowId = currentWindowId;
@@ -414,6 +415,7 @@
@@ -414,6 +419,7 @@
public void displayGUIEnchantment(int i, int j, int k)
{
getNextWidowId();
@ -16,7 +27,7 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 4, "Enchanting", 9));
craftingInventory = new ContainerEnchantment(inventory, worldObj, i, j, k);
craftingInventory.windowId = currentWindowId;
@@ -423,6 +425,7 @@
@@ -423,6 +429,7 @@
public void displayGUIChest(IInventory iinventory)
{
getNextWidowId();
@ -24,7 +35,7 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 0, iinventory.getInvName(), iinventory.getSizeInventory()));
craftingInventory = new ContainerChest(inventory, iinventory);
craftingInventory.windowId = currentWindowId;
@@ -432,6 +435,7 @@
@@ -432,6 +439,7 @@
public void displayGUIFurnace(TileEntityFurnace tileentityfurnace)
{
getNextWidowId();
@ -32,7 +43,7 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 2, tileentityfurnace.getInvName(), tileentityfurnace.getSizeInventory()));
craftingInventory = new ContainerFurnace(inventory, tileentityfurnace);
craftingInventory.windowId = currentWindowId;
@@ -441,6 +445,7 @@
@@ -441,6 +449,7 @@
public void displayGUIDispenser(TileEntityDispenser tileentitydispenser)
{
getNextWidowId();
@ -40,7 +51,7 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 3, tileentitydispenser.getInvName(), tileentitydispenser.getSizeInventory()));
craftingInventory = new ContainerDispenser(inventory, tileentitydispenser);
craftingInventory.windowId = currentWindowId;
@@ -450,6 +455,7 @@
@@ -450,6 +459,7 @@
public void displayGUIBrewingStand(TileEntityBrewingStand tileentitybrewingstand)
{
getNextWidowId();
@ -48,3 +59,42 @@
playerNetServerHandler.sendPacket(new Packet100OpenWindow(currentWindowId, 5, tileentitybrewingstand.getInvName(), tileentitybrewingstand.getSizeInventory()));
craftingInventory = new ContainerBrewingStand(inventory, tileentitybrewingstand);
craftingInventory.windowId = currentWindowId;
@@ -613,4 +623,38 @@
EntityTracker entitytracker = mcServer.getEntityTracker(dimension);
entitytracker.sendPacketToTrackedPlayersAndTrackedEntity(this, new Packet18Animation(entity, 7));
}
+
+ /**
+ * Opens a Gui for the player.
+ *
+ * @param mod The mod associated with the gui
+ * @param ID The ID number for the Gui
+ * @param world The World
+ * @param X X Position
+ * @param Y Y Position
+ * @param Z Z Position
+ */
+ @Override
+ public void openGui(BaseMod mod, int ID, World world, int X, int Y, int Z)
+ {
+ if (!(mod instanceof NetworkMod))
+ {
+ return;
+ }
+ IGuiHandler handler = MinecraftForge.getGuiHandler(mod);
+ if (handler != null)
+ {
+ Container container = handler.getGuiContainer(ID, this, world, X, Y, Z);
+ if (container != null)
+ {
+ getNextWidowId();
+ closeCraftingGui();
+ PacketOpenGUI pkt = new PacketOpenGUI(currentWindowId, MinecraftForge.getModID((NetworkMod)mod), ID, X, Y, Z);
+ playerNetServerHandler.sendPacket(pkt.getPacket());
+ craftingInventory = container;
+ craftingInventory.windowId = currentWindowId;
+ craftingInventory.onCraftGuiOpened(this);
+ }
+ }
+ }
}