Jump to content

Gui Slots Acting Weird


shucke

Recommended Posts

Hello,

I am trying to open a GuiContainer when clicked on a button when in a GuiScreen.

the gui opens just fine but when i try to drag some items to the slots they start to act all weird.

sometimes when i click on 1 of my custom slots i drag out my armour for some reason.

and when i try to move around items in my player inventory it acts all weird.

the items duplicate to the slot nesxt to them i pick up items i didnt even select and all that kind of stuf.

i hope someone can find what is wrong with my code cuz i cant find the error.

 

anyway here is my code:

 

Block

 

package legendz.common;

 

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

 

import cpw.mods.fml.common.FMLCommonHandler;

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

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

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

import cpw.mods.fml.relauncher.Side;

import net.minecraft.block.Block;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.material.Material;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.network.packet.Packet250CustomPayload;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

 

public class BlockLegendzCraftingTable extends BlockContainer{

 

public BlockLegendzCraftingTable(int par1, Material par2Material) {

super(par1, 1 ,par2Material);

this.setHardness(0.8f);

this.setCreativeTab(getCreativeTabToDisplayOn().tabDecorations);

}

 

public int getBlockTextureFromSide(int i){

if(i == 0){

return 0;

}

if(i == 1){

return 0;

}

return 1;

}

 

public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9){

TileEntityLegendzCraftingTable obj = (TileEntityLegendzCraftingTable)par1World.getBlockTileEntity(par2, par3, par4);

if(obj!=null){

 

if(par5EntityPlayer.inventory.getCurrentItem() != null){

ItemStack item = par5EntityPlayer.inventory.getCurrentItem();

if(item.itemID == BaseLegendz.recipeItem.itemID){

obj.ownedRecipes[item.getItemDamage()] = true;

//par5EntityPlayer.inventory.setCurrentItem(0, 0, false, false);

return true;

}

}

 

FMLNetworkHandler.openGui(par5EntityPlayer, BaseLegendz.LegendzInstance, 0, par1World, par2, par3, par4);

Side side = FMLCommonHandler.instance().getEffectiveSide();

if (side == Side.SERVER) {

SendPacket(par1World, par2, par3, par4, par5EntityPlayer, obj);

}

}

return true;

}

 

private void SendPacket(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, TileEntityLegendzCraftingTable te) {

byte[] b = te.getOwnedRecipes();

 

int byteAmount = b.length + 1 + 12;

ByteArrayOutputStream bos = new ByteArrayOutputStream(byteAmount);

DataOutputStream outputStream = new DataOutputStream(bos);

try {

        outputStream.writeByte(0);

        outputStream.writeInt(par2);

        outputStream.writeInt(par3);

        outputStream.writeInt(par4);

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

        if(te.ownedRecipes == true){

        outputStream.writeBoolean(true);

        }else{

            outputStream.writeBoolean(false);

        }

        }

 

} catch (Exception ex) {

        ex.printStackTrace();

}

Packet250CustomPayload packet = new Packet250CustomPayload();

packet.channel = "MinecraftLegendz";

packet.data = bos.toByteArray();

packet.length = bos.size();

 

EntityPlayerMP player = (EntityPlayerMP)par5EntityPlayer;

PacketDispatcher.sendPacketToPlayer(packet, (Player)player);

}

 

public String getTextureFile(){

return BaseLegendz.PNGBlock;

}

 

@Override

public TileEntity createNewTileEntity(World var1) {

return new TileEntityLegendzCraftingTable();

}

 

}

 

 

 

Gui (Screen)

 

package legendz.common;

 

import net.minecraft.client.gui.GuiButton;

import net.minecraft.client.gui.GuiScreen;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.util.StatCollector;

import net.minecraft.world.World;

 

import org.lwjgl.opengl.GL11;

 

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

 

public class GuiLegendzCraftingTable extends GuiScreen

{

    private TileEntityLegendzCraftingTable inventory;

   

    private GuiButton button1;

    private GuiButton button2;

    private GuiButton[] button = new GuiButton[11];

    private EntityPlayer player;

    private World world;

    private int x,y,z;

   

    int curPage = 1;

    int maxPage = 2;

   

   

    public GuiLegendzCraftingTable(InventoryPlayer par1InventoryPlayer, TileEntityLegendzCraftingTable te, EntityPlayer p, World w, int xx, int yy, int zz)

    {

        this.inventory = te;

        player = p;

        world = w;

        x = xx;

        y = yy;

        z = zz;

    }

   

    public void initGui(){

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

    controlList.clear();

    controlList.add(button1 = new GuiButton(100, width / 2 - 60, height / 2 +68, 40, 20, "<"));

    controlList.add(button2 = new GuiButton(101, width / 2 + 60-40, height / 2 +68, 40, 20, ">"));

   

    int yOffset = 0;

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

    controlList.add(button = new GuiButton(i, width / 2 - 55, height / 2 -95 + yOffset, 111, 20, ItemRecipe.recipeNames));

    yOffset += 20;

    if(yOffset == 160){

    yOffset = 0;

    }

    }

   

    for(int j = 0; j < button.length; j++){

    button[j].enabled = inventory.ownedRecipes[j];

    }

    UpdatePage();

    }

   

    public boolean doesGuiPauseGame(){

    return false;

    }

   

    protected void actionPerformed(GuiButton guiButton){

    if(guiButton.id == 100){

    curPage--;

    UpdatePage();

    return;

    }

    if(guiButton.id == 101){

    curPage++;

    UpdatePage();

    return;

    }

   

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

    if(guiButton.id == i){

    inventory.curCraftingRecipe = i;

    FMLNetworkHandler.openGui(player, BaseLegendz.LegendzInstance, 1, world, x, y, z);

    }

    }

   

}

   

    private void UpdatePage() {

    int maxRecipe = curPage * 8;

    int minRecipe = maxRecipe - 8;

    if(maxRecipe > button.length){

    maxRecipe = button.length;

    }

    for(int j = 0; j < button.length; j++){

    if(button[j] != null){

    button[j].enabled = false;

    button[j].drawButton = false;

    }

    }

    for(int i = minRecipe; i < maxRecipe; i++){

    if(button != null){

    button.drawButton = true;

    if(inventory.ownedRecipes == true){

    button.enabled = true;

    }

    }

    }

}

 

public void updateScreen(){

    if(curPage == 1){

    button1.enabled = false;

    }else{

    button1.enabled = true;

    }

   

    if(curPage == maxPage){

    button2.enabled = false;

    }else{

    button2.enabled = true;

    }

    }

 

    public void drawScreen(int par1, int par2, float f)

    {

        //drawDefaultBackground();

        int var4 = this.mc.renderEngine.getTexture("/legendz/resources/gui/craftingSelection.png");

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

        this.mc.renderEngine.bindTexture(var4);

        int var5 = width / 2 - 88;

        int var6 = height / 2 - 112;

        this.drawTexturedModalRect(var5, var6, 0, 0, 176, 224);

        //button1.drawButton(mc, width, height);

        this.drawCenteredString(this.fontRenderer, ""+curPage, this.width / 2,height/2 +72, 16777215);

        super.drawScreen(par1, par2, f);

    }

}

 

 

 

 

Gui (Container)

 

package legendz.common;

 

import org.lwjgl.opengl.GL11;

 

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

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.util.StatCollector;

 

public class GuiLegendzCrafting extends GuiContainer{

 

private int recipeID;

private TileEntityLegendzCraftingTable inventory;

 

public GuiLegendzCrafting(InventoryPlayer inv, TileEntityLegendzCraftingTable te) {

super(new ContainerLegendzCraftingTable(inv, te));

inventory = te;

}

 

public boolean doesGuiPauseGame(){

return false;

}

 

protected void drawGuiContainerForegroundLayer(int i, int j){

recipeID = inventory.curCraftingRecipe;

    this.fontRenderer.drawString(StatCollector.translateToLocal(ItemRecipe.recipeNames[recipeID]), 30, -6, 4210752);

        this.fontRenderer.drawString(StatCollector.translateToLocal("Inventory"), 8, this.ySize - 98, 4210752);

}

 

protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3){

drawDefaultBackground();

int var4 = this.mc.renderEngine.getTexture("/legendz/resources/gui/crafting.png");

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

        this.mc.renderEngine.bindTexture(var4);

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

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

        this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);

}

 

}

 

 

 

Container

 

package legendz.common;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.Slot;

 

public class ContainerLegendzCraftingTable extends Container {

 

private TileEntityLegendzCraftingTable TELCT;

 

    public ContainerLegendzCraftingTable(InventoryPlayer par1InventoryPlayer, TileEntityLegendzCraftingTable par2)

    {

    TELCT = par2;

   

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

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

    this.addSlotToContainer(new Slot(par2, (3*var59)+var60, 26 + (var60 * 18), (var59 * 18)));

    }

    } 

        //this.addSlotToContainer(new SlotGrinder(par1InventoryPlayer.player, par2, 1, 116, 35));

    this.addSlotToContainer(new Slot(par2, 0, 40, 16));

 

    int var3;

 

        for (var3 = 0; var3 < 3; ++var3)

        {

            for (int var4 = 0; var4 < 9; ++var4)

            {

                this.addSlotToContainer(new Slot(par1InventoryPlayer, var4 + var3 * 9 + 9, 8 + var4 * 18, 84 + var3 * 18));

            }

        }

 

        for (var3 = 0; var3 < 9; ++var3)

        {

            this.addSlotToContainer(new Slot(par1InventoryPlayer, var3, 8 + var3 * 18, 142));

        }

    }

 

@Override

public boolean canInteractWith(EntityPlayer var1) {

return true;

}

 

}

 

 

 

CommonProxy

 

package legendz.common;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

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

 

public class CommonProxy implements IGuiHandler{

 

public void registerRenderInformation(){

 

}

 

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

TileEntity te = world.getBlockTileEntity(x, y, z);

if(te != null){

switch(ID){

case 0: return null;

 

case 1: return new ContainerLegendzCraftingTable(player.inventory, (TileEntityLegendzCraftingTable)te);

}

 

}

return null;

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

TileEntity te = world.getBlockTileEntity(x, y, z);

if(te != null){

switch(ID){

case 0: return new GuiLegendzCraftingTable(player.inventory, (TileEntityLegendzCraftingTable)te, player, world, x, y, z);

 

case 1: return new GuiLegendzCrafting(player.inventory, (TileEntityLegendzCraftingTable)te);

}

}

return null;

}

 

}

 

 

 

TileEntity

 

package legendz.common;

 

import java.util.ArrayList;

import java.util.List;

 

import cpw.mods.fml.common.registry.GameRegistry;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.block.Block;

import net.minecraft.block.BlockFurnace;

import net.minecraft.block.material.Material;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemBlock;

import net.minecraft.item.ItemHoe;

import net.minecraft.item.ItemStack;

import net.minecraft.item.ItemSword;

import net.minecraft.item.ItemTool;

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.tileentity.TileEntity;

 

import net.minecraftforge.common.ForgeDirection;

import net.minecraftforge.common.ISidedInventory;

 

public class TileEntityLegendzCraftingTable extends TileEntity implements IInventory, ISidedInventory

{

    /**

    * The ItemStacks that hold the items currently being used in the furnace

    */

    private ItemStack[] tableItemStacks = new ItemStack[12];

    public boolean[] ownedRecipes = new boolean[20];

    public int curCraftingRecipe = 0;

 

 

    /**

    * Returns the number of slots in the inventory.

    */

    public int getSizeInventory()

    {

        return this.tableItemStacks.length;

    }

 

    /**

    * Returns the stack in slot i

    */

    public ItemStack getStackInSlot(int par1)

    {

        return this.tableItemStacks[par1];

    }

 

    /**

    * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a

    * new stack.

    */

    public ItemStack decrStackSize(int par1, int par2)

    {

        if (this.tableItemStacks[par1] != null)

        {

            ItemStack var3;

 

            if (this.tableItemStacks[par1].stackSize <= par2)

            {

                var3 = this.tableItemStacks[par1];

                this.tableItemStacks[par1] = null;

                return var3;

            }

            else

            {

                var3 = this.tableItemStacks[par1].splitStack(par2);

 

                if (this.tableItemStacks[par1].stackSize == 0)

                {

                    this.tableItemStacks[par1] = null;

                }

 

                return var3;

            }

        }

        else

        {

            return null;

        }

    }

 

    /**

    * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -

    * like when you close a workbench GUI.

    */

    public ItemStack getStackInSlotOnClosing(int par1)

    {

        if (this.tableItemStacks[par1] != null)

        {

            ItemStack var2 = this.tableItemStacks[par1];

            this.tableItemStacks[par1] = null;

            return var2;

        }

        else

        {

            return null;

        }

    }

 

    /**

    * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).

    */

    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)

    {

        this.tableItemStacks[par1] = par2ItemStack;

 

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

        {

            par2ItemStack.stackSize = this.getInventoryStackLimit();

        }

    }

 

    /**

    * Returns the name of the inventory.

    */

    public String getInvName()

    {

        return "container.legendzcraftingtable";

    }

 

    /**

    * Reads a tile entity from NBT.

    */

    public void readFromNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.readFromNBT(par1NBTTagCompound);

        NBTTagList var2 = par1NBTTagCompound.getTagList("Items");

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

 

        for (int var3 = 0; var3 < var2.tagCount(); ++var3)

        {

            NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);

            byte var5 = var4.getByte("Slot");

 

            if (var5 >= 0 && var5 < this.tableItemStacks.length)

            {

                this.tableItemStacks[var5] = ItemStack.loadItemStackFromNBT(var4);

            }

        }

    }

 

    /**

    * Writes a tile entity to NBT.

    */

    public void writeToNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.writeToNBT(par1NBTTagCompound);

        NBTTagList var2 = new NBTTagList();

 

        for (int var3 = 0; var3 < this.tableItemStacks.length; ++var3)

        {

            if (this.tableItemStacks[var3] != null)

            {

                NBTTagCompound var4 = new NBTTagCompound();

                var4.setByte("Slot", (byte)var3);

                this.tableItemStacks[var3].writeToNBT(var4);

                var2.appendTag(var4);

            }

        }

 

        par1NBTTagCompound.setTag("Items", var2);

    }

 

    /**

    * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't

    * this more of a set than a get?*

    */

    public int getInventoryStackLimit()

    {

        return 64;

    }

 

    /**

    * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count

    * ticks and creates a new spawn inside its implementation.

    */

    public void updateEntity(){

      if(tableItemStacks[10] != null){

      int dmg = tableItemStacks[10].getItemDamage();

      if(ownedRecipes[dmg] == false){

      ownedRecipes[dmg] = true;

      tableItemStacks[10] = null;

      }

      }

      /*for(int ii = 0; ii < ownedRecipes.length; ii++){

      if(ownedRecipes[ii] == true){

      System.out.println(ii);

      }

      }*/

    }

   

    public byte[] getOwnedRecipes(){

    /*ArrayList list = new ArrayList();

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

    if(ownedRecipes == true){

    list.add((byte) i);

    }

    }

   

    byte[] b = new byte[list.size()];

    for(int iii = 0; iii < ownedRecipes.length; iii++){

    if(ownedRecipes[iii] == true){

    b[]

    }

    }*/

    byte[] b = new byte[ownedRecipes.length];

    for(byte i = 0; i < ownedRecipes.length; i++){

    if(ownedRecipes == true){

    b = (byte)(i+1);

    }else{

    b = 0;

    }

    }

    return b;

    }

 

    /**

    * Do not make give this method the name canInteractWith because it clashes with Container

    */

    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)

    {

        return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;

    }

 

    public void openChest() {}

 

    public void closeChest() {}

 

    @Override

    public int getStartInventorySide(ForgeDirection side)

    {

        if (side == ForgeDirection.DOWN) return 1;

        if (side == ForgeDirection.UP) return 0;

        return 2;

    }

 

    @Override

    public int getSizeInventorySide(ForgeDirection side)

    {

        return 1;

    }

}

 

 

 

Base File

 

package legendz.common;

 

@Mod(modid = "Minecraft Legendz", name = "Minecraft Legendz", version = "0.1")

@NetworkMod( clientSideRequired = true, serverSideRequired = false, versionBounds = "[0.1]",

channels=("MinecraftLegendz"), packetHandler = PacketHandler.class/*,

clientPacketHandlerSpec = @SidedPacketHandler(channels = {"CCraftClient"}, packetHandler = centurycraft.client.ClientPacketHandler.class),

serverPacketHandlerSpec = @SidedPacketHandler(channels = {"CCraft"}, packetHandler = centurycraft.common.PacketHandler.class)*/)

 

public class BaseLegendz {

public static final String PNGBlock = "/legendz/resources/Blocks/terrain.png";

public static final String PNGItem = "/legendz/resources/gui/item.png";

public static final String PNGAnimated = "/legendz/resources/gui/animatedItem.png";

 

@SidedProxy(clientSide = "legendz.client.ClientProxy", serverSide = "legendz.common.CommonProxy")

public static CommonProxy proxy;

 

@Instance("Minecraft Legendz")

public static BaseLegendz LegendzInstance;

 

public static int startBlockID;

public static int startItemID;

 

public static Block legendzCraftingTable, testBlock;

 

public static EnumArmorMaterial ENERGY = EnumHelper.addArmorMaterial("ENERGY", 80, new int[]{12, 27, 21, 12}, 60);

 

TickHandler tickHandler = new TickHandler();

 

private void LoadBlocks(){

legendzCraftingTable = new BlockLegendzCraftingTable(startBlockID, Material.wood).setBlockName("legendzcraftingtable");

}

 

private void RegisterBlocks() {

GameRegistry.registerBlock(legendzCraftingTable, "LegendzCraftingTable");

}

 

private void InitItems(){

 

}

 

@PreInit

public void PreLoad(FMLPreInitializationEvent evt){

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

config.load();

/*startBlockID = config.getOrCreateBlockIdProperty("BlockID", 400).getInt();

startItemID = config.getOrCreateIntProperty("ItemID", Configuration.CATEGORY_ITEM, 400).getInt();*/

startBlockID = config.get("General", "blockID", 750).getInt();

startItemID = config.get("General", "itemID", 3500).getInt();

        config.save();

        LoadBlocks();

        InitItems();

}

 

@Init

public void Load(FMLInitializationEvent evt){

proxy.registerRenderInformation();

NetworkRegistry.instance().registerGuiHandler(LegendzInstance, proxy);

GameRegistry.registerWorldGenerator(new WorldGenerator());

RegisterBlocks();

RegisterTileEntitys();

TickRegistry.registerTickHandler(tickHandler, Side.CLIENT);

//AddSmelting();

AddRecipes();

}

 

private void RegisterTileEntitys() {

GameRegistry.registerTileEntity(TileEntityLegendzCraftingTable.class, "Legendz Crafting Table");

}

 

private void AddRecipes(){

 

}

}

 

 

 

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.