Jump to content

Recommended Posts

Posted (edited)

Ok now i'm working on an ITEM Inventory, not a block !

package com.kporal.lau.items;

import com.kporal.lau.Ref;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ItemKitbag extends Item implements ICapabilityProvider {
	
	private ItemStackHandler inventory;
	
	public ItemKitbag() {
		
		setUnlocalizedName( Ref.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( Ref.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		inventory = new ItemStackHandler( 9 );
		
	}

	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    	NBTTagCompound nbt = new NBTTagCompound();
    	inventory.deserializeNBT( nbt );
    	
        return new ActionResult<ItemStack>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
    }
    
    public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
    	inventory.serializeNBT();
    }

	public NBTBase serializeNBT( NBTTagCompound nbt ) {
		nbt.setTag( "ItemStackHandler", inventory.serializeNBT() );
		return null;
	}

	public void deserializeNBT( NBTBase nbt ) {
		this.inventory.deserializeNBT( (( NBTTagCompound ) nbt).getCompoundTag( "ItemStackHandler" ) );
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return true;
		}
		return false;
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return (T) this.inventory;
		}
		return null;
	}
}

So i'm here, but ... i've no error when i start the game, no error on right click etc but nothing happend, maybe i've just fuked up anywhere ... or forgot something. I still learn about forge modding, so ... maybe i need to create a GUI too ? I'm really confused now don't know how to continue this ...

Edited by Kporal
solved
Posted

Ok thank for your help :) now i've rewrite a little bit of code and do that :

package com.kporal.lau.items;

import com.kporal.lau.Ref;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ItemKitbag extends Item {
	
	private ItemStackHandler inventory;
	
	public ItemKitbag() {
		
		setUnlocalizedName( Ref.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( Ref.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		inventory = new ItemStackHandler( 9 );
		
	}

	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    	NBTTagCompound nbt = new NBTTagCompound();
    	inventory.deserializeNBT( nbt );
    	initCapabilities( playerIn.getHeldItemMainhand(), null ).getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );
        return new ActionResult<ItemStack>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
    }
    
    public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
    	inventory.serializeNBT();
    }

	public NBTBase serializeNBT( NBTTagCompound nbt ) {
		nbt.setTag( "ItemStackHandler", inventory.serializeNBT() );
		return null;
	}

	public void deserializeNBT( NBTBase nbt ) {
		this.inventory.deserializeNBT( (( NBTTagCompound ) nbt).getCompoundTag( "ItemStackHandler" ) );
	}
}

But now, ( and i think maybe i'm on the good way ) because the game crash after right click my item xD with a null pointer exception ... i must be wrong in some way ...

Posted
  On 5/8/2017 at 7:28 AM, tommyte said:

You will have to ready and write to nbt yourself, in your capability provider class.

For what you made, you don't have to inject an existing capability, and you should instead extends the ItemStackHandler class there. It seems like you did a great job of making your own implementation. You don't have to register that class. You can instantiate it in your item class. Like below. Mind you, this class I made very quickly and without much experience with capabilities for items. It seems quite different from tile entities but still. I hope i'll give you a rough idea of what you should do.

 

public class ItemExample extends Item{

	ItemStackHandler inventory;
	
	public ItemExample()
	{
		
	}
    
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	ItemStack stackToSaveTo = playerIn.getHeldItem(handIn); //Do add a check to see if this really is your item.
    	
    	//You probably wont need the capability until you open the inventory for the first time. So..
    	if(inventory == null)
    	{
    		inventory = (ItemStackHandler) initCapabilities(stackToSaveTo, null).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    	}
    	
    	//Then here you can open the inventory and things, and, load data from nbt.
    	NBTTagCompound nbt = new NBTTagCompound();
    	inventory.deserializeNBT(nbt);
    	
    	//Now the nbt has all the information you need. Granted you gave what you wanted in the method below.
    	
    	//Now you can open inventory and things, and let the player do stuff.
    	
        return new ActionResult(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
    }
    
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {
    	//This should do what it says, so now you can save the data that has changed.
    	
    	inventory.serializeNBT();
    }
	
	@Override
	public ICapabilityProvider initCapabilities(ItemStack item, NBTTagCompound nbt)
	{
		if(item.getItem() instanceof ItemExample)
		{
			return new ExampleProvider();
		}
		return null;
	}
	
	public static class ExampleProvider implements ICapabilityProvider, ICapabilitySerializable {

		@Override
		public NBTBase serializeNBT() 
		{
			//Retrieve data here
			return null;
		}

		@Override
		public void deserializeNBT(NBTBase nbt) 
		{
			//Do your saving here
		}

		@Override
		public boolean hasCapability(Capability<?> capability, EnumFacing facing) 
		{
			if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
			{
				return true;
			}
			return false;
		}

		@Override
		public <T> T getCapability(Capability<T> capability, EnumFacing facing) 
		{
			if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
			{
				return (T) new ItemStackHandler(2/**the amount of slots you want*/); 
				//This is the default implementation by forge, but you'll likely want to make your own by overriding. 
			}
			return null;
		}
	}
}

 

 

 

Just try without and see what happens. If there is information missing on the client side, you can send packages to sync up. But only when needed.

Expand  

I help me from this thread, and i understand but i really don't know how to proceed, this system is so confusing for me, my problem is where to call the initCapabilities ? so i've removed my "inventory = new ItemStackHandler( 9 );" ok but now ? did i need to write something like that :

@Override
public ICapabilityProvider initCapabilities( ItemStack item, NBTTagCompound nbt ) {
	if( item.getItem() instanceof ItemsInit.kitbag ) {
		return new KitBagProvider();
	}
	return null;
}

and then on it putting the "ItemStackHandler" ? i'm so confused xD sorry ... trying to do my best :/

Posted

My last try and fail for tonight ... getting bored by my lake of knowledge xD hope to found my problem tomorow

package com.kporal.lau.items;

import com.kporal.lau.Ref;
import com.kporal.lau.init.ItemsInit;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ItemKitbag extends Item {
	
	private ItemStackHandler inventory;
	
	public ItemKitbag() {
		
		setUnlocalizedName( Ref.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( Ref.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		
	}

	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
		NBTTagCompound nbt = new NBTTagCompound();
    	inventory.deserializeNBT( nbt );
    	//initCapabilities( playerIn.getHeldItemMainhand(), nbt.getCompoundTag( "ItemStackHandler" ) ).getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );
    	return new ActionResult<ItemStack>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
	}

	public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
		inventory.serializeNBT();
	}
	
	@Override
	public ICapabilityProvider initCapabilities( ItemStack item, NBTTagCompound nbt ) {
		if( item.getItem() == ItemsInit.kitbag ) {
			return new KitBagProvider();
		}
		return null;
	}
	
	public class KitBagProvider implements ICapabilityProvider, ICapabilitySerializable {

		@Override
		public NBTBase serializeNBT() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void deserializeNBT( NBTBase nbt ) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean hasCapability( Capability<?> capability, EnumFacing facing ) {
			if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
				return true;
			}
			return false;
		}

		@Override
		public <T> T getCapability( Capability<T> capability, EnumFacing facing ) {
			if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
				return (T) new ItemStackHandler( 9 ); 
			}
			return null;
		}
	}

	/*public NBTBase serializeNBT( NBTTagCompound nbt ) {
		nbt.setTag( "ItemStackHandler", inventory.serializeNBT() );
		return null;
	}

	public void deserializeNBT( NBTBase nbt ) {
		this.inventory.deserializeNBT( (( NBTTagCompound ) nbt).getCompoundTag( "ItemStackHandler" ) );
	}*/
}

 

Posted

So if i understand corectly my problem, i've made a new class :

Provider.class

package com.kporal.lau.items.kitbag;

import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class Provider implements ICapabilityProvider, ICapabilitySerializable {
	
	private ItemStackHandler inventory;
	
	public void Init() {
		inventory = new ItemStackHandler( 9 );
	}
	
	@Override
	public NBTBase serializeNBT() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deserializeNBT(NBTBase nbt) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return true;
		}
		return false;
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return (T) inventory; 
		}
		return null;
	}
}

So ItemKitgab.class should be like that :

package com.kporal.lau.items;

import com.kporal.lau.LAU;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
//import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

public class ItemKitbag extends Item {
	
	public ItemKitbag() {
		
		setUnlocalizedName( LAU.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( LAU.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		
	}

	public ActionResult<ItemStack> onItemRightClick( World w, EntityPlayer p, EnumHand e ) {
		//NBTTagCompound nbt = new NBTTagCompound();
    	//inventory.deserializeNBT( nbt );
    	
    	return new ActionResult<ItemStack>( EnumActionResult.PASS, p.getHeldItemMainhand() );
	}

	public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
		//inventory.serializeNBT();
	}
}

And on my main class, on FML event, registering the capability with : CapabilityManager.INSTANCE.register(capability interface class, storage, default implementation factory); ? should be good to iterate my item with a unique instance of ItemStackHandler ?

Posted

KitbagProvider:

package com.kporal.lau.items.kitbag;

import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class KitbagProvider implements ICapabilityProvider, ICapabilitySerializable {
	
	private final ItemStackHandler inventory;
	
	public KitbagProvider() {
		inventory = new ItemStackHandler( 9 );
	}
	
	@Override
	public NBTBase serializeNBT() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deserializeNBT( NBTBase nbt ) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean hasCapability( Capability<?> capability, EnumFacing facing ) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return true;
		}
		return false;
	}

	@Override
	public <T> T getCapability( Capability<T> capability, EnumFacing facing ) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return (T) inventory; 
		}
		return null;
	}
}

 

ItemKitbag:

package com.kporal.lau.items;

import com.kporal.lau.LAU;
import com.kporal.lau.items.kitbag.KitbagProvider;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

public class ItemKitbag extends Item {
	
	public ItemKitbag() {
		
		setUnlocalizedName( LAU.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( LAU.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		
	}

	public ActionResult<ItemStack> onItemRightClick( World w, EntityPlayer p, EnumHand e ) {
		//NBTTagCompound nbt = new NBTTagCompound();
    	//inventory.deserializeNBT( nbt );
    	
    	return new ActionResult<ItemStack>( EnumActionResult.PASS, p.getHeldItemMainhand() );
	}

	public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
		//inventory.serializeNBT();
	}
	
	@Override
	public ICapabilityProvider initCapabilities( ItemStack item, NBTTagCompound nbt ) {
		if( item.getItem() == LAU.kitbag ) {
			return new KitbagProvider();
		}
		return null;
	}
}

 

So should be "better" now, like you said i need to use NBT and GUI to finish this one, i know it but first i need to understand this before continue xD

Posted

Ok now i'm here ( but i'm doing soemthing wrong for sure and don't know how to proceed corectly because i'm looking at # for help me, but this tuto is made from a tileentity and not an item ):

  Reveal hidden contents

 

ItemKitbag.class

package com.kporal.lau.items;

import com.kporal.lau.LAU;
import com.kporal.lau.items.kitbag.KitbagProvider;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

public class ItemKitbag extends Item {
	
	public ItemKitbag() {
		
		setUnlocalizedName( LAU.LAUItems.KITBAG.getUnlocalizedName() );
		setRegistryName( LAU.LAUItems.KITBAG.getRegistryName() );
		setCreativeTab( CreativeTabs.TOOLS );
		this.setMaxStackSize( 1 );
		
	}

	public ActionResult<ItemStack> onItemRightClick( World w, EntityPlayer p, EnumHand e ) {
		//NBTTagCompound nbt = new NBTTagCompound();
		//inventory.deserializeNBT( nbt );

		return new ActionResult<ItemStack>( EnumActionResult.PASS, p.getHeldItemMainhand() );
	}

	public void onPlayerStoppedUsing( ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft ) {
		//inventory.serializeNBT();
	}
	
	@Override
	public ICapabilityProvider initCapabilities( ItemStack item, NBTTagCompound nbt ) {
		if( item.getItem() == LAU.kitbag ) {
			return new KitbagProvider();
		}
		return null;
	}
}

 

 

KitbagProvider.class

package com.kporal.lau.items.kitbag;

import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class KitbagProvider implements ICapabilityProvider, ICapabilitySerializable {
	
	private final ItemStackHandler inventory;
	
	public KitbagProvider() {
		inventory = new ItemStackHandler( 9 );
	}
	
	@Override
	public NBTBase serializeNBT() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deserializeNBT( NBTBase nbt ) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean hasCapability( Capability<?> capability, EnumFacing facing ) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return true;
		}
		return false;
	}

	@Override
	public <T> T getCapability( Capability<T> capability, EnumFacing facing ) {
		if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
			return (T) inventory; 
		}
		return null;
	}
}

 

KitbagContainer.class

package com.kporal.lau.items.kitbag;

import com.kporal.lau.items.ItemKitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class KitbagContainer extends Container {

	private KitbagProvider kp;
	
	public KitbagContainer( IInventory i, ItemKitbag kg ) {
		
		IItemHandler handler = kp.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );
		
		this.addSlotToContainer( new SlotItemHandler( handler, 0, 62, 17 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 1, 80, 17 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 2, 98, 17 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 3, 62, 35 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 4, 80, 35 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 5, 98, 35 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 6, 62, 53 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 7, 80, 53 ) );
		this.addSlotToContainer( new SlotItemHandler( handler, 8, 62, 53 ) );
		
		int xPos = 8;
		int yPos = 84;
		
		for( int y = 0; y < 3; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				this.addSlotToContainer( new Slot( i , x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		for( int x = 0; x < 9; ++x ) {
			this.addSlotToContainer( new Slot( i, x, xPos + x * 18, 99 ));
		}
		
	}
	
	@Override
	public boolean canInteractWith( EntityPlayer p ) {
		// TODO Auto-generated method stub
		return false;
	}

}

 

KitbagGuiHandler.class

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class KitbagGuiHandler implements IGuiHandler {

	public static final int ITEM_KITBAG = 0;
	
	@Override
	public Object getServerGuiElement(int ID, EntityPlayer p, World w, int x, int y, int z) {
		if( ID == ITEM_KITBAG ) {
			return new KitbagContainer( p.inventory, ... );
		}
		return null;
	}

	@Override
	public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
		// TODO Auto-generated method stub
		return null;
	}

}

 

And NetworkRegistry.INSTANCE.registerGuiHandler( LAU.instance, new KitbagGuiHandler() ); is defined on the main class.

So i now where is my problem at KitbagGuiHandler.class line 15 ( return new KitbagContainer( p.inventory, ... ); ), the second argument needed is ... so ItemKitbag and i think i've made a mistake somewhere ... trying to search where and why but ... f*** myself 

Posted

Ok really thank for your help !!! I've test something and it work fine, reworking my "inventory" to match my 54 slot wanted, but now the only last thing i'm wronk is here :

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraftforge.items.IItemHandler;

public class KitbagContainer extends Container {
	
	public KitbagContainer( IItemHandler iItemHandler, EntityPlayer p ) {
		
		int xPos = 8;
		int yPos = 18;
		
		for( int y = 0; y < 6; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( (IInventory) iItemHandler, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		yPos = 140;
		
		for( int y = 0; y < 3; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( p.inventory, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		for( int x = 0; x < 9; ++x ) {
			addSlotToContainer( new Slot( p.inventory, x, xPos + x * 18, 198 ));
		}
		
	}
	
	@Override
	public boolean canInteractWith( EntityPlayer p ) {
		return true;
	}

}

At line 18 ( addSlotToContainer( new Slot( (IInventory) iItemHandler, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 )); ), i know it's wrong, but i need to understand 2 thing before editing corectly my code, first, if i'm right i need to read the inventory of ItemStackHandler ? or IInventory ? not sure at all and the second points is about ID of slots ... actually i've just copied the other line so now the game crash but without this line, all work corectly.

java.lang.ClassCastException: net.minecraftforge.items.ItemStackHandler cannot be cast to net.minecraft.inventory.IInventory
	at com.kporal.lau.items.kitbag.KitbagContainer.<init>(KitbagContainer.java:18)

 

Posted

Omg : 

public KitbagContainer( IItemHandler iItemHandler, EntityPlayer p )

I implement IItemHandler rather than IInventory ... should be that my error

Posted

KitbagContainer.java

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;

public class KitbagContainer extends Container {
	
	public KitbagContainer( IInventory i, EntityPlayer p ) {
		
		int xPos = 8;
		int yPos = 18;
		
		for( int y = 0; y < 6; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( i, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		yPos = 140;
		
		for( int y = 0; y < 3; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( p.inventory, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		for( int x = 0; x < 9; ++x ) {
			addSlotToContainer( new Slot( p.inventory, x, xPos + x * 18, 198 ));
		}
		
	}
	
	@Override
	public boolean canInteractWith( EntityPlayer p ) {
		return true;
	}
}

 

KitbagGui.java

package com.kporal.lau.items.kitbag;

import com.kporal.lau.LAU;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;

public class KitbagGui extends GuiContainer {

	@SuppressWarnings("unused")
	private IInventory i;
	
	public KitbagGui( IInventory i, EntityPlayer p ) {
		super( new KitbagContainer( i, p ));
		
		this.xSize = 175;
		this.ySize = 221;
		this.i = i;
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
		this.mc.getTextureManager().bindTexture( new ResourceLocation( LAU.MODID, "textures/gui/container/kitbag.png" ) );
		this.drawTexturedModalRect( this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize );
	}
}

 

KitbagGuiHandler.java

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.items.CapabilityItemHandler;

public class KitbagGuiHandler implements IGuiHandler {
	
	@Override
	public Object getServerGuiElement(int ID, EntityPlayer p, World w, int x, int y, int z) {
		return new KitbagContainer( (IInventory) p.getHeldItemMainhand().getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null ), p );
	}

	@Override
	public Object getClientGuiElement(int ID, EntityPlayer p, World w, int x, int y, int z) {
		return new KitbagGui( (IInventory) p.getHeldItemMainhand().getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null ), p );
	}
}

 

I think i'm nearly done but ... something weird i'm stuck, i know where is my problem but idk how to proceed :/

Posted

You can't cast an ItemStackHandler to IInventory, instead, take in an IItemHandler in the container and the gui for the container and then inside the container when you are sorting out the slots, there is a addSlotToContainer which takes in a 

new SlotItemHandler(item_stack_handler, index, x_pos, y_pos)

which you should be able to fill in easily

Posted

Yes i'm on it actually :

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class KitbagContainer extends Container {
	
	public KitbagContainer( IItemHandler i, EntityPlayer p ) {
		
		int xPos = 8;
		int yPos = 18;
	
		for( int y = 0; y < 6; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new SlotItemHandler( p.getHeldItemMainhand().getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null ), x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		yPos = 140;
		
		for( int y = 0; y < 3; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( p.inventory, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		for( int x = 0; x < 9; ++x ) {
			addSlotToContainer( new Slot( p.inventory, x, xPos + x * 18, 198 ));
		}
		
	}
	
	@Override
	public boolean canInteractWith( EntityPlayer p ) {
		return true;
	}
}

 

All should be fine but not for ID, i get an error and a crash after opening the GUI : java.util.concurrent.ExecutionException: java.lang.RuntimeException: Slot 54 not in valid range - [0,54), so this is certainly due to the dynamic SlotItemHandler ID, maybe like other i need to create each slot with ID manually ..

Posted

Holy crap it work ... thank for your help <3 ( basicaly i've just made some little mistake but it work without problem now )

package com.kporal.lau.items.kitbag;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class KitbagContainer extends Container {
	
	public KitbagContainer( IItemHandler i, EntityPlayer p ) {
		
		int xPos = 8;
		int yPos = 18;
		int iid = 0;
	
		for( int y = 0; y < 6; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new SlotItemHandler( i, iid, xPos + x * 18, yPos + y * 18 ));
				iid++;
			}
		}
		
		yPos = 140;
		
		for( int y = 0; y < 3; ++y ) {
			for( int x = 0; x < 9; ++x ) {
				addSlotToContainer( new Slot( p.inventory, x + y * 9 + 9, xPos + x * 18, yPos + y * 18 ));
			}
		}
		
		for( int x = 0; x < 9; ++x ) {
			addSlotToContainer( new Slot( p.inventory, x, xPos + x * 18, 198 ));
		}
		
	}
	
	@Override
	public boolean canInteractWith( EntityPlayer p ) {
		return true;
	}
}

 

Posted

hum come back because theire is one weird thing, my inventory work fine, persistent etc all work, but ... about shift clicking ... this cause the game crash each time :/

  Reveal hidden contents

 

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.