forked from public-mirrors/Uniform
feat: add Sponge 11 platform support
parent
8c00cdefa3
commit
3e2a0a35fe
@ -0,0 +1,13 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java-library'
|
||||||
|
id 'maven-publish'
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation project(path: ':common')
|
||||||
|
|
||||||
|
compileOnly 'org.spongepowered:spongeapi:11.0.0'
|
||||||
|
compileOnly 'org.projectlombok:lombok:1.18.32'
|
||||||
|
|
||||||
|
annotationProcessor 'org.projectlombok:lombok:1.18.32'
|
||||||
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
/*
|
||||||
|
* 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.sponge;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
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.Permission;
|
||||||
|
import net.william278.uniform.Uniform;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.spongepowered.api.Game;
|
||||||
|
import org.spongepowered.api.command.Command.Raw;
|
||||||
|
import org.spongepowered.api.command.CommandCause;
|
||||||
|
import org.spongepowered.api.command.CommandCompletion;
|
||||||
|
import org.spongepowered.api.command.CommandResult;
|
||||||
|
import org.spongepowered.api.command.exception.CommandException;
|
||||||
|
import org.spongepowered.api.command.parameter.ArgumentReader;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class SpongeCommand extends BaseCommand<CommandCause> {
|
||||||
|
|
||||||
|
private @Nullable Permission permission;
|
||||||
|
|
||||||
|
public SpongeCommand(@NotNull Command command) {
|
||||||
|
super(command);
|
||||||
|
this.permission = command.getPermission().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpongeCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
|
||||||
|
super(name, description, aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpongeCommand(@NotNull String name, @NotNull List<String> aliases) {
|
||||||
|
super(name, aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
Impl getImpl() {
|
||||||
|
return new Impl(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class Impl implements Raw {
|
||||||
|
|
||||||
|
private static final int COMMAND_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
|
||||||
|
private final CommandDispatcher<CommandCause> dispatcher = new CommandDispatcher<>();
|
||||||
|
private final SpongeCommand command;
|
||||||
|
private final @Nullable Permission permission;
|
||||||
|
|
||||||
|
public Impl(@NotNull SpongeCommand command) {
|
||||||
|
this.dispatcher.register(command.createBuilder());
|
||||||
|
this.command = command;
|
||||||
|
this.permission = command.permission;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult process(CommandCause cause, ArgumentReader.Mutable arguments) throws CommandException {
|
||||||
|
try {
|
||||||
|
return dispatcher.execute(arguments.immutable().remaining(), cause) == COMMAND_SUCCESS
|
||||||
|
? CommandResult.success()
|
||||||
|
: CommandResult.error(Component.translatable("command.failed", NamedTextColor.RED));
|
||||||
|
} catch (CommandSyntaxException e) {
|
||||||
|
throw new CommandException(Component
|
||||||
|
.translatable("command.context.parse_error", NamedTextColor.RED).arguments(
|
||||||
|
Component.text(e.getRawMessage().getString()),
|
||||||
|
Component.text(e.getCursor()),
|
||||||
|
Component.text(e.getContext())
|
||||||
|
), e, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CommandCompletion> complete(CommandCause cause, ArgumentReader.Mutable arguments) throws CommandException {
|
||||||
|
return dispatcher.getCompletionSuggestions(
|
||||||
|
dispatcher.parse(arguments.remaining(), cause),
|
||||||
|
arguments.cursor()
|
||||||
|
)
|
||||||
|
.thenApply(suggestions -> suggestions.getList().stream().map(s -> CommandCompletion.of(
|
||||||
|
s.getText(), Component.text(s.getTooltip().getString())
|
||||||
|
)).toList())
|
||||||
|
.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canExecute(CommandCause cause) {
|
||||||
|
if (permission == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return new SpongeCommandUser(cause).checkPermission(permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Component> shortDescription(CommandCause cause) {
|
||||||
|
return Optional.of(Component.text(command.getDescription()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Component> extendedDescription(CommandCause cause) {
|
||||||
|
return Optional.of(Component.text(command.getDescription()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component usage(CommandCause cause) {
|
||||||
|
return Component.text(dispatcher.getSmartUsage(dispatcher.getRoot(), cause)
|
||||||
|
.values().stream().map("/%s"::formatted).collect(Collectors.joining("\n")));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addSubCommand(@NotNull Command command) {
|
||||||
|
addSubCommand(new SpongeCommand(command));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Uniform getUniform() {
|
||||||
|
return SpongeUniform.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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.sponge;
|
||||||
|
|
||||||
|
import net.kyori.adventure.audience.Audience;
|
||||||
|
import net.william278.uniform.CommandUser;
|
||||||
|
import net.william278.uniform.Permission;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.spongepowered.api.command.CommandCause;
|
||||||
|
import org.spongepowered.api.entity.living.player.Player;
|
||||||
|
import org.spongepowered.api.util.Tristate;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record SpongeCommandUser(@NotNull CommandCause executor) implements CommandUser {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public Audience getAudience() {
|
||||||
|
return executor.audience();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public String getName() {
|
||||||
|
return executor instanceof Player player ? player.name() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public UUID getUuid() {
|
||||||
|
return executor instanceof Player player ? player.uniqueId() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean checkPermission(@NotNull Permission permission) {
|
||||||
|
final Tristate state = executor.permissionValue(permission.toString());
|
||||||
|
if (state == Tristate.UNDEFINED && permission.defaultValue() == Permission.Default.IF_OP) {
|
||||||
|
return executor.hasPermission(permission.toString());
|
||||||
|
}
|
||||||
|
return state == Tristate.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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.sponge;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import net.william278.uniform.BaseCommand;
|
||||||
|
import net.william278.uniform.Command;
|
||||||
|
import net.william278.uniform.CommandUser;
|
||||||
|
import net.william278.uniform.Uniform;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.spongepowered.api.Game;
|
||||||
|
import org.spongepowered.api.command.Command.Raw;
|
||||||
|
import org.spongepowered.api.command.CommandCause;
|
||||||
|
import org.spongepowered.api.event.Listener;
|
||||||
|
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
|
||||||
|
import org.spongepowered.plugin.PluginContainer;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public final class SpongeUniform implements Uniform {
|
||||||
|
|
||||||
|
static SpongeUniform INSTANCE;
|
||||||
|
|
||||||
|
private final Set<SpongeCommand> commands = new HashSet<>();
|
||||||
|
private final PluginContainer plugin;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
Function<Object, CommandUser> commandUserSupplier = (cause) -> new SpongeCommandUser((CommandCause) cause);
|
||||||
|
|
||||||
|
private SpongeUniform(@NotNull PluginContainer plugin, @NotNull Game game) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
game.eventManager().registerListeners(plugin, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the SpongeUniform instance for registering commands
|
||||||
|
*
|
||||||
|
* @param plugin The plugin container
|
||||||
|
* @param game The game instance
|
||||||
|
* @return The SpongeUniform instance
|
||||||
|
* @since 1.1.10
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public static SpongeUniform getInstance(@NotNull PluginContainer plugin, @NotNull Game game) {
|
||||||
|
return INSTANCE != null ? INSTANCE : (INSTANCE = new SpongeUniform(plugin, game));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Listener
|
||||||
|
public void onRegisterCommands(@NotNull RegisterCommandEvent<Raw> event) {
|
||||||
|
commands.forEach(command -> event.register(
|
||||||
|
plugin, command.getImpl(), command.getName(), command.getAliases().toArray(String[]::new)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a command with the server's command manager
|
||||||
|
*
|
||||||
|
* @param commands The commands to register
|
||||||
|
* @param <S> The command source type
|
||||||
|
* @param <T> The command type
|
||||||
|
* @since 1.1.10
|
||||||
|
*/
|
||||||
|
@SafeVarargs
|
||||||
|
@Override
|
||||||
|
public final <S, T extends BaseCommand<S>> void register(T... commands) {
|
||||||
|
Arrays.stream(commands).map(c -> (SpongeCommand) c).forEach(this.commands::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a command with the server's command manager
|
||||||
|
*
|
||||||
|
* @param commands The commands to register
|
||||||
|
* @since 1.1.10
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void register(Command... commands) {
|
||||||
|
register(Arrays.stream(commands).map(SpongeCommand::new).toArray(SpongeCommand[]::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue