From 15a6774acefc3b78e558edd41caab94405074af0 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 7 Jun 2020 11:55:09 +0200 Subject: [PATCH] Refactoring --- .../listener/CommandListener.java | 31 +++++++++++++------ .../chestcommands/util/StringUtils.java | 19 ------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/listener/CommandListener.java b/Plugin/src/main/java/me/filoghost/chestcommands/listener/CommandListener.java index d10f92b..cd18f1d 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/listener/CommandListener.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/listener/CommandListener.java @@ -21,7 +21,6 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent; import me.filoghost.chestcommands.MenuManager; import me.filoghost.chestcommands.internal.ExtendedIconMenu; -import me.filoghost.chestcommands.util.StringUtils; public class CommandListener implements Listener { @@ -33,18 +32,32 @@ public class CommandListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onCommand(PlayerCommandPreprocessEvent event) { - // Very fast method compared to split & substring - String command = StringUtils.getCleanCommand(event.getMessage()); - - if (command.isEmpty()) { + String command = getCommandName(event.getMessage()); + + if (command == null) { return; } ExtendedIconMenu menu = menuManager.getMenuByCommand(command); - - if (menu != null) { - event.setCancelled(true); - menu.openCheckingPermission(event.getPlayer()); + + if (menu == null) { + return; + } + + event.setCancelled(true); + menu.openCheckingPermission(event.getPlayer()); + } + + private static String getCommandName(String fullCommand) { + if (!fullCommand.startsWith("/")) { + return null; + } + + int firstSpace = fullCommand.indexOf(' '); + if (firstSpace >= 1) { + return fullCommand.substring(1, firstSpace); + } else { + return fullCommand.substring(1); } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/util/StringUtils.java b/Plugin/src/main/java/me/filoghost/chestcommands/util/StringUtils.java index 4b0cb2c..3d31cff 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/util/StringUtils.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/util/StringUtils.java @@ -18,25 +18,6 @@ public final class StringUtils { private StringUtils() {} - // Removes the first slash, and returns the all the chars until a space is encontered - public static String getCleanCommand(String message) { - char[] chars = message.toCharArray(); - - if (chars.length <= 1) { - return ""; - } - - int pos = 0; - for (int i = 1; i < chars.length; i++) { - if (chars[i] == ' ') { - break; - } - - chars[(pos++)] = chars[i]; - } - - return new String(chars, 0, pos); - } public static String stripChars(String input, char... removed) { if (input == null || input.isEmpty() || removed.length == 0) {