Jump to content

Recommended Posts

Posted

How to delete a Item form an inventory and then spawn the same one with the same damage? If possible, with different damage.

PS: I know how to save the damage, I just don't know how to delete and spawn the item.

If I helped then you help, hit that Thank You button or Applaud.

Posted

I found out how to spawn the item but, if I do: player.inventory.addItemStackToInventory(new ItemStack(Blocks.dirt)); it looks like the item is there but it isn't.

I tried doing player.inventoryContainer.putStackInSlot(15, (new ItemStack(Blocks.dirt))); but the same thing happens, plus I want it to take up the slot where the deleted item was. And also I still haven't figured out how to spawn the tool damaged.

If I helped then you help, hit that Thank You button or Applaud.

Posted

new ItemStack(Item, numberOfItems, stackDamage)

 

When using player.inventory.addOrRemoveStuff, make sure you are only doing it on the server side or you will have issues of items looking like they are or aren't there when they shouldn't or should be.

 

To place the new stack in the same slot, first you'll have to get the slot index of the item to replace and use setInventorySlotContents(int slot, ItemStack stack).

Posted

How to get the slot index and how to add them in the server? I tought that if I add it in the container that would do it since gui is client and container is server...

If I helped then you help, hit that Thank You button or Applaud.

Posted

To get the slot number, typically you have to iterate through the inventory until you find it:

for (int i = 0; i < player.inventory.getSizeInventory(); ++i) {
ItemStack stack = player.inventory.getStackInSlot(i);
// check if stack is not null and if it is the item you want
// if it is, you now know the slot number is ' i '
// do what you want, then break, since you don't need to keep iterating
}

 

Containers are both server AND client - why do you think a container instance is used in the GuiContainer class? Where are you trying to call all this code from, anyway?

Posted

Ohh, ok. I looked in it more and I tried a bunch of lines... And nothing worked when I try to add it to the server.

If I helped then you help, hit that Thank You button or Applaud.

Posted

These are a couple of the lines I tried but I got really confused, I sorry if this is totally wrong.

player.inventory.addItemStackToInventory
player.inventory.setItemStack
player.inventory.setInventorySlotContents
player.inventoryContainer.putStackInSlot

Those are the methods I tried.... It still is just on the client.

If I helped then you help, hit that Thank You button or Applaud.

Posted

    public boolean onItemUse(ItemStack itemstack, EntityPlayer player, World world, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
	for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
	{
		ItemStack stack = player.inventory.getStackInSlot(i);
		if(stack != null)
		{
			if(stack.getItem() != null && stack.getItem() == ModItems.EnrichedBoneSword)
			{

			}
		}
    	
    	if(world.isRemote)
    	{
    		if(player.isSneaking())
    		{
            	if(this.Enchant == 0)
            	{
            		this.Enchant = 1;
            		player.inventory.setInventorySlotContents(i, new ItemStack(ModItems.EnrichedBoneSword));
            	}
            	
            	else if(this.Enchant == 1)
            	{
            		this.Enchant = 2;
            		player.inventory.setItemStack(new ItemStack(ModItems.EnrichedBoneSword));
            	}
            	
            	else if(this.Enchant == 2)
            	{
            		this.Enchant = 3;
            		player.inventory.addItemStackToInventory(new ItemStack(ModItems.EnrichedBoneSword));
            	}
            	
            	else if(this.Enchant == 3)
            	{
            		this.Enchant = 1;
            		player.inventoryContainer.putStackInSlot(i, new ItemStack(ModItems.EnrichedBoneSword));
            	}
    		}
    	}
	}
        return false;
    }

I tried them sort of like that but I was spawning them in random slots because I didn't have the thing where it checks the slot done. I was add dirt, it looked like it was in my inv but it was a ghost item...

If I helped then you help, hit that Thank You button or Applaud.

Posted

Also, setting "this.field = 1" will set it for ALL Items of that type, so every single player who has that item will have their item changed.

 

You need to put your code within the "if (stack != null && stack.getItem() == YourMod.yourItem)" statement, otherwise you are running that code for every single slot in the inventory without checking.

 

FYI, if a stack is not null, stack.getItem() will never be null.

 

Finally, be sure to break out of the loop after setting the inventory stack.

Posted

Thanks diesieben07!!

 

Also, setting "this.field = 1" will set it for ALL Items of that type, so every single player who has that item will have their item changed.

 

You need to put your code within the "if (stack != null && stack.getItem() == YourMod.yourItem)" statement, otherwise you are running that code for every single slot in the inventory without checking.

 

FYI, if a stack is not null, stack.getItem() will never be null.

 

Finally, be sure to break out of the loop after setting the inventory stack.

It still sets all the items in my inventory to the same as the one I pressed...

If I helped then you help, hit that Thank You button or Applaud.

Posted

Sorry I forgot.

    public boolean onItemUse(ItemStack itemstack, EntityPlayer player, World world, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
	for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
	{
		ItemStack stack = player.inventory.getStackInSlot(i);

		if(stack != null)
		{
			if(stack != null && stack.getItem() == ModItems.EnrichedBoneSword)
			{
		    	if(!world.isRemote)
		    	{
		    		if(player.isSneaking())
		    		{
		            	if(this.Enchant == 0)
		            	{
		            		this.Enchant = 1;
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(this.Enchant == 1)
		            	{
		            		this.Enchant = 2;
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(this.Enchant == 2)
		            	{
		            		this.Enchant = 3;
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(this.Enchant == 3)
		            	{
		            		this.Enchant = 1;
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		    		}
				}
			}
    	}
	}
        return false;
    }

It changes all of them :(

Would I have to set the metadata and all, because it even changes the one in the creative tab.

If I set the metadata will it keep the value if I leave and rejoin the world?

If I helped then you help, hit that Thank You button or Applaud.

Posted

I thought I just told you that "if(this.Enchant == 0)" kind of stuff will not work... you need to use the ItemStack NBT, not a local field in your Item class.

 

Items are declared as static instances, meaning that ALL class fields (e.g. private int enchant, etc.) are shared amongst ALL instances of that Item.

 

Here you go: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Posted

Ok. I changed the: "public static int Enchant" to "public int Enchant". Now is changes all of the values but it only sets to one in the hand to a diamond. Now just to learn the NBT stuff. O.o

If I helped then you help, hit that Thank You button or Applaud.

Posted

If your item has subtypes or is damageable, then you will not have the metadata (i.e. stack damage) option.

 

I didn't mean that removing the "static" modifier from "public static int enchant" would solve your problems: Items are all declared statically, so all Item class fields are also effectively static no matter what you do because there is only one instance of the Item in the game.

 

Unless you want all of your enchanted items to always have the same enchantment, not just for you but for everyone else that has one, then you cannot store any data like that in a class field. Use the stack's damage if you can, or NBT if you cannot.

Posted

I tried the NBT thing and I don't think it really worked. :(

 

public class EnrichedBoneSword extends ItemSword
{
public NBTBase Enchant;

public EnrichedBoneSword(ToolMaterial material)
{
	super(material);
}

    public boolean onItemUse(ItemStack itemstack, EntityPlayer player, World world, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
	for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
	{
		ItemStack stack = player.inventory.getStackInSlot(i);

		if(stack != null)
		{
			if(stack != null && stack.getItem() == ModItems.EnrichedBoneSword)
			{
		    	if(!world.isRemote)
		    	{
		    		if(player.isSneaking())
		    		{
		            	if(itemstack.getTagCompound().getInteger("Enchant") == 0)
		            	{
		            		itemstack.setTagInfo("1", Enchant);
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(itemstack.getTagCompound().getInteger("Enchant") == 1)
		            	{
		            		itemstack.setTagInfo("2", Enchant);
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(itemstack.getTagCompound().getInteger("Enchant") == 2)
		            	{
		            		itemstack.setTagInfo("3", Enchant);
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		            	
		            	else if(itemstack.getTagCompound().getInteger("Enchant") == 3)
		            	{
		            		itemstack.setTagInfo("1", Enchant);
		            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
		            	}
		    		}
				}
			}
    	}
	}
        return false;
    }
    
public void addInformation(ItemStack itemstack, EntityPlayer player, List list, boolean par4)
{
	if(itemstack.getTagCompound().getInteger("Enchant") == 1)
	{
		list.add("Sharpness");
	}
	else if(itemstack.getTagCompound().getInteger("Enchant") == 2)
	{
		list.add("Smite");
	}
	else if(itemstack.getTagCompound().getInteger("Enchant") == 3)
	{
		list.add("Bane of Arthropods");
	}
}
}

 

---- Minecraft Crash Report ----
// This is a token for 1 free hug. Redeem at your nearest Mojangsta: [~~HUG~~]

Time: 29.03.14 15:44
Description: Rendering screen

java.lang.NullPointerException: Rendering screen
at bulkyzanka.electro.mod.items.tools.EnrichedBoneSword.addInformation(EnrichedBoneSword.java:80)
at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:633)
at net.minecraft.client.gui.GuiScreen.renderToolTip(GuiScreen.java:136)
at net.minecraft.client.gui.inventory.GuiContainerCreative.renderToolTip(GuiContainerCreative.java:774)
at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:204)
at net.minecraft.client.renderer.InventoryEffectRenderer.drawScreen(InventoryEffectRenderer.java:44)
at net.minecraft.client.gui.inventory.GuiContainerCreative.drawScreen(GuiContainerCreative.java:678)
at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1209)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1066)
at net.minecraft.client.Minecraft.run(Minecraft.java:954)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
at bulkyzanka.electro.mod.items.tools.EnrichedBoneSword.addInformation(EnrichedBoneSword.java:80)
at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:633)
at net.minecraft.client.gui.GuiScreen.renderToolTip(GuiScreen.java:136)
at net.minecraft.client.gui.inventory.GuiContainerCreative.renderToolTip(GuiContainerCreative.java:774)
at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:204)
at net.minecraft.client.renderer.InventoryEffectRenderer.drawScreen(InventoryEffectRenderer.java:44)
at net.minecraft.client.gui.inventory.GuiContainerCreative.drawScreen(GuiContainerCreative.java:678)

-- Screen render details --
Details:
Screen name: net.minecraft.client.gui.inventory.GuiContainerCreative
Mouse location: Scaled: (274, 232). Absolute: (549, 577)
Screen size: Scaled: (456, 521). Absolute: (911, 1042). Scale factor of 2

-- Affected level --
Details:
Level name: MpServer
All players: 1 total; [EntityClientPlayerMP['Player328'/258, l='MpServer', x=126,98, y=71,62, z=-164,05]]
Chunk stats: MultiplayerChunkCache: 225, 225
Level seed: 0
Level generator: ID 00 - default, ver 1. Features enabled: false
Level generator options: 
Level spawn location: World: (148,64,-152), Chunk: (at 4,4,8 in 9,-10; contains blocks 144,0,-160 to 159,255,-145), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)
Level time: 34073 game time, 6707 day time
Level dimension: 0
Level storage version: 0x00000 - Unknown?
Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
Forced entities: 86 total; [EntityCreeper['Creeper'/137, l='MpServer', x=148,59, y=37,00, z=-203,00], EntitySheep['Sheep'/136, l='MpServer', x=151,38, y=65,49, z=-206,46], EntityCow['Cow'/139, l='MpServer', x=154,34, y=67,00, z=-183,38], EntityBat['Bat'/138, l='MpServer', x=146,25, y=32,00, z=-179,80], EntityZombie['Zombie'/131, l='MpServer', x=154,50, y=35,00, z=-230,06], EntityBat['Bat'/133, l='MpServer', x=147,25, y=20,10, z=-210,00], EntityBat['Bat'/132, l='MpServer', x=147,25, y=20,10, z=-208,25], EntitySkeleton['Skeleton'/135, l='MpServer', x=162,69, y=34,93, z=-212,50], EntityBat['Bat'/134, l='MpServer', x=147,25, y=20,10, z=-208,25], EntityCreeper['Creeper'/152, l='MpServer', x=163,50, y=38,00, z=-207,50], EntityCreeper['Creeper'/153, l='MpServer', x=169,73, y=39,00, z=-205,75], EntityClientPlayerMP['Player328'/258, l='MpServer', x=126,98, y=71,62, z=-164,05], EntityBat['Bat'/154, l='MpServer', x=163,25, y=17,10, z=-107,16], EntitySlime['Slime'/20, l='MpServer', x=47,69, y=54,00, z=-131,31], EntitySheep['Sheep'/146, l='MpServer', x=172,34, y=72,00, z=-240,47], EntityZombie['Zombie'/147, l='MpServer', x=165,06, y=32,00, z=-219,41], EntityZombie['Zombie'/148, l='MpServer', x=171,22, y=52,00, z=-215,16], EntitySlime['Slime'/149, l='MpServer', x=166,69, y=24,00, z=-200,31], EntityZombie['Zombie'/150, l='MpServer', x=161,31, y=18,00, z=-200,98], EntityBat['Bat'/151, l='MpServer', x=171,21, y=39,86, z=-204,93], EntitySkeleton['Skeleton'/171, l='MpServer', x=175,72, y=47,00, z=-229,25], EntitySkeleton['Skeleton'/170, l='MpServer', x=180,06, y=47,00, z=-228,41], EntityCreeper['Creeper'/35, l='MpServer', x=57,03, y=37,00, z=-225,06], EntitySpider['Spider'/169, l='MpServer', x=188,50, y=11,00, z=-231,50], EntityCreeper['Creeper'/38, l='MpServer', x=63,50, y=44,00, z=-197,50], EntityZombie['Zombie'/175, l='MpServer', x=170,31, y=51,85, z=-206,47], EntityBat['Bat'/174, l='MpServer', x=174,50, y=38,68, z=-210,57], EntitySheep['Sheep'/36, l='MpServer', x=54,63, y=64,00, z=-228,91], EntityCreeper['Creeper'/173, l='MpServer', x=182,50, y=28,00, z=-215,50], EntitySheep['Sheep'/37, l='MpServer', x=49,56, y=64,00, z=-234,56], EntityCreeper['Creeper'/172, l='MpServer', x=186,41, y=28,00, z=-212,00], EntitySkeleton['Skeleton'/46, l='MpServer', x=55,16, y=20,00, z=-130,78], EntityCreeper['Creeper'/47, l='MpServer', x=64,03, y=31,00, z=-133,44], EntitySquid['Squid'/44, l='MpServer', x=63,81, y=61,34, z=-150,25], EntitySquid['Squid'/45, l='MpServer', x=68,66, y=59,09, z=-145,75], EntitySheep['Sheep'/186, l='MpServer', x=201,44, y=66,00, z=-174,66], EntityBat['Bat'/51, l='MpServer', x=50,72, y=23,72, z=-116,36], EntityCow['Cow'/187, l='MpServer', x=192,81, y=64,00, z=-163,22], EntitySpider['Spider'/50, l='MpServer', x=60,78, y=22,00, z=-121,00], EntitySkeleton['Skeleton'/184, l='MpServer', x=201,50, y=35,00, z=-195,50], EntityBat['Bat'/49, l='MpServer', x=52,51, y=25,17, z=-114,13], EntityZombie['Zombie'/185, l='MpServer', x=187,51, y=27,69, z=-185,58], EntitySkeleton['Skeleton'/48, l='MpServer', x=54,50, y=21,00, z=-123,91], EntityBat['Bat'/55, l='MpServer', x=53,54, y=24,20, z=-112,58], EntityBat['Bat'/54, l='MpServer', x=51,48, y=25,61, z=-115,30], EntityZombie['Zombie'/53, l='MpServer', x=54,67, y=31,00, z=-129,54], EntitySkeleton['Skeleton'/52, l='MpServer', x=63,25, y=33,00, z=-119,16], EntityCow['Cow'/178, l='MpServer', x=183,53, y=63,00, z=-179,75], EntityCow['Cow'/179, l='MpServer', x=186,53, y=64,00, z=-177,66], EntityZombie['Zombie'/176, l='MpServer', x=190,50, y=49,00, z=-206,50], EntityZombie['Zombie'/177, l='MpServer', x=188,50, y=49,00, z=-204,50], EntitySpider['Spider'/182, l='MpServer', x=193,94, y=8,45, z=-218,50], EntityZombie['Zombie'/183, l='MpServer', x=206,50, y=25,00, z=-194,50], EntityCreeper['Creeper'/181, l='MpServer', x=205,84, y=27,00, z=-225,00], EntityCreeper['Creeper'/69, l='MpServer', x=60,22, y=36,00, z=-230,38], EntityCreeper['Creeper'/70, l='MpServer', x=59,44, y=36,00, z=-230,69], EntityZombie['Zombie'/71, l='MpServer', x=65,50, y=35,00, z=-231,50], EntitySquid['Squid'/76, l='MpServer', x=69,66, y=59,00, z=-139,09], EntityCreeper['Creeper'/78, l='MpServer', x=72,50, y=16,00, z=-122,50], EntityBat['Bat'/79, l='MpServer', x=76,56, y=19,10, z=-117,25], EntityCreeper['Creeper'/72, l='MpServer', x=67,59, y=44,00, z=-188,97], EntityPig['Pig'/73, l='MpServer', x=74,63, y=62,07, z=-187,53], EntitySquid['Squid'/74, l='MpServer', x=76,09, y=61,00, z=-182,47], EntitySquid['Squid'/75, l='MpServer', x=76,91, y=60,34, z=-151,81], EntityCreeper['Creeper'/81, l='MpServer', x=68,69, y=34,00, z=-126,31], EntityBat['Bat'/80, l='MpServer', x=65,68, y=24,45, z=-111,04], EntityZombie['Zombie'/93, l='MpServer', x=91,50, y=21,00, z=-205,97], EntityPig['Pig'/95, l='MpServer', x=89,25, y=66,00, z=-196,53], EntityPig['Pig'/94, l='MpServer', x=81,53, y=65,00, z=-193,69], EntitySquid['Squid'/100, l='MpServer', x=80,50, y=60,13, z=-155,50], EntitySheep['Sheep'/98, l='MpServer', x=92,09, y=63,00, z=-172,03], EntityCow['Cow'/99, l='MpServer', x=89,50, y=69,00, z=-163,25], EntityPig['Pig'/96, l='MpServer', x=81,31, y=63,00, z=-186,22], EntitySheep['Sheep'/97, l='MpServer', x=85,75, y=63,00, z=-190,41], EntitySheep['Sheep'/106, l='MpServer', x=95,91, y=63,00, z=-150,13], EntityCreeper['Creeper'/105, l='MpServer', x=100,53, y=57,00, z=-179,31], EntityItem['item.item.EnrichedBoneSpade'/115, l='MpServer', x=123,22, y=71,13, z=-174,59], EntityItem['item.item.diamond'/114, l='MpServer', x=117,66, y=65,13, z=-170,75], EntitySheep['Sheep'/113, l='MpServer', x=126,53, y=69,00, z=-175,47], EntityBat['Bat'/112, l='MpServer', x=114,25, y=15,00, z=-195,53], EntityCreeper['Creeper'/126, l='MpServer', x=134,59, y=21,00, z=-96,00], EntitySlime['Slime'/125, l='MpServer', x=134,69, y=58,00, z=-115,45], EntitySheep['Sheep'/124, l='MpServer', x=131,50, y=70,00, z=-178,50], EntitySkeleton['Skeleton'/123, l='MpServer', x=143,56, y=64,00, z=-193,94], EntityZombie['Zombie'/122, l='MpServer', x=142,50, y=39,00, z=-219,50], EntitySkeleton['Skeleton'/120, l='MpServer', x=141,50, y=30,00, z=-242,50]]
Retry entities: 0 total; []
Server brand: fml,forge
Server type: Integrated singleplayer server
Stacktrace:
at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:418)
at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2559)
at net.minecraft.client.Minecraft.run(Minecraft.java:976)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

-- System Details --
Details:
Minecraft Version: 1.7.2
Operating System: Windows 7 (amd64) version 6.1
Java Version: 1.7.0_21, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 116366408 bytes (110 MB) / 532414464 bytes (507 MB) up to 1905197056 bytes (1816 MB)
JVM Flags: 0 total; 
AABB Pool Size: 22323 (1250088 bytes; 1 MB) allocated, 9 (504 bytes; 0 MB) used
IntCache: cache: 15, tcache: 0, allocated: 13, tallocated: 95
FML: MCP v9.01-pre FML v7.2.125.1031 Minecraft Forge 10.12.0.1031 5 mods loaded, 5 mods active
mcp{8.09} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{7.2.125.1031} [Forge Mod Loader] (forgeSrc-1.7.2-10.12.0.1031.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{10.12.0.1031} [Minecraft Forge] (forgeSrc-1.7.2-10.12.0.1031.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
examplemod{1.0} [Example Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
electro{Alpha 0.1A} [Electrocuted] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Launched Version: 1.6
LWJGL: 2.9.0
OpenGL: AMD Radeon HD 6800 Series GL version 4.2.12422 Compatibility Profile Context 13.152.0.0, ATI Technologies Inc.
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Resource Packs: []
Current Language: ~~ERROR~~ NullPointerException: null
Profiler Position: N/A (disabled)
Vec3 Pool Size: 3001 (168056 bytes; 0 MB) allocated, 84 (4704 bytes; 0 MB) used
Anisotropic Filtering: Off (1)

When ever even try to roll my mouse over the sword, it crashes. I pretty sure that I messed up with the NBT thing so whenever its trying to add a tooltip, it crashes.

If I helped then you help, hit that Thank You button or Applaud.

Posted

If your item has subtypes or is damageable, then you will not have the metadata (i.e. stack damage) option.

Not quite true. Item damage is a short, meaning 2 full bytes. You can squeeze a lot of information in 2 bytes :)

Lol - of course you'd point that out xD I usually avoid mentioning bit operators unless in the context of Block metadata, just cause... you know. :P

 

@OP It crashes because you are not null-checking; stack NBT will be null until you set a new NBTTagCompound. Always check if the tag is not null before using it.

Posted

I tried this but I crashed when I tried to right click.

 

				            	if(itemstack.getTagCompound() != null && itemstack.getTagCompound().getInteger("Enchant") == 0)
			            	{
			            		itemstack.setTagInfo("1", Enchant);
			            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
			            	}

 

---- Minecraft Crash Report ----
// I blame Dinnerbone.

Time: 29.03.14 17:56
Description: Unexpected error

java.lang.NullPointerException: Unexpected error
at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:462)
at net.minecraft.item.ItemStack.copy(ItemStack.java:403)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.<init>(C08PacketPlayerBlockPlacement.java:33)
at net.minecraft.client.multiplayer.PlayerControllerMP.sendUseItem(PlayerControllerMP.java:450)
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1568)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2053)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1038)
at net.minecraft.client.Minecraft.run(Minecraft.java:954)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:462)
at net.minecraft.item.ItemStack.copy(ItemStack.java:403)
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.<init>(C08PacketPlayerBlockPlacement.java:33)
at net.minecraft.client.multiplayer.PlayerControllerMP.sendUseItem(PlayerControllerMP.java:450)
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1568)

-- Affected level --
Details:
Level name: MpServer
All players: 1 total; [EntityClientPlayerMP['Player643'/233, l='MpServer', x=137,30, y=76,54, z=-163,77]]
Chunk stats: MultiplayerChunkCache: 240, 240
Level seed: 0
Level generator: ID 00 - default, ver 1. Features enabled: false
Level generator options: 
Level spawn location: World: (148,64,-152), Chunk: (at 4,4,8 in 9,-10; contains blocks 144,0,-160 to 159,255,-145), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)
Level time: 38136 game time, 10770 day time
Level dimension: 0
Level storage version: 0x00000 - Unknown?
Level weather: Rain time: 0 (now: true), thunder time: 0 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
Forced entities: 80 total; [EntityCreeper['Creeper'/137, l='MpServer', x=162,50, y=30,00, z=-215,50], EntitySkeleton['Skeleton'/136, l='MpServer', x=177,06, y=47,00, z=-228,53], EntitySkeleton['Skeleton'/139, l='MpServer', x=160,94, y=19,00, z=-203,53], EntitySlime['Slime'/138, l='MpServer', x=166,69, y=25,00, z=-200,31], EntityCreeper['Creeper'/141, l='MpServer', x=173,50, y=20,00, z=-146,50], EntityBat['Bat'/140, l='MpServer', x=161,75, y=53,10, z=-204,25], EntitySkeleton['Skeleton'/143, l='MpServer', x=164,50, y=26,00, z=-93,50], EntityZombie['Zombie'/142, l='MpServer', x=160,91, y=40,00, z=-142,72], EntitySheep['Sheep'/135, l='MpServer', x=172,34, y=72,00, z=-240,47], EntityZombie['Zombie'/154, l='MpServer', x=177,03, y=38,00, z=-209,00], EntityZombie['Zombie'/155, l='MpServer', x=169,78, y=35,00, z=-215,13], EntityCreeper['Creeper'/156, l='MpServer', x=185,38, y=40,00, z=-217,03], EntityBat['Bat'/157, l='MpServer', x=181,44, y=50,10, z=-207,25], EntityCow['Cow'/158, l='MpServer', x=186,53, y=64,00, z=-177,66], EntityCow['Cow'/159, l='MpServer', x=188,91, y=64,00, z=-173,09], EntityZombie['Zombie'/144, l='MpServer', x=172,38, y=44,00, z=-84,66], EntitySkeleton['Skeleton'/145, l='MpServer', x=171,22, y=44,00, z=-86,31], EntityCreeper['Creeper'/170, l='MpServer', x=204,78, y=48,00, z=-127,16], EntityCow['Cow'/169, l='MpServer', x=185,97, y=64,00, z=-159,94], EntitySheep['Sheep'/168, l='MpServer', x=207,78, y=66,00, z=-174,41], EntityChicken['Chicken'/175, l='MpServer', x=209,44, y=68,00, z=-213,59], EntityCreeper['Creeper'/174, l='MpServer', x=209,09, y=12,00, z=-230,25], EntitySpider['Spider'/39, l='MpServer', x=63,25, y=52,39, z=-99,28], EntitySkeleton['Skeleton'/37, l='MpServer', x=62,50, y=21,00, z=-120,88], EntityCreeper['Creeper'/160, l='MpServer', x=187,30, y=19,20, z=-103,70], EntityZombie['Zombie'/167, l='MpServer', x=201,94, y=38,00, z=-172,44], EntityZombie['Zombie'/166, l='MpServer', x=200,84, y=19,00, z=-186,19], EntitySkeleton['Skeleton'/165, l='MpServer', x=199,94, y=36,00, z=-195,50], EntityZombie['Zombie'/164, l='MpServer', x=193,25, y=46,00, z=-214,25], EntityPig['Pig'/50, l='MpServer', x=70,94, y=62,05, z=-187,91], EntitySkeleton['Skeleton'/49, l='MpServer', x=68,31, y=47,00, z=-229,50], EntityBat['Bat'/55, l='MpServer', x=65,89, y=37,20, z=-142,44], EntityBat['Bat'/54, l='MpServer', x=58,59, y=26,85, z=-147,69], EntityBat['Bat'/53, l='MpServer', x=65,88, y=22,42, z=-114,91], EntityZombie['Zombie'/52, l='MpServer', x=72,44, y=23,00, z=-136,88], EntityZombie['Zombie'/178, l='MpServer', x=215,50, y=46,00, z=-181,50], EntityBat['Bat'/59, l='MpServer', x=68,25, y=42,00, z=-108,25], EntitySpider['Spider'/179, l='MpServer', x=215,28, y=46,00, z=-163,91], EntityBat['Bat'/58, l='MpServer', x=67,41, y=21,79, z=-122,56], EntityBat['Bat'/57, l='MpServer', x=77,25, y=13,39, z=-117,75], EntitySpider['Spider'/177, l='MpServer', x=220,81, y=43,00, z=-197,56], EntityCreeper['Creeper'/56, l='MpServer', x=77,00, y=20,00, z=-116,50], EntityZombie['Zombie'/182, l='MpServer', x=210,84, y=13,00, z=-128,31], EntityZombie['Zombie'/183, l='MpServer', x=218,50, y=24,00, z=-138,03], EntityZombie['Zombie'/180, l='MpServer', x=215,41, y=47,00, z=-159,00], EntityZombie['Zombie'/61, l='MpServer', x=79,50, y=13,00, z=-85,50], EntitySkeleton['Skeleton'/181, l='MpServer', x=215,06, y=14,00, z=-132,47], EntityZombie['Zombie'/60, l='MpServer', x=75,41, y=13,00, z=-94,00], EntityPig['Pig'/69, l='MpServer', x=81,53, y=65,00, z=-193,69], EntityPig['Pig'/70, l='MpServer', x=81,06, y=65,00, z=-195,13], EntityPig['Pig'/71, l='MpServer', x=77,13, y=63,00, z=-188,44], EntitySheep['Sheep'/72, l='MpServer', x=93,78, y=64,00, z=-189,25], EntitySpider['Spider'/73, l='MpServer', x=84,56, y=27,00, z=-153,72], EntityCow['Cow'/74, l='MpServer', x=89,47, y=68,00, z=-158,25], EntitySheep['Sheep'/81, l='MpServer', x=96,06, y=64,00, z=-172,97], EntitySheep['Sheep'/83, l='MpServer', x=99,81, y=63,00, z=-150,34], EntitySkeleton['Skeleton'/82, l='MpServer', x=104,44, y=23,00, z=-157,16], EntitySheep['Sheep'/89, l='MpServer', x=126,53, y=69,00, z=-175,47], EntitySkeleton['Skeleton'/88, l='MpServer', x=119,50, y=21,00, z=-169,50], EntityZombie['Zombie'/102, l='MpServer', x=139,66, y=34,00, z=-195,25], EntityBat['Bat'/103, l='MpServer', x=138,41, y=35,00, z=-190,97], EntityWitch['Witch'/100, l='MpServer', x=130,50, y=42,00, z=-230,09], EntityCreeper['Creeper'/101, l='MpServer', x=135,50, y=45,00, z=-219,50], EntityZombie['Zombie'/99, l='MpServer', x=132,66, y=38,00, z=-224,94], EntityClientPlayerMP['Player643'/233, l='MpServer', x=137,30, y=76,54, z=-163,77], EntitySlime['Slime'/108, l='MpServer', x=134,69, y=58,00, z=-116,26], EntityCow['Cow'/106, l='MpServer', x=140,44, y=71,00, z=-173,47], EntityBat['Bat'/107, l='MpServer', x=140,84, y=39,17, z=-149,53], EntitySkeleton['Skeleton'/104, l='MpServer', x=137,56, y=63,00, z=-184,84], EntitySheep['Sheep'/105, l='MpServer', x=131,50, y=70,00, z=-178,50], EntitySheep['Sheep'/119, l='MpServer', x=149,41, y=66,00, z=-212,53], EntitySpider['Spider'/118, l='MpServer', x=159,44, y=28,00, z=-215,50], EntityBat['Bat'/117, l='MpServer', x=150,66, y=15,10, z=-220,59], EntityZombie['Zombie'/115, l='MpServer', x=154,44, y=32,00, z=-238,91], EntitySkeleton['Skeleton'/125, l='MpServer', x=148,50, y=19,00, z=-89,50], EntityZombie['Zombie'/124, l='MpServer', x=159,50, y=41,00, z=-132,50], EntityZombie['Zombie'/123, l='MpServer', x=159,88, y=40,00, z=-144,44], EntityZombie['Zombie'/122, l='MpServer', x=151,06, y=33,00, z=-186,53], EntitySkeleton['Skeleton'/121, l='MpServer', x=147,16, y=32,00, z=-179,59], EntityCreeper['Creeper'/120, l='MpServer', x=149,38, y=19,00, z=-181,03]]
Retry entities: 0 total; []
Server brand: fml,forge
Server type: Integrated singleplayer server
Stacktrace:
at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:418)
at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2559)
at net.minecraft.client.Minecraft.run(Minecraft.java:983)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

-- System Details --
Details:
Minecraft Version: 1.7.2
Operating System: Windows 7 (amd64) version 6.1
Java Version: 1.7.0_21, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 284170264 bytes (271 MB) / 598212608 bytes (570 MB) up to 1905197056 bytes (1816 MB)
JVM Flags: 0 total; 
AABB Pool Size: 28749 (1609944 bytes; 1 MB) allocated, 2 (112 bytes; 0 MB) used
IntCache: cache: 15, tcache: 0, allocated: 13, tallocated: 95
FML: MCP v9.01-pre FML v7.2.125.1031 Minecraft Forge 10.12.0.1031 5 mods loaded, 5 mods active
mcp{8.09} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{7.2.125.1031} [Forge Mod Loader] (forgeSrc-1.7.2-10.12.0.1031.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{10.12.0.1031} [Minecraft Forge] (forgeSrc-1.7.2-10.12.0.1031.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
examplemod{1.0} [Example Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
electro{Alpha 0.1A} [Electrocuted] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Launched Version: 1.6
LWJGL: 2.9.0
OpenGL: AMD Radeon HD 6800 Series GL version 4.2.12422 Compatibility Profile Context 13.152.0.0, ATI Technologies Inc.
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Resource Packs: []
Current Language: ~~ERROR~~ NullPointerException: null
Profiler Position: N/A (disabled)
Vec3 Pool Size: 1768 (99008 bytes; 0 MB) allocated, 18 (1008 bytes; 0 MB) used
Anisotropic Filtering: Off (1)

 

Then I tried this and it didn't even let me look at it.

			    			if(itemstack.getTagCompound() != null)
		    			{
			            	if(itemstack.getTagCompound().getInteger("Enchant") == 0)
			            	{
			            		itemstack.setTagInfo("1", Enchant);
			            		player.inventory.setInventorySlotContents(i, new ItemStack(Items.diamond));
			            	}

If I helped then you help, hit that Thank You button or Applaud.

Posted

But I set in to 0 at the begining

    	itemstack.setTagInfo("0", Enchant);

If I helped then you help, hit that Thank You button or Applaud.

Posted

My question is: is this the correct way to check if it's not null?

 

itemstack.getTagCompound(). != null && itemstack.getTagCompound().getInteger("Enchant") == 0

If I helped then you help, hit that Thank You button or Applaud.

Posted

You are setting the value incorrectly, use itemstack.getTagCompound().setInteger(String, int). ItemStack#setTagInfo adds an NBT tag to the item's tag - you can't just use it for any type of data.

 

Also, tags are stored with a String identifier, so "0" is probably not what you think it is. It should be: setInteger("enchant", 0). You should really look more closely at vanilla code examples using NBT, or read the wiki tutorial on it.

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.