mirror of
https://github.com/aikar/commands.git
synced 2026-05-31 06:11:55 +00:00
Fix getFirstElement to not care if there is more than 1
This commit is contained in:
@@ -62,6 +62,7 @@ public class ACFBukkitUtil {
|
||||
|
||||
/**
|
||||
* Move to Message Keys on the CommandIssuer
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
@@ -75,6 +76,7 @@ public class ACFBukkitUtil {
|
||||
public static Location stringToLocation(String storedLoc) {
|
||||
return stringToLocation(storedLoc, null);
|
||||
}
|
||||
|
||||
public static Location stringToLocation(String storedLoc, World forcedWorld) {
|
||||
if (storedLoc == null) {
|
||||
return null;
|
||||
@@ -162,15 +164,18 @@ public class ACFBukkitUtil {
|
||||
public static double distance(@NotNull Entity e1, @NotNull Entity e2) {
|
||||
return distance(e1.getLocation(), e2.getLocation());
|
||||
}
|
||||
|
||||
public static double distance2d(@NotNull Entity e1, @NotNull Entity e2) {
|
||||
return distance2d(e1.getLocation(), e2.getLocation());
|
||||
}
|
||||
public static double distance2d(@NotNull Location loc1, @NotNull Location loc2) {
|
||||
|
||||
public static double distance2d(@NotNull Location loc1, @NotNull Location loc2) {
|
||||
loc1 = loc1.clone();
|
||||
loc1.setY(loc2.getY());
|
||||
return distance(loc1, loc2);
|
||||
}
|
||||
public static double distance(@NotNull Location loc1, @NotNull Location loc2) {
|
||||
|
||||
public static double distance(@NotNull Location loc1, @NotNull Location loc2) {
|
||||
if (loc1.getWorld() != loc2.getWorld()) {
|
||||
return 0;
|
||||
}
|
||||
@@ -180,9 +185,11 @@ public class ACFBukkitUtil {
|
||||
public static Location getTargetLoc(Player player) {
|
||||
return getTargetLoc(player, 128);
|
||||
}
|
||||
|
||||
public static Location getTargetLoc(Player player, int maxDist) {
|
||||
return getTargetLoc(player, maxDist, 1.5);
|
||||
}
|
||||
|
||||
public static Location getTargetLoc(Player player, int maxDist, double addY) {
|
||||
try {
|
||||
Location target = player.getTargetBlock((Set<Material>) null, maxDist).getLocation();
|
||||
@@ -196,14 +203,17 @@ public class ACFBukkitUtil {
|
||||
public static Location getRandLoc(Location loc, int radius) {
|
||||
return getRandLoc(loc, radius, radius, radius);
|
||||
}
|
||||
|
||||
public static Location getRandLoc(Location loc, int xzRadius, int yRadius) {
|
||||
return getRandLoc(loc, xzRadius, yRadius, xzRadius);
|
||||
}
|
||||
@NotNull public static Location getRandLoc(Location loc, int xRadius, int yRadius, int zRadius) {
|
||||
|
||||
@NotNull
|
||||
public static Location getRandLoc(Location loc, int xRadius, int yRadius, int zRadius) {
|
||||
Location newLoc = loc.clone();
|
||||
newLoc.setX(ACFUtil.rand(loc.getX()-xRadius, loc.getX()+xRadius));
|
||||
newLoc.setY(ACFUtil.rand(loc.getY()-yRadius, loc.getY()+yRadius));
|
||||
newLoc.setZ(ACFUtil.rand(loc.getZ()-zRadius, loc.getZ()+zRadius));
|
||||
newLoc.setX(ACFUtil.rand(loc.getX() - xRadius, loc.getX() + xRadius));
|
||||
newLoc.setY(ACFUtil.rand(loc.getY() - yRadius, loc.getY() + yRadius));
|
||||
newLoc.setZ(ACFUtil.rand(loc.getZ() - zRadius, loc.getZ() + zRadius));
|
||||
return newLoc;
|
||||
}
|
||||
|
||||
@@ -215,6 +225,7 @@ public class ACFBukkitUtil {
|
||||
public static String replaceChatString(String message, String replace, String with) {
|
||||
return replaceChatString(message, Pattern.compile(Pattern.quote(replace), Pattern.CASE_INSENSITIVE), with);
|
||||
}
|
||||
|
||||
public static String replaceChatString(String message, Pattern replace, String with) {
|
||||
final String[] split = replace.split(message + "1");
|
||||
|
||||
@@ -233,12 +244,14 @@ public class ACFBukkitUtil {
|
||||
public static boolean isWithinDistance(@NotNull Player p1, @NotNull Player p2, int dist) {
|
||||
return isWithinDistance(p1.getLocation(), p2.getLocation(), dist);
|
||||
}
|
||||
|
||||
public static boolean isWithinDistance(@NotNull Location loc1, @NotNull Location loc2, int dist) {
|
||||
return loc1.getWorld() == loc2.getWorld() && loc1.distance(loc2) <= dist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Please move to the CommandIssuer version
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public static Player findPlayerSmart(CommandSender requester, String search) {
|
||||
@@ -275,12 +288,11 @@ public class ACFBukkitUtil {
|
||||
|
||||
//noinspection Duplicates
|
||||
if (matches.isEmpty()) {
|
||||
if (confirmList.isEmpty()) {
|
||||
issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER,
|
||||
"{search}", name);
|
||||
Player player = ACFUtil.getFirstElement(confirmList);
|
||||
if (player == null) {
|
||||
issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER, "{search}", name);
|
||||
return null;
|
||||
} else {
|
||||
Player player = ACFUtil.getFirstElement(confirmList);
|
||||
issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ public final class ACFUtil {
|
||||
|
||||
public static final Random RANDOM = new Random();
|
||||
|
||||
private ACFUtil() {}
|
||||
private ACFUtil() {
|
||||
}
|
||||
|
||||
public static String padRight(String s, int n) {
|
||||
return String.format("%1$-" + n + "s", s);
|
||||
@@ -64,6 +65,7 @@ public final class ACFUtil {
|
||||
public static <T extends Enum> T getEnumFromName(T[] types, String name) {
|
||||
return getEnumFromName(types, name, null);
|
||||
}
|
||||
|
||||
public static <T extends Enum> T getEnumFromName(T[] types, String name, T def) {
|
||||
for (T type : types) {
|
||||
if (type.name().equalsIgnoreCase(name)) {
|
||||
@@ -72,6 +74,7 @@ public final class ACFUtil {
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public static <T extends Enum> T getEnumFromOrdinal(T[] types, int ordinal) {
|
||||
for (T type : types) {
|
||||
if (type.ordinal() == ordinal) {
|
||||
@@ -95,45 +98,53 @@ public final class ACFUtil {
|
||||
}
|
||||
try {
|
||||
return Double.parseDouble(var);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public static Float parseFloat(String var) {
|
||||
return parseFloat(var, null);
|
||||
}
|
||||
|
||||
public static Float parseFloat(String var, Float def) {
|
||||
if (var == null) {
|
||||
return def;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(var);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public static Long parseLong(String var) {
|
||||
return parseLong(var, null);
|
||||
}
|
||||
|
||||
public static Long parseLong(String var, Long def) {
|
||||
if (var == null) {
|
||||
return def;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(var);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public static Integer parseInt(String var) {
|
||||
return parseInt(var, null);
|
||||
}
|
||||
|
||||
public static Integer parseInt(String var, Integer def) {
|
||||
if (var == null) {
|
||||
return def;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(var);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
@@ -149,9 +160,11 @@ public final class ACFUtil {
|
||||
public static String join(Collection<String> args) {
|
||||
return ApacheCommonsLangUtil.join(args, " ");
|
||||
}
|
||||
|
||||
public static String join(Collection<String> args, String sep) {
|
||||
return ApacheCommonsLangUtil.join(args, sep);
|
||||
}
|
||||
|
||||
public static String join(String[] args) {
|
||||
return join(args, 0, ' ');
|
||||
}
|
||||
@@ -159,6 +172,7 @@ public final class ACFUtil {
|
||||
public static String join(String[] args, String sep) {
|
||||
return ApacheCommonsLangUtil.join(args, sep);
|
||||
}
|
||||
|
||||
public static String join(String[] args, char sep) {
|
||||
return join(args, 0, sep);
|
||||
}
|
||||
@@ -192,8 +206,9 @@ public final class ACFUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int roundUp(int num, int multiple) {
|
||||
if(multiple == 0) {
|
||||
if (multiple == 0) {
|
||||
return num;
|
||||
}
|
||||
|
||||
@@ -211,6 +226,7 @@ public final class ACFUtil {
|
||||
|
||||
/**
|
||||
* Plain string replacement, escapes replace value.
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -222,6 +238,7 @@ public final class ACFUtil {
|
||||
|
||||
/**
|
||||
* Regex version of {@link #replace(String, Pattern, String)}
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -233,6 +250,7 @@ public final class ACFUtil {
|
||||
|
||||
/**
|
||||
* Plain String replacement. If you need regex patterns, see {@link #replacePattern(String, String, String)}
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -244,6 +262,7 @@ public final class ACFUtil {
|
||||
|
||||
/**
|
||||
* Regex version of {@link #replace(String, String, String)}
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -252,8 +271,10 @@ public final class ACFUtil {
|
||||
public static String replacePattern(String string, String pattern, String repl) {
|
||||
return replace(string, ACFPatterns.getPattern(pattern), repl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure Regex Pattern matching and replacement, no escaping
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -265,6 +286,7 @@ public final class ACFUtil {
|
||||
|
||||
/**
|
||||
* Pure Regex Pattern matching and replacement, no escaping
|
||||
*
|
||||
* @param string
|
||||
* @param pattern
|
||||
* @param repl
|
||||
@@ -280,19 +302,20 @@ public final class ACFUtil {
|
||||
}
|
||||
for (int i = 0; i < replacements.length; i += 2) {
|
||||
String key = replacements[i];
|
||||
String value = replacements[i+1];
|
||||
String value = replacements[i + 1];
|
||||
if (value == null) value = "";
|
||||
string = replace(string, key, value);
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public static String replacePatterns(String string, String... replacements) {
|
||||
if (replacements.length < 2 || replacements.length % 2 != 0) {
|
||||
throw new IllegalArgumentException("Invalid Replacements");
|
||||
}
|
||||
for (int i = 0; i < replacements.length; i += 2) {
|
||||
String key = replacements[i];
|
||||
String value = replacements[i+1];
|
||||
String value = replacements[i + 1];
|
||||
if (value == null) value = "";
|
||||
string = replacePattern(string, key, value);
|
||||
}
|
||||
@@ -302,6 +325,7 @@ public final class ACFUtil {
|
||||
public static String capitalize(String str, char[] delimiters) {
|
||||
return ApacheCommonsLangUtil.capitalize(str, delimiters);
|
||||
}
|
||||
|
||||
private static boolean isDelimiter(char ch, char[] delimiters) {
|
||||
return ApacheCommonsLangUtil.isDelimiter(ch, delimiters);
|
||||
}
|
||||
@@ -312,6 +336,7 @@ public final class ACFUtil {
|
||||
}
|
||||
return arr.get(RANDOM.nextInt(arr.size()));
|
||||
}
|
||||
|
||||
public static <T> T random(T[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return null;
|
||||
@@ -322,6 +347,7 @@ public final class ACFUtil {
|
||||
/**
|
||||
* Added as im sure we will try to "Find this" again. This is no different than Enum.values() passed to above method logically
|
||||
* but the array version is slightly faster.
|
||||
*
|
||||
* @param enm
|
||||
* @param <T>
|
||||
* @return
|
||||
@@ -375,11 +401,11 @@ public final class ACFUtil {
|
||||
}
|
||||
|
||||
public static String rtrim(String s) {
|
||||
int i = s.length()-1;
|
||||
int i = s.length() - 1;
|
||||
while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
|
||||
i--;
|
||||
}
|
||||
return s.substring(0,i+1);
|
||||
return s.substring(0, i + 1);
|
||||
}
|
||||
|
||||
public static List<String> enumNames(Enum<?>[] values) {
|
||||
@@ -393,6 +419,7 @@ public final class ACFUtil {
|
||||
public static String combine(String[] args) {
|
||||
return combine(args, 0);
|
||||
}
|
||||
|
||||
public static String combine(String[] args, int start) {
|
||||
int size = 0;
|
||||
for (int i = start; i < args.length; i++) {
|
||||
@@ -406,7 +433,8 @@ public final class ACFUtil {
|
||||
}
|
||||
|
||||
|
||||
@Nullable public static <E extends Enum<E>> E simpleMatch(Class<? extends Enum<?>> list, String item) {
|
||||
@Nullable
|
||||
public static <E extends Enum<E>> E simpleMatch(Class<? extends Enum<?>> list, String item) {
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -467,7 +495,7 @@ public final class ACFUtil {
|
||||
List<T> list = new ArrayList<>();
|
||||
|
||||
for (T t : list1) {
|
||||
if(list2.contains(t)) {
|
||||
if (list2.contains(t)) {
|
||||
list.add(t);
|
||||
}
|
||||
}
|
||||
@@ -482,6 +510,7 @@ public final class ACFUtil {
|
||||
/**
|
||||
* 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
|
||||
@@ -570,7 +599,7 @@ public final class ACFUtil {
|
||||
|
||||
public static void sneaky(Throwable t) {
|
||||
//noinspection RedundantTypeArguments
|
||||
throw ACFUtil.<RuntimeException>superSneaky( t );
|
||||
throw ACFUtil.<RuntimeException>superSneaky(t);
|
||||
}
|
||||
|
||||
private static <T extends Throwable> T superSneaky(Throwable t) throws T {
|
||||
@@ -590,13 +619,15 @@ public final class ACFUtil {
|
||||
}
|
||||
|
||||
public static <T> T getFirstElement(Iterable<T> iterable) {
|
||||
if (iterable == null) {
|
||||
return null;
|
||||
}
|
||||
Iterator<T> iterator = iterable.iterator();
|
||||
T first = iterator.next();
|
||||
if (!iterator.hasNext()) {
|
||||
return first;
|
||||
if (iterator.hasNext()) {
|
||||
return iterator.next();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Expected one element in iterable");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ApplyModifierToNumber {
|
||||
@@ -620,16 +651,16 @@ public final class ACFUtil {
|
||||
public ApplyModifierToNumber invoke() {
|
||||
mod = 1;
|
||||
if (suffixes) {
|
||||
switch (num.charAt(num.length()-1)) {
|
||||
switch (num.charAt(num.length() - 1)) {
|
||||
case 'M':
|
||||
case 'm':
|
||||
mod = 1000000D;
|
||||
num = num.substring(0, num.length()-1);
|
||||
num = num.substring(0, num.length() - 1);
|
||||
break;
|
||||
case 'K':
|
||||
case 'k':
|
||||
mod = 1000D;
|
||||
num = num.substring(0, num.length()-1);
|
||||
num = num.substring(0, num.length() - 1);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
||||
@@ -522,7 +522,7 @@ public abstract class BaseCommand {
|
||||
}
|
||||
|
||||
Set<RegisteredCommand> defaultCommands = subCommands.get(DEFAULT);
|
||||
RegisteredCommand defCommand = !defaultCommands.isEmpty() ? ACFUtil.getFirstElement(defaultCommands) : null;
|
||||
RegisteredCommand defCommand = ACFUtil.getFirstElement(defaultCommands);
|
||||
if (defCommand != null && (args.length == 0 || defCommand.consumeInputResolvers > 0)) {
|
||||
findAndExecuteCommand(commandContext, DEFAULT, issuer, args);
|
||||
} else if (subCommands.get(CATCHUNKNOWN) != null) {
|
||||
|
||||
@@ -113,26 +113,25 @@ public interface RootCommand {
|
||||
|
||||
default BaseCommand getBaseCommand(String[] args) {
|
||||
SetMultimap<String, RegisteredCommand> subCommands = getSubCommands();
|
||||
Set<RegisteredCommand> registeredCommands;
|
||||
RegisteredCommand command;
|
||||
for (int i = args.length; i >= 0; i--) {
|
||||
String checkSub = ApacheCommonsLangUtil.join(args, " ", 0, i).toLowerCase();
|
||||
registeredCommands = subCommands.get(checkSub);
|
||||
if (!registeredCommands.isEmpty()) {
|
||||
return ACFUtil.getFirstElement(registeredCommands).scope;
|
||||
command = ACFUtil.getFirstElement(subCommands.get(checkSub));
|
||||
if (command != null) {
|
||||
return command.scope;
|
||||
}
|
||||
}
|
||||
|
||||
registeredCommands = subCommands.get(DEFAULT);
|
||||
if (!registeredCommands.isEmpty()) {
|
||||
RegisteredCommand command = ACFUtil.getFirstElement(registeredCommands);
|
||||
command = ACFUtil.getFirstElement(subCommands.get(DEFAULT));
|
||||
if (command != null) {
|
||||
if (args.length == 0 || command.consumeInputResolvers > 0) {
|
||||
return command.scope;
|
||||
}
|
||||
}
|
||||
|
||||
registeredCommands = subCommands.get(CATCHUNKNOWN);
|
||||
if (!registeredCommands.isEmpty()) {
|
||||
return ACFUtil.getFirstElement(registeredCommands).scope;
|
||||
command = ACFUtil.getFirstElement(subCommands.get(CATCHUNKNOWN));
|
||||
if (command != null) {
|
||||
return command.scope;
|
||||
}
|
||||
return getDefCommand();
|
||||
}
|
||||
|
||||
@@ -39,12 +39,12 @@ public class ACFSpongeUtil {
|
||||
}
|
||||
|
||||
if (matches.isEmpty()) {
|
||||
if (confirmList.isEmpty()) {
|
||||
issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER,
|
||||
"{search}", name);
|
||||
Player player = ACFUtil.getFirstElement(confirmList);
|
||||
if (player == null) {
|
||||
issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER, "{search}", name);
|
||||
return null;
|
||||
} else {
|
||||
Player player = ACFUtil.getFirstElement(confirmList);
|
||||
|
||||
issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user