From c02047aaf821964e986ef018ac52a5a36b269a9e Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 24 Apr 2021 20:54:38 +0200 Subject: [PATCH] Change group id to com.viaversion --- .../java/us/myles/ViaVersion/api/Via.java | 62 +++++++++++-------- build.gradle.kts | 2 +- .../commands/ViaCommandHandler.java | 42 +++++++------ 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/api/src/main/java/us/myles/ViaVersion/api/Via.java b/api/src/main/java/us/myles/ViaVersion/api/Via.java index 00d8521d0..9f2689e03 100644 --- a/api/src/main/java/us/myles/ViaVersion/api/Via.java +++ b/api/src/main/java/us/myles/ViaVersion/api/Via.java @@ -26,44 +26,56 @@ import com.google.common.base.Preconditions; import us.myles.ViaVersion.ViaManager; import us.myles.ViaVersion.api.platform.ViaPlatform; -public class Via { +public final class Via { private static ViaManager manager; + /** + * Returns the API associated with the current platform. + * + * @return API instance + * @throws IllegalArgumentException if the platform has not loaded yet + */ + public static ViaAPI getAPI() { + return manager().getPlatform().getApi(); + } + + /** + * Returns the ViaManager with methods beyond the simple API {@link ViaAPI} provides. + * + * @return manager to interact with various Via parts + * @throws IllegalArgumentException if the platform has not loaded yet + */ + public static ViaManager getManager() { + return manager(); + } + + /** + * Returns the config associated with the current platform. + * + * @return config instance + * @throws IllegalArgumentException if the platform has not loaded yet + */ + public static ViaVersionConfig getConfig() { + return manager().getPlatform().getConf(); + } + + public static ViaPlatform getPlatform() { + return manager().getPlatform(); + } + /** * Register the ViaManager associated with the platform. * * @param viaManager The ViaManager + * @throws IllegalArgumentException if the manager has already been set */ public static void init(ViaManager viaManager) { Preconditions.checkArgument(manager == null, "ViaManager is already set"); Via.manager = viaManager; } - /** - * Returns the API associated with the current platform. - * - * @return API instance - */ - public static ViaAPI getAPI() { + private static ViaManager manager() { Preconditions.checkArgument(manager != null, "ViaVersion has not loaded the platform yet"); - return manager.getPlatform().getApi(); - } - - /** - * Get the config associated with the current platform. - * - * @return Config instance - */ - public static ViaVersionConfig getConfig() { - Preconditions.checkArgument(manager != null, "ViaVersion has not loaded the platform yet"); - return manager.getPlatform().getConf(); - } - - public static ViaPlatform getPlatform() { - return manager.getPlatform(); - } - - public static ViaManager getManager() { return manager; } } diff --git a/build.gradle.kts b/build.gradle.kts index 8fafec1fb..946b9ec6a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } allprojects { - group = "us.myles" + group = "com.viaversion" version = "4.0.0-21w16a" description = "Allow newer clients to join older server versions." } diff --git a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index d68e6a5f5..4a3b56f72 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -45,22 +45,16 @@ import java.util.Set; import static us.myles.ViaVersion.api.command.ViaSubCommand.color; public abstract class ViaCommandHandler implements ViaVersionCommand { - private final Map commandMap; + private final Map commandMap = new HashMap<>(); - public ViaCommandHandler() { - commandMap = new HashMap<>(); - try { - registerDefaults(); - } catch (Exception e) { - //ignore never throws exception because it doesn't exists - } + protected ViaCommandHandler() { + registerDefaults(); } @Override - public void registerSubCommand(ViaSubCommand command) throws Exception { + public void registerSubCommand(ViaSubCommand command) { Preconditions.checkArgument(command.name().matches("^[a-z0-9_-]{3,15}$"), command.name() + " is not a valid sub-command name."); - if (hasSubCommand(command.name())) - throw new Exception("ViaSubCommand " + command.name() + " does already exists!"); //Maybe another exception later. + Preconditions.checkArgument(!hasSubCommand(command.name()), "ViaSubCommand " + command.name() + " does already exists!"); commandMap.put(command.name().toLowerCase(Locale.ROOT), command); } @@ -95,8 +89,9 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { String[] subArgs = Arrays.copyOfRange(args, 1, args.length); boolean result = handler.execute(sender, subArgs); - if (!result) + if (!result) { sender.sendMessage("Usage: /viaversion " + handler.usage()); + } return result; } @@ -108,20 +103,24 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { //SubCommands tabcomplete if (args.length == 1) { if (!args[0].isEmpty()) { - for (ViaSubCommand sub : allowed) - if (sub.name().toLowerCase().startsWith(args[0].toLowerCase(Locale.ROOT))) + for (ViaSubCommand sub : allowed) { + if (sub.name().toLowerCase().startsWith(args[0].toLowerCase(Locale.ROOT))) { output.add(sub.name()); + } + } } else { - for (ViaSubCommand sub : allowed) + for (ViaSubCommand sub : allowed) { output.add(sub.name()); + } } } //Let the SubCommand handle it else if (args.length >= 2) { if (getSubCommand(args[0]) != null) { ViaSubCommand sub = getSubCommand(args[0]); - if (!allowed.contains(sub)) + if (!allowed.contains(sub)) { return output; + } String[] subArgs = Arrays.copyOfRange(args, 1, args.length); @@ -147,16 +146,19 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { } sender.sendMessage(color("&aViaVersion &c" + Via.getPlatform().getPluginVersion())); sender.sendMessage(color("&6Commands:")); - for (ViaSubCommand cmd : allowed) + for (ViaSubCommand cmd : allowed) { sender.sendMessage(color(String.format("&2/viaversion %s &7- &6%s", cmd.usage(), cmd.description()))); + } allowed.clear(); } private Set calculateAllowedCommands(ViaCommandSender sender) { Set cmds = new HashSet<>(); - for (ViaSubCommand sub : commandMap.values()) - if (hasPermission(sender, sub.permission())) + for (ViaSubCommand sub : commandMap.values()) { + if (hasPermission(sender, sub.permission())) { cmds.add(sub); + } + } return cmds; } @@ -164,7 +166,7 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { return permission == null || sender.hasPermission(permission); } - private void registerDefaults() throws Exception { + private void registerDefaults() { registerSubCommand(new ListSubCmd()); registerSubCommand(new PPSSubCmd()); registerSubCommand(new DebugSubCmd());