From 3b09398ebc54f96a5f9e0d801b4a908d9521d1f7 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sat, 19 Feb 2022 12:50:09 +0100 Subject: [PATCH] Added /mmocore admin exp take --- .../mmocore/api/player/PlayerData.java | 2 +- .../rpg/admin/ExperienceCommandTreeNode.java | 25 +++++++++++-------- .../mmocore/experience/PlayerProfessions.java | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index 5742b8e4..61a132eb 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -555,7 +555,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc if (event.isCancelled()) return; - experience += event.getExperience(); + experience = Math.max(0, experience + event.getExperience()); // Calculate the player's next level int oldLevel = level, needed; diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandTreeNode.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandTreeNode.java index 9c3f0db7..523c21dc 100644 --- a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandTreeNode.java +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandTreeNode.java @@ -1,21 +1,21 @@ package net.Indyuce.mmocore.command.rpg.admin; -import java.util.function.BiConsumer; - +import io.lumine.mythic.lib.commands.mmolib.api.CommandTreeNode; +import io.lumine.mythic.lib.commands.mmolib.api.Parameter; +import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.command.CommandVerbose; +import net.Indyuce.mmocore.command.MMOCoreCommandTreeRoot; +import net.Indyuce.mmocore.experience.EXPSource; +import net.Indyuce.mmocore.experience.PlayerProfessions; +import net.Indyuce.mmocore.experience.Profession; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import net.Indyuce.mmocore.MMOCore; -import net.Indyuce.mmocore.experience.EXPSource; -import net.Indyuce.mmocore.experience.PlayerProfessions; -import net.Indyuce.mmocore.experience.Profession; -import net.Indyuce.mmocore.api.player.PlayerData; -import net.Indyuce.mmocore.command.CommandVerbose; -import net.Indyuce.mmocore.command.MMOCoreCommandTreeRoot; -import io.lumine.mythic.lib.commands.mmolib.api.CommandTreeNode; -import io.lumine.mythic.lib.commands.mmolib.api.Parameter; +import java.util.function.BiConsumer; public class ExperienceCommandTreeNode extends CommandTreeNode { public ExperienceCommandTreeNode(CommandTreeNode parent) { @@ -24,6 +24,8 @@ public class ExperienceCommandTreeNode extends CommandTreeNode { addChild(new ActionCommandTreeNode(this, "set", PlayerData::setExperience, PlayerProfessions::setExperience)); addChild(new ActionCommandTreeNode(this, "give", (data, value) -> data.giveExperience(value, EXPSource.COMMAND), (professions, profession, value) -> professions.giveExperience(profession, value, EXPSource.COMMAND, professions.getPlayerData().getPlayer().getLocation()))); + addChild(new ActionCommandTreeNode(this, "take", (data, value) -> data.giveExperience(-value, EXPSource.COMMAND), (professions, profession, + value) -> professions.giveExperience(profession, -value, EXPSource.COMMAND, professions.getPlayerData().getPlayer().getLocation()))); } public static class ActionCommandTreeNode extends CommandTreeNode { @@ -56,6 +58,7 @@ public class ExperienceCommandTreeNode extends CommandTreeNode { int amount; try { amount = Integer.parseInt(args[5]); + Validate.isTrue(amount > 0); } catch (NumberFormatException exception) { sender.sendMessage(ChatColor.RED + args[5] + " is not a valid number."); return CommandResult.FAILURE; diff --git a/src/main/java/net/Indyuce/mmocore/experience/PlayerProfessions.java b/src/main/java/net/Indyuce/mmocore/experience/PlayerProfessions.java index 4d1287fd..4bcfb903 100644 --- a/src/main/java/net/Indyuce/mmocore/experience/PlayerProfessions.java +++ b/src/main/java/net/Indyuce/mmocore/experience/PlayerProfessions.java @@ -177,7 +177,7 @@ public class PlayerProfessions { if (event.isCancelled()) return; - exp.put(profession.getId(), exp.containsKey(profession.getId()) ? exp.get(profession.getId()) + event.getExperience() : event.getExperience()); + exp.put(profession.getId(), Math.max(0, exp.getOrDefault(profession.getId(), 0) + event.getExperience())); int needed, exp, level, oldLevel = getLevel(profession); /*