Jump to content

Inventory Slots Problem.


SenpaiSubaraki

Recommended Posts

whenever i place an Item in my slot, it wont place it down, but place a copy of it. so i have two items. and when i want to take it out, it just disapears !

 

Everything is working fine, and its the last thing I need for it to complete !

 

Here are a few files you might want to take a look at if you want to help me :)

I'm pretty sure it's just a matter of a line.

Server and Client are out of Sync, thats what I think, but dont know where :/

 

inventory

 

package RpgInventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

public class RpgInventory implements IInventory {

 static ItemStack[] armorSlots = new ItemStack[6];

 private EntityPlayer player;
 public RpgInventory instance;
 public RpgInventory(EntityPlayer par1EntityPlayer)
    {
        this.player = par1EntityPlayer;
        instance = this;
    }

 public boolean inventoryChanged = false;

 @Override
public int getSizeInventory() {
	return 6;
}

public ItemStack getJewelInSlot(int par1)
    {
        return this.armorSlots[par1];
    }
 /**
     * Returns a slot index in main inventory containing a specific itemID
     */
    private int findJewel(int par1)
    {
        for (int var2 = 0; var2 < this.armorSlots.length; ++var2)
        {
            if (this.armorSlots[var2] != null && this.armorSlots[var2].itemID == par1)
            {
                return var2;
            }
        }

        return -1;
    }
    
    public boolean getJewel(int par1)
    {
        int var2 = this.findJewel(par1);
        return var2 >= 0;
    }
    
    public ItemStack getJewelFromStack(int par1)
    {
        ItemStack[] var2 = this.armorSlots;

        if (par1 >= var2.length)
        {
            par1 -= var2.length;
            var2 = this.armorSlots;
        }

        return var2[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)
    {
        ItemStack[] var3 = this.armorSlots;

        if (var3[par1] != null)
        {
            ItemStack var4;

            if (var3[par1].stackSize <= par2)
            {
                var4 = var3[par1];
                var3[par1] = null;
                return var4;
            }
            else
            {
                var4 = var3[par1].splitStack(par2);

                if (var3[par1].stackSize == 0)
                {
                    var3[par1] = null;
                }

                return var4;
            }
        }
        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)
    {
        ItemStack[] var2 = this.armorSlots;

        if (var2[par1] != null)
        {
            ItemStack var3 = var2[par1];
            var2[par1] = null;
            return var3;
        }
        else
        {
            return null;
        }
    }
    
    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
    {
        ItemStack[] var3 = this.armorSlots;
        var3[par1] = par2ItemStack;
    }
    
    
    public ItemStack getStackInSlot(int par1)
    {
        ItemStack[] var2 = this.armorSlots;
        return var2[par1];
    }
    
    public String getInvName()
    {
        return "RpgInventory";
    }
    
    public int getInventoryStackLimit()
    {
        return 64;
    }
    
    public void onInventoryChanged()
    {
        this.inventoryChanged = true;
    }
    
    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return this.player.isDead ? false : par1EntityPlayer.getDistanceSqToEntity(this.player) <= 64.0D;
    }
    
  
    /**
     * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80
     * for crafting).
     */
    public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound)
        {
            System.out.println("written to NBT");

            NBTTagList var2 = new NBTTagList();

            for (int var3 = 0; var3 < this.armorSlots.length; ++var3)
            {
                if (this.armorSlots[var3] != null)
                {
                    NBTTagCompound compoundSlot = new NBTTagCompound();
                    compoundSlot .setByte("Slot", (byte)var3);
                    this.armorSlots[var3].writeToNBT(compoundSlot );
                    var2.appendTag(compoundSlot);
                }
            }

            par1NBTTagCompound.setTag("Items", var2);
            return par1NBTTagCompound;
        }

    /**
     * Reads from the given tag list and fills the slots in the inventory with the correct items.
     */
    public static void readFromNBT(NBTTagCompound par1NBTTagCompound)
        {
            System.out.println("read from NBT");
            NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
            armorSlots = new ItemStack[6];

            for (int var3 = 0; var3 < var2.tagCount(); ++var3)
            {
                    NBTTagCompound compoundSlot = (NBTTagCompound)var2.tagAt(var3);
                    byte var5 = compoundSlot .getByte("Slot");

                    if (var5 >= 0 && var5 < armorSlots.length)
                    {
                            armorSlots[var5] = ItemStack.loadItemStackFromNBT(compoundSlot );
                    }
            }
        }

    public void openChest() {}

    public void closeChest() {}

}

 

 

Container

 

package RpgInventory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class RpgContainer extends Container {
	 File rpgfile = new File("rpg.dat");
	 NBTTagCompound nbt1;
	 NBTTagCompound nbt2;

public RpgInventory rpgInv;
public RpgContainer (EntityPlayer p1)

    {
	 super();
     rpgInv = new RpgInventory(p1);
     
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  0,  6,  16,  0));// necklace 
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  1,  6,  37,  1));//shield
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  2,  82, 16,  2));//cloak
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  3,  82, 38,  3));//gloves
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  4,  82, 59,  4));//ring
	this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  5,  6,  58,  4));//ring


	 for (int var4 = 0; var4 < 3; ++var4)
        {
            for (int var5 = 0; var5 < 9; ++var5)
            {
                this.addSlotToContainer(new Slot(p1.inventory, (var5 + (var4 + 1) * 9), 8 + var5 * 18, 84 + var4 * 18));
            }
        }

        for (int var4 = 0; var4 < 9; ++var4)
        {
            this.addSlotToContainer(new Slot(p1.inventory, var4, 8 + var4 * 18, 142));
        }
      this.onGuiOpen(rpgInv,p1);
    }

public void onGuiOpen(RpgInventory inv, EntityPlayer player)
{ 
	if(!player.worldObj.isRemote)
	{
		try {
			File var42 = new File("rpg.dat");
			NBTTagCompound var52 = new NBTTagCompound();
			var52 = CompressedStreamTools.readCompressed(new FileInputStream(var42));
			inv.readFromNBT(var52);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 @Override
 public void onCraftGuiClosed(EntityPlayer player)
 { 			   
	 if(!player.worldObj.isRemote)
	 {
		 try {
			 File var4 = new File("rpg.dat");
			 NBTTagCompound var2 = new NBTTagCompound();
			 var2 = rpgInv.writeToNBT(new NBTTagCompound());
			 CompressedStreamTools.writeCompressed(var2, new FileOutputStream(var4));

		 } catch (FileNotFoundException e) {
			 e.printStackTrace();
		 } catch (IOException e) {
			 e.printStackTrace();
		 }
	 } 
 }

  public boolean doesGuiPauseGame()
    {
        return false;
    }
  
  @Override
  public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        return null;	
    }
  
public boolean canInteractWith(EntityPlayer var1) {
	return true;
}
}

 

 

SlotArmorRpg

 

package RpgInventory;

import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

class SlotRpgArmor extends Slot
{
    /**
     * The armor type that can be placed on that slot, it uses the same values of armorType field on ItemArmor.
     */
    final int armorType;

    /**
     * The parent class of this clot, ContainerPlayer, SlotArmor is a Anon inner class.
     */
    final Container parent;

    SlotRpgArmor(Container par1ContainerPlayer, IInventory par2IInventory, int par3, int par4, int par5, int par6)
    {
        super(par2IInventory, par3, par4, par5);
        this.parent = par1ContainerPlayer;
        this.armorType = par6;
    }

    /**
     * Returns the maximum stack size for a given slot (usually the same as getInventoryStackLimit(), but 1 in the case
     * of armor slots)
     */
    public int getSlotStackLimit()
    {
        return 1;
    }

    /**
     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
     */
    @Override
    public boolean isItemValid(ItemStack par1ItemStack)
    {
    	if((par1ItemStack.getItem() instanceof ItemArmorRpg))
    	{
    		return true;
    	}
	return false;
    }
}

 

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.

Announcements



×
×
  • Create New...

Important Information

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