diff --git a/README.md b/README.md index 729f02c..83b5528 100644 --- a/README.md +++ b/README.md @@ -17,23 +17,17 @@ Uniform _currently_ targets the following platforms: -Please note Uniform on Fabric requires [adventure-platform-fabric](https://docs.advntr.dev/platform/fabric.html) and the [Fabric API](https://fabricmc.net/) as dependencies. - -| Platform | Artifact | Minecraft | Java | -|---------------|-------------------------|:----------:|:------:| -| Common | `uniform-common` | - | \>`17` | -| Paper | `uniform-paper` | \>`1.20.6` | \>`21` | -| Velocity | `uniform-velocity` | \>`3.3.0` | \>`17` | -| Fabric 1.20.1 | `uniform-fabric-1_20_1` | =`1.20.1` | \>`17` | -| Fabric 1.20.6 | `uniform-fabric-1_20_6` | =`1.20.6` | \>`21` | - -Uniform _plans_ to support the following platforms in the future: - -| Platform | Version | Java | -|----------|------------|:------:| -| Spigot | \>`1.17.1` | \>`17` | -| Sponge 8 | =`1.19.4` | \>`17` | - +| Platform | Artifact | Minecraft | Java | +|---------------|--------------------|:----------:|:------:| +| Common | `uniform-common` | - | \>`17` | +| Paper | `uniform-paper` | \>`1.20.6` | \>`21` | +| Velocity | `uniform-velocity` | \>`3.3.0` | \>`17` | +| Fabric 1.20.1 | `uniform-fabric` | =`1.20.1` | \>`17` | +| Fabric 1.20.6 | `uniform-fabric` | =`1.20.6` | \>`21` | +| Bukkit | `uniform-bukkit` | \>`1.17.1` | \>`17` | + +* **Fabric:** Please note Uniform on Fabric requires [adventure-platform-fabric](https://docs.advntr.dev/platform/fabric.html) and the [Fabric API](https://fabricmc.net/) as dependencies. To target Fabric, use `uniform-fabric` as the artifact and `+` as the version (e.g. `net.william278.uniform:uniform-fabric:1.0+1.20.1`). +* **Sponge**: Support for Sponge 8 is also planned in a future version. ## Setup Uniform is available [on Maven](https://repo.william278.net/#/releases/net/william278/uniform/). You can browse the Javadocs [here](https://repo.william278.net/javadoc/releases/net/william278/uniform/latest). diff --git a/build.gradle b/build.gradle index b828f62..7cfaf77 100644 --- a/build.gradle +++ b/build.gradle @@ -60,6 +60,7 @@ allprojects { maven { url 'https://repo.papermc.io/repository/maven-public/' } maven { url 'https://libraries.minecraft.net/' } maven { url 'https://maven.fabricmc.net/' } + maven { url 'https://mvn-repo.arim.space/lesser-gpl3/' } } dependencies { @@ -146,6 +147,19 @@ subprojects { } } + if (['bukkit'].contains(project.name)) { + publications { + mavenJavaBukkit(MavenPublication) { + groupId = 'net.william278.uniform' + artifactId = 'uniform-bukkit' + version = "$rootProject.version" + artifact shadowJar + artifact sourcesJar + artifact javadocJar + } + } + } + if (['velocity'].contains(project.name)) { publications { mavenJavaVelocity(MavenPublication) { @@ -158,24 +172,26 @@ subprojects { } } } + if (['fabric-1.20.1'].contains(project.name)) { publications { mavenJavaFabric1_20_1(MavenPublication) { groupId = 'net.william278.uniform' - artifactId = 'uniform-fabric-1_20_1' - version = "$rootProject.version" + artifactId = 'uniform-fabric' + version = "${rootProject.version}+1.20.1" artifact shadowJar artifact sourcesJar artifact javadocJar } } } + if (['fabric-1.20.6'].contains(project.name)) { publications { mavenJavaFabric1_20_6(MavenPublication) { groupId = 'net.william278.uniform' - artifactId = 'uniform-fabric-1_20_6' - version = "$rootProject.version" + artifactId = 'uniform-fabric' + version = "${rootProject.version}+1.20.6" artifact shadowJar artifact sourcesJar artifact javadocJar diff --git a/bukkit/build.gradle b/bukkit/build.gradle new file mode 100644 index 0000000..226d8bc --- /dev/null +++ b/bukkit/build.gradle @@ -0,0 +1,16 @@ +plugins { + id 'java-library' + id 'maven-publish' +} + +dependencies { + api project(path: ':common') + + implementation 'net.kyori:adventure-platform-bukkit:4.3.3' + implementation 'space.arim.morepaperlib:morepaperlib:0.4.3' + + compileOnlyApi 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT' + compileOnly 'org.projectlombok:lombok:1.18.32' + + annotationProcessor 'org.projectlombok:lombok:1.18.32' +} \ No newline at end of file diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java new file mode 100644 index 0000000..8d2e4e3 --- /dev/null +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java @@ -0,0 +1,153 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.bukkit; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.Suggestion; +import com.mojang.brigadier.tree.LiteralCommandNode; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.william278.uniform.BaseCommand; +import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; + +@SuppressWarnings("unused") +public class BukkitCommand extends BaseCommand { + + private final JavaPlugin plugin; + private BukkitAudiences audiences; + + public BukkitCommand(@NotNull Command command, @NotNull JavaPlugin plugin) { + super(command); + this.plugin = plugin; + } + + public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull String description, + @NotNull List aliases) { + super(name, description, aliases); + this.plugin = plugin; + } + + public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull List aliases) { + super(name, aliases); + this.plugin = plugin; + } + + @NotNull + Impl getImpl() { + return new Impl(this); + } + + static final class Impl extends org.bukkit.command.Command { + + private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS; + + private final LiteralCommandNode commandNode; + private final CommandDispatcher dispatcher; + private final JavaPlugin plugin; + private BukkitAudiences audiences; + + public Impl(@NotNull BukkitCommand command) { + super(command.getName()); + this.audiences = command.audiences; + this.plugin = command.plugin; + + // Register command, setup description and aliases + this.dispatcher = new CommandDispatcher<>(); + this.commandNode = this.dispatcher.register(command.build().createBuilder()); + this.setDescription(command.getDescription()); + this.setAliases(command.getAliases()); + } + + @Override + public boolean execute(@NotNull CommandSender commandSender, @NotNull String alias, @NotNull String[] args) { + try { + final String string = getInput(alias, args); + System.out.println("Usage: \"" + Arrays.toString(dispatcher.getAllUsage(commandNode, commandSender, false)) + "\""); + return dispatcher.execute(string, commandSender) == COMMAND_SUCCESS; + } catch (CommandSyntaxException e) { + getAudience(commandSender).sendMessage(Component + .translatable("command.context.parse_error", NamedTextColor.RED) + .arguments( + Component.text(e.getRawMessage().getString()), + Component.text(e.getCursor()), + Component.text(e.getContext()) + )); + return false; + } catch (CommandException e) { + getAudience(commandSender).sendMessage(Component.text(e.getMessage(), NamedTextColor.RED)); + return true; + } + } + + @NotNull + @Override + public List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) + throws IllegalArgumentException { + final String passed = getInput(alias, args); + return dispatcher.getCompletionSuggestions( + dispatcher.parse(passed, sender), + passed.length() // Spigot API limitation - we can only TAB complete the full text length :( + ) + .thenApply(suggestions -> suggestions.getList().stream().map(Suggestion::getText).toList()) + .join(); + } + + @NotNull + private Audience getAudience(@NotNull CommandSender sender) { + if (audiences == null) { + audiences = BukkitAudiences.create(plugin); + } + return audiences.sender(sender); + } + + @NotNull + private String getInput(@NotNull String alias, @NotNull String[] args) { + return args.length == 0 ? alias : "%s %s".formatted(alias, String.join(" ", args)); + } + } + + @Override + @NotNull + protected CommandUser getUser(@NotNull Object user) { + if (audiences == null) { + audiences = BukkitAudiences.create(plugin); + } + return new BukkitCommandUser((CommandSender) user, audiences); + } + + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new BukkitCommand(command, plugin)); + } + +} diff --git a/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java similarity index 65% rename from example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java rename to bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java index bafb466..b1cd941 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java @@ -19,33 +19,35 @@ * along with this program. If not, see . */ -package net.william278.uniform; +package net.william278.uniform.bukkit; import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.Component; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.william278.uniform.CommandUser; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class ExampleCrossPlatCommand implements Command { +import java.util.UUID; + +public record BukkitCommandUser(@NotNull CommandSender sender, @NotNull BukkitAudiences adv) implements CommandUser { @Override @NotNull - public String getName() { - return "example-crossplat"; + public Audience getAudience() { + return adv.sender(sender); } @Override - @NotNull - public String getDescription() { - return "An example cross-platform command"; + public String getName() { + return sender.getName(); } @Override - public void provide(@NotNull BaseCommand command) { - command.setCondition(source -> true); - command.setDefaultExecutor((ctx) -> { - final Audience user = command.getUser(ctx.getSource()).getAudience(); - user.sendMessage(Component.text("Hello, world!")); - }); + @Nullable + public UUID getUuid() { + return sender instanceof Player player ? player.getUniqueId() : null; } } diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java new file mode 100644 index 0000000..85d8517 --- /dev/null +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java @@ -0,0 +1,110 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.bukkit;/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import net.william278.uniform.Command; +import net.william278.uniform.Uniform; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import space.arim.morepaperlib.MorePaperLib; +import space.arim.morepaperlib.commands.CommandRegistration; + +import java.util.Arrays; +import java.util.Locale; + +/** + * A class for registering commands with the Bukkit server's CommandMap + * + * @since 1.0 + */ +public final class BukkitUniform implements Uniform { + + private static BukkitUniform INSTANCE; + + private final JavaPlugin plugin; + private final CommandRegistration registrar; + + private BukkitUniform(@NotNull JavaPlugin plugin) { + this.plugin = plugin; + this.registrar = new MorePaperLib(plugin).commandRegistration(); + } + + /** + * Get the BukkitUniform instance for registering commands + * + * @param plugin The plugin instance + * @return BukkitUniform instance + * @since 1.0 + */ + @NotNull + public static BukkitUniform getInstance(@NotNull JavaPlugin plugin) { + return INSTANCE != null ? INSTANCE : (INSTANCE = new BukkitUniform(plugin)); + } + + /** + * Register a command to be added to the server's command map + * + * @param commands The commands to register + * @since 1.0 + */ + @Override + public void register(@NotNull BukkitCommand... commands) { + registrar.getServerCommandMap().registerAll( + plugin.getName().toLowerCase(Locale.ENGLISH), + Arrays.stream(commands).map(c -> (org.bukkit.command.Command) c.getImpl()).toList() + ); + } + + /** + * Register command(s) to be added to the server's command map + * + * @param commands The commands to register + * @since 1.0 + */ + @Override + public void register(@NotNull Command... commands) { + register(Arrays.stream(commands) + .map(command -> new BukkitCommand(command, plugin)) + .toArray(BukkitCommand[]::new)); + } + +} diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index 15698df..7015142 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -75,91 +75,112 @@ public abstract class BaseCommand { private final List> subCommands = new ArrayList<>(); @NotNull - protected abstract CommandUser getUser(@NotNull S user); + protected abstract CommandUser getUser(@NotNull Object user); - protected final void setCondition(@NotNull Predicate condition) { + public final void setCondition(@NotNull Predicate condition) { this.condition = condition; } - protected final void setDefaultExecutor(@NotNull CommandExecutor executor) { + public final void setDefaultExecutor(@NotNull CommandExecutor executor) { this.defaultExecutor = executor; } @SafeVarargs - protected final void addConditionalSyntax(@Nullable Predicate condition, @NotNull CommandExecutor executor, - @NotNull CommandElement... elements) { + public final void addConditionalSyntax(@Nullable Predicate condition, @NotNull CommandExecutor executor, + @NotNull CommandElement... elements) { var syntax = new CommandSyntax<>(condition, executor, List.of(elements)); this.syntaxes.add(syntax); } @SafeVarargs - protected final void addSyntax(@NotNull CommandExecutor executor, @NotNull CommandElement... elements) { + public final void addSyntax(@NotNull CommandExecutor executor, @NotNull CommandElement... elements) { this.addConditionalSyntax(null, executor, elements); } - protected final void addSubCommand(@NotNull BaseCommand command) { + public final void addSubCommand(@NotNull BaseCommand command) { this.subCommands.add(command); } + public final void addSubCommand(@NotNull String name, @NotNull CommandProvider provider) { + this.addSubCommand(new Command.SubCommand(name, provider).command()); + } + + public final void addSubCommand(@NotNull String name, @NotNull List aliases, + @NotNull CommandProvider provider) { + this.addSubCommand(new Command.SubCommand(name, provider, aliases).command()); + } + + public abstract void addSubCommand(@NotNull Command command); + @NotNull public final LiteralCommandNode build() { return Graph.create(this).build(); } @NotNull - protected static LiteralElement literalArg(@NotNull String name) { + public static LiteralElement literal(@NotNull String name) { return new LiteralElement<>(name); } @NotNull - protected static ArgumentElement stringArg(@NotNull String name) { - return argument(name, StringArgumentType.string()); + public static ArgumentElement greedyString(@NotNull String name) { + return arg(name, StringArgumentType.greedyString()); + } + + @NotNull + public static ArgumentElement string(@NotNull String name) { + return arg(name, StringArgumentType.string()); + } + + @NotNull + public static ArgumentElement word(@NotNull String name) { + return arg(name, StringArgumentType.word()); } @NotNull - protected static ArgumentElement integerArg(@NotNull String name) { - return argument(name, IntegerArgumentType.integer()); + public static ArgumentElement intNum(@NotNull String name) { + return arg(name, IntegerArgumentType.integer()); } @NotNull - protected static ArgumentElement integerArg(@NotNull String name, int min) { - return argument(name, IntegerArgumentType.integer(min)); + public static ArgumentElement intNum(@NotNull String name, int min) { + return arg(name, IntegerArgumentType.integer(min)); } @NotNull - protected static ArgumentElement integerArg(@NotNull String name, int min, int max) { - return argument(name, IntegerArgumentType.integer(min, max)); + public static ArgumentElement intNum(@NotNull String name, int min, int max) { + return arg(name, IntegerArgumentType.integer(min, max)); } @NotNull - protected static ArgumentElement floatArg(@NotNull String name) { - return argument(name, FloatArgumentType.floatArg()); + public static ArgumentElement floatNum(@NotNull String name) { + return arg(name, FloatArgumentType.floatArg()); } @NotNull - protected static ArgumentElement floatArg(@NotNull String name, float min) { - return argument(name, FloatArgumentType.floatArg(min)); + public static ArgumentElement floatNum(@NotNull String name, float min) { + return arg(name, FloatArgumentType.floatArg(min)); } @NotNull - protected static ArgumentElement floatArg(@NotNull String name, float min, float max) { - return argument(name, FloatArgumentType.floatArg(min, max)); + public static ArgumentElement floatNum(@NotNull String name, float min, float max) { + return arg(name, FloatArgumentType.floatArg(min, max)); } @NotNull - protected static ArgumentElement booleanArg(@NotNull String name) { - return argument(name, BoolArgumentType.bool()); + public static ArgumentElement bool(@NotNull String name) { + return arg(name, BoolArgumentType.bool()); } @NotNull - protected static ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type, - @Nullable SuggestionProvider suggestionProvider) { + public static ArgumentElement arg(@NotNull String name, @NotNull ArgumentType type, + @Nullable SuggestionProvider suggestionProvider) { return new ArgumentElement<>(name, type, suggestionProvider); } @NotNull - protected static ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type) { - return argument(name, type, null); + public static ArgumentElement arg(@NotNull String name, @NotNull ArgumentType type) { + return arg(name, type, null); } } \ No newline at end of file diff --git a/common/src/main/java/net/william278/uniform/Command.java b/common/src/main/java/net/william278/uniform/Command.java index 77415e3..78db99a 100644 --- a/common/src/main/java/net/william278/uniform/Command.java +++ b/common/src/main/java/net/william278/uniform/Command.java @@ -21,25 +21,38 @@ package net.william278.uniform; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.jetbrains.annotations.NotNull; import java.util.List; -public interface Command { - - @NotNull - String getName(); - - @NotNull - default String getDescription() { - return ""; +@Getter +@Setter +@RequiredArgsConstructor +@AllArgsConstructor +public abstract class Command implements CommandProvider { + + private final String name; + private String description = ""; + private List aliases = List.of(); + + record SubCommand(@NotNull String name, @NotNull CommandProvider provider, @NotNull List aliases) { + SubCommand(@NotNull String name, @NotNull CommandProvider provider) { + this(name, provider, List.of()); + } + + @NotNull + Command command() { + return new Command(name, "", aliases) { + @Override + public void provide(@NotNull BaseCommand command) { + provider.provide(command); + } + }; + } } - @NotNull - default List getAliases() { - return List.of(); - } - - void provide(@NotNull BaseCommand command); - } diff --git a/common/src/main/java/net/william278/uniform/CommandProvider.java b/common/src/main/java/net/william278/uniform/CommandProvider.java new file mode 100644 index 0000000..7d6a09a --- /dev/null +++ b/common/src/main/java/net/william278/uniform/CommandProvider.java @@ -0,0 +1,10 @@ +package net.william278.uniform; + +import org.jetbrains.annotations.NotNull; + +@FunctionalInterface +public interface CommandProvider { + + void provide(@NotNull BaseCommand command); + +} diff --git a/common/src/main/java/net/william278/uniform/Uniform.java b/common/src/main/java/net/william278/uniform/Uniform.java new file mode 100644 index 0000000..32d5007 --- /dev/null +++ b/common/src/main/java/net/william278/uniform/Uniform.java @@ -0,0 +1,31 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform; + +public interface Uniform> { + + void register(Command... commands); + + @SuppressWarnings("unchecked") + void register(T... commands); + +} diff --git a/example-plugin/build.gradle b/example-plugin/build.gradle index 624e554..b0b5da9 100644 --- a/example-plugin/build.gradle +++ b/example-plugin/build.gradle @@ -4,7 +4,8 @@ plugins { } dependencies { - implementation(project(":paper")) + implementation(project(":paper")) +// implementation(project(":bukkit")) compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' } diff --git a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java index 56a6570..6f7f2da 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java +++ b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java @@ -21,21 +21,28 @@ package net.william278.uniform; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.william278.uniform.paper.PaperCommand; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; -import java.util.List; +import static net.william278.uniform.BaseCommand.greedyString; -@SuppressWarnings("UnstableApiUsage") -public class ExampleCommand extends PaperCommand { +public class ExampleCommand extends Command { public ExampleCommand() { - super("example", "An example command", List.of("silly-command")); - addSyntax((context) -> { - context.getSource().getSender().sendMessage("Woah!!!!"); - String arg = context.getArgument("message", String.class); - context.getSource().getSender().sendMessage(MiniMessage.miniMessage().deserialize(arg)); - }, stringArg("message")); + super("example"); + setDescription("An example command for Uniform"); + } + + @Override + public void provide(@NotNull BaseCommand command) { + command.setDefaultExecutor((ctx) -> { + final CommandUser user = command.getUser(ctx.getSource()); + user.getAudience().sendMessage(Component.text("Hello, World!")); + }); + command.addSubCommand("message", (sub) -> sub.addSyntax((ctx) -> { + final CommandUser user = sub.getUser(ctx.getSource()); + user.getAudience().sendMessage(Component.text(ctx.getArgument("message", String.class))); + }, greedyString("message"))); } } diff --git a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java index 7d80e14..ead198e 100644 --- a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java +++ b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java @@ -31,7 +31,6 @@ public class UniformExample extends JavaPlugin { public void onLoad() { PaperUniform uniform = PaperUniform.getInstance(this); uniform.register(new ExampleCommand()); - uniform.register(new ExampleCrossPlatCommand()); } } \ No newline at end of file diff --git a/example-plugin/src/main/resources/plugin.yml b/example-plugin/src/main/resources/paper-plugin.yml similarity index 100% rename from example-plugin/src/main/resources/plugin.yml rename to example-plugin/src/main/resources/paper-plugin.yml diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java index 2f6191a..00744ac 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -81,7 +81,13 @@ public class FabricCommand extends BaseCommand { @Override @NotNull - protected CommandUser getUser(@NotNull ServerCommandSource user) { - return new FabricCommandUser(user); + protected CommandUser getUser(@NotNull Object user) { + return new FabricCommandUser((ServerCommandSource) user); } + + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new FabricCommand(command)); + } + } diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java index ec3651c..376e1ac 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricUniform.java @@ -24,7 +24,9 @@ package net.william278.uniform.fabric; import com.google.common.collect.Sets; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.server.command.ServerCommandSource; import net.william278.uniform.Command; +import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -36,7 +38,7 @@ import java.util.Set; * * @since 1.0 */ -public final class FabricUniform { +public final class FabricUniform implements Uniform { private static FabricUniform INSTANCE; @@ -65,6 +67,7 @@ public final class FabricUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull FabricCommand... commands) { Collections.addAll(this.commands, commands); } @@ -75,6 +78,7 @@ public final class FabricUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull Command... commands) { register(Arrays.stream(commands).map(FabricCommand::new).toArray(FabricCommand[]::new)); } diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java index 277c728..06f375e 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -81,8 +81,12 @@ public class FabricCommand extends BaseCommand { @Override @NotNull - protected CommandUser getUser(@NotNull ServerCommandSource user) { - return new FabricCommandUser(user); + protected CommandUser getUser(@NotNull Object user) { + return new FabricCommandUser((ServerCommandSource) user); } + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new FabricCommand(command)); + } } diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java index 5345531..d0a9f29 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricUniform.java @@ -24,7 +24,9 @@ package net.william278.uniform.fabric; import com.google.common.collect.Sets; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.server.command.ServerCommandSource; import net.william278.uniform.Command; +import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -36,7 +38,7 @@ import java.util.Set; * * @since 1.0 */ -public final class FabricUniform { +public final class FabricUniform implements Uniform { private static FabricUniform INSTANCE; @@ -65,6 +67,7 @@ public final class FabricUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull FabricCommand... commands) { Collections.addAll(this.commands, commands); } @@ -75,6 +78,7 @@ public final class FabricUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull Command... commands) { register(Arrays.stream(commands).map(FabricCommand::new).toArray(FabricCommand[]::new)); } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java index f0ccaff..70e1dfe 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -88,8 +88,13 @@ public class PaperCommand extends BaseCommand { @Override @NotNull - protected CommandUser getUser(@NotNull CommandSourceStack user) { - return new PaperCommandUser(user); + protected CommandUser getUser(@NotNull Object user) { + return new PaperCommandUser((CommandSourceStack) user); + } + + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new PaperCommand(command)); } } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java index 941e830..78b0170 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -22,14 +22,15 @@ package net.william278.uniform.paper; import com.google.common.collect.Sets; +import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.william278.uniform.Command; +import net.william278.uniform.Uniform; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Collections; -import java.util.List; import java.util.Set; /** @@ -38,7 +39,7 @@ import java.util.Set; * @since 1.0 */ @SuppressWarnings("UnstableApiUsage") -public final class PaperUniform { +public final class PaperUniform implements Uniform { private static PaperUniform INSTANCE; @@ -74,6 +75,7 @@ public final class PaperUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull PaperCommand... commands) { Collections.addAll(this.commands, commands); } @@ -84,6 +86,7 @@ public final class PaperUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull Command... commands) { register(Arrays.stream(commands).map(PaperCommand::new).toArray(PaperCommand[]::new)); } diff --git a/settings.gradle b/settings.gradle index 02238ad..b9e783b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,6 +11,7 @@ include( // Server Plugins 'paper', + 'bukkit', // Proxy Plugins 'velocity', diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java index 9a6c064..f52a605 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java @@ -97,7 +97,13 @@ public class VelocityCommand extends BaseCommand { @Override @NotNull - protected CommandUser getUser(@NotNull CommandSource user) { - return new VelocityCommandUser(user); + protected CommandUser getUser(@NotNull Object user) { + return new VelocityCommandUser((CommandSource) user); } + + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new VelocityCommand(command)); + } + } diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java index b2b0db2..fc9b83b 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityUniform.java @@ -22,8 +22,10 @@ package net.william278.uniform.velocity; import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.ProxyServer; import net.william278.uniform.Command; +import net.william278.uniform.Uniform; import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -34,7 +36,7 @@ import java.util.Arrays; * @since 1.0 */ @SuppressWarnings("unused") -public final class VelocityUniform { +public final class VelocityUniform implements Uniform { private static VelocityUniform INSTANCE; @@ -62,6 +64,7 @@ public final class VelocityUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull VelocityCommand... commands) { Arrays.stream(commands).forEach(cmd -> server.getCommandManager().register(new BrigadierCommand(cmd.build()))); } @@ -72,6 +75,7 @@ public final class VelocityUniform { * @param commands The commands to register * @since 1.0 */ + @Override public void register(@NotNull Command... commands) { register(Arrays.stream(commands).map(VelocityCommand::new).toArray(VelocityCommand[]::new)); }