public static String replaceStrings(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]; if (value == null) value = ""; string = replace(string, key, value); } return string; } public static String replacePatterns(String string, String... replacements) {
@Nullable public static <E extends Enum<E>> E simpleMatch(Class<? extends Enum<?>> list, String item) { if (item == null) { return null; } item = ACFUtil.simplifyString(item); for (Enum<?> s : list.getEnumConstants()) { String simple = ACFUtil.simplifyString(s.name()); if (item.equals(simple)) { //noinspection unchecked return (E) s; } } return null; }
public static List<String> enumNames(Class<? extends Enum<?>> cls) { return enumNames(cls.getEnumConstants()); }
/** * 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); }
filter = ACFUtil.simplifyString(filter); String finalFilter = filter; colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.name()))); ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first); if (match == null) { String valid = colors .map(color -> "<c2>" + ACFUtil.simplifyString(color.name()) + "</c2>") .collect(Collectors.joining("<c1>,</c1> ")); Double x = ACFUtil.parseDouble(split[0]); Double y = ACFUtil.parseDouble(split[1]); Double z = ACFUtil.parseDouble(split[2]); Float yaw = ACFUtil.parseFloat(split[3]); Float pitch = ACFUtil.parseFloat(split[4]); Matcher matcher = versionPattern.matcher(Bukkit.getVersion()); if (matcher.find()) { int mcMajorVersion = ACFUtil.parseInt(matcher.toMatchResult().group(1), 0); int mcMinorVersion = ACFUtil.parseInt(matcher.toMatchResult().group(2), 0); manager.log(LogLevel.INFO, "Minecraft Version: " + mcMajorVersion + "." + mcMinorVersion); if (mcMajorVersion >= 1 && mcMinorVersion >= 12) {
registerContext(BigDecimal.class, (c) -> { try { BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes")); validateMinMax(c, number); return number; registerContext(BigInteger.class, (c) -> { try { BigDecimal number = ACFUtil.parseBigNumber(c.popFirstArg(), c.hasFlag("suffixes")); validateMinMax(c, number); return number.toBigIntegerExact(); registerContext(Boolean.class, (c) -> ACFUtil.isTruthy(c.popFirstArg())); registerContext(boolean.class, (c) -> ACFUtil.isTruthy(c.popFirstArg())); registerContext(char.class, c -> { String s = c.popFirstArg(); ACFUtil.join(c.getArgs()) val = ACFUtil.join(args); } else { val = c.popFirstArg(); ACFUtil.sneaky(new IllegalStateException("Weird Command signature... String[] should be last or @Split")); Enum<?> match = ACFUtil.simpleMatch(enumCls, first); if (match == null) { List<String> names = ACFUtil.enumNames(enumCls); throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", ACFUtil.join(names));
return null; String name = ACFUtil.replace(search, ":confirm", ""); if (name.length() < 3) { issuer.sendError(MinecraftMessageKeys.USERNAME_TOO_SHORT); return null; } else { Player player = ACFUtil.getFirstElement(confirmList); issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName()); return null;
filter = ACFUtil.simplifyString(filter); String finalFilter = filter; colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.name()))); ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first); if (match == null) { String valid = colors .map(color -> "<c2>" + ACFUtil.simplifyString(color.name()) + "</c2>") .collect(Collectors.joining("<c1>,</c1> "));
registerAsyncCompletion("mobs", c -> { final Stream<String> normal = Stream.of(EntityType.values()) .map(entityType -> ACFUtil.simplifyString(entityType.getName())); return normal.collect(Collectors.toList()); }); .map(ACFUtil::simplifyString).collect(Collectors.toSet()); colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color.name()))); return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList()); }); registerAsyncCompletion("dyecolors", c -> ACFUtil.enumNames(DyeColor.values())); registerCompletion("worlds", c -> ( Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList())
public static Integer parseInt(String var) { return parseInt(var, null); } public static Integer parseInt(String var, Integer def) {
/** * @deprecated Feature Not done yet * @param id * @param classes * @return */ CommandCompletionHandler setDefaultCompletion(String id, Class... classes) { // get completion with specified id id = id.toLowerCase(); CommandCompletionHandler completion = completionMap.get(id); if(completion == null) { // Throw something because no completion with specified id ACFUtil.sneaky(new CommandCompletionTextLookupException()); } for(Class clazz : classes) { defaultCompletions.put(clazz, id); } return completion; }
RegisteredCommand cmd = null; if (cmds.size() == 1) { cmd = ACFUtil.getFirstElement(cmds); } else { Optional<RegisteredCommand> optCmd = cmds.stream().filter(c -> {
public static Double parseDouble(String var) { return parseDouble(var, null); }
public static Float parseFloat(String var) { return parseFloat(var, null); } public static Float parseFloat(String var, Float def) {
@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)); return newLoc; }
return null; String name = ACFUtil.replace(search, ":confirm", ""); return null; } else { Player player = ACFUtil.getFirstElement(confirmList); issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName()); return null;
public Integer getConfigValue(String flag, Integer def) { return ACFUtil.parseInt(this.configs.get(flag), def); } }
@NotNull private CommandMap hookCommandMap() { CommandMap commandMap = null; try { Server server = Bukkit.getServer(); Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap"); getCommandMap.setAccessible(true); commandMap = (CommandMap) getCommandMap.invoke(server); if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) { this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName()); this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory."); this.log(LogLevel.ERROR, "We can not guarantee things are going to work."); Field cmField = server.getClass().getDeclaredField("commandMap"); commandMap = new ProxyCommandMap(this, commandMap); cmField.set(server, commandMap); this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck..."); } Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands"); knownCommands.setAccessible(true); //noinspection unchecked this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap); } catch (Exception e) { this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function."); ACFUtil.sneaky(e); } return commandMap; }
cmds.addAll(completeCommand(issuer, search.cmd, Arrays.copyOfRange(args, search.argIndex, args.length), commandLabel, isAsync)); } else if (subCommands.get(CATCHUNKNOWN).size() == 1) { cmds.addAll(completeCommand(issuer, ACFUtil.getFirstElement(subCommands.get(CATCHUNKNOWN)), args, commandLabel, isAsync)); } else if (subCommands.get(DEFAULT).size() == 1) { cmds.addAll(completeCommand(issuer, ACFUtil.getFirstElement(subCommands.get(DEFAULT)), args, commandLabel, isAsync));
public Double getFlagValue(String flag, Double def) { return ACFUtil.parseDouble(this.flags.get(flag), def); }