Gui Handler
A gui handler...handles your guis. In other tutorials I will explain how to make a gui, but this page is specifically for how to open a gui when called. This is what confused me the most about guis so I'll try to make it as easy as possible for you to understand.
First, in your main method, create a reference to the GuiHandler you will make.
public GuiHandler guiHandler = new GuiHandler();
Next, in your init method, add this line:
NetworkRegistry.instance().registerGuiHandler(instance, guiHandler);
Then create your GuiHandler class.
package tutorial.handler;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
import tutorial.inventory.ContainerForge;
import tutorial.inventory.ContainerGrindstone;
import tutorial.inventory.ContainerSmelter;
import tutorial.inventory.GuiForge;
import tutorial.inventory.GuiGrindstone;
import tutorial.inventory.GuiSmelter;
import tutorial.tileentity.TileEntityGrindstone;
import tutorial.tileentity.TileEntitySmelter;
import cpw.mods.fml.common.network.IGuiHandler;
public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if(tileEntity != null)
{
switch(ID)
{
case 0: return new ContainerSmelter(player.inventory, (TileEntitySmelter)tileEntity);
case 1: return new ContainerChest(player.inventory, (TileEntityChest)tileEntity);
case 2: return new ContainerGrindstone(player.inventory, (TileEntityGrindstone)tileEntity);
}
}
if (ID == 3)
{
return new ContainerForge(player.inventory, world, x, y, z);
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null)
{
switch(ID)
{
case 0: return new GuiSmelter(player.inventory, (TileEntitySmelter)tileEntity);
case 1: return new GuiChest(player.inventory, (TileEntityChest)tileEntity);
case 2: return new GuiGrindstone(player.inventory, (TileEntityGrindstone)tileEntity);
}
}
if (ID == 3)
{
return new GuiForge(player.inventory, world, x, y, z);
}
return null;
}
}
As you can see here, there are many things being called. I've added a few just to show you how it works. First, you should know how a gui is called.
player.openGui(TutorialMain.instance, 3, world, (int) player.posX, (int) player.posY, (int) player.posZ);
The first argument is your mod. The second is a number relating to which gui you want opened. Look at the code above. See the numbers before each statement? And then the next few args won't change.
In the getServerGuiElement, this is where you place your containers. The getClientGuiElement is where you place your guis.
Notice how my GuiForge and ContainerForge are outside the switch block. This is because my Forge block doesn't have a TileEntity. If you want to make your life a little easier, you can change your method to only if's and remove
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null)
{
}
That pretty much covers a GuiHandler. Look out for some Gui related tutorials.
First, in your main method, create a reference to the GuiHandler you will make.
public GuiHandler guiHandler = new GuiHandler();
Next, in your init method, add this line:
NetworkRegistry.instance().registerGuiHandler(instance, guiHandler);
Then create your GuiHandler class.
package tutorial.handler;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
import tutorial.inventory.ContainerForge;
import tutorial.inventory.ContainerGrindstone;
import tutorial.inventory.ContainerSmelter;
import tutorial.inventory.GuiForge;
import tutorial.inventory.GuiGrindstone;
import tutorial.inventory.GuiSmelter;
import tutorial.tileentity.TileEntityGrindstone;
import tutorial.tileentity.TileEntitySmelter;
import cpw.mods.fml.common.network.IGuiHandler;
public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if(tileEntity != null)
{
switch(ID)
{
case 0: return new ContainerSmelter(player.inventory, (TileEntitySmelter)tileEntity);
case 1: return new ContainerChest(player.inventory, (TileEntityChest)tileEntity);
case 2: return new ContainerGrindstone(player.inventory, (TileEntityGrindstone)tileEntity);
}
}
if (ID == 3)
{
return new ContainerForge(player.inventory, world, x, y, z);
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null)
{
switch(ID)
{
case 0: return new GuiSmelter(player.inventory, (TileEntitySmelter)tileEntity);
case 1: return new GuiChest(player.inventory, (TileEntityChest)tileEntity);
case 2: return new GuiGrindstone(player.inventory, (TileEntityGrindstone)tileEntity);
}
}
if (ID == 3)
{
return new GuiForge(player.inventory, world, x, y, z);
}
return null;
}
}
As you can see here, there are many things being called. I've added a few just to show you how it works. First, you should know how a gui is called.
player.openGui(TutorialMain.instance, 3, world, (int) player.posX, (int) player.posY, (int) player.posZ);
The first argument is your mod. The second is a number relating to which gui you want opened. Look at the code above. See the numbers before each statement? And then the next few args won't change.
In the getServerGuiElement, this is where you place your containers. The getClientGuiElement is where you place your guis.
Notice how my GuiForge and ContainerForge are outside the switch block. This is because my Forge block doesn't have a TileEntity. If you want to make your life a little easier, you can change your method to only if's and remove
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity != null)
{
}
That pretty much covers a GuiHandler. Look out for some Gui related tutorials.