Jump to content

Change item damage to represent value


Roboguy99

Recommended Posts

I'm trying to create an item which, when you right click on a specific block (gas blocks), the block gets removed and the item damage value changes accordingly (like how powered items work). I've got a system which slightly works, except the damage bar goes the wrong way (starts full and empties as you collect gas) and when you fill it with gas the game crashes, giving this obscure error:

 

 

 

---- Minecraft Crash Report ----

// There are four lights!

 

Time: 13/07/14 10:35

Description: Initializing game

 

java.lang.NullPointerException: Initializing game

at foodTech.blocks.CreateBlocks.setBlockNames(CreateBlocks.java:33)

at foodTech.blocks.CreateBlocks.<init>(CreateBlocks.java:24)

at foodTech.Main.preInit(Main.java:39)

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 cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:513)

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 com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:208)

at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:187)

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 com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:118)

at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:513)

at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:239)

at net.minecraft.client.Minecraft.startGame(Minecraft.java:532)

at net.minecraft.client.Minecraft.run(Minecraft.java:941)

at net.minecraft.client.main.Main.main(Main.java:164)

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 foodTech.blocks.CreateBlocks.setBlockNames(CreateBlocks.java:33)

at foodTech.blocks.CreateBlocks.<init>(CreateBlocks.java:24)

at foodTech.Main.preInit(Main.java:39)

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 cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:513)

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 com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:208)

at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:187)

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 com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:118)

at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:513)

at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:239)

at net.minecraft.client.Minecraft.startGame(Minecraft.java:532)

 

-- Initialization --

Details:

Stacktrace:

at net.minecraft.client.Minecraft.run(Minecraft.java:941)

at net.minecraft.client.main.Main.main(Main.java:164)

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.10

Operating System: Windows 8.1 (amd64) version 6.3

Java Version: 1.7.0_55, Oracle Corporation

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

Memory: 94008344 bytes (89 MB) / 326631424 bytes (311 MB) up to 1908932608 bytes (1820 MB)

JVM Flags: 0 total;

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

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

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

Roboguy99FoodTech{0.0.1} [Food Tech] (bin) Unloaded->Constructed->Errored

Launched Version: 1.7.10

LWJGL: 2.9.1

OpenGL: AMD Radeon R9 200 Series GL version 4.4.12874 Compatibility Profile Context 14.100.0.0, ATI Technologies Inc.

GL Caps: Using GL 1.3 multitexturing.

Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.

Anisotropic filtering is supported and maximum anisotropy is 16.

Shaders are available because OpenGL 2.1 is supported.

 

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: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

Anisotropic Filtering: Off (1)

 

 

 

My item class is as follows:

 

 

package roboguy99.foodTech.common.item.tool;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import roboguy99.foodTech.FoodTech;
import cpw.mods.fml.common.registry.GameRegistry;

public class ItemToolGas extends Item
{
private int storedGas = 0;
private static final int MAX_GAS = 3000;

public ItemToolGas(String name)
{
	this.setMaxStackSize(1);
	this.setUnlocalizedName(name);
	this.setCreativeTab(FoodTech.TAB_FOODTECH);
	this.setMaxDamage(MAX_GAS);
	GameRegistry.registerItem(this, name);
}

public void collectGas(int gas, ItemStack itemStack, EntityPlayer player)
{
	this.incrementGas(gas);
	itemStack.setItemDamage(getStoredGas());
}

public int getStoredGas()
{
	return this.storedGas;
}

private void setStoredGas(int gas)
{
	this.storedGas = gas;
}

private void incrementGas(int gas)
{
	this.setStoredGas(this.getStoredGas() + gas);
}

public int getMaxGas()
{
	return MAX_GAS;
}

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
	FoodTech.print(this.storedGas);
	return null;
}

/**
     * Determines if the durability bar should be rendered for this item.
     * Defaults to vanilla stack.isDamaged behavior.
     * But modders can use this for any data they wish.
     * 
     * @param stack The current Item Stack
     * @return True if it should render the 'durability' bar.
     */
    public boolean showDurabilityBar(ItemStack stack)
    {
        return true;
    }

    /**
     * Queries the percentage of the 'Durability' bar that should be drawn.
     * 
     * @param stack The current ItemStack
     * @return 1.0 for 100% 0 for 0%
     */
    public double getDurabilityForDisplay(ItemStack stack)
    {
        return (double)this.getStoredGas() / this.getMaxGas();
    }
}

 

 

 

And here's the block class:

 

 

package roboguy99.foodTech.common.block.worldgen;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import roboguy99.foodTech.common.block.prefab.BaseBlock;
import roboguy99.foodTech.common.item.Items;
import roboguy99.foodTech.common.item.tool.ItemToolGas;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

/**
* Worldgen block which can be contained and used for gas machines
*/
public class BlockGas extends BaseBlock
{
private static final int GAS_PER_BLOCK = 1000;

public BlockGas(Material material, String name)
{
	super(material, name);
	this.setBlockUnbreakable();
	this.setLightOpacity(1);
	this.setBlockTextureName("roboguy99:blockGas");
}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) 
{	
	if(player.inventory.getCurrentItem() != null)
    {	
		if(player.inventory.getCurrentItem().getItem() == Items.itemToolGas )
    	{
			ItemStack itemToolGasStack = player.inventory.getCurrentItem();
			ItemToolGas itemToolGas = (ItemToolGas) itemToolGasStack.getItem();

			if(itemToolGas.getStoredGas() <= itemToolGas.getMaxDamage() + GAS_PER_BLOCK)
			{
				world.setBlock(x, y, z, Blocks.air);
				itemToolGas.collectGas(GAS_PER_BLOCK, itemToolGasStack, player);
				return true;
			}
    	}
    }
	return false;
}

@SideOnly(Side.CLIENT)
    public int getRenderBlockPass()
    {
        return 1;
    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }
    
    protected boolean canSilkHarvest()
    {
        return false;
    }
    
    public boolean isOpaqueCube()
    {
    	return false;
    }
    
    @Override
    @SideOnly(Side.CLIENT)
    public boolean shouldSideBeRendered (IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
    {
        Block i1 = par1IBlockAccess.getBlock(par2, par3, par4);
        return i1 == (Block) this ? false : super.shouldSideBeRendered(par1IBlockAccess, par2, par3, par4, par5);
    }
    
    /**
     * Remove the block collision box
     */
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int i, int j, int k)
    {
        return null;
    }
}

 

 

 

Does anybody know how I could do this? Thanks.

I have no idea what I'm doing.

Link to comment
Share on other sites

The "storedGas" field must not be in your Item class. There will only be one instance of that class. You must always use the damage value. Also don't use "player.damageItem", use setItemDamage on the ItemStack.

Then to make a custom damage bar implementation override showDurabilityBar and getDurabilityForDisplay in your Item.

 

For that crash: It cannot be from filling with gas, it occurs when starting up the game. Show your main mod class.

 

Ok I'll try and fix those points you put. As for the crash, I don't think it's on startup because it occurs when I right click. Here's my main class anyway:

 

 

 

package roboguy99.foodTech;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import roboguy99.foodTech.client.gui.GuiHandler;
import roboguy99.foodTech.common.block.Blocks;
import roboguy99.foodTech.common.event.RegisterEvents;
import roboguy99.foodTech.common.fluid.Fluids;
import roboguy99.foodTech.common.item.Items;
import roboguy99.foodTech.common.tile.RegisterTileEntities;
import roboguy99.foodTech.proxies.CommonProxy;
import roboguy99.foodTech.util.WorldGen;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;


@Mod(modid = FoodTech.modID, version = FoodTech.modVersion, name = FoodTech.name)
public class FoodTech 
{
//Mod data. Fallback if mc-mod.info fails to load.
public static final String modID = "Roboguy99FoodTech";
public static final String modVersion = "0.0.1";
public static final String name = "Food Tech";

public static FoodTech instance;

@SidedProxy(clientSide = "roboguy99.foodTech.proxies.ClientProxy", serverSide = "roboguy99.foodTech.proxies.CommonProxy")
public static CommonProxy proxy;

@EventHandler
public void preInit(FMLPreInitializationEvent event) //Pre-initialisation loading
{					
	print("Loading pre-initialization stage");
	instance = this;
	new WorldGen();
	print("Pre-initialization stage loaded successfully");
}

@EventHandler
public void init(FMLInitializationEvent event) //Initialisation loading
{
	print("Initializing");

	//Loads common content into the game
	new Fluids();
	new Blocks();
	new Items();

	//Loads client content into the game
	new RegisterTileEntities();
	new RegisterEvents();

	proxy.registerProxies(); //Load the proxies

	NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler()); //Load the GUI handler

	print("Initialization finished");
}

public static void print(Object message)
{
	System.out.println("[Food Tech] " + message.toString());
}

 public static final CreativeTabs TAB_FOODTECH = new CreativeTabs("tabFoodTech") 
 {

	public Item getTabIconItem() 
	{
		return Items.itemGrindstone;
	}
 };
}

 

 

 

EDIT: I've updated my code to match what it looks like now, and I think I've changed damageItem() to setItemDamage() and overridden the durability bar correctly. I'm unsure what you meant by "The "storedGas" field must not be in your Item class. There will only be one instance of that class. You must always use the damage value." though.

I have no idea what I'm doing.

Link to comment
Share on other sites

EDIT: I've updated my code to match what it looks like now, and I think I've changed damageItem() to setItemDamage() and overridden the durability bar correctly. I'm unsure what you meant by "The "storedGas" field must not be in your Item class. There will only be one instance of that class. You must always use the damage value." though.

 

This bit:

 

public class ItemToolGas extends Item
{
private int storedGas = 0;

 

That variable is effectively static because ItemToolGas is only ever instantiated once (its a singleton class).  Each copy of it that exists in your in-game inventory is actually an instance of ItemStack which references the shared[/s] singleton.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

EDIT: I've updated my code to match what it looks like now, and I think I've changed damageItem() to setItemDamage() and overridden the durability bar correctly. I'm unsure what you meant by "The "storedGas" field must not be in your Item class. There will only be one instance of that class. You must always use the damage value." though.

 

This bit:

 

public class ItemToolGas extends Item
{
private int storedGas = 0;

 

That variable is effectively static because ItemToolGas is only ever instantiated once (its a singleton class).  Each copy of it that exists in your in-game inventory is actually an instance of ItemStack which references the shared[/s] singleton.

 

Ok so here come the stupid questions from me, starting with how do I fix it? Do I create the field and initiate it in the constructor?

I have no idea what I'm doing.

Link to comment
Share on other sites

No...you use the ItemStack's damage value or you use the ItemStack's NBT.  Those are your options.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

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

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

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

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

×   Your previous content has been restored.   Clear editor

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

Announcements



×
×
  • Create New...

Important Information

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