Jump to content

[1.16.4] Remove Experience from the Player and update the Expereince Bar


IntentScarab

Recommended Posts

Hello,

I am trying to create an item that holds experience, later it will be blocks but little steps, and when I move the experience from the Player into the book, the exp bar doesn't update.

Right now I have this:

playerIn.experienceTotal -= expToStore;

And the next time I use the Experience Book it does say that I don't have any experience left, just that the bar doesn't update.

 

I've tried looking at the Enchanting Table Block/Tile Entity/Container, and from what I've seen they mark the inventory dirty when using the player experience, so I added this:

playerIn.inventory.markDirty();

This hasn't fixed my problem, so I must be missing something.

Full code for onItemRightClick here:

 

    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {

        ItemStack stack = playerIn.getHeldItem(Hand.MAIN_HAND).getStack();

        currentStoredExp = stack.getOrCreateTag().getInt("exp");
        int expLevel = ExperienceHelper.playerTotalExp(playerIn);
        int expTotal = playerIn.experienceTotal;

        if (expTotal <= 0 || currentStoredExp == MAX_EXP) {
            return new ActionResult<>(ActionResultType.PASS, stack);
        }

        if ((!playerIn.isSneaking()) && handIn == Hand.MAIN_HAND && currentStoredExp < MAX_EXP) {

            int expToStore = (expLevel < (MAX_EXP - currentStoredExp)) ? expLevel : (MAX_EXP - currentStoredExp);

            //TODO: not the way to remove exp from the player app
            playerIn.experienceTotal -= expToStore;

            expToStore += currentStoredExp;
            stack.getTag().putInt("exp", expToStore);

            playerIn.inventory.markDirty();
            return new ActionResult<>(ActionResultType.SUCCESS, stack);

        }

        return new ActionResult<>(ActionResultType.PASS, stack);
    }

If I have missed anything, please let me know.

Thank you!

Link to comment
Share on other sites

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.