Posted September 3, 201510 yr 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?
September 3, 201510 yr A NBTTagCompund should work i think. You can update the values while your machine works.
September 3, 201510 yr Author 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; } }
September 3, 201510 yr I dont think you are doing this right. You are creating a new tag each tick which will overwrite the previous data.
September 3, 201510 yr Author I dont think you are doing this right. You are creating a new tag each tick which will overwrite the previous data. Okay - ill add a check then if the stack holds a tag already.
September 3, 201510 yr Author 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); } }
September 3, 201510 yr Author Oh - true, i tried that once, where else should i store it? IEEP would make sense here i think.
September 3, 201510 yr Author So i just have to get the value change it and saveit back all in the NBT? Okay, strange that minecraft hasn't got a better way yet.
September 3, 201510 yr NBT is good enough. You dont need a better way. Minecraft also created the advanced metadata a.k.a tileentity but that isnt necassary for you in this one.
September 3, 201510 yr Author 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?
September 3, 201510 yr 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.
September 3, 201510 yr Author Nono, its okay, just was wondering about where i had to update it Thanks!
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.