1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-01 15:03:36 +01:00

exp command cleanup

This commit is contained in:
Zrips 2023-01-06 14:30:50 +02:00
parent 6715fa7bee
commit 2dc25b232d
4 changed files with 156 additions and 143 deletions

View File

@ -197,7 +197,7 @@
<artifactId>CMILib</artifactId> <artifactId>CMILib</artifactId>
<version>latest</version> <version>latest</version>
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/libs/CMILib1.2.3.3.jar</systemPath> <systemPath>${basedir}/libs/CMILib1.2.4.1.jar</systemPath>
</dependency> </dependency>
<!-- WildStacker --> <!-- WildStacker -->
<dependency> <dependency>
@ -286,7 +286,7 @@
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version> <version>2.3.1</version>
<configuration> <configuration>
<!-- <outputDirectory>D:\MC\Server 1.19\plugins\</outputDirectory> --> <outputDirectory>D:\MC\Server 1.19\plugins\</outputDirectory>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -28,6 +28,7 @@ import net.Zrips.CMILib.ActionBar.CMIActionBar;
import net.Zrips.CMILib.Container.CMIArray; import net.Zrips.CMILib.Container.CMIArray;
import net.Zrips.CMILib.Container.PageInfo; import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Locale.LC; import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.RawMessages.RawMessage; import net.Zrips.CMILib.RawMessages.RawMessage;
@ -51,6 +52,7 @@ public class JobsCommands implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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()) if (sender instanceof Player && !Jobs.getGCManager().canPerformActionInWorld(((Player) sender).getWorld())
&& !sender.hasPermission("jobs.disabledworld.commands")) { && !sender.hasPermission("jobs.disabledworld.commands")) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.worldisdisabled")); sender.sendMessage(Jobs.getLanguage().getMessage("general.error.worldisdisabled"));

View File

@ -1,5 +1,7 @@
package com.gamingmesh.jobs.commands.list; package com.gamingmesh.jobs.commands.list;
import java.util.Random;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; 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.Job;
import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.i18n.Language;
import java.util.Random;
public class exp implements Cmd { public class exp implements Cmd {
@ -17,43 +18,58 @@ public class exp implements Cmd {
Set, Add, Take Set, Add, Take
} }
Random rand = new Random();
@Override @Override
public boolean perform(Jobs plugin, CommandSender sender, String[] args) { public boolean perform(Jobs plugin, CommandSender sender, String[] args) {
if (args.length < 4) { if (args.length < 4) {
Jobs.getCommandManager().sendUsage(sender, "exp"); Jobs.getCommandManager().sendUsage(sender, "exp");
return true; 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]); JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]);
if (jPlayer == null) { 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; return true;
} }
Job job = Jobs.getJob(args[1]); Job job = Jobs.getJob(args[1]);
if (job == null) { if (job == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job")); Language.sendMessage(sender, "general.error.job");
return true; return true;
} }
Action action = Action.Add; Action action = Action.Add;
switch (args[2].toLowerCase()) { switch (args[2].toLowerCase()) {
case "add": case "add":
action = Action.Add; action = Action.Add;
break; break;
case "set": case "set":
action = Action.Set; action = Action.Set;
break; break;
case "take": case "take":
action = Action.Take; action = Action.Take;
break; break;
default: default:
break; break;
} }
double amount = 0.0; double amount = 0.0;
/* Add random argument, ex: rand_5-10 */ /* Add random argument, ex: rand_5-10 */
if (args[3].startsWith("rand_")) { if (args[3].startsWith("rand_")) {
String data = args[3].split("(?i)rand_")[1]; String data = args[3].split("(?i)rand_")[1];
@ -62,8 +78,8 @@ public class exp implements Cmd {
int amountMin = Integer.parseInt(arr[0]); int amountMin = Integer.parseInt(arr[0]);
int amountMax = Integer.parseInt(arr[1]); int amountMax = Integer.parseInt(arr[1]);
if (amountMin <= amountMax) { if (amountMin < amountMax) {
amount = amountMin + new Random().nextDouble() * (amountMax - amountMin); amount = amountMin + rand.nextInt(amountMax - amountMin);
} else { } else {
amount = amountMax; 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 { try {
// check if player already has the job // check if player already has the job
if (jPlayer.isInJob(job)) { JobProgression prog = jPlayer.getJobProgression(job);
JobProgression prog = jPlayer.getJobProgression(job);
switch (action) { switch (action) {
case Add: case Add:
int oldLevel = prog.getLevel(); int oldLevel = prog.getLevel();
if (prog.addExperience(amount)) if (prog.addExperience(amount))
Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel); Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel);
break; break;
case Set: case Set:
prog.setExperience(amount); prog.setExperience(amount);
break; break;
case Take: case Take:
prog.takeExperience(amount); prog.takeExperience(amount);
break; break;
default: default:
break; 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"));
} }
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) { } 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; return true;
} }

View File

@ -24,6 +24,7 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
@ -41,11 +42,15 @@ public class Language {
* Reloads the config * Reloads the config
*/ */
public void reload() { public void reload() {
String ls = Jobs.getGCManager().localeString.toLowerCase(); String ls = Jobs.getGCManager().localeString.toLowerCase();
customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig(); customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig();
enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig(); enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig();
if (customlocale == null) if (customlocale == null)
customlocale = enlocale; 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 * @return the message
*/ */
public String getMessage(String key) { public String getMessage(String key) {
return getMessage(key, ""); return getMessage(key, "");
} }
public String getMessage(String key, Object... variables) { public String getMessage(String key, Object... variables) {
String missing = "MLF " + key; String missing = "MLF " + key;
String msg = ""; String msg = "";
try { try {
if (!customlocale.contains(key)) if (!customlocale.contains(key))
msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing; msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing;
else else
msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing; msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing;
} catch (Exception e) { } catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage()); Jobs.consoleMsg(e.getLocalizedMessage());
return ""; return "";
} }
if (msg.isEmpty() || msg.equals(missing)) { if (msg.isEmpty() || msg.equals(missing)) {
msg = ""; msg = "";
try { try {
List<String> ls = null; List<String> ls = null;
if (customlocale.isList(key)) if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true); ls = colorsArray(customlocale.getStringList(key), true);
else if (enlocale.isList(key)) { else if (enlocale.isList(key)) {
ls = enlocale.getStringList(key); ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
} }
if (ls != null) if (ls != null)
for (String one : ls) { for (String one : ls) {
if (!msg.isEmpty()) if (!msg.isEmpty())
msg += "\n"; msg += "\n";
msg += one; msg += one;
} }
} catch (Exception e) { } catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key); Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage()); Jobs.consoleMsg(e.getLocalizedMessage());
return ""; return "";
} }
} }
if (variables != null && variables.length > 0) if (variables != null && variables.length > 0)
for (int i = 0; i < variables.length; i++) { for (int i = 0; i < variables.length; i++) {
if (variables.length >= i + 2) if (variables.length >= i + 2)
msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1])); msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1]));
i++; i++;
} }
return msg; return msg;
} }
/** /**
@ -113,47 +118,47 @@ public class Language {
* @return the message * @return the message
*/ */
public List<String> getMessageList(String key, Object... variables) { public List<String> getMessageList(String key, Object... variables) {
String missing = "MLF " + key + " "; String missing = "MLF " + key + " ";
List<String> ls; List<String> ls;
if (customlocale.isList(key)) if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true); ls = colorsArray(customlocale.getStringList(key), true);
else { else {
ls = enlocale.getStringList(key); ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing); ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
} }
if (variables != null && variables.length > 0) if (variables != null && variables.length > 0)
for (int i = 0; i < ls.size(); i++) { for (int i = 0; i < ls.size(); i++) {
String msg = ls.get(i); String msg = ls.get(i);
for (int y = 0; y < variables.length; y += 2) { for (int y = 0; y < variables.length; y += 2) {
msg = msg.replace(String.valueOf(variables[y]), String.valueOf(variables[y + 1])); 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) { public String filterNewLine(String msg) {
Matcher match = patern.matcher(msg); Matcher match = patern.matcher(msg);
while (match.find()) { while (match.find()) {
msg = msg.replace(match.group(0), "\n"); msg = msg.replace(match.group(0), "\n");
} }
return msg; return msg;
} }
public List<String> colorsArray(List<String> text, boolean colorize) { public List<String> colorsArray(List<String> text, boolean colorize) {
List<String> temp = new ArrayList<>(); List<String> temp = new ArrayList<>();
for (String part : text) { for (String part : text) {
if (colorize) if (colorize)
part = CMIChatColor.translate(part); part = CMIChatColor.translate(part);
temp.add(part); temp.add(part);
} }
return temp; return temp;
} }
} }