I'm pretty sure I showed that in the tutorial for the ItemMagicBag or whatever, but you just create a new Inventory with the stack:
ItemStack stack = player.getHeldItem(); // may also use the ItemStack from the method parameters
IInventory inventory = new InventoryMagicBag(stack); // create new inventory with the stack
inventory.setInventorySlotContents(0, new ItemStack(Items.diamond, 64)); // do something with the inventory
When I click on a slot in my custom inventory, I get an error. Any Ideas?
[03:57:52] [Server thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.StackOverflowError
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_66]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_66]
at net.minecraft.util.Util.func_181617_a(Util.java:23) [Util.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:730) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:675) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:159) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:542) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
Caused by: java.lang.StackOverflowError
at java.util.ArrayList.iterator(Unknown Source) ~[?:1.8.0_66]
at net.minecraft.nbt.NBTTagList.copy(NBTTagList.java:264) ~[NBTTagList.class:?]
Basically, NBTTagList.copy is called hundreds of times and then it crashes. I didn't include that part of the crash.
It looks like the error is coming from super.slotClick, but I'm unsure as to why.
More information. Looks like a null pointer exception, but I don't know where...
-- Entity being ticked --
Details:
Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
Entity ID: 0
Entity Name: Player636
Entity's Exact location: -1091.47, 51.00, 549.61
Entity's Block location: -1092.00,51.00,549.00 - World: (-1092,51,549), Chunk: (at 12,3,5 in -69,34; contains blocks -1104,0,544 to -1089,255,559), Region: (-3,1; contains chunks -96,32 to -65,63, blocks -1536,0,512 to -1025,255,1023)
Entity's Momentum: 0.00, -0.08, 0.00
Entity's Rider: ~~ERROR~~ NullPointerException: null
Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:1818)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:559)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:771)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:675)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:159)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:542)
at java.lang.Thread.run(Unknown Source)
You're doing the same thing as another person on MinecraftForge.net - DO NOT use #setInventorySlotContents when loading from NBT, set the inventory stack directly. I suppose that's my fault for not updating this page in so long... should probably just take it down.
I edited the code so that it's not using #setInventorySlotContents
public void readFromNBT(NBTTagCompound compound)
{
// Gets the custom taglist we wrote to this compound, if any
NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_LIST);
//NBTTagList items = information.getCompoundTagAt(0).getTagList("ItemInventory", Constants.NBT.TAG_LIST);
for (int i = 0; i < items.tagCount(); ++i){
NBTTagCompound item = items.getCompoundTagAt(i);
int slot = item.getInteger("Slot");
// Just double-checking that the saved slot index is within our inventory array bounds
if (slot >= 0 && slot < getSizeInventory()) {
this.inventory<em> = ItemStack.loadItemStackFromNBT(item);
}
}
}</em>
I still get the same error though
Thanks for the speedy response!
Yes, that is, and I edited the OP to link to Github instead. I suggest you re-read it, as you are doing some stuff that is evil, e.g.:
this.tunneler = (Tunneler)stack.getItem();
if (tunneler.hasInventory) { // What?!?! Item instances cannot store mutable fields!!!
You CAN NOT store ANY data in your Item class - there is only ONE item of that type for all of them in existence, so if you have a Tunneler item and I have one, and yours has an inventory, then mine will have one, too. If I change #hasInventory in mine, it will change that field for yours, too, and you will suddenly lose your inventory.
P.S. I wish we could disable spell-checking - I suspect it is the main cause of the crazy lag between the time I type a character and the time it actually appears in the editor -.-
Hello, I'm Abelatox and I work on a mod where we want to put bags to be opened with right click. We followed your tutorial and works fine but I've seen a problem which is:
If you transfer using shift click from custom inventory to player inventory it works fine, but if you transfer from player's inventory to the bag inventory, it puts a 0 instead of setting the item to null.
The Meaning of Life, the Universe, and Everything.
Join Date:
8/9/2013
Posts:
211
Minecraft:
HappyKiller1O1
Xbox:
HappyKiller101
Member Details
Hey coolAlias! So I noticed, after using your code for KeyBinding, that it was very difficult to reference my key for something else (such as using the same key to close the inventory). So, I thought I would provide my little fix for others if they so need it.
So first, a little fix for the key handler. Considering the array that holds all of our bindings is private let's make a static reference to our key (this can be done multiple times if needed):
//This goes right above the constructor of your KeyHandler class
public static KeyBinding keyYourKeyBinding;
Next, all we have to do is register our reference in the constructor of the KeyHandler class:
//This goes right under your for loop to register your keys
this.keyYourKeyBinding = keys[CUSTOM_INV]; //This "CUSTOM_INV" is just the int you set for your KeyBinding at the start of your KeyHandler
And that's about it. I will quickly show you how I use it, to close my custom inventory with the KeyBinding. The reason to use this way, is in case they change it to another key, it will still work.
//This is in your GUI class, NOT your container class!
protected void keyTyped(char typedChar, int keyCode) throws IOException {
super.keyTyped(typedChar, keyCode);
if(keyCode == KeyHandler.keyYourKeyBinding.getKeyCode()) {
this.mc.thePlayer.closeScreen();
}
}
Hm, don't know why mine are private then! Still, a bit of a cleaner way to reference it.
Yes, at the cost of a completely redundant variable You may as well forego the array entirely and simply make them all individual references; I use an array because I have a lot of key bindings, but if you only have a handful of them, the array isn't that helpful.
The Meaning of Life, the Universe, and Everything.
Join Date:
8/9/2013
Posts:
211
Minecraft:
HappyKiller1O1
Xbox:
HappyKiller101
Member Details
That's what I thought about. I'm going to keep the array in case I accumulate a lot of KeyBindings. I just feel like others would prefer calling a one word reference instead of the keys[], for the sake of organization. Thank you for the tutorials though, may I ask if you took Java courses? Or perhaps where you started programming?
If I want to have class member variables, where would I put them so that they are unique to each individual item? Would storing them in the InventoryItem class work?
If I want to have class member variables, where would I put them so that they are unique to each individual item? Would storing them in the InventoryItem class work?
Store them in the ItemStack's NBT tag, like everything else. I would only put them in the inventory class if they were specifically related to the inventory.
Would this work for giving a block a custom inventory?
That depends. Usually, Blocks use a TileEntity to store their inventory, e.g. chests. Some, such as the EnderChest, however, only use the Block to open the inventory, but the inventory is actually tied to the player entity; i.e. the inventory is per player, not per block.
If you want an inventory that is per-block, like chests are, then use a TileEntity-based approach. If you want a per-player inventory, like the Ender Chest, then use IEEP to store the inventory just like adding custom slots to a player's inventory - you can open the inventory from a Block, a key press, an item, or anything else, doesn't matter.
Hi, I'm working on a mod and I have a custom inventory (in fact 3) but there is a little problem, when I make the item to be consumed without being in the gui, when I load the gui it re spawns, which means I can't delete the item.
hi guys I want to create rpg mod
How How do I learn this rpg modding ?
Last post on the previous page:
When I click on a slot in my custom inventory, I get an error. Any Ideas?
[03:57:52] [Server thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.StackOverflowError
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_66]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_66]
at net.minecraft.util.Util.func_181617_a(Util.java:23) [Util.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:730) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:675) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:159) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:542) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
Caused by: java.lang.StackOverflowError
at java.util.ArrayList.iterator(Unknown Source) ~[?:1.8.0_66]
at net.minecraft.nbt.NBTTagList.copy(NBTTagList.java:264) ~[NBTTagList.class:?]
Basically, NBTTagList.copy is called hundreds of times and then it crashes. I didn't include that part of the crash.
It looks like the error is coming from super.slotClick, but I'm unsure as to why.
More information. Looks like a null pointer exception, but I don't know where...
-- Entity being ticked --
Details:
Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
Entity ID: 0
Entity Name: Player636
Entity's Exact location: -1091.47, 51.00, 549.61
Entity's Block location: -1092.00,51.00,549.00 - World: (-1092,51,549), Chunk: (at 12,3,5 in -69,34; contains blocks -1104,0,544 to -1089,255,559), Region: (-3,1; contains chunks -96,32 to -65,63, blocks -1536,0,512 to -1025,255,1023)
Entity's Momentum: 0.00, -0.08, 0.00
Entity's Rider: ~~ERROR~~ NullPointerException: null
Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:1818)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:559)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:771)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:675)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:159)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:542)
at java.lang.Thread.run(Unknown Source)
I complicate easy problems, come begging for help, and leave with a simple solution.
StackOverFlow errors almost always happen when you inadvertently create an infinite loop or recursion. Show your code.
Container Item:
http://pastebin.com/bpNtqpzF
Inventory Item:
http://pastebin.com/nMYz93Dv
I complicate easy problems, come begging for help, and leave with a simple solution.
You're doing the same thing as another person on MinecraftForge.net - DO NOT use #setInventorySlotContents when loading from NBT, set the inventory stack directly. I suppose that's my fault for not updating this page in so long... should probably just take it down.
Is this what you were talking about?
I edited the code so that it's not using #setInventorySlotContents
I still get the same error though
Thanks for the speedy response!
I complicate easy problems, come begging for help, and leave with a simple solution.
P.S. I wish we could disable spell-checking - I suspect it is the main cause of the crazy lag between the time I type a character and the time it actually appears in the editor -.-
Hello, I'm Abelatox and I work on a mod where we want to put bags to be opened with right click. We followed your tutorial and works fine but I've seen a problem which is:If you transfer using shift click from custom inventory to player inventory it works fine, but if you transfer from player's inventory to the bag inventory, it puts a 0 instead of setting the item to null.This is the Container class for the bag: Kingdom Keys GithubThank youEdit: We are using 1.8.9 forge.Never mind, it's fixed now.
Thanks.
Hey coolAlias! So I noticed, after using your code for KeyBinding, that it was very difficult to reference my key for something else (such as using the same key to close the inventory). So, I thought I would provide my little fix for others if they so need it.
Next, all we have to do is register our reference in the constructor of the KeyHandler class:
And that's about it. I will quickly show you how I use it, to close my custom inventory with the KeyBinding. The reason to use this way, is in case they change it to another key, it will still work.
Hope this helps anyone that needed it!
@HappyKiller101 Thanks - mine are public, though.
Hm, don't know why mine are private then! Still, a bit of a cleaner way to reference it.
Yes, at the cost of a completely redundant variable You may as well forego the array entirely and simply make them all individual references; I use an array because I have a lot of key bindings, but if you only have a handful of them, the array isn't that helpful.
That's what I thought about. I'm going to keep the array in case I accumulate a lot of KeyBindings. I just feel like others would prefer calling a one word reference instead of the keys[], for the sake of organization. Thank you for the tutorials though, may I ask if you took Java courses? Or perhaps where you started programming?
If I want to have class member variables, where would I put them so that they are unique to each individual item? Would storing them in the InventoryItem class work?
I complicate easy problems, come begging for help, and leave with a simple solution.
Would this work for giving a block a custom inventory?
Store them in the ItemStack's NBT tag, like everything else. I would only put them in the inventory class if they were specifically related to the inventory.
That depends. Usually, Blocks use a TileEntity to store their inventory, e.g. chests. Some, such as the EnderChest, however, only use the Block to open the inventory, but the inventory is actually tied to the player entity; i.e. the inventory is per player, not per block.
If you want an inventory that is per-block, like chests are, then use a TileEntity-based approach. If you want a per-player inventory, like the Ender Chest, then use IEEP to store the inventory just like adding custom slots to a player's inventory - you can open the inventory from a Block, a key press, an item, or anything else, doesn't matter.
Hi, I'm working on a mod and I have a custom inventory (in fact 3) but there is a little problem, when I make the item to be consumed without being in the gui, when I load the gui it re spawns, which means I can't delete the item.
Gui Class, Container Class, Inventory Class, Common Proxy Class, Main Class
I hope you can help me please.
Thanks.
You need to delete items only on the server.
Yeah thanks, I managed to fix it and then i read you answered, thanks