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.

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

Featured Replies

Posted

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!

13 hours ago, IntentScarab said:

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

Full code for onItemRightClick here:

I think you should use that:

			player.addExperienceLevel(-5);

 

  • Author

Oh wow, that worked.

Thank you very much :)

 

I have another issue with the durability bar, but I'll work on that for a while before asking the forum.

Thanks again!

 

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.