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

So I have an item that I want to make lost a durability every 5 seconds, so every 100 ticks. I've done some looking around about implementing a tick counter into NBT to help track this, but so far I have been unsuccessful in getting this to work(as entire functions that are present in the tutorial are not there any more). 

Override the

onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {}

 method of your Item. It's called every tick the Item is in the players inventory.

 

Define "doesn't work."

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.

2 hours ago, TheSunCat said:

I tried that, and adding a counter, but it refuses to work.

I done that once before and it act perfect:

Spoiler

    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged)
    {
        return false;
    }

    @Override
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
    {
        if(entityIn instanceof EntityLivingBase)
        {
            if(!stack.hasTagCompound())
            {
                stack.setTagCompound(new NBTTagCompound());
            }
            
            if (stack.getTagCompound().hasKey("TickCounter"))
            {
                int tickCounter = stack.getTagCompound().getInteger("TickCounter");
                if (tickCounter <= 0)
                {
                    stack.damageItem(1, ((EntityLivingBase) entityIn));
                    stack.getTagCompound().setInteger("TickCounter", 100);
                }
                else
                {
                    stack.getTagCompound().setInteger("TickCounter", --tickCounter);
                }
            }
            else
            {
                stack.getTagCompound().setInteger("TickCounter", 100);
            }
        }
    }

 

 

 

Edited by Lhykos

  • Author

How do you go about adding the NBT tag to an item? and should i put it in the onCreated meathod?

  • Author

Whenever I load a world with the code above in my item class I crash with this error.

Spoiler

[09:40:09] [Server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking player
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:210) ~[NetworkSystem.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:817) ~[MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:698) ~[MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) ~[IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:547) [MinecraftServer.class:?]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
Caused by: java.lang.NullPointerException
    at com.Frost2779.AWillToLive.Items.ItemFullWill.onUpdate(ItemFullWill.java:43) ~[ItemFullWill.class:?]
    at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:503) ~[ItemStack.class:?]
    at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:390) ~[InventoryPlayer.class:?]
    at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:563) ~[EntityPlayer.class:?]
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2292) ~[EntityLivingBase.class:?]
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:258) ~[EntityPlayer.class:?]
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:367) ~[EntityPlayerMP.class:?]
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:176) ~[NetHandlerPlayServer.class:?]
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:218) ~[NetworkDispatcher$1.class:?]
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:308) ~[NetworkManager.class:?]
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:195) ~[NetworkSystem.class:?]
    ... 5 more

 

Crash is useless without your Item class.

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.

  • Author

My mistake

package com.Frost2779.AWillToLive.Items;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemFullWill extends Item {

    public ItemFullWill(){
        this.setMaxDamage(100);
        this.setMaxStackSize(1);

    }

    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
        return false;
    }

    @Override
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {

        if(entityIn instanceof EntityLivingBase)
        {
            if (stack.hasTagCompound() && stack.getTagCompound().hasKey("TickCounter"))
            {
                int tickCounter = stack.getTagCompound().getInteger("TickCounter");
                if (tickCounter <= 0)
                {
                    stack.damageItem(1, ((EntityLivingBase) entityIn));
                    stack.getTagCompound().setInteger("TickCounter", 100);
                }
                else
                {
                    stack.getTagCompound().setInteger("TickCounter", --tickCounter);
                }
            }
            else
            {
                stack.getTagCompound().setInteger("TickCounter", 100);
            }
        }
    }
}

 

1 hour ago, TheSunCat said:

You need to check if the NBTTag is not null.

Oh, I didn't see that I not checked that. I fix that better in my code.

  • Author
3 minutes ago, Lhykos said:

Oh, I didn't see that I not checked that. I fix that better in my code.

What in your code from earlier should i change?

7 minutes ago, Frost2779 said:

What in your code from earlier should i change?

I already changed it. Just look in the code from my earlier post.

 

 

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.