From 610045c3194419ab28fd0627007c232b6e0479b2 Mon Sep 17 00:00:00 2001 From: ASangarin Date: Sun, 11 Oct 2020 16:49:30 +0200 Subject: [PATCH] !small refactor added force class command --- .../player/profess/SavedClassInformation.java | 32 +++++++------ .../rpg/admin/AdminCommandTreeNode.java | 3 +- .../rpg/admin/ClassCommandTreeNode.java | 28 +++++++++-- .../rpg/admin/ForceClassCommandTreeNode.java | 48 +++++++++++++++++++ .../mmocore/gui/ClassConfirmation.java | 10 ++-- .../manager/data/PlayerDataManager.java | 20 -------- .../data/mysql/MySQLPlayerDataManager.java | 2 +- .../data/yaml/YAMLPlayerDataManager.java | 2 +- 8 files changed, 100 insertions(+), 45 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmocore/command/rpg/admin/ForceClassCommandTreeNode.java diff --git a/src/main/java/net/Indyuce/mmocore/api/player/profess/SavedClassInformation.java b/src/main/java/net/Indyuce/mmocore/api/player/profess/SavedClassInformation.java index b869be5a..10de7efc 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/profess/SavedClassInformation.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/profess/SavedClassInformation.java @@ -13,6 +13,7 @@ import com.google.gson.JsonObject; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute; import net.Indyuce.mmocore.api.skill.Skill; +import net.Indyuce.mmocore.manager.data.PlayerDataManager.DefaultPlayerData; public class SavedClassInformation { private final int level, experience, skillPoints, attributePoints, attributeReallocationPoints; @@ -53,23 +54,26 @@ public class SavedClassInformation { } public SavedClassInformation(PlayerData player) { - level = player.getLevel(); - skillPoints = player.getSkillPoints(); - experience = player.getExperience(); - skills = player.mapSkillLevels(); - attributes = player.getAttributes().mapPoints(); - attributePoints = player.getAttributePoints(); - attributeReallocationPoints = player.getAttributeReallocationPoints(); + this(player.getLevel(), player.getExperience(), player.getSkillPoints(), player.getAttributePoints(), + player.getAttributeReallocationPoints(), player.mapSkillLevels(), player.getAttributes().mapPoints()); + } + + public SavedClassInformation(DefaultPlayerData data) { + this(data.getLevel(), 0, data.getSkillPoints(), data.getAttributePoints(), data.getAttrReallocPoints()); } public SavedClassInformation(int level, int experience, int skillPoints, int attributePoints, int attributeReallocationPoints) { - this.level = level; - this.experience = experience; - this.skillPoints = skillPoints; - this.attributePoints = attributePoints; - this.attributeReallocationPoints = attributeReallocationPoints; - attributes = new HashMap<>(); - skills = new HashMap<>(); + this(level, experience, skillPoints, attributePoints, attributeReallocationPoints, new HashMap<>(), new HashMap<>()); + } + + private SavedClassInformation(int l, int e, int sp, int ap, int arp, Map a, Map s) { + this.level = l; + this.experience = e; + this.skillPoints = sp; + this.attributePoints = ap; + this.attributeReallocationPoints = arp; + this.attributes = a; + this.skills = s; } public int getLevel() { diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/AdminCommandTreeNode.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/AdminCommandTreeNode.java index 8dd104a0..fa58c407 100644 --- a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/AdminCommandTreeNode.java +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/AdminCommandTreeNode.java @@ -7,12 +7,13 @@ import net.mmogroup.mmolib.command.api.CommandTreeNode; public class AdminCommandTreeNode extends CommandTreeNode { public AdminCommandTreeNode(CommandTreeNode parent) { super(parent, "admin"); - + addChild(new HideActionBarCommandTreeNode(this)); addChild(new NoCooldownCommandTreeNode(this)); addChild(new ResetCommandTreeNode(this)); addChild(new InfoCommandTreeNode(this)); addChild(new ClassCommandTreeNode(this)); + addChild(new ForceClassCommandTreeNode(this)); addChild(new ExperienceCommandTreeNode(this)); addChild(new LevelCommandTreeNode(this)); diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ClassCommandTreeNode.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ClassCommandTreeNode.java index 8bd9b993..44866502 100644 --- a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ClassCommandTreeNode.java +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ClassCommandTreeNode.java @@ -2,12 +2,15 @@ package net.Indyuce.mmocore.command.rpg.admin; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Sound; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.event.PlayerChangeClassEvent; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.profess.PlayerClass; +import net.Indyuce.mmocore.api.player.profess.SavedClassInformation; import net.mmogroup.mmolib.command.api.CommandTreeNode; import net.mmogroup.mmolib.command.api.Parameter; @@ -16,8 +19,8 @@ public class ClassCommandTreeNode extends CommandTreeNode { super(parent, "class"); addParameter(Parameter.PLAYER); - addParameter( - new Parameter("", (explorer, list) -> MMOCore.plugin.classManager.getAll().forEach(profess -> list.add(profess.getId())))); + addParameter(new Parameter("", (explorer, list) -> MMOCore.plugin.classManager.getAll() + .forEach(profess -> list.add(profess.getId())))); } @Override @@ -40,9 +43,24 @@ public class ClassCommandTreeNode extends CommandTreeNode { PlayerClass profess = MMOCore.plugin.classManager.get(format); PlayerData data = PlayerData.get(player); - data.setClass(profess); - sender.sendMessage( - ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD + profess.getName() + ChatColor.YELLOW + "."); + PlayerChangeClassEvent called = new PlayerChangeClassEvent(data, profess); + Bukkit.getPluginManager().callEvent(called); + if (called.isCancelled()) + return CommandResult.SUCCESS; + + data.giveClassPoints(-1); + (data.hasSavedClass(profess) ? data.getClassInfo(profess) : new SavedClassInformation( + MMOCore.plugin.dataProvider.getDataManager().getDefaultData())).load(profess, data); + while (data.hasSkillBound(0)) + data.unbindSkill(0); + if (data.isOnline()) { + MMOCore.plugin.configManager.getSimpleMessage("class-select", "class", profess.getName()) + .send(data.getPlayer()); + data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, 1, 1); + } + + sender.sendMessage(ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD + + profess.getName() + ChatColor.YELLOW + "."); return CommandResult.SUCCESS; } } diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ForceClassCommandTreeNode.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ForceClassCommandTreeNode.java new file mode 100644 index 00000000..4535f0f6 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ForceClassCommandTreeNode.java @@ -0,0 +1,48 @@ +package net.Indyuce.mmocore.command.rpg.admin; + +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.api.player.PlayerData; +import net.Indyuce.mmocore.api.player.profess.PlayerClass; +import net.mmogroup.mmolib.command.api.CommandTreeNode; +import net.mmogroup.mmolib.command.api.Parameter; + +public class ForceClassCommandTreeNode extends CommandTreeNode { + public ForceClassCommandTreeNode(CommandTreeNode parent) { + super(parent, "force-class"); + + addParameter(Parameter.PLAYER); + addParameter( + new Parameter("", (explorer, list) -> MMOCore.plugin.classManager.getAll().forEach(profess -> list.add(profess.getId())))); + } + + @Override + public CommandResult execute(CommandSender sender, String[] args) { + if (args.length < 4) + return CommandResult.THROW_USAGE; + + Player player = Bukkit.getPlayer(args[2]); + if (player == null) { + sender.sendMessage(ChatColor.RED + "Could not find the player called " + args[2] + "."); + return CommandResult.FAILURE; + } + + String format = args[3].toUpperCase().replace("-", "_"); + if (!MMOCore.plugin.classManager.has(format)) { + sender.sendMessage(ChatColor.RED + "Could not find class " + format + "."); + return CommandResult.FAILURE; + } + + PlayerClass profess = MMOCore.plugin.classManager.get(format); + + PlayerData data = PlayerData.get(player); + data.setClass(profess); + sender.sendMessage( + ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " is now a " + ChatColor.GOLD + profess.getName() + ChatColor.YELLOW + "."); + return CommandResult.SUCCESS; + } +} diff --git a/src/main/java/net/Indyuce/mmocore/gui/ClassConfirmation.java b/src/main/java/net/Indyuce/mmocore/gui/ClassConfirmation.java index 1262a68f..ce335373 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/ClassConfirmation.java +++ b/src/main/java/net/Indyuce/mmocore/gui/ClassConfirmation.java @@ -92,7 +92,9 @@ public class ClassConfirmation extends EditableInventory { @Override public ItemStack display(GeneratedInventory inv, int n) { - return inv.getPlayerData().hasSavedClass(((ClassConfirmationInventory) inv).profess) ? unlocked.display(inv, n) : locked.display(inv, n); + return inv.getPlayerData().hasSavedClass(((ClassConfirmationInventory) inv).profess) + ? unlocked.display(inv, n) + : locked.display(inv, n); } @Override @@ -105,7 +107,8 @@ public class ClassConfirmation extends EditableInventory { private final PlayerClass profess; private final PluginInventory last; - public ClassConfirmationInventory(PlayerData playerData, EditableInventory editable, PlayerClass profess, PluginInventory last) { + public ClassConfirmationInventory(PlayerData playerData, EditableInventory editable, PlayerClass profess, + PluginInventory last) { super(playerData, editable); this.profess = profess; @@ -128,7 +131,8 @@ public class ClassConfirmation extends EditableInventory { return; playerData.giveClassPoints(-1); - (playerData.hasSavedClass(profess) ? playerData.getClassInfo(profess) : new SavedClassInformation(1, 0, 0, 0, 0)).load(profess, playerData); + (playerData.hasSavedClass(profess) ? playerData.getClassInfo(profess) : new SavedClassInformation( + MMOCore.plugin.dataProvider.getDataManager().getDefaultData())).load(profess, playerData); while (playerData.hasSkillBound(0)) playerData.unbindSkill(0); MMOCore.plugin.configManager.getSimpleMessage("class-select", "class", profess.getName()).send(player); diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java index 2c1fa375..cee60a44 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/PlayerDataManager.java @@ -99,25 +99,5 @@ public abstract class PlayerDataManager { attributePoints = 0; attrReallocPoints = 0; } - - public int getLevel() { - return level; - } - - public int getSkillPoints() { - return skillPoints; - } - - public int getClassPoints() { - return classPoints; - } - - public int getAttributeReallocationPoints() { - return attrReallocPoints; - } - - public int getAttributePoints() { - return attributePoints; - } } } diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java index 57d0e913..164139b5 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java @@ -43,7 +43,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager { data.setClassPoints(getDefaultData().getClassPoints()); data.setSkillPoints(getDefaultData().getSkillPoints()); data.setAttributePoints(getDefaultData().getAttributePoints()); - data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints()); + data.setAttributeReallocationPoints(getDefaultData().getAttrReallocPoints()); data.setExperience(0); data.setMana(data.getStats().getStat(StatType.MAX_MANA)); data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA)); diff --git a/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java b/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java index 38cfaf20..0bdb09be 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataManager.java @@ -27,7 +27,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager { data.setClassPoints(config.getInt("class-points", getDefaultData().getClassPoints())); data.setSkillPoints(config.getInt("skill-points", getDefaultData().getSkillPoints())); data.setAttributePoints(config.getInt("attribute-points", getDefaultData().getAttributePoints())); - data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttributeReallocationPoints())); + data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttrReallocPoints())); data.setLevel(config.getInt("level", getDefaultData().getLevel())); data.setExperience(config.getInt("experience")); if (config.contains("class"))