Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

I can't get The GuiHandler working


ItsAMysteriousYT
 Share

Recommended Posts

I want to create a Drawer in which you can store items, but everything i do, does not remove the two problems i have:

1: I can't click on the last six slots in the playerinventory otherwise the game crashes

2: I can only store items by shift-clicking, otherwise they disappear.

 

Here are the classes:

Mainclass:

 

 

package de.ItsAMysterious.mods.reallifemod;

 

import java.io.File;

import java.util.logging.Logger;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.settings.KeyBinding;

import net.minecraft.item.Item;

import net.minecraft.item.Item.ToolMaterial;

import net.minecraftforge.common.MinecraftForge;

import net.minecraftforge.common.config.Configuration;

import net.minecraftforge.common.util.EnumHelper;

 

import org.lwjgl.input.Keyboard;

 

import cpw.mods.fml.client.FMLClientHandler;

import cpw.mods.fml.client.event.ConfigChangedEvent;

import cpw.mods.fml.client.registry.ClientRegistry;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.Mod;

import cpw.mods.fml.common.Mod.EventHandler;

import cpw.mods.fml.common.SidedProxy;

import cpw.mods.fml.common.event.FMLInitializationEvent;

import cpw.mods.fml.common.event.FMLPostInitializationEvent;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

import cpw.mods.fml.common.event.FMLServerStartingEvent;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.network.NetworkRegistry;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import de.ItsAMysterious.mods.reallifemod.api.util.TLMCustomCreativeTabs;

import de.ItsAMysterious.mods.reallifemod.config.RealLifeModConfig;

import de.ItsAMysterious.mods.reallifemod.core.blocks.furniture.BlockDrawer;

import de.ItsAMysterious.mods.reallifemod.core.gui.reallifemodHUD;

import de.ItsAMysterious.mods.reallifemod.core.handlers.ForgeEventHandler;

import de.ItsAMysterious.mods.reallifemod.core.handlers.RLMEventHandler_Common;

import de.ItsAMysterious.mods.reallifemod.core.handlers.RLMTerrainHandler;

import de.ItsAMysterious.mods.reallifemod.core.handlers.TLMKeysHandler;

import de.ItsAMysterious.mods.reallifemod.init.Reference;

import de.ItsAMysterious.mods.reallifemod.server.RLMCommands;

import de.ItsAMysterious.mods.reallifemod.server.ServerProxy;

import de.ItsAMysterious.mods.reallifemod.server.TutorialBotCommand;

import de.ItsAMysterious.mods.reallifemod.core.gui.GuiHandler;

 

@Mod(modid = Reference.ID, name = Reference.NAME, version = Reference.VERSION, guiFactory = "de.ItsAMysterious.mods.reallifemod.init.GuiFactory")

public class RealLifeMod {

public static final String ID = "reallifemod";

public static final String NAME = "Real Life Mod";

public static final String VERSION = "0.15";

 

@Mod.Instance(RealLifeMod.ID)

public static RealLifeMod instance;

 

@SidedProxy(clientSide = "de.ItsAMysterious.mods.reallifemod.client.ClientProxy", serverSide = "de.ItsAMysterious.mods.reallifemod.server.ServerProxy")

public static ServerProxy proxy;

public static Logger log = Logger.getLogger("Minecraft");

 

public static boolean enableMinecraftStyle = false;

 

public static KeyBinding character;

public static KeyBinding Phone;

public static KeyBinding Key_Horn;

public static ToolMaterial Krupp = EnumHelper.addToolMaterial("Krupp", 4,

3000, 4.0F, 12, 10);

 

public static final TLMCustomCreativeTabs TLMAgriculture = new TLMCustomCreativeTabs(

"reallifemod-agriculture");

public static final TLMCustomCreativeTabs rlmfood = new TLMCustomCreativeTabs(

"reallifemod-food") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.noodlesoup;

}

};

 

public static final TLMCustomCreativeTabs RLMTechnologie = new TLMCustomCreativeTabs(

"reallifemod-technology") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.circuitBrd;

}

};

public static final TLMCustomCreativeTabs Furniture = new TLMCustomCreativeTabs(

"reallifemod-furniture") {

@Override

public Item getTabIconItem() {

return Item.getItemFromBlock(RealLifeMod_Blocks.cupboard);

}

};

public static final TLMCustomCreativeTabs Outdoor = new TLMCustomCreativeTabs(

"reallifemod-outdoor") {

@Override

public Item getTabIconItem() {

return Item.getItemFromBlock(RealLifeMod_Blocks.oaktree);

}

};

public static final TLMCustomCreativeTabs medicine = new TLMCustomCreativeTabs(

"reallifemod-medicine") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.syringe;

}

};

public static final TLMCustomCreativeTabs TLMEverydaylife = new TLMCustomCreativeTabs(

"reallifemod-everydalife stuff") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.ID;

}

};

public static final TLMCustomCreativeTabs Streets = new TLMCustomCreativeTabs(

"reallifemod-roads") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.Lowry;

}

};

public static final TLMCustomCreativeTabs Work = new TLMCustomCreativeTabs(

"Weapons and Tools") {

@Override

public Item getTabIconItem() {

return RealLifeMod_Items.ak;

}

};

public static Configuration config;

public static File Dir;

public static File dir;

 

@EventHandler

public void preInit(FMLPreInitializationEvent event) {

config = new Configuration(event.getSuggestedConfigurationFile());

RealLifeModConfig.syncConfig();

proxy.registerTileEntities();

proxy.setIDs();

MinecraftForge.EVENT_BUS.register(new ForgeEventHandler());

MinecraftForge.EVENT_BUS.register(new ServerProxy());

MinecraftForge.EVENT_BUS.register(new reallifemodHUD(FMLClientHandler.instance().getClient()));

FMLCommonHandler.instance().bus().register(new RLMEventHandler_Common());

MinecraftForge.EVENT_BUS.register(new RLMTerrainHandler());

NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

 

}

 

@EventHandler

public void init(FMLInitializationEvent event) {

FMLCommonHandler.instance().bus().register(instance);

dir = Minecraft.getMinecraft().mcDataDir;

RealLifeMod.Dir = new File(dir, "reallifemod");

proxy.createFolders();

RealLifeMod_Blocks.defineBlocks();

RealLifeMod_Items.defineItems();

proxy.doStartUp();

character = new KeyBinding("Open chararcter setup", 38, "real life mod");

Phone = new KeyBinding("Open Phone", Keyboard.KEY_P, "real life mod");

Key_Horn = new KeyBinding("Horn", 23, "real life mod");

 

if (event.getSide() == Side.CLIENT) {

ClientRegistry.registerKeyBinding(RealLifeMod.character);

ClientRegistry.registerKeyBinding(RealLifeMod.Phone);

ClientRegistry.registerKeyBinding(RealLifeMod.Key_Horn);

}

 

MinecraftForge.EVENT_BUS.register(new TLMKeysHandler());

}

 

@EventHandler

public void PostInit(FMLPostInitializationEvent event) {

proxy.doPostInit();

config.load();

}

 

@SubscribeEvent

public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {

if (event.modID.equals(ID)) {

RealLifeModConfig.syncConfig();

}

}

 

@EventHandler

public void onServerStart(FMLServerStartingEvent event) {

event.registerServerCommand(new RLMCommands());

event.registerServerCommand(new TutorialBotCommand());

}

 

/*

* Wenn player spawn, ACHIEVEMENT GET! Downloaded Real Life Mod!

*/

 

public void createRequiredFolders(){

File mc=Minecraft.getMinecraft().mcDataDir;

}

}

 

 

 

 

GuiHandler:

 

 

package de.ItsAMysterious.mods.reallifemod.core.gui;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

import cpw.mods.fml.common.network.IGuiHandler;

import de.ItsAMysterious.mods.reallifemod.core.gui.containers.*;

import de.ItsAMysterious.mods.reallifemod.core.tiles.*;

 

public class GuiHandler implements IGuiHandler {

 

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world,

int x, int y, int z) {

TileEntity tileEntity = world.getTileEntity(x, y, z);

if (tileEntity instanceof FreezerTE) {

return new ContainerFreezer(player.inventory,

((FreezerTE) tileEntity));

}else

if (tileEntity instanceof DrawerTE) {

return

new ContainerDrawer(player.inventory,((DrawerTE) tileEntity));

} else {

return null;

}

 

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world,

int x, int y, int z) {

TileEntity tileEntity = world.getTileEntity(x, y, z);

if (tileEntity instanceof FreezerTE){return new ContainerFreezer(player.inventory,

((FreezerTE) tileEntity));

}

else

if (tileEntity instanceof SideboardTE){

return

new ContainerSideboard(player.inventory,((SideboardTE) tileEntity));}

else

if (tileEntity instanceof DrawerTE) {return

new ContainerDrawer(player.inventory,((DrawerTE) tileEntity));

} else {

return null;

}

}

 

}

 

 

 

 

TileEntity:

 

 

package de.ItsAMysterious.mods.reallifemod.core.tiles;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.inventory.ISidedInventory;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.network.NetworkManager;

import net.minecraft.network.Packet;

import net.minecraft.network.play.server.S35PacketUpdateTileEntity;

import net.minecraft.tileentity.TileEntity;

 

public class DrawerTE extends TileEntity implements ISidedInventory {

public static final int INV_SIZE = 15;

private ItemStack[] inventory;

private final String name = "Drawer Inventory";

private final String tagName = "DrawerInvTag";

 

public DrawerTE(){

inventory = new ItemStack[15];

}

 

public int getSizeInventory() {

return inventory.length;

}

 

public boolean receiveClientEvent(int par1, int par2) {

return super.receiveClientEvent(par1, par2);

}

 

public ItemStack getStackInSlot(int slot) {

return inventory[slot];

}

 

public ItemStack decrStackSize(int slot, int amount) {

ItemStack stack = getStackInSlot(slot);

if (stack != null) {

if (stack.stackSize > amount) {

stack = stack.splitStack(amount);

if (stack.stackSize == 0) {

setInventorySlotContents(slot, null);

}

} else {

setInventorySlotContents(slot, null);

}

 

this.onInventoryChanged();

}

return stack;

}

 

public ItemStack getStackInSlotOnClosing(int slot) {

ItemStack stack = getStackInSlot(slot);

if (stack != null)

setInventorySlotContents(slot, null);

return stack;

}

 

public void onInventoryChanged() {

for (int i = 0; i < inventory.length; ++i) {

if (this.getStackInSlot(i) != null

&& this.getStackInSlot(i).stackSize == 0)

this.setInventorySlotContents(i, null);

}

}

 

public void setInventorySlotContents(int slot, ItemStack stack) {

this.inventory[slot] = stack;

if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {

stack.stackSize = this.getInventoryStackLimit();

}

worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

markDirty();

}

 

public String getInventoryName() {

return "reallifemod.drawerinventory";

}

 

public boolean hasCustomInventoryName() {

return name.length() > 0;

}

 

public int getInventoryStackLimit() {

return 64;

}

 

public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {

return getWorldObj().getTileEntity(this.xCoord, this.yCoord,

this.zCoord) == this

&& p_70300_1_.getDistanceSq((double) this.xCoord + 0.5D,

(double) this.yCoord + 0.5D,

(double) this.zCoord + 0.5D) <= 64.0D;

 

}

public void openInventory() {

}

 

public void closeInventory() {

}

 

public boolean isItemValidForSlot(int slot, ItemStack itemstack) {

// If you have different kinds of slots, then check them here:

// if (slot == SLOT_SHIELD && itemstack.getItem() instanceof ItemShield)

// return true;

 

// For now, only ItemUseMana items can be stored in these slots

if(slot==0){

return true;

}

return false;

}

 

public void readFromNBT(NBTTagCompound p_145839_1_) {

super.readFromNBT(p_145839_1_);

NBTTagList nbttaglist = p_145839_1_.getTagList("DrawerItems", 10);

this.inventory = new ItemStack[getSizeInventory()];

 

for (int i = 0; i < nbttaglist.tagCount(); ++i) {

NBTTagCompound tag = nbttaglist.getCompoundTagAt(i);

int j = tag.getByte("Slot") & 255;

 

if (j >= 0 && j < inventory.length) {

this.inventory[j] = ItemStack.loadItemStackFromNBT(tag);

}

}

}

 

public void writeToNBT(NBTTagCompound comp) {

super.writeToNBT(comp);

NBTTagList nbttaglist = new NBTTagList();

 

for (int i = 0; i < this.inventory.length; ++i) {

ItemStack stack = inventory;

if (stack != null) {

NBTTagCompound tag = new NBTTagCompound();

tag.setByte("Slot", (byte) i);

stack.writeToNBT(tag);

nbttaglist.appendTag(tag);

}

}

 

comp.setTag("DrawerItems", nbttaglist);

 

}

 

public ItemStack getItem(int slot) {

ItemStack theItem = inventory[slot];

if (theItem != null)

return theItem;

else

return null;

}

 

@Override

public boolean canUpdate() {

return false;

}

 

public Packet getDescriptionPacket() {

NBTTagCompound dataTag = new NBTTagCompound();

NBTTagList itemList = new NBTTagList();

for (int i = 0; i < inventory.length; i++) {

ItemStack stack = inventory;

if (stack != null) {

NBTTagCompound tag = new NBTTagCompound();

tag.setLong("Slot", (byte) i);

stack.writeToNBT(tag);

itemList.appendTag(tag);

}

}

dataTag.setTag("DrawerItems", itemList);

this.writeToNBT(dataTag);

return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, dataTag);

 

}

 

public void onDataPacket(NetworkManager manager,

S35PacketUpdateTileEntity packet) {

NBTTagCompound nbtData = packet.func_148857_g();

NBTTagList tagList = nbtData.getTagList("DrawerItems", 10);

inventory = new ItemStack[getSizeInventory()];

for (int i = 0; i < inventory.length; i++) {

NBTTagCompound tag = tagList.getCompoundTagAt(i);

byte slot = tag.getByte("Slot");

if (slot >= 0 && slot < inventory.length)

inventory[slot] = ItemStack.loadItemStackFromNBT(tag);

}

worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

markDirty();

}

 

 

@Override

public int getBlockMetadata() {

if (this.worldObj != null) {

if (this.blockMetadata == -1) {

this.blockMetadata = this.worldObj.getBlockMetadata(

this.xCoord, this.yCoord, this.zCoord);

}

return this.blockMetadata;

} else

return 1;

}

 

@Override

public int[] getAccessibleSlotsFromSide(int slot) {

int[] slots = {

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

};

return slots;

}

 

@Override

public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_,

int p_102007_3_) {

return true;

}

 

@Override

public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_,

int p_102008_3_) {

// TODO Auto-generated method stub

return true;

}

}

 

 

 

Container:

 

 

package de.ItsAMysterious.mods.reallifemod.core.gui.containers;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.IInventory;

import net.minecraft.inventory.Slot;

import net.minecraft.item.ItemStack;

import de.ItsAMysterious.mods.reallifemod.core.gui.containers.slots.SlotFreezer;

import de.ItsAMysterious.mods.reallifemod.core.tiles.DrawerTE;

 

public class ContainerDrawer extends Container {

protected DrawerTE DrawerInventory;

 

public ContainerDrawer(InventoryPlayer playerInventory,

DrawerTE drawerInventory) {

this.DrawerInventory = drawerInventory;

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 0, 44, 20 ));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 1, 62, 20));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 2, 80, 20));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 3, 98, 20));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 4, 116, 20));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 5, 44, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 6, 62, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 7, 80, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 8, 98, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 9, 116, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 10, 44, 38));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 11, 62, 56));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 12, 80, 56));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 13, 98, 56));

this.addSlotToContainer(new SlotDrawer(this,drawerInventory, 14, 116, 56));

this.addPlayerInventorySlots(playerInventory);

 

}

 

public void addPlayerInventorySlots(InventoryPlayer inventoryPlayer) {

for (int i = 0; i < 3; ++i) {

for (int j = 0; j < 9; ++j) {

this.addSlotToContainer(new Slot(inventoryPlayer,j + i * 9+9 , 8 + j * 18, 87 + i * 18));

}

}

 

// Add ACTION BAR - just copied/pasted from vanilla classes

for (int i = 0; i < 9; ++i) {

this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18,

145));

}

}

 

/**

* This should always return true, since custom inventory can be accessed

* from anywhere

*/

@Override

public boolean canInteractWith(EntityPlayer player) {

return this.DrawerInventory.isUseableByPlayer(player);

}

 

@Override

public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int slotNumber) {

ItemStack itemstack = null;

Slot slot = (Slot) inventorySlots.get(slotNumber);

 

if (slot != null && slot.getHasStack()) {

ItemStack itemstack1 = slot.getStack();

itemstack = itemstack1.copy();

 

if (slotNumber < 15) {

if (!this.mergeItemStack(itemstack1, 15, 51, false)) {

return null;

}

} else if (!this.mergeItemStack(itemstack1, 0, 15, false)) {

return null;

}

 

if (itemstack1.stackSize == 0) {

slot.putStack((ItemStack) null);

} else {

slot.onSlotChanged();

}

}

        slot.onPickupFromSlot(p_82846_1_, itemstack);

return itemstack;

}

 

}

 

 

 

GuiContainer:

 

 

package de.ItsAMysterious.mods.reallifemod.core.gui;

 

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.client.resources.I18n;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.util.ResourceLocation;

 

import org.lwjgl.opengl.GL11;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import de.ItsAMysterious.mods.reallifemod.core.gui.containers.ContainerDrawer;

import de.ItsAMysterious.mods.reallifemod.core.tiles.DrawerTE;

 

@SideOnly(Side.CLIENT)

public class GuiDrawer extends GuiContainer

{

    private static final ResourceLocation field_147017_u = new ResourceLocation("textures/gui/container/generic_54.png");

    private IInventory upperChestInventory;

    private IInventory lowerChestInventory;

    /** window height is calculated with these values; the more rows, the heigher */

    private int inventoryRows;

    private static final String __OBFID = "CL_00000749";

    public static final int GUI_ID = 20;

 

 

    public GuiDrawer(InventoryPlayer p_i1083_1_, DrawerTE p_i1083_2_)

    {

        super(new ContainerDrawer(p_i1083_1_, p_i1083_2_));

        this.upperChestInventory = p_i1083_1_;

        this.lowerChestInventory=p_i1083_2_;

        this.allowUserInput = true;

        short short1 = 222;

        int i = short1 - 108;

        this.inventoryRows = p_i1083_2_.getSizeInventory()/5;

        this.ySize = i + this.inventoryRows * 18;

    }

 

    /**

    * Draw the foreground layer for the GuiContainer (everything in front of the items)

    */

    protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)

    {

        this.fontRendererObj.drawString(this.lowerChestInventory.hasCustomInventoryName() ? this.lowerChestInventory.getInventoryName() : I18n.format(this.lowerChestInventory.getInventoryName(), new Object[0]), 8, 6, 4210752);

        this.fontRendererObj.drawString(this.upperChestInventory.hasCustomInventoryName() ? this.upperChestInventory.getInventoryName() : I18n.format(this.upperChestInventory.getInventoryName(), new Object[0]), 8, this.ySize - 96 + 2, 4210752);

    }

 

 

   

    protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)

    {

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(  new ResourceLocation("reallifemod:textures/gui/container/Drawer.png"));

        int k = (this.width - this.xSize) / 2;

        int l = (this.height - this.ySize) / 2;

        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.inventoryRows * 18 + 18);

        this.drawTexturedModalRect(k, l + this.inventoryRows * 18 + 18, 0, 72, this.xSize, 96);

    }

}

 

 

 

I hope someone can help me im really stuck :(

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.