Working GUIs and Containers

This commit is contained in:
Christian Weeks 2012-01-27 19:07:39 -05:00
parent b9dda8a2c1
commit cf605508ab
11 changed files with 297 additions and 4 deletions

View File

@ -0,0 +1,77 @@
package cpw.mods.ironchest.client;
import java.lang.reflect.InvocationTargetException;
import org.lwjgl.opengl.GL11;
import cpw.mods.ironchest.ContainerDiamondChest;
import cpw.mods.ironchest.ContainerGoldChest;
import cpw.mods.ironchest.IronChestContainer;
import cpw.mods.ironchest.IronChestType;
import cpw.mods.ironchest.TileEntityIronChest;
import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.GuiContainer;
import net.minecraft.src.IInventory;
import net.minecraft.src.ModLoader;
public class GUIChest extends GuiContainer {
public enum GUI {
GOLD(ContainerGoldChest.class,184,256,"/ic2/sprites/goldcontainer.png",IronChestType.GOLD),
DIAMOND(ContainerDiamondChest.class,238,256,"/ic2/sprites/diamondcontainer.png",IronChestType.DIAMOND);
private Class<? extends IronChestContainer> clazz;
private int xSize;
private int ySize;
private String guiTexture;
private IronChestType mainType;
private GUI(Class<? extends IronChestContainer> clazz, int xSize, int ySize, String guiTexture, IronChestType mainType) {
this.clazz=clazz;
this.xSize=xSize;
this.ySize=ySize;
this.guiTexture=guiTexture;
this.mainType=mainType;
}
protected Container makeContainer(IInventory player, IInventory chest) {
try {
return clazz.getConstructor(IInventory.class,IInventory.class).newInstance(player,chest);
} catch (Exception e) {
// unpossible
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void showGUI(TileEntityIronChest te, EntityPlayer player) {
for (GUI gui : values()) {
if (te.getType()==gui.mainType) {
ModLoader.OpenGUI(player, new GUIChest(gui,player.inventory,te));
return;
}
}
player.displayGUIChest(te);
}
}
private GUI type;
private GUIChest(GUI type, IInventory player, IInventory chest) {
super(type.makeContainer(player,chest));
this.type=type;
this.xSize=type.xSize;
this.ySize=type.ySize;
this.allowUserInput=false;
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
int tex = mc.renderEngine.getTexture(type.guiTexture);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(tex);
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}
}

View File

@ -5,6 +5,8 @@ import java.io.File;
import cpw.mods.ironchest.BlockIronChest; import cpw.mods.ironchest.BlockIronChest;
import cpw.mods.ironchest.IronChestType; import cpw.mods.ironchest.IronChestType;
import cpw.mods.ironchest.ItemIronChest; import cpw.mods.ironchest.ItemIronChest;
import cpw.mods.ironchest.TileEntityIronChest;
import cpw.mods.ironchest.client.GUIChest;
import cpw.mods.ironchest.client.IronChestRenderHelper; import cpw.mods.ironchest.client.IronChestRenderHelper;
import cpw.mods.ironchest.client.TileEntityIronChestRenderer; import cpw.mods.ironchest.client.TileEntityIronChestRenderer;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -34,6 +36,7 @@ public class mod_IronChest extends BaseModMp {
compatibilityMode = Boolean.parseBoolean(cfg.getOrCreateBooleanProperty("compatibilityMode", Configuration.GENERAL_PROPERTY, compatibilityMode = Boolean.parseBoolean(cfg.getOrCreateBooleanProperty("compatibilityMode", Configuration.GENERAL_PROPERTY,
defaultCompatibility).value); defaultCompatibility).value);
ironChestBlock = new BlockIronChest(Integer.parseInt(cfg.getOrCreateBlockIdProperty("blockVeryLargeChest", 181).value)); ironChestBlock = new BlockIronChest(Integer.parseInt(cfg.getOrCreateBlockIdProperty("blockVeryLargeChest", 181).value));
IronChestType.initGUIs(cfg);
} catch (Exception e) { } catch (Exception e) {
ModLoader.getLogger().severe("IronChest was unable to load it's configuration successfully"); ModLoader.getLogger().severe("IronChest was unable to load it's configuration successfully");
e.printStackTrace(System.err); e.printStackTrace(System.err);
@ -51,4 +54,8 @@ public class mod_IronChest extends BaseModMp {
MinecraftForgeClient.preloadTexture("ic2/sprites/ironchest_block_tex.png"); MinecraftForgeClient.preloadTexture("ic2/sprites/ironchest_block_tex.png");
} }
public static void openGUI(EntityPlayer player, TileEntityIronChest te) {
GUIChest.GUI.showGUI(te, player);
}
} }

View File

@ -2,11 +2,13 @@ package cpw.mods.ironchest;
import net.minecraft.src.BlockContainer; import net.minecraft.src.BlockContainer;
import net.minecraft.src.EntityLiving; import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IBlockAccess; import net.minecraft.src.IBlockAccess;
import net.minecraft.src.Material; import net.minecraft.src.Material;
import net.minecraft.src.MathHelper; import net.minecraft.src.MathHelper;
import net.minecraft.src.TileEntity; import net.minecraft.src.TileEntity;
import net.minecraft.src.World; import net.minecraft.src.World;
import net.minecraft.src.mod_IronChest;
import net.minecraft.src.forge.ITextureProvider; import net.minecraft.src.forge.ITextureProvider;
public class BlockIronChest extends BlockContainer implements ITextureProvider { public class BlockIronChest extends BlockContainer implements ITextureProvider {
@ -73,6 +75,27 @@ public class BlockIronChest extends BlockContainer implements ITextureProvider {
return typ.getTextureRow()*16; return typ.getTextureRow()*16;
} }
} }
@Override
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) {
TileEntity te = world.getBlockTileEntity(i, j, k);
if(te == null || !(te instanceof TileEntityIronChest))
{
return true;
}
if(world.isBlockSolidOnSide(i, j + 1, k, 0))
{
return true;
}
if (world.isRemote) {
return true;
}
mod_IronChest.openGUI(player, (TileEntityIronChest)te);
return true;
}
@Override @Override
public void onBlockAdded(World world, int i, int j, int k) { public void onBlockAdded(World world, int i, int j, int k) {

View File

@ -0,0 +1,46 @@
package cpw.mods.ironchest;
import net.minecraft.src.IInventory;
import net.minecraft.src.Slot;
public class ContainerDiamondChest extends IronChestContainer {
public ContainerDiamondChest(IInventory playerInventory, IInventory chestInventory) {
super(playerInventory, chestInventory);
}
private static final int NUM_ROWS = 9;
private static final int ROW_LENGTH = 12;
@Override
protected void layoutContainer(IInventory playerInventory, IInventory chestInventory) {
for(int i = 0; i < NUM_ROWS; i++)
{
for(int l = 0; l < ROW_LENGTH; l++)
{
addSlot(new Slot(chestInventory, l + i * ROW_LENGTH, 12 + l * 18, 8 + i * 18));
}
}
for(int j = 0; j < 3; j++)
{
for(int i1 = 0; i1 < 9; i1++)
{
addSlot(new Slot(playerInventory, i1 + j * 9 + 9, 39 + i1 * 18, 174 + j * 18));
}
}
for(int k = 0; k < 9; k++)
{
addSlot(new Slot(playerInventory, k, 39 + k * 18, 232));
}
}
@Override
protected int getRowLength() {
return ROW_LENGTH;
}
}

View File

@ -0,0 +1,50 @@
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode
package cpw.mods.ironchest;
import net.minecraft.src.IInventory;
import net.minecraft.src.Slot;
public class ContainerGoldChest extends IronChestContainer
{
private static final int NUM_ROWS = 9;
private static final int ROW_LENGTH = 9;
public ContainerGoldChest(IInventory playerInventory, IInventory chestInventory) {
super(playerInventory,chestInventory);
}
@Override
protected int getRowLength() {
return ROW_LENGTH;
}
protected void layoutContainer(IInventory playerInventory, IInventory chestInventory) {
for(int i = 0; i < NUM_ROWS; i++)
{
for(int l = 0; l < NUM_ROWS; l++)
{
addSlot(new Slot(chestInventory, l + i * ROW_LENGTH, 12 + l * 18, 8 + i * 18));
}
}
for(int j = 0; j < 3; j++)
{
for(int i1 = 0; i1 < 9; i1++)
{
addSlot(new Slot(playerInventory, i1 + j * 9 + 9, 12 + i1 * 18, 174 + j * 18));
}
}
for(int k = 0; k < 9; k++)
{
addSlot(new Slot(playerInventory, k, 12 + k * 18, 232));
}
}
}

View File

@ -0,0 +1,64 @@
package cpw.mods.ironchest;
import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Slot;
public abstract class IronChestContainer extends Container {
public IronChestContainer(IInventory playerInventory, IInventory chestInventory) {
numRows = chestInventory.getSizeInventory() / getRowLength();
chest = chestInventory;
chestInventory.openChest();
layoutContainer(playerInventory, chestInventory);
}
protected abstract void layoutContainer(IInventory playerInventory, IInventory chestInventory);
protected abstract int getRowLength();
public boolean canInteractWith(EntityPlayer player)
{
return chest.isUseableByPlayer(player);
}
public ItemStack transferStackInSlot(int i)
{
ItemStack itemstack = null;
Slot slot = (Slot)inventorySlots.get(i);
if(slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if(i < numRows * getRowLength())
{
if(!mergeItemStack(itemstack1, numRows * getRowLength(), inventorySlots.size(), true))
{
return null;
}
} else
if(!mergeItemStack(itemstack1, 0, numRows * getRowLength(), false))
{
return null;
}
if(itemstack1.stackSize == 0)
{
slot.putStack(null);
} else
{
slot.onSlotChanged();
}
}
return itemstack;
}
public void onCraftGuiClosed(EntityPlayer entityplayer)
{
super.onCraftGuiClosed(entityplayer);
chest.closeChest();
}
private IInventory chest;
private int numRows;
}

View File

@ -7,22 +7,26 @@ import net.minecraft.src.ModLoader;
import net.minecraft.src.TileEntity; import net.minecraft.src.TileEntity;
import net.minecraft.src.TileEntitySpecialRenderer; import net.minecraft.src.TileEntitySpecialRenderer;
import net.minecraft.src.mod_IronChest; import net.minecraft.src.mod_IronChest;
import net.minecraft.src.forge.Configuration;
public enum IronChestType { public enum IronChestType {
IRON(54, "Iron Chest", "ironchest.png", 0, Item.ingotIron, TileEntityIronChest.class, "mmmmPmmmm"), IRON(54, "Iron Chest", null, "ironchest.png", 0, Item.ingotIron, TileEntityIronChest.class, "mmmmPmmmm"),
GOLD(81, "Gold Chest", "goldchest.png", 1, Item.ingotGold, TileEntityGoldChest.class, "mmmmPmmmm"); GOLD(81, "Gold Chest", "guiGoldChest", "goldchest.png", 1, Item.ingotGold, TileEntityGoldChest.class, "mmmmPmmmm"),
// DIAMOND(108,"DiamondChest","diamondchest.png",2); DIAMOND(108,"Diamond Chest","guiDiamondChest", "diamondchest.png", 2, Item.diamond, TileEntityDiamondChest.class, "mmmmPmmmm");
int size; int size;
String friendlyName; String friendlyName;
private String modelTexture; private String modelTexture;
private String guiName;
private int textureRow; private int textureRow;
private Class<? extends TileEntityIronChest> clazz; private Class<? extends TileEntityIronChest> clazz;
private Item mat; private Item mat;
private String[] recipes; private String[] recipes;
private int guiId;
IronChestType(int size, String friendlyName, String modelTexture, int textureRow, Item mat, Class<? extends TileEntityIronChest> clazz, String... recipes) { IronChestType(int size, String friendlyName, String guiName, String modelTexture, int textureRow, Item mat, Class<? extends TileEntityIronChest> clazz, String... recipes) {
this.size = size; this.size = size;
this.friendlyName = friendlyName; this.friendlyName = friendlyName;
this.guiName=guiName;
this.modelTexture = "/ic2/sprites/"+modelTexture; this.modelTexture = "/ic2/sprites/"+modelTexture;
this.textureRow = textureRow; this.textureRow = textureRow;
this.clazz = clazz; this.clazz = clazz;
@ -99,4 +103,19 @@ public enum IronChestType {
private static void addRecipe(ItemStack is, Object... parts) { private static void addRecipe(ItemStack is, Object... parts) {
ModLoader.AddRecipe(is, parts); ModLoader.AddRecipe(is, parts);
} }
public int getGUI() {
return guiId;
}
public static void initGUIs(Configuration cfg) {
int defGUI=51;
for (IronChestType typ : values()) {
if (typ.guiName!=null) {
typ.guiId=Integer.parseInt(cfg.getOrCreateIntProperty(typ.guiName, Configuration.GENERAL_PROPERTY, defGUI++).value);
} else {
typ.guiId=-1;
}
}
}
} }

View File

@ -0,0 +1,7 @@
package cpw.mods.ironchest;
public class TileEntityDiamondChest extends TileEntityIronChest {
public TileEntityDiamondChest() {
super(IronChestType.DIAMOND);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB