forked from public-mirrors/Uniform
feat: bukkit support, x-plat sub-commands
Also changes how the fabric modules are publisheddependabot/gradle/org.projectlombok-lombok-1.18.34
parent
d78847550c
commit
331f54407b
@ -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'
|
||||
}
|
@ -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 <will27528@gmail.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<CommandSender> {
|
||||
|
||||
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<String> aliases) {
|
||||
super(name, description, aliases);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public BukkitCommand(@NotNull JavaPlugin plugin, @NotNull String name, @NotNull List<String> 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<CommandSender> commandNode;
|
||||
private final CommandDispatcher<CommandSender> 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<String> 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));
|
||||
}
|
||||
|
||||
}
|
@ -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 <will27528@gmail.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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 <will27528@gmail.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<CommandSender, BukkitCommand> {
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package net.william278.uniform;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CommandProvider {
|
||||
|
||||
void provide(@NotNull BaseCommand<?> command);
|
||||
|
||||
}
|
@ -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 <will27528@gmail.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.william278.uniform;
|
||||
|
||||
public interface Uniform<S, T extends BaseCommand<S>> {
|
||||
|
||||
void register(Command... commands);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
void register(T... commands);
|
||||
|
||||
}
|
Loading…
Reference in New Issue