Jump to content

Recommended Posts

Posted

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?

Posted

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;
    }

}

Posted

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);
	}
}

Posted

Yes, NBT is just how this works.

@Cerandior: TileEntities are for Blocks. This is an item.

For the updating procedure, do i just call onUpdate in my TileEntity then or do i have to make that out of the itemStack somehow so it does not forcecrash the game?

Posted

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.

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.