From ffef7f0bcb9dff5c79544beb5e2993b0ae493372 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 14:37:15 +0200 Subject: [PATCH 01/22] Add SubCommand.parameters Add SWCommand.internalHelp --- .../src/de/steamwar/command/SWCommand.java | 47 +++++++++++++++++++ .../src/de/steamwar/command/SubCommand.java | 3 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ab72fc5..700153c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -34,6 +34,9 @@ import java.util.stream.Collectors; public abstract class SWCommand { + private boolean hasHelp = false; + private final List help = new ArrayList<>(); + private final Command command; private final List commandList = new ArrayList<>(); private final List commandHelpList = new ArrayList<>(); @@ -87,6 +90,14 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } + if (method.getName().equals("internalHelp") && method.getDeclaringClass() == SWCommand.class) { + if (hasHelp) { + return; + } + } else if (!hasHelp) { + commandHelpList.clear(); + hasHelp = true; + } commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); }); } @@ -166,6 +177,36 @@ public abstract class SWCommand { SWCommandUtils.commandMap.register("steamwar", this.command); } + @Register + private void internalHelp(CommandSender sender, String... args){ + if (help.isEmpty()) { + commandList.forEach(subCommand -> { + StringBuilder st = new StringBuilder(); + st.append("§8/§7").append(command.getName()).append(" "); + st.append("§7").append(String.join(" ", subCommand.subCommand)).append(" "); + int i = 1; + while (i < subCommand.parameters.length) { + Parameter parameter = subCommand.parameters[i]; + Name name = parameter.getAnnotation(Name.class); + st.append("§8[§7"); + if (name != null) { + st.append(name.name()); + } else { + st.append(parameter.getName()); + } + st.append("§8]"); + i++; + } + if (subCommand.varArgType != null) { + st.append("§7..."); + } + help.add(st.toString()); + }); + } + sender.sendMessage("§7Help§8: §e" + command.getName()); + help.forEach(sender::sendMessage); + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Repeatable(Register.Registeres.class) @@ -196,4 +237,10 @@ public abstract class SWCommand { boolean local() default false; } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + protected @interface Name { + String name(); + } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 15dad9a..29642a4 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -34,6 +34,7 @@ class SubCommand { private SWCommand swCommand; private Method method; + Parameter[] parameters; String[] subCommand; TypeMapper[] arguments; private Predicate commandSenderPredicate; @@ -44,7 +45,7 @@ class SubCommand { this.swCommand = swCommand; this.method = method; - Parameter[] parameters = method.getParameters(); + parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); commandSenderFunction = sender -> parameters[0].getType().cast(sender); this.subCommand = subCommand; -- 2.39.2 From 2a9636b99506bd2782829636866a56eb8a9b609a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 14:38:39 +0200 Subject: [PATCH 02/22] Add SWCommand.internalHelp --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 700153c..c44c0bb 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -178,7 +178,7 @@ public abstract class SWCommand { } @Register - private void internalHelp(CommandSender sender, String... args){ + private void internalHelp(CommandSender sender, String... args) { if (help.isEmpty()) { commandList.forEach(subCommand -> { StringBuilder st = new StringBuilder(); @@ -200,10 +200,11 @@ public abstract class SWCommand { if (subCommand.varArgType != null) { st.append("§7..."); } + st.append("§8 - §7Description Here"); help.add(st.toString()); }); } - sender.sendMessage("§7Help§8: §e" + command.getName()); + sender.sendMessage("§7----==== §e" + command.getName() + "§7====----"); help.forEach(sender::sendMessage); } -- 2.39.2 From e40db3d711553765ff81bd31173fb891c9536b3c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 15:36:04 +0200 Subject: [PATCH 03/22] Add SWCommand.methods --- .../src/de/steamwar/command/SWCommand.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index c44c0bb..0e502b3 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -70,7 +70,7 @@ public abstract class SWCommand { unregister(); register(); - Method[] methods = getClass().getDeclaredMethods(); + List methods = methods(); for (Method method : methods) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper); @@ -90,7 +90,8 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - if (method.getName().equals("internalHelp") && method.getDeclaringClass() == SWCommand.class) { + if (method.getName().equals("internalHelp")) { + System.out.println(method + " " + hasHelp); if (hasHelp) { return; } @@ -167,6 +168,12 @@ public abstract class SWCommand { }); } + private List methods() { + List methods = Arrays.asList(getClass().getDeclaredMethods()); + methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); + return methods; + } + public void unregister() { SWCommandUtils.knownCommandMap.remove(command.getName()); command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); @@ -177,7 +184,7 @@ public abstract class SWCommand { SWCommandUtils.commandMap.register("steamwar", this.command); } - @Register + @Register(help = true) private void internalHelp(CommandSender sender, String... args) { if (help.isEmpty()) { commandList.forEach(subCommand -> { -- 2.39.2 From 8985393fe1a637263ba618f23f55e55bf8c297c3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 15:50:49 +0200 Subject: [PATCH 04/22] Add SWCommand.inject --- .../src/de/steamwar/command/SWCommand.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 0e502b3..06757f5 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -22,6 +22,8 @@ package de.steamwar.command; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; import java.lang.annotation.*; import java.lang.reflect.Method; @@ -169,8 +171,15 @@ public abstract class SWCommand { } private List methods() { - List methods = Arrays.asList(getClass().getDeclaredMethods()); - methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); + Class current = getClass(); + List methods = new ArrayList<>(); + int count = 0; + while (current != null && count < 4) { + methods.addAll(Arrays.asList(current.getDeclaredMethods())); + current = current.getSuperclass(); + count++; + } + System.out.println(methods); return methods; } @@ -184,6 +193,16 @@ public abstract class SWCommand { SWCommandUtils.commandMap.register("steamwar", this.command); } + public void inject(Plugin plugin) { + new BukkitRunnable() { + @Override + public void run() { + SWCommand.this.unregister(); + SWCommand.this.register(); + } + }.runTask(plugin); + } + @Register(help = true) private void internalHelp(CommandSender sender, String... args) { if (help.isEmpty()) { -- 2.39.2 From 4b23274f8df4eeaf408f4ff40c3e0d6942c36631 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:09:26 +0200 Subject: [PATCH 05/22] Add SubCommand.description --- .../src/de/steamwar/command/SWCommand.java | 60 ++++++++----------- .../src/de/steamwar/command/SubCommand.java | 4 +- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 06757f5..f7cc6e0 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -92,16 +92,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - if (method.getName().equals("internalHelp")) { - System.out.println(method + " " + hasHelp); - if (hasHelp) { - return; - } - } else if (!hasHelp) { - commandHelpList.clear(); - hasHelp = true; - } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); + commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>())); }); } for (Method method : methods) { @@ -123,7 +114,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper)); + commandList.add(new SubCommand(this, method, anno.description(), anno.value(), localTypeMapper)); }); this.commandList.sort((o1, o2) -> { @@ -171,15 +162,8 @@ public abstract class SWCommand { } private List methods() { - Class current = getClass(); - List methods = new ArrayList<>(); - int count = 0; - while (current != null && count < 4) { - methods.addAll(Arrays.asList(current.getDeclaredMethods())); - current = current.getSuperclass(); - count++; - } - System.out.println(methods); + List methods = Arrays.asList(getClass().getDeclaredMethods()); + methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); return methods; } @@ -209,28 +193,30 @@ public abstract class SWCommand { commandList.forEach(subCommand -> { StringBuilder st = new StringBuilder(); st.append("§8/§7").append(command.getName()).append(" "); - st.append("§7").append(String.join(" ", subCommand.subCommand)).append(" "); - int i = 1; - while (i < subCommand.parameters.length) { - Parameter parameter = subCommand.parameters[i]; - Name name = parameter.getAnnotation(Name.class); - st.append("§8[§7"); - if (name != null) { - st.append(name.name()); - } else { - st.append(parameter.getName()); - } - st.append("§8]"); - i++; - } + st.append("§7").append(String.join(" ", subCommand.subCommand)); + String cmd = Arrays.stream(subCommand.parameters) + .skip(1) + .map(parameter -> { + Name name = parameter.getAnnotation(Name.class); + if (name != null) { + return name.name(); + } else { + return parameter.getName(); + } + }) + .map(param -> "§8[§e" + param + "§8]") + .collect(Collectors.joining(" ")); + st.append(cmd); if (subCommand.varArgType != null) { st.append("§7..."); } - st.append("§8 - §7Description Here"); + if (!subCommand.description.isEmpty()) { + st.append("§8 - §7").append(subCommand.description); + } help.add(st.toString()); }); } - sender.sendMessage("§7----==== §e" + command.getName() + "§7====----"); + sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); help.forEach(sender::sendMessage); } @@ -240,6 +226,8 @@ public abstract class SWCommand { protected @interface Register { String[] value() default {}; + String description() default ""; + boolean help() default false; @Retention(RetentionPolicy.RUNTIME) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 29642a4..d530b1a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -34,6 +34,7 @@ class SubCommand { private SWCommand swCommand; private Method method; + String description; Parameter[] parameters; String[] subCommand; TypeMapper[] arguments; @@ -41,9 +42,10 @@ class SubCommand { private Function commandSenderFunction; Class varArgType = null; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper) { + SubCommand(SWCommand swCommand, Method method, String description, String[] subCommand, Map> localTypeMapper) { this.swCommand = swCommand; this.method = method; + this.description = description; parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); -- 2.39.2 From 698f2c6cbb55581502f133acfbe9816b8a7a0575 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:20:53 +0200 Subject: [PATCH 06/22] Fix UnsupportedOperationException --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index f7cc6e0..d418e02 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -162,7 +162,7 @@ public abstract class SWCommand { } private List methods() { - List methods = Arrays.asList(getClass().getDeclaredMethods()); + List methods = new ArrayList<>(Arrays.asList(getClass().getDeclaredMethods())); methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); return methods; } -- 2.39.2 From 7ce4eacbce71173faa330d50fa3ca4289352f965 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:34:47 +0200 Subject: [PATCH 07/22] Fix SWCommand --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index d418e02..d7ba5d8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -204,8 +204,8 @@ public abstract class SWCommand { return parameter.getName(); } }) - .map(param -> "§8[§e" + param + "§8]") - .collect(Collectors.joining(" ")); + .map(param -> " §8[§e" + param + "§8]") + .collect(Collectors.joining("")); st.append(cmd); if (subCommand.varArgType != null) { st.append("§7..."); -- 2.39.2 From 6c9f0af1b2274469a9be44a615d5e23575163c48 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:50:23 +0200 Subject: [PATCH 08/22] Add SWCommand aliases --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index d7ba5d8..1122554 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -217,6 +217,7 @@ public abstract class SWCommand { }); } sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); + sender.sendMessage("§eAliases§8:§7 " + String.join("§8,§7 ", command.getAliases())); help.forEach(sender::sendMessage); } -- 2.39.2 From 22e86872b2dd3661205d8b05a2252f8de65c2045 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 16:56:59 +0200 Subject: [PATCH 09/22] Add SWCommand aliases --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 1122554..9402a96 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -217,7 +217,7 @@ public abstract class SWCommand { }); } sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); - sender.sendMessage("§eAliases§8:§7 " + String.join("§8,§7 ", command.getAliases())); + sender.sendMessage("§7Aliases§8:§e " + String.join("§8,§e ", command.getAliases())); help.forEach(sender::sendMessage); } -- 2.39.2 From 8949062c839ecc656a826a0b4cddda9464b5d7df Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 20:26:13 +0200 Subject: [PATCH 10/22] Update SWCommand.internalHelp --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 9402a96..cbea022 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -216,9 +216,14 @@ public abstract class SWCommand { help.add(st.toString()); }); } + String string = "/" + command.getName() + " " + String.join(" ", args); sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); sender.sendMessage("§7Aliases§8:§e " + String.join("§8,§e ", command.getAliases())); - help.forEach(sender::sendMessage); + help.forEach(s -> { + if (s.replaceAll("§[0-9A-Z]", "").startsWith(string)) { + sender.sendMessage(s); + } + }); } @Retention(RetentionPolicy.RUNTIME) -- 2.39.2 From 9db16383e450a026b8465e5569a592eadf43fc26 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 20:56:39 +0200 Subject: [PATCH 11/22] Remove unused fields --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index cbea022..ff920b7 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,7 +36,6 @@ import java.util.stream.Collectors; public abstract class SWCommand { - private boolean hasHelp = false; private final List help = new ArrayList<>(); private final Command command; -- 2.39.2 From 78985f15deb9e5fbc55c104d5bff5750a97781f4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 21:03:55 +0200 Subject: [PATCH 12/22] Fix SWCommand help list sorting --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 10 +++++++++- .../src/de/steamwar/command/SubCommand.java | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ff920b7..0df0148 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -125,7 +125,15 @@ public abstract class SWCommand { o2.varArgType != null ? Integer.MAX_VALUE : o2.arguments.length); } }); - commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length)); + commandHelpList.sort((o1, o2) -> { + int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length); + if (compare != 0) { + return compare; + } else { + return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 0 : 1, + o2.method.getDeclaringClass() == SWCommand.class ? 0 : 1); + } + }); } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index d530b1a..0ea3852 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -33,7 +33,7 @@ import static de.steamwar.command.SWCommandUtils.*; class SubCommand { private SWCommand swCommand; - private Method method; + Method method; String description; Parameter[] parameters; String[] subCommand; -- 2.39.2 From 96630e13d27c80d7027deb3994aaca0e062d491b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 14 May 2021 21:19:30 +0200 Subject: [PATCH 13/22] Simplify SWCommand --- .../src/de/steamwar/command/SWCommand.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 0df0148..14bb850 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -79,16 +79,17 @@ public abstract class SWCommand { addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper); }); - add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { + add(Register.class, method, i -> i != 2, true, null, (anno, parameters) -> { if (!anno.help()) return; - if (parameters.length != 2) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); - } + List errors = new ArrayList<>(); if (!parameters[parameters.length - 1].isVarArgs()) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); + errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); } if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); + errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); + } + if (!errors.isEmpty()) { + errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); return; } commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>())); @@ -130,8 +131,8 @@ public abstract class SWCommand { if (compare != 0) { return compare; } else { - return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 0 : 1, - o2.method.getDeclaringClass() == SWCommand.class ? 0 : 1); + return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0, + o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0); } }); } @@ -142,16 +143,18 @@ public abstract class SWCommand { if (anno == null || anno.length == 0) return; Parameter[] parameters = method.getParameters(); + List errors = new ArrayList<>(); if (!parameterTester.test(parameters.length)) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); - return; + errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); } if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); - return; + errors.add("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); } if (returnType != null && method.getReturnType() != returnType) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); + errors.add("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); + } + if (!errors.isEmpty()) { + errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); return; } Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters)); -- 2.39.2 From 4d1b73dafc8f47208276c1e5703531edb38d507a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 15 May 2021 11:46:46 +0200 Subject: [PATCH 14/22] Fix SWCommand error message --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 14bb850..35779c5 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -79,9 +79,12 @@ public abstract class SWCommand { addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper); }); - add(Register.class, method, i -> i != 2, true, null, (anno, parameters) -> { + add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { if (!anno.help()) return; List errors = new ArrayList<>(); + if (parameters.length != 2) { + errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); + } if (!parameters[parameters.length - 1].isVarArgs()) { errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); } -- 2.39.2 From 8706146317d61863229d6877246b8e0e3d6a9592 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 12:16:55 +0200 Subject: [PATCH 15/22] Add SWCommand multilingual --- .../src/de/steamwar/command/SWCommand.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 35779c5..708ed53 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -26,10 +26,10 @@ import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import java.lang.annotation.*; -import java.lang.reflect.Method; -import java.lang.reflect.Parameter; +import java.lang.reflect.*; import java.util.*; import java.util.function.BiConsumer; +import java.util.function.BiFunction; import java.util.function.IntPredicate; import java.util.logging.Level; import java.util.stream.Collectors; @@ -43,6 +43,8 @@ public abstract class SWCommand { private final List commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); + private BiFunction message = (s, commandSender) -> s; + protected SWCommand(String command) { this(command, new String[0]); } @@ -71,6 +73,8 @@ public abstract class SWCommand { unregister(); register(); + messageSystem(); + List methods = methods(); for (Method method : methods) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { @@ -141,6 +145,26 @@ public abstract class SWCommand { } } + private void messageSystem() { + try { + Field field = getClass().getDeclaredField("MESSAGE"); + if (!field.getType().getTypeName().equals("de.steamwar.message.Message")) { + return; + } + Object o = Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this); + Method method = o.getClass().getMethod("parse"); + message = (s, commandSender) -> { + try { + return (String) method.invoke(o, s, commandSender); + } catch (IllegalAccessException | InvocationTargetException e) { + return s; + } + }; + } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + // Ignored + } + } + private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { T[] anno = SWCommandUtils.getAnnotation(method, annotation); if (anno == null || anno.length == 0) return; -- 2.39.2 From 02e7918e17b72c0d4e9df66cde505f57f9d41da9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 12:19:23 +0200 Subject: [PATCH 16/22] Optimize SWCommand.internalHelp --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 708ed53..ac4fde8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,7 +36,7 @@ import java.util.stream.Collectors; public abstract class SWCommand { - private final List help = new ArrayList<>(); + private List help = null; private final Command command; private final List commandList = new ArrayList<>(); @@ -226,7 +226,8 @@ public abstract class SWCommand { @Register(help = true) private void internalHelp(CommandSender sender, String... args) { - if (help.isEmpty()) { + if (help == null) { + help = new ArrayList<>(); commandList.forEach(subCommand -> { StringBuilder st = new StringBuilder(); st.append("§8/§7").append(command.getName()).append(" "); -- 2.39.2 From e09fc62bb2addace114573ca2115586d08752296 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 12:21:46 +0200 Subject: [PATCH 17/22] Optimize SWCommand.internalHelp --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ac4fde8..7c095f6 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -249,7 +249,7 @@ public abstract class SWCommand { st.append("§7..."); } if (!subCommand.description.isEmpty()) { - st.append("§8 - §7").append(subCommand.description); + st.append("§8 - §7").append(message.apply(subCommand.description, sender)); } help.add(st.toString()); }); -- 2.39.2 From 6859d9bc10b3b709020be187926b161e05299ddc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 12:49:38 +0200 Subject: [PATCH 18/22] Fix SWCommand --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 7c095f6..1753470 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -28,6 +28,7 @@ import org.bukkit.scheduler.BukkitRunnable; import java.lang.annotation.*; import java.lang.reflect.*; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.IntPredicate; @@ -151,16 +152,19 @@ public abstract class SWCommand { if (!field.getType().getTypeName().equals("de.steamwar.message.Message")) { return; } - Object o = Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this); + AtomicReference o = new AtomicReference<>(null); Method method = o.getClass().getMethod("parse"); message = (s, commandSender) -> { try { - return (String) method.invoke(o, s, commandSender); + if (o.get() == null) { + o.set(Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this)); + } + return (String) method.invoke(o.get(), s, commandSender); } catch (IllegalAccessException | InvocationTargetException e) { return s; } }; - } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + } catch (NoSuchFieldException | NoSuchMethodException e) { // Ignored } } -- 2.39.2 From 7ea204b2894334cc45fe37ef421c36aae59aa94c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 13:07:10 +0200 Subject: [PATCH 19/22] Update SWCommand --- .../src/de/steamwar/command/SWCommand.java | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 1753470..72a83da 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -19,7 +19,6 @@ package de.steamwar.command; -import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; @@ -32,7 +31,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.IntPredicate; -import java.util.logging.Level; import java.util.stream.Collectors; public abstract class SWCommand { @@ -86,19 +84,14 @@ public abstract class SWCommand { }); add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { if (!anno.help()) return; - List errors = new ArrayList<>(); if (parameters.length != 2) { - errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); + throw new SecurityException("The method '" + method.toString() + "' is lacking parameters or has too many"); } if (!parameters[parameters.length - 1].isVarArgs()) { - errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); + throw new SecurityException("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); } if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { - errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); - } - if (!errors.isEmpty()) { - errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); - return; + throw new SecurityException("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); } commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>())); }); @@ -118,8 +111,7 @@ public abstract class SWCommand { } String name = mapper != null ? mapper.value() : clazz.getTypeName(); if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) { - Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); - return; + throw new SecurityException("The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); } } commandList.add(new SubCommand(this, method, anno.description(), anno.value(), localTypeMapper)); @@ -174,19 +166,14 @@ public abstract class SWCommand { if (anno == null || anno.length == 0) return; Parameter[] parameters = method.getParameters(); - List errors = new ArrayList<>(); if (!parameterTester.test(parameters.length)) { - errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); + throw new SecurityException("The method '" + method.toString() + "' is lacking parameters or has too many"); } if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { - errors.add("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); + throw new SecurityException("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); } if (returnType != null && method.getReturnType() != returnType) { - errors.add("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); - } - if (!errors.isEmpty()) { - errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s)); - return; + throw new SecurityException("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); } Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters)); } -- 2.39.2 From 5987f71f72a94e1632c846436b9a90ee1199bd94 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 5 Jun 2021 17:07:27 +0200 Subject: [PATCH 20/22] Removing Message reflections --- .../src/de/steamwar/command/SWCommand.java | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 72a83da..2f942c2 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -19,17 +19,17 @@ package de.steamwar.command; +import de.steamwar.message.Message; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import java.lang.annotation.*; -import java.lang.reflect.*; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; -import java.util.function.BiFunction; import java.util.function.IntPredicate; import java.util.stream.Collectors; @@ -42,13 +42,22 @@ public abstract class SWCommand { private final List commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); - private BiFunction message = (s, commandSender) -> s; + private final Message message; protected SWCommand(String command) { this(command, new String[0]); } + protected SWCommand(String command, Message message) { + this(command, message, new String[0]); + } + protected SWCommand(String command, String... aliases) { + this(command, null, aliases); + } + + protected SWCommand(String command, Message message, String... aliases) { + this.message = message; this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) { @Override public boolean execute(CommandSender sender, String alias, String[] args) { @@ -72,8 +81,6 @@ public abstract class SWCommand { unregister(); register(); - messageSystem(); - List methods = methods(); for (Method method : methods) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { @@ -138,29 +145,6 @@ public abstract class SWCommand { } } - private void messageSystem() { - try { - Field field = getClass().getDeclaredField("MESSAGE"); - if (!field.getType().getTypeName().equals("de.steamwar.message.Message")) { - return; - } - AtomicReference o = new AtomicReference<>(null); - Method method = o.getClass().getMethod("parse"); - message = (s, commandSender) -> { - try { - if (o.get() == null) { - o.set(Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this)); - } - return (String) method.invoke(o.get(), s, commandSender); - } catch (IllegalAccessException | InvocationTargetException e) { - return s; - } - }; - } catch (NoSuchFieldException | NoSuchMethodException e) { - // Ignored - } - } - private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { T[] anno = SWCommandUtils.getAnnotation(method, annotation); if (anno == null || anno.length == 0) return; @@ -240,7 +224,7 @@ public abstract class SWCommand { st.append("§7..."); } if (!subCommand.description.isEmpty()) { - st.append("§8 - §7").append(message.apply(subCommand.description, sender)); + st.append("§8 - §7").append(message.parse(subCommand.description, sender)); } help.add(st.toString()); }); -- 2.39.2 From eb41edb2bfa51175e0992ecc1961440b8a24a4cd Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 5 Jun 2021 17:07:59 +0200 Subject: [PATCH 21/22] Removing Message reflections --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 2f942c2..33f9974 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -224,7 +224,7 @@ public abstract class SWCommand { st.append("§7..."); } if (!subCommand.description.isEmpty()) { - st.append("§8 - §7").append(message.parse(subCommand.description, sender)); + st.append("§8 - §7").append(message != null ? message.parse(subCommand.description, sender) : subCommand.description); } help.add(st.toString()); }); -- 2.39.2 From 8769e4bf8a8a6ac35c278988550b008711744273 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 8 Jul 2021 17:12:12 +0200 Subject: [PATCH 22/22] Fix stuff --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 33f9974..4261cb1 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -231,7 +231,7 @@ public abstract class SWCommand { } String string = "/" + command.getName() + " " + String.join(" ", args); sender.sendMessage("§7----==== §e" + command.getName() + " §7====----"); - sender.sendMessage("§7Aliases§8:§e " + String.join("§8,§e ", command.getAliases())); + sender.sendMessage("§7" + (message != null ? message.parse("COMMAND_ALIASES", sender) : "Aliases") + "§8:§e " + String.join("§8,§e ", command.getAliases())); help.forEach(s -> { if (s.replaceAll("§[0-9A-Z]", "").startsWith(string)) { sender.sendMessage(s); @@ -239,6 +239,10 @@ public abstract class SWCommand { }); } + private String translate(String description, CommandSender sender) { + return message != null ? message.parse(description, sender) : description; + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Repeatable(Register.Registeres.class) -- 2.39.2