Jump to content

Creating a batteryitem with energyvalue?


ItsAMysteriousYT

Recommended Posts

I wanna creat a batteryitem that gets empty after a while when in use(equipped in a specific TileEntity OR Item). How can i save the current energyvalue and where do i have to update it? There is a method called onUpdate i think - anything i have to do special in there e.g only update server/clientSide any packethandling stuff or just changing the energyvalue?

Link to comment
Share on other sites

Okay - im doing this right now - is this right?

package itsamysterious.mods.reallifemod.core.items;

import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ItemBattery extends Item{
private float voltage;

public ItemBattery() {
	this.voltage=100;
	setUnlocalizedName("itemBattery");
	GameRegistry.registerItem(this, getUnlocalizedName().substring(5));

}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	NBTTagCompound batteryTag = new NBTTagCompound();
	batteryTag.setFloat("Voltage", voltage);
	stack.writeToNBT(batteryTag);
}

    public boolean updateItemStackNBT(NBTTagCompound nbt)
    {
    	NBTTagCompound batteryTag = nbt.getCompoundTag("BatteryTag");
    	this.voltage=batteryTag.getFloat("Voltage");
        return true;
    }

}

Link to comment
Share on other sites

Better?

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	if(!stack.hasTagCompound()){
		NBTTagCompound batteryTag = new NBTTagCompound();
		batteryTag.setFloat("Voltage", voltage);
		stack.writeToNBT(batteryTag);
	}else
	{
		NBTTagCompound batteryTag = stack.getTagCompound();
		batteryTag.setFloat("Voltage", voltage);
		stack.writeToNBT(batteryTag);
	}
}

Link to comment
Share on other sites

You use nbt to create your voltage. You only want to get rid of your energy while your machine is working. What i mean is that you probably have used a boolean in your machine which will hold the state of your machine. If your machine is working then each tick you want to get out 1 energy unit from your battery. Thats an exmaple. I am sorry that i cant write you a piece of code because i moved to another apartment and my computer is packed. Cant write code from my phone. Try watching an example on guthub.

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.