Jump to content

Change a tool based on NBT data - Solved


iLegendx98

Recommended Posts

Hi, I am trying to make a tool that is upgradable. This is done by sneak + right clicking with it to open a GUI and putting an item in there. I have done all of this. In the inventory class, I have it detect what item is in this one slot container and write some nbt data to the tool based on which one of the 7 "orbs" can be placed in. This is done in this method in the Inventory class:

 

 

String  fireOrb = "Heat Orb", 
		hasteOrb = "Brisk Orb", 
		jumpOrb = "Spring Orb", 
		regenOrb = "Regenerating Orb", 
		resistOrb = "Endurant Orb",
		speedOrb = "Swift Orb",
		strengthOrb = "Power Orb",
		none = "No Upgrade";

@Override
public void markDirty() 
{
	NBTTagCompound upgrades = new NBTTagCompound(); 
	for (int i = 0; i < getSizeInventory(); ++i) 
	{
		ItemStack stack = getStackInSlot(i);
		if (stack != null) 
		{
			if (stack.stackSize == 0) 
			{
				inventory[i] = null;
			} 
			else if (stack.getItem() instanceof ItemOrbFire) 
			{ 	
				invItem.stackTagCompound = new NBTTagCompound();
				invItem.stackTagCompound.setString("upgrade", fireOrb);	
			}
			else if (stack.getItem() instanceof ItemOrbHaste)
			{
				invItem.stackTagCompound.setString("upgrade", hasteOrb);
			}
			else if (stack.getItem() instanceof ItemOrbJump)
			{
				invItem.stackTagCompound.setString("upgrade", jumpOrb);
			}
			else if (stack.getItem() instanceof ItemOrbRegeneration)
			{
				invItem.stackTagCompound.setString("upgrade", regenOrb);
			}
			else if (stack.getItem() instanceof ItemOrbResistance)
			{
				invItem.stackTagCompound.setString("upgrade", resistOrb);
			}
			else if (stack.getItem() instanceof ItemOrbSpeed)
			{
				invItem.stackTagCompound.setString("upgrade", speedOrb);
			}
			else if (stack.getItem() instanceof ItemOrbStrength)
			{
				invItem.stackTagCompound.setString("upgrade", strengthOrb);
			}
			else
			{
				invItem.stackTagCompound.setString("upgrade", none);
			}
		}
		else
		{
			invItem.stackTagCompound.setString("upgrade", none);
		}
	}
	invItem.getTagCompound().setTag("Upgrades", upgrades);
	writeToNBT(invItem.getTagCompound());
}

 

 

The above works fine and in the tool class I have an "addInformation" method that detects what NBT data is stored on the tool and writed a tool tip saying: (Example) Upgrade: Heat Orb. This also works fine. but when do the same for an onUpdate method, it crashes. This is the whole tool class here:

 

 

 

package tools;

import java.util.List;

import items.ItemOrbFire;
import items.ItemShardFire;
import gui.GuiID;
import gui.InventoryAdaptedPick;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.network.handshake.FMLHandshakeMessage.ModIdData;
import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import main.RunicScrolls;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;

public class ToolAdaptedPick extends ItemPickaxe
{	
public ToolAdaptedPick(ToolMaterial mat)
{
	super(mat);
}

@Override
public int getMaxItemUseDuration(ItemStack stack) 
{
	return 1; 
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) 
{
	if (!world.isRemote) 
	{
		if (player.isSneaking())
		{
			player.openGui(RunicScrolls.instance, GuiID.guiIDAdaptedPick, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
		}
		else
		{
			//Nothing Yet
		}
	}
	return stack;
}

public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) 
{	
	if (stack.stackTagCompound != null) 
	{
		String upgrade = stack.stackTagCompound.getString("upgrade");
        list.add("Upgrade: " + upgrade); //NBT Tag works perfect here//
	}
	else
	{
		list.add("Upgrade: No Upgrade");
	}
}

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean isHeld) 
{
	super.onUpdate(stack, world, entity, par4, isHeld);

	String upgrade = stack.stackTagCompound.getString("upgrade"); //This line crashes me!!!!!!//

	EntityPlayer player = (EntityPlayer) entity;
	ItemStack equipped = player.getCurrentEquippedItem();
	if(equipped != null && equipped == stack) 
	{
		if(upgrade == "Heat Orb")
		{
			player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 1, 0, true));
		}

	}
}
}

 

 

 

It crashes when I go into my creative tab and put the tool in my hand.

This is the crash report:

 

 

---- Minecraft Crash Report ----

// Shall we play a game?

 

Time: 8/20/14 9:20 PM

Description: Ticking player

 

java.lang.NullPointerException: Ticking player

at tools.ToolAdaptedPick.onUpdate(ToolAdaptedPick.java:84)

at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:478)

at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:349)

at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:624)

at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:1826)

at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:341)

at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:334)

at net.minecraft.network.NetHandlerPlayServer.processPlayer(NetHandlerPlayServer.java:329)

at net.minecraft.network.play.client.C03PacketPlayer.processPacket(C03PacketPlayer.java:37)

at net.minecraft.network.play.client.C03PacketPlayer$C06PacketPlayerPosLook.processPacket(C03PacketPlayer.java:271)

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

 

 

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

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at tools.ToolAdaptedPick.onUpdate(ToolAdaptedPick.java:84)

at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:478)

at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:349)

at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:624)

at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:1826)

at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:341)

 

-- Player being ticked --

Details:

Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)

Entity ID: 154

Entity Name: ForgeDevName

Entity's Exact location: -2204.51, 4.00, -613.27

Entity's Block location: World: (-2205,4,-614), Chunk: (at 3,0,10 in -138,-39; contains blocks -2208,0,-624 to -2193,255,-609), Region: (-5,-2; contains chunks -160,-64 to -129,-33, blocks -2560,0,-1024 to -2049,255,-513)

Entity's Momentum: 0.00, -0.08, 0.00

Stacktrace:

at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:334)

at net.minecraft.network.NetHandlerPlayServer.processPlayer(NetHandlerPlayServer.java:329)

at net.minecraft.network.play.client.C03PacketPlayer.processPacket(C03PacketPlayer.java:37)

at net.minecraft.network.play.client.C03PacketPlayer$C06PacketPlayerPosLook.processPacket(C03PacketPlayer.java:271)

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

 

-- Ticking connection --

Details:

Connection: net.minecraft.network.NetworkManager@55f6a5dd

Stacktrace:

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

 

-- System Details --

Details:

Minecraft Version: 1.7.10

Operating System: Windows 7 (amd64) version 6.1

Java Version: 1.8.0_05, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 950485352 bytes (906 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

FML: MCP v9.05 FML v7.10.18.1180 Minecraft Forge 10.13.0.1180 4 mods loaded, 4 mods active

mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

FML{7.10.18.1180} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Forge{10.13.0.1180} [Minecraft Forge] (forgeSrc-1.7.10-10.13.0.1180.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

ilegendsrunicscrolls{v2.2 Stable} [iLegends Runic Scrolls] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Profiler Position: N/A (disabled)

Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

Player Count: 1 / 8; [EntityPlayerMP['ForgeDevName'/154, l='New World', x=-2204.51, y=4.00, z=-613.27]]

Type: Integrated Server (map_client.txt)

Is Modded: Definitely; Client brand changed to 'fml,forge'

 

 

 

So my question is...

Whats the issue and how could I resolve it so I can still use the tool as intended? If you need anymore code, i can paste it here for you.

-Thanks

Link to comment
Share on other sites

in your AddInformation you do it right, by making sure the tagCompound is not null, but then you forget to add that in update

 

this should help you out:

 

Short Story make sure yo check that there is a NBTTagCompond on your itemstack before you try anything.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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