forked from public-mirrors/Uniform
feat: make x-plat command registering possible
parent
e5c8975339
commit
785b33e845
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.mojang.brigadier.arguments.*;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import net.william278.uniform.element.ArgumentElement;
|
||||
import net.william278.uniform.element.CommandElement;
|
||||
import net.william278.uniform.element.LiteralElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class BaseCommand<S> {
|
||||
|
||||
@Getter
|
||||
private final String name;
|
||||
@Getter
|
||||
private final String[] aliases;
|
||||
|
||||
public BaseCommand(@NotNull Command command) {
|
||||
this.name = command.getNamespace();
|
||||
this.aliases = command.getAliases().toArray(new String[0]);
|
||||
command.provide(this);
|
||||
}
|
||||
|
||||
public BaseCommand(@NotNull String name, @NotNull String... aliases) {
|
||||
this.name = name;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private Predicate<S> condition;
|
||||
@Nullable
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private CommandExecutor<S> defaultExecutor;
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final List<CommandSyntax<S>> syntaxes = new ArrayList<>();
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final List<BaseCommand<S>> subCommands = new ArrayList<>();
|
||||
|
||||
@NotNull
|
||||
protected abstract CommandUser getUser(@NotNull S user);
|
||||
|
||||
protected final void setCondition(@NotNull Predicate<S> condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
protected final void setDefaultExecutor(@NotNull CommandExecutor<S> executor) {
|
||||
this.defaultExecutor = executor;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
protected final void addConditionalSyntax(@Nullable Predicate<S> condition, @NotNull CommandExecutor<S> executor,
|
||||
@NotNull CommandElement<S>... elements) {
|
||||
var syntax = new CommandSyntax<>(condition, executor, List.of(elements));
|
||||
this.syntaxes.add(syntax);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
protected final void addSyntax(@NotNull CommandExecutor<S> executor, @NotNull CommandElement<S>... elements) {
|
||||
this.addConditionalSyntax(null, executor, elements);
|
||||
}
|
||||
|
||||
protected final void addSubCommand(@NotNull BaseCommand<S> command) {
|
||||
this.subCommands.add(command);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final LiteralCommandNode<S> build() {
|
||||
return Graph.create(this).build();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> LiteralElement<S> literalArg(@NotNull String name) {
|
||||
return new LiteralElement<>(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, String> stringArg(@NotNull String name) {
|
||||
return argument(name, StringArgumentType.string());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name) {
|
||||
return argument(name, IntegerArgumentType.integer());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name, int min) {
|
||||
return argument(name, IntegerArgumentType.integer(min));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name, int min, int max) {
|
||||
return argument(name, IntegerArgumentType.integer(min, max));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name) {
|
||||
return argument(name, FloatArgumentType.floatArg());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name, float min) {
|
||||
return argument(name, FloatArgumentType.floatArg(min));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name, float min, float max) {
|
||||
return argument(name, FloatArgumentType.floatArg(min, max));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S> ArgumentElement<S, Boolean> booleanArg(@NotNull String name) {
|
||||
return argument(name, BoolArgumentType.bool());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S, T> ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type,
|
||||
@Nullable SuggestionProvider<S> suggestionProvider) {
|
||||
return new ArgumentElement<>(name, type, suggestionProvider);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static <S, T> ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type) {
|
||||
return argument(name, type, null);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface CommandUser {
|
||||
|
||||
Audience getAudience();
|
||||
|
||||
String getName();
|
||||
|
||||
UUID getUniqueId();
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ExampleCrossPlatCommand implements Command {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getNamespace() {
|
||||
return "example";
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<String> getAliases() {
|
||||
return List.of("cross-plat");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void provide(@NotNull BaseCommand<S> command) {
|
||||
command.setCondition(source -> true);
|
||||
command.setDefaultExecutor((ctx) -> {
|
||||
final Audience user = command.getUser(ctx.getSource()).getAudience();
|
||||
user.sendMessage(Component.text("Hello, world!"));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id 'fabric-loom' version '1.6-SNAPSHOT'
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api project(path: ':common')
|
||||
|
||||
minecraft 'com.mojang:minecraft:1.20.1'
|
||||
mappings 'net.fabricmc:yarn:1.20.1+build.10:v2'
|
||||
|
||||
modCompileOnly 'net.fabricmc:fabric-loader:0.15.11'
|
||||
modCompileOnly 'net.fabricmc.fabric-api:fabric-api:0.92.2+1.20.1'
|
||||
|
||||
modImplementation include('net.kyori:adventure-platform-fabric:5.9.0')
|
||||
|
||||
compileOnly 'org.projectlombok:lombok:1.18.32'
|
||||
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.32'
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.InvalidIdentifierException;
|
||||
import net.william278.uniform.BaseCommand;
|
||||
import net.william278.uniform.Command;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import net.william278.uniform.element.ArgumentElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class FabricCommand extends BaseCommand<ServerCommandSource> {
|
||||
|
||||
public FabricCommand(@NotNull Command command) {
|
||||
super(command);
|
||||
}
|
||||
|
||||
public FabricCommand(@NotNull String name, @NotNull String... aliases) {
|
||||
super(name, aliases);
|
||||
}
|
||||
|
||||
protected static ArgumentElement<ServerCommandSource, Item> itemArg(String name) {
|
||||
return registryArg(name, Registries.ITEM);
|
||||
}
|
||||
|
||||
protected static ArgumentElement<ServerCommandSource, Block> blockArg(String name) {
|
||||
return registryArg(name, Registries.BLOCK);
|
||||
}
|
||||
|
||||
protected static <T> ArgumentElement<ServerCommandSource, T> registryArg(String name, Registry<T> registry) {
|
||||
return new ArgumentElement<>(name, reader -> {
|
||||
String itemId = reader.readString();
|
||||
final Identifier id;
|
||||
try {
|
||||
id = Identifier.tryParse(itemId);
|
||||
} catch (InvalidIdentifierException e) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
|
||||
}
|
||||
if (registry.getOrEmpty(id).isEmpty()) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
|
||||
}
|
||||
return registry.get(id);
|
||||
}, (context, builder) -> {
|
||||
registry.getIds().forEach(id -> builder.suggest(id.toString()));
|
||||
return builder.buildFuture();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected CommandUser getUser(@NotNull ServerCommandSource user) {
|
||||
return new FabricCommandUser(user);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record FabricCommandUser(@NotNull ServerCommandSource source) implements CommandUser {
|
||||
|
||||
@Override
|
||||
public Audience getAudience() {
|
||||
return source.getPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getName() {
|
||||
return source.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public UUID getUniqueId() {
|
||||
return source.getPlayer() != null ? source.getPlayer().getUuid() : null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.william278.uniform.Command;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A class for registering commands with the Fabric (1.20.1) server
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class FabricUniform {
|
||||
|
||||
private static FabricUniform INSTANCE;
|
||||
|
||||
private final Set<FabricCommand> commands = Sets.newHashSet();
|
||||
|
||||
private FabricUniform() {
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
|
||||
commands.forEach(command -> dispatcher.register(command.build().createBuilder()))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the FabricUniform instance for registering commands
|
||||
*
|
||||
* @return The FabricUniform instance
|
||||
* @since 1.0
|
||||
*/
|
||||
@NotNull
|
||||
public static FabricUniform getInstance() {
|
||||
return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform());
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a command to be added to the server's command manager
|
||||
*
|
||||
* @param commands The commands to register
|
||||
* @since 1.0
|
||||
*/
|
||||
public void register(@NotNull FabricCommand... commands) {
|
||||
Collections.addAll(this.commands, commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a command to be added to the server's command manager
|
||||
*
|
||||
* @param commands The commands to register
|
||||
* @since 1.0
|
||||
*/
|
||||
public void register(@NotNull Command... commands) {
|
||||
register(Arrays.stream(commands).map(FabricCommand::new).toArray(FabricCommand[]::new));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id 'fabric-loom' version '1.6-SNAPSHOT'
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api project(path: ':common')
|
||||
|
||||
minecraft 'com.mojang:minecraft:1.20.6'
|
||||
mappings 'net.fabricmc:yarn:1.20.6+build.3:v2'
|
||||
|
||||
modCompileOnly 'net.fabricmc:fabric-loader:0.15.11'
|
||||
modCompileOnly 'net.fabricmc.fabric-api:fabric-api:0.100.0+1.20.6'
|
||||
|
||||
modImplementation include('net.kyori:adventure-platform-fabric:5.13.0')
|
||||
|
||||
compileOnly 'org.projectlombok:lombok:1.18.32'
|
||||
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.32'
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.InvalidIdentifierException;
|
||||
import net.william278.uniform.BaseCommand;
|
||||
import net.william278.uniform.Command;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import net.william278.uniform.element.ArgumentElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class FabricCommand extends BaseCommand<ServerCommandSource> {
|
||||
|
||||
public FabricCommand(@NotNull String name, @NotNull String... aliases) {
|
||||
super(name, aliases);
|
||||
}
|
||||
|
||||
public FabricCommand(@NotNull Command command) {
|
||||
super(command);
|
||||
}
|
||||
|
||||
protected static ArgumentElement<ServerCommandSource, Item> itemArg(String name) {
|
||||
return registryArg(name, Registries.ITEM);
|
||||
}
|
||||
|
||||
protected static ArgumentElement<ServerCommandSource, Block> blockArg(String name) {
|
||||
return registryArg(name, Registries.BLOCK);
|
||||
}
|
||||
|
||||
protected static <T> ArgumentElement<ServerCommandSource, T> registryArg(String name, Registry<T> registry) {
|
||||
return new ArgumentElement<>(name, reader -> {
|
||||
String itemId = reader.readString();
|
||||
final Identifier id;
|
||||
try {
|
||||
id = Identifier.tryParse(itemId);
|
||||
} catch (InvalidIdentifierException e) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
|
||||
}
|
||||
if (registry.getOrEmpty(id).isEmpty()) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
|
||||
}
|
||||
return registry.get(id);
|
||||
}, (context, builder) -> {
|
||||
registry.getIds().forEach(id -> builder.suggest(id.toString()));
|
||||
return builder.buildFuture();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected CommandUser getUser(@NotNull ServerCommandSource user) {
|
||||
return new FabricCommandUser(user);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record FabricCommandUser(@NotNull ServerCommandSource source) implements CommandUser {
|
||||
|
||||
@Override
|
||||
public Audience getAudience() {
|
||||
return source.getPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getName() {
|
||||
return source.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public UUID getUniqueId() {
|
||||
return source.getPlayer() != null ? source.getPlayer().getUuid() : null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.fabric;
|
||||
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.william278.uniform.Command;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A class for registering commands with the Fabric (1.20.6) server
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class FabricUniform {
|
||||
|
||||
private static FabricUniform INSTANCE;
|
||||
|
||||
private final Set<FabricCommand> commands = Sets.newHashSet();
|
||||
|
||||
private FabricUniform() {
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registry, environment) ->
|
||||
commands.forEach(command -> dispatcher.register(command.build().createBuilder()))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the FabricUniform instance for registering commands
|
||||
*
|
||||
* @return The FabricUniform instance
|
||||
* @since 1.0
|
||||
*/
|
||||
@NotNull
|
||||
public static FabricUniform getInstance() {
|
||||
return INSTANCE != null ? INSTANCE : (INSTANCE = new FabricUniform());
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a command to be added to the server's command manager
|
||||
*
|
||||
* @param commands The commands to register
|
||||
* @since 1.0
|
||||
*/
|
||||
public void register(@NotNull FabricCommand... commands) {
|
||||
Collections.addAll(this.commands, commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a command to be added to the server's command manager
|
||||
*
|
||||
* @param commands The commands to register
|
||||
* @since 1.0
|
||||
*/
|
||||
public void register(@NotNull Command... commands) {
|
||||
register(Arrays.stream(commands).map(FabricCommand::new).toArray(FabricCommand[]::new));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.paper;
|
||||
|
||||
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record PaperCommandUser(@NotNull BukkitBrigadierCommandSource source) implements CommandUser {
|
||||
|
||||
|
||||
@Override
|
||||
public Audience getAudience() {
|
||||
return source.getBukkitSender();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getName() {
|
||||
return source.getBukkitEntity() != null ? source.getBukkitSender().getName() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public UUID getUniqueId() {
|
||||
return source.getBukkitEntity() != null ? source.getBukkitEntity().getUniqueId() : null;
|
||||
}
|
||||
}
|
@ -1,14 +1,24 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
gradlePluginPortal()
|
||||
maven { url 'https://maven.fabricmc.net/' }
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = 'Uniform'
|
||||
include(
|
||||
'common',
|
||||
|
||||
// Server Plugins
|
||||
'paper',
|
||||
|
||||
// Proxy Plugins
|
||||
'velocity',
|
||||
|
||||
// Fabric Server-Side Mods
|
||||
'fabric-1.20.1',
|
||||
'fabric-1.20.6',
|
||||
|
||||
// Example plugin
|
||||
'example-plugin'
|
||||
)
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.velocity;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.william278.uniform.CommandUser;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record VelocityCommandUser(CommandSource source) implements CommandUser {
|
||||
|
||||
@Override
|
||||
public Audience getAudience() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getName() {
|
||||
return source instanceof Player ? ((Player) source).getUsername() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public UUID getUniqueId() {
|
||||
return source instanceof Player ? ((Player) source).getUniqueId() : null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue