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 looked source code from some mods that store player xp, to remove it they use this method from OpenMods: https://github.com/OpenMods/OpenModsLib/blob/1.16/src/main/java/openmods/utils/EnchantmentUtils.java

The place i looked at: https://github.com/bl4ckscor3/XP-Tome/blob/1.18/src/main/java/bl4ckscor3/mod/xptome/XPTomeItem.java#L76 (from line 70 to 76)

I copied the class too, and even used both PlayerXpEvents, at first it removes the xp i put but when i kill myself or something is like the xp never leaved me, it is not a "keepinventoryrule" cause im removing the xp from code :b

BTW: in case you need to know, im removing the xp from a screen (client) when i press a button

public class TotisPlayerUtils {
  	private static int sum(int n, int a0, int d) {
        return n * (2 * a0 + (n - 1) * d) / 2;
    }
    private static int xpBarCap(int level) {
        if (level >= 30)
            return 112 + (level - 30) * 9;

        if (level >= 15)
            return 37 + (level - 15) * 5;

        return 7 + level * 2;
    }
    public static int getLevelForExperience(int targetXp) {
        int level = 0;
        while (true) {
            final int xpToNextLevel = xpBarCap(level);
            if (targetXp < xpToNextLevel) return level;
            level++;
            targetXp -= xpToNextLevel;
        }
    }
    public static int getExperienceForLevel(int level) {
        if (level == 0) return 0;
        if (level <= 15) return sum(level, 7, 2);
        if (level <= 30) return 315 + sum(level - 15, 37, 5);
        return 1395 + sum(level - 30, 112, 9);
    }
    public static int getPlayerXP(Player player) {
        return (int)(getExperienceForLevel(player.experienceLevel) + (player.experienceProgress * player.getXpNeededForNextLevel()));
    }

    public static void addPlayerXP(Player player, int amount) {
        int experience = getPlayerXP(player) + amount;
        player.totalExperience = experience;
        player.experienceLevel = getLevelForExperience(experience);
        int expForLevel = getExperienceForLevel(player.experienceLevel);
        player.experienceProgress = (experience - expForLevel) / (float)player.getXpNeededForNextLevel();
    }
}

 

The screen part

int xp = TotisPlayerUtils.getPlayerXP(player);
if(xp >= 1) {
	int depositedXp = 20;
	int previousLevel = player.experienceLevel;
	MinecraftForge.EVENT_BUS.post(new PlayerXpEvent.XpChange(player, -depositedXp));
	TotisPlayerUtils.addPlayerXP(player, -depositedXp); //should remove 20 xp, it does but not permanently
	if(previousLevel != player.experienceLevel) MinecraftForge.EVENT_BUS.post(new PlayerXpEvent.LevelChange(player, player.experienceLevel));
} else {
	player.sendMessage(new TextComponent(ChatFormatting.RED + "Error: no XP to deposit"), player.getUUID());
}

 

32 minutes ago, ElTotisPro50 said:

BTW: in case you need to know, im removing the xp from a screen (client) when i press a button

You cannot remove xp from the client player; it will not sync to the server. You should instead send a packet to the server and remove the experience from the player there. You can set experience points using either `Player#giveExperiencePoints` or `Player#giveExperienceLevels` or directly through `Player#setExperiencePoints` or `Player#setExperienceLevels`. Additionally, that is not how you listen to an event. I would suggest reading how to do so on the docs.

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.