Jump to content

[1.7.10] modifying NBT in onUSingTIck prevents onPlayerStoppedUsing being called


Recommended Posts

Posted

Is this a bug or is there some reason that writing NBT data changes how right mouse up is handled, or am I doing something different wrong?

Also by adding some simple logging and using @SubscribeEvent that the PlayerUseItemEvent.Stop event isn't always triggered when the use button is released if NBT data is written.

Example code snippet:

public class ItemRFBlaster {
    //some methods ommited for brevity
    @Override
    public void onPlayerStoppedUsing(ItemStack itemstack, World world, EntityPlayer entityPlayer, int itemInUseCount)
    {
        super.onPlayerStoppedUsing(itemstack,world,entityPlayer,itemInUseCount);
        logger.info("Stopped using");
        if(getFiremode(itemstack) == Firemode.Single) {
            tryFire(itemstack, world, entityPlayer, itemInUseCount);
        }
        setCoolingdown(itemstack);
        setPartialTicks(itemstack, 0);
    }

    @Override
    public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
        super.onUsingTick(stack, player, count);
        logger.info("count: {}",count);
        if(player.getEntityWorld().isRemote)
            return;
        assert(player.isUsingItem());
        if(!canFire(stack)) {
            setCoolingdown(stack);
            return;
        }
        if(getFiremode(stack) == Firemode.Auto){
            tryFire(stack,player.getEntityWorld(),player,count);
        }else if(getFiremode(stack) == Firemode.Burst){
            if(tryFire(stack,player.getEntityWorld(),player,count)) {
                incrBurst(stack);
            }
        }
    }

    @Override
    public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityPlayer)
    {
        if(entityPlayer.isSneaking())
        {
            if(!world.isRemote) {
                cycleFiremode(itemstack, entityPlayer);
            }
        }
        else if (entityPlayer.capabilities.isCreativeMode || getEnergyStored(itemstack) >= 5)
        {
            entityPlayer.setItemInUse(itemstack, this.getMaxItemUseDuration(itemstack));
        }

        return itemstack;
    }
    
}

 

Where tryFire, incrBurst, and canFire read and write NBT data.

If the NBT data writing is commented out then onPlayerStoppedUsing is always called when I expect it.

Posted

Writing to the NBT causes Minecraft to believe that the item stack is a new item stack and therefore interrupts any in-use actions.

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.

Posted

I don't have enough of your code to know what you're doing with the NBT.

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.

Posted

It would be best not to change NBT data while the item is in use, e.g. by using a nextWorldTime for your cooldown instead of an actual counter.

 

That said, it looks like you are also doing other things with the NBT which you may not be able to get away from. You could try setting the item back in use after the NBT is set, but it may still interrupt the action, particularly on the client-side when the new ItemStack is set.

 

If the above doesn't work, you're going to have to come up with an alternate storage method such as using IExtendedEntityProperties to store data about the current item in use, update THAT data each tick instead of actually updating the item, and then (if you still need the data stored on the ItemStack, that is) write that data back to the item when #onPlayerStoppedUsing is called. It's a somewhat cumbersome workaround, but I can vouch for its effectiveness.

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.

×
×
  • Create New...

Important Information

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