From 2dc25b232d45ef125cf47e047f9393ecce908d7a Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 6 Jan 2023 14:30:50 +0200 Subject: [PATCH] exp command cleanup --- pom.xml | 4 +- .../jobs/commands/JobsCommands.java | 2 + .../gamingmesh/jobs/commands/list/exp.java | 136 +++++++-------- .../com/gamingmesh/jobs/i18n/Language.java | 157 +++++++++--------- 4 files changed, 156 insertions(+), 143 deletions(-) diff --git a/pom.xml b/pom.xml index 1826a065..933d2a26 100644 --- a/pom.xml +++ b/pom.xml @@ -197,7 +197,7 @@ CMILib latest system - ${basedir}/libs/CMILib1.2.3.3.jar + ${basedir}/libs/CMILib1.2.4.1.jar @@ -286,7 +286,7 @@ maven-jar-plugin 2.3.1 - + D:\MC\Server 1.19\plugins\ diff --git a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java index b388435d..10468698 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java +++ b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java @@ -28,6 +28,7 @@ import net.Zrips.CMILib.ActionBar.CMIActionBar; import net.Zrips.CMILib.Container.CMIArray; import net.Zrips.CMILib.Container.PageInfo; import net.Zrips.CMILib.Locale.LC; +import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.RawMessages.RawMessage; @@ -51,6 +52,7 @@ public class JobsCommands implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + CMIDebug.d("jobs commands"); if (sender instanceof Player && !Jobs.getGCManager().canPerformActionInWorld(((Player) sender).getWorld()) && !sender.hasPermission("jobs.disabledworld.commands")) { sender.sendMessage(Jobs.getLanguage().getMessage("general.error.worldisdisabled")); diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/exp.java b/src/main/java/com/gamingmesh/jobs/commands/list/exp.java index 312b7b5f..4a4a1fc6 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/exp.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/exp.java @@ -1,5 +1,7 @@ package com.gamingmesh.jobs.commands.list; +import java.util.Random; + import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -8,8 +10,7 @@ import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; - -import java.util.Random; +import com.gamingmesh.jobs.i18n.Language; public class exp implements Cmd { @@ -17,43 +18,58 @@ public class exp implements Cmd { Set, Add, Take } + Random rand = new Random(); + @Override public boolean perform(Jobs plugin, CommandSender sender, String[] args) { + if (args.length < 4) { Jobs.getCommandManager().sendUsage(sender, "exp"); return true; } + boolean silent = false; + boolean silentAdmin = false; + + for (String one : args) { + if (one.equalsIgnoreCase("-s")) { + silent = true; + continue; + } + if (one.equalsIgnoreCase("-sa")) { + silentAdmin = true; + } + } + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); if (jPlayer == null) { - sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", args[0])); + Language.sendMessage(sender, "general.error.noinfoByPlayer", "%playername%", args[0]); return true; } Job job = Jobs.getJob(args[1]); if (job == null) { - sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job")); + Language.sendMessage(sender, "general.error.job"); return true; } Action action = Action.Add; switch (args[2].toLowerCase()) { - case "add": - action = Action.Add; - break; - case "set": - action = Action.Set; - break; - case "take": - action = Action.Take; - break; - default: - break; + case "add": + action = Action.Add; + break; + case "set": + action = Action.Set; + break; + case "take": + action = Action.Take; + break; + default: + break; } double amount = 0.0; - /* Add random argument, ex: rand_5-10 */ if (args[3].startsWith("rand_")) { String data = args[3].split("(?i)rand_")[1]; @@ -62,8 +78,8 @@ public class exp implements Cmd { int amountMin = Integer.parseInt(arr[0]); int amountMax = Integer.parseInt(arr[1]); - if (amountMin <= amountMax) { - amount = amountMin + new Random().nextDouble() * (amountMax - amountMin); + if (amountMin < amountMax) { + amount = amountMin + rand.nextInt(amountMax - amountMin); } else { amount = amountMax; } @@ -77,58 +93,48 @@ public class exp implements Cmd { } } + if (!jPlayer.isInJob(job)) { + Language.sendMessage(sender, "command.exp.error.nojob"); + return true; + } + try { // check if player already has the job - if (jPlayer.isInJob(job)) { - JobProgression prog = jPlayer.getJobProgression(job); + JobProgression prog = jPlayer.getJobProgression(job); - switch (action) { - case Add: - int oldLevel = prog.getLevel(); - if (prog.addExperience(amount)) - Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel); - break; - case Set: - prog.setExperience(amount); - break; - case Take: - prog.takeExperience(amount); - break; - default: - break; - } - - Player player = jPlayer.getPlayer(); - boolean isSilent = false; - boolean isSilentAdmin = false; - if (player != null) { - for (String one : args) { - if (one.equalsIgnoreCase("-s")) { - isSilent = true; - break; - } - } - if (!isSilent) { - player.sendMessage(Jobs.getLanguage().getMessage("command.exp.output.target", "%jobname%", job.getDisplayName(), "%level%", prog.getLevelFormatted(), "%exp%", prog - .getExperience())); - } - for (String one : args) { - if (one.equalsIgnoreCase("-sa")) { - isSilentAdmin = true; - break; - } - } - if (!isSilentAdmin) { - sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success")); - } - } else { - sender.sendMessage(Jobs.getLanguage().getMessage("general.give.output.notonline")); - } - } else { - sender.sendMessage(Jobs.getLanguage().getMessage("command.exp.error.nojob")); + switch (action) { + case Add: + int oldLevel = prog.getLevel(); + if (prog.addExperience(amount)) + Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel); + break; + case Set: + prog.setExperience(amount); + break; + case Take: + prog.takeExperience(amount); + break; + default: + break; } + + Player player = jPlayer.getPlayer(); + if (player == null) { + sender.sendMessage(Jobs.getLanguage().getMessage("general.give.output.notonline")); + return true; + } + + if (!silent) + player.sendMessage(Jobs.getLanguage().getMessage("command.exp.output.target", "%jobname%", job.getDisplayName(), "%level%", prog.getLevelFormatted(), "%exp%", prog + .getExperience())); + + if (!silentAdmin) + sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success")); + } catch (Exception e) { - sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error")); + if (!silentAdmin) + sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error")); + e.printStackTrace(); } return true; } diff --git a/src/main/java/com/gamingmesh/jobs/i18n/Language.java b/src/main/java/com/gamingmesh/jobs/i18n/Language.java index cf551599..2b3b9420 100644 --- a/src/main/java/com/gamingmesh/jobs/i18n/Language.java +++ b/src/main/java/com/gamingmesh/jobs/i18n/Language.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import com.gamingmesh.jobs.Jobs; @@ -41,11 +42,15 @@ public class Language { * Reloads the config */ public void reload() { - String ls = Jobs.getGCManager().localeString.toLowerCase(); - customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig(); - enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig(); - if (customlocale == null) - customlocale = enlocale; + String ls = Jobs.getGCManager().localeString.toLowerCase(); + customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig(); + enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig(); + if (customlocale == null) + customlocale = enlocale; + } + + public static void sendMessage(CommandSender sender, String key, Object... variables) { + sender.sendMessage(Jobs.getLanguage().getMessage(key, variables)); } /** @@ -54,57 +59,57 @@ public class Language { * @return the message */ public String getMessage(String key) { - return getMessage(key, ""); + return getMessage(key, ""); } public String getMessage(String key, Object... variables) { - String missing = "MLF " + key; - String msg = ""; - try { - if (!customlocale.contains(key)) - msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing; - else - msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing; - } catch (Exception e) { - Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); - Jobs.consoleMsg(e.getLocalizedMessage()); - return ""; - } + String missing = "MLF " + key; + String msg = ""; + try { + if (!customlocale.contains(key)) + msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing; + else + msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing; + } catch (Exception e) { + Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); + Jobs.consoleMsg(e.getLocalizedMessage()); + return ""; + } - if (msg.isEmpty() || msg.equals(missing)) { - msg = ""; - try { + if (msg.isEmpty() || msg.equals(missing)) { + msg = ""; + try { - List ls = null; + List ls = null; - if (customlocale.isList(key)) - ls = colorsArray(customlocale.getStringList(key), true); - else if (enlocale.isList(key)) { - ls = enlocale.getStringList(key); - ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); - } + if (customlocale.isList(key)) + ls = colorsArray(customlocale.getStringList(key), true); + else if (enlocale.isList(key)) { + ls = enlocale.getStringList(key); + ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); + } - if (ls != null) - for (String one : ls) { - if (!msg.isEmpty()) - msg += "\n"; - msg += one; - } - } catch (Exception e) { - Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); - Jobs.consoleMsg(e.getLocalizedMessage()); - return ""; - } - } + if (ls != null) + for (String one : ls) { + if (!msg.isEmpty()) + msg += "\n"; + msg += one; + } + } catch (Exception e) { + Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); + Jobs.consoleMsg(e.getLocalizedMessage()); + return ""; + } + } - if (variables != null && variables.length > 0) - for (int i = 0; i < variables.length; i++) { - if (variables.length >= i + 2) - msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1])); - i++; - } + if (variables != null && variables.length > 0) + for (int i = 0; i < variables.length; i++) { + if (variables.length >= i + 2) + msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1])); + i++; + } - return msg; + return msg; } /** @@ -113,47 +118,47 @@ public class Language { * @return the message */ public List getMessageList(String key, Object... variables) { - String missing = "MLF " + key + " "; + String missing = "MLF " + key + " "; - List ls; - if (customlocale.isList(key)) - ls = colorsArray(customlocale.getStringList(key), true); - else { - ls = enlocale.getStringList(key); - ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); - } + List ls; + if (customlocale.isList(key)) + ls = colorsArray(customlocale.getStringList(key), true); + else { + ls = enlocale.getStringList(key); + ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); + } - if (variables != null && variables.length > 0) - for (int i = 0; i < ls.size(); i++) { - String msg = ls.get(i); - for (int y = 0; y < variables.length; y += 2) { - msg = msg.replace(String.valueOf(variables[y]), String.valueOf(variables[y + 1])); - } + if (variables != null && variables.length > 0) + for (int i = 0; i < ls.size(); i++) { + String msg = ls.get(i); + for (int y = 0; y < variables.length; y += 2) { + msg = msg.replace(String.valueOf(variables[y]), String.valueOf(variables[y + 1])); + } - ls.set(i, CMIChatColor.translate(filterNewLine(msg))); - } + ls.set(i, CMIChatColor.translate(filterNewLine(msg))); + } - return ls; + return ls; } public String filterNewLine(String msg) { - Matcher match = patern.matcher(msg); - while (match.find()) { - msg = msg.replace(match.group(0), "\n"); - } - return msg; + Matcher match = patern.matcher(msg); + while (match.find()) { + msg = msg.replace(match.group(0), "\n"); + } + return msg; } public List colorsArray(List text, boolean colorize) { - List temp = new ArrayList<>(); + List temp = new ArrayList<>(); - for (String part : text) { - if (colorize) - part = CMIChatColor.translate(part); + for (String part : text) { + if (colorize) + part = CMIChatColor.translate(part); - temp.add(part); - } + temp.add(part); + } - return temp; + return temp; } }