From 2ff4d919a4996c28c75d509e16165be1b4b0e65e Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 7 Jan 2017 15:07:03 -0500 Subject: [PATCH] Restructure things --- .../commands/{Log.java => CommandLog.java} | 4 +- .../java/co/aikar/commands/CommandUtil.java | 177 ++++++++++++++++-- src/main/java/co/aikar/commands/NumUtil.java | 111 ----------- .../co/aikar/commands/RegisteredCommand.java | 2 +- src/main/java/co/aikar/commands/UserUtil.java | 68 ------- .../commands/contexts/CommandContexts.java | 7 +- 6 files changed, 172 insertions(+), 197 deletions(-) rename src/main/java/co/aikar/commands/{Log.java => CommandLog.java} (96%) delete mode 100644 src/main/java/co/aikar/commands/NumUtil.java delete mode 100644 src/main/java/co/aikar/commands/UserUtil.java diff --git a/src/main/java/co/aikar/commands/Log.java b/src/main/java/co/aikar/commands/CommandLog.java similarity index 96% rename from src/main/java/co/aikar/commands/Log.java rename to src/main/java/co/aikar/commands/CommandLog.java index 24f277c2..7353d59b 100644 --- a/src/main/java/co/aikar/commands/Log.java +++ b/src/main/java/co/aikar/commands/CommandLog.java @@ -11,10 +11,10 @@ import org.apache.commons.lang.exception.ExceptionUtils; import java.util.logging.Logger; -public final class Log { +public final class CommandLog { public static Logger LOGGER; - private Log() {} + private CommandLog() {} public static void log(String message) { diff --git a/src/main/java/co/aikar/commands/CommandUtil.java b/src/main/java/co/aikar/commands/CommandUtil.java index 6b01beb3..7cc8e673 100644 --- a/src/main/java/co/aikar/commands/CommandUtil.java +++ b/src/main/java/co/aikar/commands/CommandUtil.java @@ -7,6 +7,7 @@ package co.aikar.commands; +import com.google.common.collect.Iterables; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.WordUtils; import org.bukkit.Bukkit; @@ -26,6 +27,7 @@ import java.text.Normalizer.Form; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Set; @@ -74,7 +76,7 @@ public final class CommandUtil { message = color(message); if (player == null) { for (String msg : Patterns.NEWLINE.split(message)) { - Log.info(msg); + CommandLog.info(msg); } } else { for (String msg : Patterns.NEWLINE.split(message)) { @@ -123,15 +125,15 @@ public final class CommandUtil { return (new StringBuilder(64)) .append(loc.getWorld().getName()) .append(':') - .append(NumUtil.precision(loc.getX(), 4)) + .append(precision(loc.getX(), 4)) .append(':') - .append(NumUtil.precision(loc.getY(), 4)) + .append(precision(loc.getY(), 4)) .append(':') - .append(NumUtil.precision(loc.getZ(), 4)) + .append(precision(loc.getZ(), 4)) .append(':') - .append(NumUtil.precision(loc.getPitch(), 4)) + .append(precision(loc.getPitch(), 4)) .append(':') - .append(NumUtil.precision(loc.getYaw(), 4)) + .append(precision(loc.getYaw(), 4)) .toString(); } @@ -148,9 +150,9 @@ public final class CommandUtil { .append(':') .append(loc.getBlockZ()) .append(':') - .append(NumUtil.precision(loc.getPitch(), 4)) + .append(precision(loc.getPitch(), 4)) .append(':') - .append(NumUtil.precision(loc.getYaw(), 4)) + .append(precision(loc.getYaw(), 4)) .toString(); } @@ -606,9 +608,9 @@ public final class CommandUtil { } @NotNull public static Location getRandLoc(Location loc, int xRadius, int yRadius, int zRadius) { Location newLoc = loc.clone(); - newLoc.setX(NumUtil.rand(loc.getX()-xRadius, loc.getX()+xRadius)); - newLoc.setY(NumUtil.rand(loc.getY()-yRadius, loc.getY()+yRadius)); - newLoc.setZ(NumUtil.rand(loc.getZ()-zRadius, loc.getZ()+zRadius)); + newLoc.setX(rand(loc.getX()-xRadius, loc.getX()+xRadius)); + newLoc.setY(rand(loc.getY()-yRadius, loc.getY()+yRadius)); + newLoc.setZ(rand(loc.getZ()-zRadius, loc.getZ()+zRadius)); return newLoc; } @@ -681,4 +683,157 @@ public final class CommandUtil { return list; } + + public static int rand(int min, int max) { + return min + RANDOM.nextInt(max - min + 1); + } + + /** + * Calculate random between 2 points, excluding a center + * ex: Util.rand(-12, -6, 6, 12) would not return -5 to 5 + * @param min1 + * @param max1 + * @param min2 + * @param max2 + * @return + */ + public static int rand(int min1, int max1, int min2, int max2) { + return randBool() ? rand(min1, max1) : rand(min2, max2); + } + + public static double rand(double min, double max) { + return RANDOM.nextDouble() * (max - min) + min; + } + + public static boolean isNumber(String str) { + return StringUtils.isNumeric(str); + } + + public static String intToRoman(int integer) { + if (integer == 1) { + return "I"; + } + if (integer == 2) { + return "II"; + } + if (integer == 3) { + return "III"; + } + if (integer == 4) { + return "IV"; + } + if (integer == 5) { + return "V"; + } + if (integer == 6) { + return "VI"; + } + if (integer == 7) { + return "VII"; + } + if (integer == 8) { + return "VIII"; + } + if (integer == 9) { + return "IX"; + } + if (integer == 10) { + return "X"; + } + return null; + } + + public static boolean isInteger(String string) { + if (!Patterns.INTEGER.matcher(string).matches()) { + return false; + } + return true; + } + + public static boolean isFloat(String string) { + try { + Float.parseFloat(string); + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean isDouble(String string) { + try { + Double.parseDouble(string); + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean isBetween(float num, double min, double max) { + if (num >= min && num <= max){ + return true; + } else { + return false; + } + } + + public static double precision(double x, int p) { + double pow = Math.pow(10, p); + return Math.round(x * pow) / pow; + } + + public static Player findPlayerSmart(CommandSender requester, String origname) { + String name = replace(origname, ":confirm", ""); + if (name.length() < 3) { + requester.sendMessage("§cUsername too short, must be at least three characters"); + return null; + } + if (!isValidName(name)) { + requester.sendMessage("§c'" + name + "' is not a valid username"); + return null; + } + + List matches = Bukkit.getServer().matchPlayer(name); + List confirmList = new ArrayList<>(); + + // Remove confirmList players from smart matching. + Iterator iter = matches.iterator(); + while (iter.hasNext()) { + Player player = iter.next(); + if (requester instanceof Player && !((Player) requester).canSee(player)) { + if (requester.hasPermission("command.seevanish")) { + if (!origname.endsWith(":confirm")) { + confirmList.add(player); + iter.remove(); + } + } else { + iter.remove(); + } + } + } + + if (matches.size() > 1 || confirmList.size() > 1) { + requester.sendMessage("§cMultiple players matched '" + name + "', please be more specific"); + return null; + } + + if (matches.isEmpty()) { + if (confirmList.isEmpty()) { + requester.sendMessage("§cNo player matching '" + name + "' is connected to this server"); + return null; + } else { + Player player = Iterables.getOnlyElement(confirmList); + sendMsg(requester, + "&cWarning: " + player.getDisplayName() + "&c is confirmList. Do not blow their cover!\n" + + "&cTo confirm your action, add &f:confirm&c to the end of their name. \n" + + "&bEx: &e/g " + player.getName() + ":confirm"); + return null; + } + } + + return matches.get(0); + } + + public static boolean isValidName(String name) { + return name != null && !name.isEmpty() && Patterns.VALID_NAME_PATTERN.matcher(name).matches(); + } } diff --git a/src/main/java/co/aikar/commands/NumUtil.java b/src/main/java/co/aikar/commands/NumUtil.java deleted file mode 100644 index c640fc26..00000000 --- a/src/main/java/co/aikar/commands/NumUtil.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2016. Starlis LLC / dba Empire Minecraft - * - * This source code is proprietary software and must not be redistributed without Starlis LLC's approval - * - */ - -package co.aikar.commands; - -import org.apache.commons.lang.StringUtils; - -public final class NumUtil { - private NumUtil() {} - - public static int rand(int min, int max) { - return min + CommandUtil.RANDOM.nextInt(max - min + 1); - } - - /** - * Calculate random between 2 points, excluding a center - * ex: Util.rand(-12, -6, 6, 12) would not return -5 to 5 - * @param min1 - * @param max1 - * @param min2 - * @param max2 - * @return - */ - public static int rand(int min1, int max1, int min2, int max2) { - return CommandUtil.randBool() ? rand(min1, max1) : rand(min2, max2); - } - - public static double rand(double min, double max) { - return CommandUtil.RANDOM.nextDouble() * (max - min) + min; - } - - public static boolean isNumber(String str) { - return StringUtils.isNumeric(str); - } - - public static String intToRoman(int integer) { - if (integer == 1) { - return "I"; - } - if (integer == 2) { - return "II"; - } - if (integer == 3) { - return "III"; - } - if (integer == 4) { - return "IV"; - } - if (integer == 5) { - return "V"; - } - if (integer == 6) { - return "VI"; - } - if (integer == 7) { - return "VII"; - } - if (integer == 8) { - return "VIII"; - } - if (integer == 9) { - return "IX"; - } - if (integer == 10) { - return "X"; - } - return null; - } - - public static boolean isInteger(String string) { - if (!Patterns.INTEGER.matcher(string).matches()) { - return false; - } - return true; - } - - public static boolean isFloat(String string) { - try { - Float.parseFloat(string); - return true; - } catch (Exception e) { - return false; - } - } - - public static boolean isDouble(String string) { - try { - Double.parseDouble(string); - return true; - } catch (Exception e) { - return false; - } - } - - public static boolean isBetween(float num, double min, double max) { - if (num >= min && num <= max){ - return true; - } else { - return false; - } - } - - public static double precision(double x, int p) { - double pow = Math.pow(10, p); - return Math.round(x * pow) / pow; - } -} diff --git a/src/main/java/co/aikar/commands/RegisteredCommand.java b/src/main/java/co/aikar/commands/RegisteredCommand.java index f90170d6..8736801f 100644 --- a/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -170,7 +170,7 @@ public class RegisteredCommand { } } else { CommandUtil.sendMsg(sender, "&cI'm sorry, but there was an error performing this command."); - Log.exception("Exception in command: " + command + " " + CommandUtil.join(args), e.getCause()); + CommandLog.exception("Exception in command: " + command + " " + CommandUtil.join(args), e.getCause()); } } } diff --git a/src/main/java/co/aikar/commands/UserUtil.java b/src/main/java/co/aikar/commands/UserUtil.java deleted file mode 100644 index 71bcad12..00000000 --- a/src/main/java/co/aikar/commands/UserUtil.java +++ /dev/null @@ -1,68 +0,0 @@ -package co.aikar.commands; - -import com.google.common.collect.Iterables; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class UserUtil { - public static Player findPlayerSmart(CommandSender requester, String origname) { - String name = CommandUtil.replace(origname, ":confirm", ""); - if (name.length() < 3) { - requester.sendMessage("§cUsername too short, must be at least three characters"); - return null; - } - if (!isValidName(name)) { - requester.sendMessage("§c'" + name + "' is not a valid username"); - return null; - } - - List matches = Bukkit.getServer().matchPlayer(name); - List confirmList = new ArrayList<>(); - - // Remove confirmList players from smart matching. - Iterator iter = matches.iterator(); - while (iter.hasNext()) { - Player player = iter.next(); - if (requester instanceof Player && !((Player) requester).canSee(player)) { - if (requester.hasPermission("command.seevanish")) { - if (!origname.endsWith(":confirm")) { - confirmList.add(player); - iter.remove(); - } - } else { - iter.remove(); - } - } - } - - if (matches.size() > 1 || confirmList.size() > 1) { - requester.sendMessage("§cMultiple players matched '" + name + "', please be more specific"); - return null; - } - - if (matches.isEmpty()) { - if (confirmList.isEmpty()) { - requester.sendMessage("§cNo player matching '" + name + "' is connected to this server"); - return null; - } else { - Player player = Iterables.getOnlyElement(confirmList); - CommandUtil.sendMsg(requester, - "&cWarning: " + player.getDisplayName() + "&c is confirmList. Do not blow their cover!\n" + - "&cTo confirm your action, add &f:confirm&c to the end of their name. \n" + - "&bEx: &e/g " + player.getName() + ":confirm"); - return null; - } - } - - return matches.get(0); - } - - public static boolean isValidName(String name) { - return name != null && !name.isEmpty() && Patterns.VALID_NAME_PATTERN.matcher(name).matches(); - } -} diff --git a/src/main/java/co/aikar/commands/contexts/CommandContexts.java b/src/main/java/co/aikar/commands/contexts/CommandContexts.java index 86fdcac7..3d9d36ea 100644 --- a/src/main/java/co/aikar/commands/contexts/CommandContexts.java +++ b/src/main/java/co/aikar/commands/contexts/CommandContexts.java @@ -12,10 +12,9 @@ import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Split; import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Values; -import co.aikar.commands.Log; +import co.aikar.commands.CommandLog; import co.aikar.commands.Patterns; import co.aikar.commands.SneakyThrow; -import co.aikar.commands.UserUtil; import co.aikar.commands.CommandUtil; import com.google.common.collect.Maps; import org.bukkit.Bukkit; @@ -107,7 +106,7 @@ public final class CommandContexts { registerContext(OnlinePlayer.class, (c) -> { final String playercheck = c.popFirstArg(); - Player player = UserUtil.findPlayerSmart(c.getSender(), playercheck); + Player player = CommandUtil.findPlayerSmart(c.getSender(), playercheck); if (player == null) { CommandUtil.sendMsg(c.getSender(), "&cCould not find a player by the name " + playercheck); throw new InvalidCommandArgument(false); @@ -171,7 +170,7 @@ public final class CommandContexts { } } while ((type = type.getSuperclass()) != null); - Log.exception(new InvalidConfigurationException("No context resolver defined for " + rootType.getName())); + CommandLog.exception(new InvalidConfigurationException("No context resolver defined for " + rootType.getName())); return null; } private static boolean isValidItem(ItemStack item) {