Jump to content

(1.18.2) remove xp from player


ElTotisPro50

Recommended Posts

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());
}

 

Link to comment
Share on other sites

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.

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.