Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

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

}

I dont think you are doing this right. You are creating a new tag each tick which will overwrite the previous data.

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

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

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.

  • 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?

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.