Refactoring

This commit is contained in:
filoghost 2020-06-07 11:55:09 +02:00
parent 76b1fc6b8d
commit 15a6774ace
2 changed files with 22 additions and 28 deletions

View File

@ -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);
}
}

View File

@ -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) {