use java8 to build

master
InkerBot 3 months ago
parent e4dff33015
commit ad25f16f4d

@ -9,7 +9,7 @@ plugins {
id 'java'
}
group 'net.william278'
group 'org.inksnow.husk'
version "$ext.library_version${versionMetadata()}"
description "$ext.library_description"
defaultTasks 'licenseFormat', 'build'
@ -21,30 +21,12 @@ ext {
publishing {
repositories {
if (System.getenv("RELEASES_MAVEN_USERNAME") != null) {
maven {
name = "william278-releases"
url = "https://repo.william278.net/releases"
credentials {
username = System.getenv("RELEASES_MAVEN_USERNAME")
password = System.getenv("RELEASES_MAVEN_PASSWORD")
}
authentication {
basic(BasicAuthentication)
}
}
}
if (System.getenv("SNAPSHOTS_MAVEN_USERNAME") != null) {
maven {
name = "william278-snapshots"
url = "https://repo.william278.net/snapshots"
credentials {
username = System.getenv("SNAPSHOTS_MAVEN_USERNAME")
password = System.getenv("SNAPSHOTS_MAVEN_PASSWORD")
}
authentication {
basic(BasicAuthentication)
}
maven {
name = 'husk-release'
url = findProperty("repository.huskrelease.url")
credentials {
username = findProperty("repository.huskrelease.username")
password = findProperty("repository.huskrelease.password")
}
}
}
@ -56,7 +38,7 @@ allprojects {
apply plugin: 'java'
compileJava.options.encoding = 'UTF-8'
compileJava.options.release.set 17
compileJava.options.release.set 8
javadoc.options.encoding = 'UTF-8'
javadoc.options.addStringOption('Xdoclint:none', '-quiet')
@ -134,7 +116,7 @@ subprojects {
if (['common'].contains(project.name)) {
publications {
mavenJavaCommon(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-common'
version = "$rootProject.version"
artifact shadowJar
@ -147,7 +129,7 @@ subprojects {
if (['paper'].contains(project.name)) {
publications {
mavenJavaPaper(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-paper'
version = "$rootProject.version"
artifact shadowJar
@ -160,7 +142,7 @@ subprojects {
if (['bukkit'].contains(project.name)) {
publications {
mavenJavaBukkit(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-bukkit'
version = "$rootProject.version"
artifact shadowJar
@ -173,7 +155,7 @@ subprojects {
if (['velocity'].contains(project.name)) {
publications {
mavenJavaVelocity(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-velocity'
version = "$rootProject.version"
artifact shadowJar
@ -186,7 +168,7 @@ subprojects {
if (['fabric-1.20.1'].contains(project.name)) {
publications {
mavenJavaFabric1_20_1(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-fabric'
version = "${rootProject.version}+1.20.1"
artifact remapJar
@ -199,7 +181,7 @@ subprojects {
if (['fabric-1.21'].contains(project.name)) {
publications {
mavenJavaFabric1_21(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-fabric'
version = "${rootProject.version}+1.21"
artifact remapJar
@ -212,7 +194,7 @@ subprojects {
if (['sponge-11'].contains(project.name)) {
publications {
mavenSponge11(MavenPublication) {
groupId = 'net.william278.uniform'
groupId = 'org.inksnow.husk.uniform'
artifactId = 'uniform-sponge'
version = "${rootProject.version}+11"
artifact shadowJar

@ -32,6 +32,7 @@ import net.william278.uniform.BaseCommand;
import net.william278.uniform.Command;
import net.william278.uniform.Permission;
import net.william278.uniform.Uniform;
import net.william278.uniform.util.StringUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
@ -82,14 +83,17 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
this.setAliases(command.getAliases());
this.setUsage(getUsageText());
if (permission != null) {
this.setPermission(permission.node());
this.setPermission(permission.getNode());
}
}
@NotNull
private String getUsageText() {
return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender()).values().stream()
.map("/%s"::formatted).collect(Collectors.joining("\n"));
return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender())
.values()
.stream()
.map("/"::concat)
.collect(Collectors.joining("\n"));
}
@SuppressWarnings("deprecation")
@ -121,13 +125,15 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
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())
.thenApply(suggestions -> suggestions.getList().stream()
.map(Suggestion::getText)
.collect(Collectors.toList()))
.join();
}
@Override
public boolean testPermissionSilent(@NotNull CommandSender target) {
if (permission == null || permission.node().isBlank()) {
if (permission == null || StringUtil.isBlank(permission.getNode())) {
return true;
}
return new BukkitCommandUser(target).checkPermission(permission);
@ -140,7 +146,10 @@ public class BukkitCommand extends BaseCommand<CommandSender> {
@NotNull
private String getInput(@NotNull String[] args) {
return args.length == 0 ? getName() : "%s %s".formatted(getName(), String.join(" ", args));
if (args.length == 0) {
return getName();
}
return getName() + " " + String.join(" ", args);
}
}

@ -21,6 +21,7 @@
package net.william278.uniform.bukkit;
import lombok.Value;
import net.kyori.adventure.audience.Audience;
import net.william278.uniform.CommandUser;
import net.william278.uniform.Permission;
@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public record BukkitCommandUser(@NotNull CommandSender sender) implements CommandUser {
@Value
public class BukkitCommandUser implements CommandUser {
@NotNull CommandSender sender;
@Override
@NotNull
@ -48,15 +52,15 @@ public record BukkitCommandUser(@NotNull CommandSender sender) implements Comman
@Override
@Nullable
public UUID getUuid() {
return sender instanceof Player player ? player.getUniqueId() : null;
return sender instanceof Player ? ((Player) sender).getUniqueId() : null;
}
@Override
public boolean checkPermission(@NotNull Permission permission) {
if (sender.isPermissionSet(permission.node())) {
return sender.hasPermission(permission.node());
if (sender.isPermissionSet(permission.getNode())) {
return sender.hasPermission(permission.getNode());
}
return permission.defaultValue().check(
return permission.getDefaultValue().check(
sender.isOp() || sender instanceof ConsoleCommandSender
);
}

@ -37,6 +37,7 @@ import space.arim.morepaperlib.commands.CommandRegistration;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* A class for registering commands with the Bukkit server's CommandMap
@ -91,7 +92,8 @@ public final class BukkitUniform implements Uniform {
registrar.getServerCommandMap().registerAll(
PLUGIN.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""),
Arrays.stream(commands).map(c -> (BukkitCommand) c)
.map(c -> (org.bukkit.command.Command) c.getImpl(this)).toList()
.map(c -> (org.bukkit.command.Command) c.getImpl(this))
.collect(Collectors.toList())
);
}

@ -6,6 +6,7 @@ plugins {
dependencies {
compileOnlyApi 'com.mojang:brigadier:1.1.8'
compileOnlyApi 'net.kyori:adventure-api:4.17.0'
compileOnlyApi 'com.google.guava:guava:21.0'
compileOnly 'org.jetbrains:annotations:24.1.0'
compileOnly 'org.projectlombok:lombok:1.18.32'

@ -21,6 +21,7 @@
package net.william278.uniform;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider;
@ -114,7 +115,7 @@ public abstract class BaseCommand<S> {
@SafeVarargs
public final void addConditionalSyntax(@Nullable Predicate<S> condition, @NotNull CommandExecutor<S> executor,
@NotNull CommandElement<S>... elements) {
var syntax = new CommandSyntax<>(condition, executor, List.of(elements));
CommandSyntax<S> syntax = new CommandSyntax<>(condition, executor, ImmutableList.copyOf(elements));
this.syntaxes.add(syntax);
}

@ -21,10 +21,13 @@
package net.william278.uniform;
import com.google.common.collect.ImmutableList;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.Value;
import net.william278.uniform.annotations.Argument;
import net.william278.uniform.annotations.CommandDescription;
import net.william278.uniform.annotations.CommandNode;
@ -39,6 +42,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
@ -51,7 +55,7 @@ import static net.william278.uniform.CommandExecutor.methodToExecutor;
public abstract class Command implements CommandProvider {
private final String name;
private List<String> aliases = List.of();
private List<String> aliases = Collections.emptyList();
private String description = "";
@Getter(AccessLevel.NONE)
private @Nullable Permission permission = null;
@ -70,7 +74,7 @@ public abstract class Command implements CommandProvider {
throw new IllegalArgumentException("@CommandNode annotation is required on annotated command/sub-commands");
}
this.name = node.value();
this.aliases = List.of(node.aliases());
this.aliases = ImmutableList.copyOf(node.aliases());
this.description = node.description();
this.scope = node.scope();
Permission.annotated(node.permission()).ifPresent(this::setPermission);
@ -87,11 +91,16 @@ public abstract class Command implements CommandProvider {
}
public boolean contains(@NotNull CommandUser user) {
return switch (this) {
case IN_GAME -> !user.isConsole();
case CONSOLE -> user.isConsole();
case ALL -> true;
};
switch (this) {
case IN_GAME:
return !user.isConsole();
case CONSOLE:
return user.isConsole();
case ALL:
return true;
default:
throw new IllegalArgumentException("Unknown execution scope: " + this);
}
}
}
@ -194,8 +203,15 @@ public abstract class Command implements CommandProvider {
}
}
public record SubCommand(@NotNull String name, @NotNull List<String> aliases, @Nullable Permission permission,
@NotNull ExecutionScope scope, @NotNull CommandProvider provider) {
@Value
@RequiredArgsConstructor
public static class SubCommand {
@NotNull String name;
@NotNull List<String> aliases;
@Nullable Permission permission;
@NotNull ExecutionScope scope;
@NotNull CommandProvider provider;
public SubCommand(@NotNull String name, @NotNull List<String> aliases, @Nullable Permission permission,
@NotNull CommandProvider provider) {
this(name, aliases, permission, ExecutionScope.ALL, provider);
@ -206,11 +222,11 @@ public abstract class Command implements CommandProvider {
}
public SubCommand(@NotNull String name, @Nullable Permission permission, @NotNull CommandProvider provider) {
this(name, List.of(), permission, ExecutionScope.ALL, provider);
this(name, Collections.emptyList(), permission, ExecutionScope.ALL, provider);
}
public SubCommand(@NotNull String name, @NotNull CommandProvider provider) {
this(name, List.of(), null, ExecutionScope.ALL, provider);
this(name, Collections.emptyList(), null, ExecutionScope.ALL, provider);
}

@ -39,7 +39,7 @@ public interface CommandExecutor<S> {
@NotNull BaseCommand<?> cmd) {
return (context) -> {
try {
method.invoke(instance, injectParams(method, context, cmd));
method.invoke(instance, private$injectParams(method, context, cmd));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Failed to invoke command executor from annotated method", e);
}
@ -47,8 +47,8 @@ public interface CommandExecutor<S> {
}
@Nullable
private static Object @NotNull [] injectParams(@NotNull Method method, @NotNull CommandContext<?> context,
@NotNull BaseCommand<?> cmd) {
static Object @NotNull [] private$injectParams(@NotNull Method method, @NotNull CommandContext<?> context,
@NotNull BaseCommand<?> cmd) {
final Object[] params = new Object[method.getParameterCount()];
for (int i = 0; i < method.getParameterCount(); i++) {
final Parameter param = method.getParameters()[i];

@ -21,6 +21,7 @@
package net.william278.uniform;
import lombok.Value;
import net.william278.uniform.element.CommandElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -28,7 +29,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Predicate;
public record CommandSyntax<S>(@Nullable Predicate<S> condition, @NotNull CommandExecutor<S> executor,
@NotNull List<CommandElement<S>> elements) {
@Value
public class CommandSyntax<S> {
@Nullable Predicate<S> condition;
@NotNull CommandExecutor<S> executor;
@NotNull List<CommandElement<S>> elements;
}

@ -21,6 +21,9 @@
package net.william278.uniform;
import com.google.common.collect.ImmutableList;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import net.william278.uniform.element.CommandElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -31,8 +34,13 @@ import java.util.Map;
import static net.william278.uniform.Graph.commandToElement;
record ConversionNode<S>(@NotNull CommandElement<S> element, @Nullable Execution<S> execution,
@NotNull Map<CommandElement<S>, ConversionNode<S>> nextMap) {
@Value
@RequiredArgsConstructor
public class ConversionNode<S> {
@NotNull CommandElement<S> element;
@Nullable Execution<S> execution;
@NotNull Map<CommandElement<S>, ConversionNode<S>> nextMap;
static <S> @NotNull ConversionNode<S> fromCommand(@NotNull BaseCommand<S> command) {
ConversionNode<S> root = new ConversionNode<>(commandToElement(command), Execution.fromCommand(command));
@ -40,8 +48,8 @@ record ConversionNode<S>(@NotNull CommandElement<S> element, @Nullable Execution
for (CommandSyntax<S> syntax : command.getSyntaxes()) {
ConversionNode<S> syntaxNode = root;
for (CommandElement<S> element : syntax.elements()) {
boolean last = element == syntax.elements().get(syntax.elements().size() - 1);
for (CommandElement<S> element : syntax.getElements()) {
boolean last = element == syntax.getElements().get(syntax.getElements().size() - 1);
syntaxNode = syntaxNode.nextMap.computeIfAbsent(element, e -> {
Execution<S> execution = last ? Execution.fromSyntax(syntax) : null;
return new ConversionNode<>(e, execution);
@ -69,7 +77,7 @@ record ConversionNode<S>(@NotNull CommandElement<S> element, @Nullable Execution
nodes[i++] = entry.toNode();
}
return new Node<>(this.element, this.execution, List.of(nodes));
return new Node<>(this.element, this.execution, ImmutableList.copyOf(nodes));
}
}

@ -22,6 +22,7 @@
package net.william278.uniform;
import com.mojang.brigadier.builder.ArgumentBuilder;
import lombok.Value;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -29,11 +30,16 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S> defaultExecutor,
@Nullable CommandExecutor<S> executor, @Nullable Predicate<S> condition) implements Predicate<S> {
@Value
public class Execution<S> implements Predicate<S> {
private static final Executor CACHED_EXECUTOR = Executors.newCachedThreadPool();
@NotNull Predicate<S> predicate;
@Nullable CommandExecutor<S> defaultExecutor;
@Nullable CommandExecutor<S> executor;
@Nullable Predicate<S> condition;
@NotNull
static <S> Execution<S> fromCommand(@NotNull BaseCommand<S> command) {
CommandExecutor<S> defaultExecutor = command.getDefaultExecutor();
@ -42,9 +48,9 @@ record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S
CommandExecutor<S> executor = defaultExecutor;
Predicate<S> condition = defaultCondition;
for (CommandSyntax<S> syntax : command.getSyntaxes()) {
if (!syntax.elements().isEmpty()) continue;
executor = syntax.executor();
condition = syntax.condition();
if (!syntax.getElements().isEmpty()) continue;
executor = syntax.getExecutor();
condition = syntax.getCondition();
break;
}
@ -53,8 +59,8 @@ record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S
@NotNull
static <S> Execution<S> fromSyntax(@NotNull CommandSyntax<S> syntax) {
CommandExecutor<S> executor = syntax.executor();
Predicate<S> condition = syntax.condition();
CommandExecutor<S> executor = syntax.getExecutor();
Predicate<S> condition = syntax.getCondition();
return new Execution<>(source -> condition == null || condition.test(source), null, executor, condition);
}

@ -24,11 +24,14 @@ package net.william278.uniform;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import lombok.Value;
import net.william278.uniform.element.CommandElement;
import net.william278.uniform.element.LiteralElement;
import org.jetbrains.annotations.NotNull;
record Graph<S>(@NotNull Node<S> root) {
@Value
public class Graph<S> {
@NotNull Node<S> root;
static <S> @NotNull Graph<S> create(@NotNull BaseCommand<S> command) {
return new Graph<>(Node.command(command));
@ -41,10 +44,10 @@ record Graph<S>(@NotNull Node<S> root) {
@NotNull
LiteralCommandNode<S> build() {
CommandNode<S> node = this.root.build();
if (!(node instanceof LiteralCommandNode<S> literalNode)) {
if (!(node instanceof LiteralCommandNode)) {
throw new IllegalStateException("Root node is somehow not a literal node. This should be impossible.");
}
return literalNode;
return (LiteralCommandNode<S>) node;
}
@NotNull
@ -52,7 +55,7 @@ record Graph<S>(@NotNull Node<S> root) {
final LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder.literal(name);
final CommandNode<S> command = this.root.build();
builder.executes(command.getCommand());
this.root.children().forEach(child -> builder.then(child.build()));
this.root.getChildren().forEach(child -> builder.then(child.build()));
return builder;
}

@ -23,13 +23,18 @@ package net.william278.uniform;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import lombok.Value;
import net.william278.uniform.element.CommandElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
record Node<S>(@NotNull CommandElement<S> element, @Nullable Execution<S> execution, @NotNull List<Node<S>> children) {
@Value
public class Node<S> {
@NotNull CommandElement<S> element;
@Nullable Execution<S> execution;
@NotNull List<Node<S>> children;
static <S> @NotNull Node<S> command(@NotNull BaseCommand<S> command) {
return ConversionNode.fromCommand(command).toNode();

@ -21,14 +21,22 @@
package net.william278.uniform;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import net.william278.uniform.annotations.PermissionNode;
import net.william278.uniform.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.function.Predicate;
@SuppressWarnings("unused")
public record Permission(@NotNull String node, @NotNull Default defaultValue) {
@Value
@RequiredArgsConstructor
public class Permission {
@NotNull String node;
@NotNull Default defaultValue;
Permission(@NotNull String node) {
this(node, Default.FALSE);
@ -51,7 +59,7 @@ public record Permission(@NotNull String node, @NotNull Default defaultValue) {
@NotNull
static Optional<Permission> annotated(@NotNull PermissionNode annotation) {
if (annotation.value().isBlank()) {
if (StringUtil.isBlank(annotation.value())) {
return Optional.empty();
}
return Optional.of(new Permission(annotation.value(), annotation.defaultValue()));
@ -63,11 +71,12 @@ public record Permission(@NotNull String node, @NotNull Default defaultValue) {
FALSE;
public boolean check(boolean op) {
return switch (this) {
case IF_OP -> op;
case TRUE -> true;
case FALSE -> false;
};
switch (this) {
case IF_OP: return op;
case TRUE: return true;
case FALSE: return false;
default: throw new IllegalStateException("Unexpected value: " + this);
}
}
}

@ -32,7 +32,7 @@ public interface Uniform {
default void register(@NotNull Object... annotated) {
register(Arrays.stream(annotated)
.map(c -> c instanceof Command cmd ? cmd : new Command.AnnotatedCommand(c))
.map(c -> c instanceof Command ? (Command) c : new Command.AnnotatedCommand(c))
.toArray(Command[]::new));
}

@ -50,7 +50,7 @@ public final class ArgumentElement<S, T> implements CommandElement<S> {
@Override
@NotNull
public ArgumentBuilder<S, ?> toBuilder() {
var builder = RequiredArgumentBuilder.<S, T>argument(this.name, this.type);
RequiredArgumentBuilder<S, T> builder = RequiredArgumentBuilder.<S, T>argument(this.name, this.type);
if (this.suggestionProvider != null) builder.suggests(this.suggestionProvider);
return builder;
}

@ -23,9 +23,12 @@ package net.william278.uniform.element;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import lombok.Value;
import org.jetbrains.annotations.NotNull;
public record LiteralElement<S>(@NotNull String name) implements CommandElement<S> {
@Value
public class LiteralElement<S> implements CommandElement<S> {
@NotNull String name;
@Override
@NotNull

@ -0,0 +1,40 @@
/*
* 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.util;
import lombok.experimental.UtilityClass;
@UtilityClass
public class StringUtil {
public static boolean isBlank(String $this) {
final int strLen = $this.length();
if (strLen == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace($this.charAt(i))) {
return false;
}
}
return true;
}
}

@ -21,6 +21,7 @@
package net.william278.uniform;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.kyori.adventure.text.Component;
import net.william278.uniform.element.ArgumentElement;
@ -38,7 +39,7 @@ public class ExtendedCommand extends Command {
public ExtendedCommand() {
super("example");
setDescription("An example command for Uniform");
setAliases(List.of("helloworld"));
setAliases(ImmutableList.of("helloworld"));
setPermission(Permission.defaultIfOp("uniform.example"));
}
@ -56,9 +57,21 @@ public class ExtendedCommand extends Command {
final CommandUser user = sub.getUser(ctx.getSource());
final IceCreamFlavor flavor = ctx.getArgument("flavor", IceCreamFlavor.class);
switch (flavor) {
case VANILLA -> user.getAudience().sendMessage(Component.text("Vanilla ice cream is fine!"));
case CHOCOLATE -> user.getAudience().sendMessage(Component.text("Chocolate ice cream is kino!"));
case STRAWBERRY -> user.getAudience().sendMessage(Component.text("Strawberry ice cream is ok..."));
case VANILLA: {
user.getAudience().sendMessage(Component.text("Vanilla ice cream is fine!"));
break;
}
case CHOCOLATE: {
user.getAudience().sendMessage(Component.text("Chocolate ice cream is kino!"));
break;
}
case STRAWBERRY: {
user.getAudience().sendMessage(Component.text("Strawberry ice cream is ok..."));
break;
}
default: {
// do nothing
}
}
}, exampleCustomArg()));
}

@ -27,6 +27,7 @@ import com.mojang.brigadier.suggestion.Suggestion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.william278.uniform.*;
import net.william278.uniform.util.StringUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
@ -87,14 +88,14 @@ public class LegacyPaperCommand extends BaseCommand<CommandSender> {
this.setAliases(command.getAliases());
this.setUsage(getUsageText());
if (permission != null) {
this.setPermission(permission.node());
this.setPermission(permission.getNode());
}
}
@NotNull
private String getUsageText() {
return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender()).values().stream()
.map("/%s"::formatted).collect(Collectors.joining("\n"));
.map("/"::concat).collect(Collectors.joining("\n"));
}
@SuppressWarnings("deprecation")
@ -126,13 +127,16 @@ public class LegacyPaperCommand extends BaseCommand<CommandSender> {
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())
.thenApply(suggestions -> suggestions.getList()
.stream()
.map(Suggestion::getText)
.collect(Collectors.toList()))
.join();
}
@Override
public boolean testPermissionSilent(@NotNull CommandSender target) {
if (permission == null || permission.node().isBlank()) {
if (permission == null || StringUtil.isBlank(permission.getNode())) {
return true;
}
return new LegacyPaperCommandUser(target).checkPermission(permission);
@ -140,7 +144,10 @@ public class LegacyPaperCommand extends BaseCommand<CommandSender> {
@NotNull
private String getInput(@NotNull String[] args) {
return args.length == 0 ? getName() : "%s %s".formatted(getName(), String.join(" ", args));
if (args.length == 0) {
return getName();
}
return getName() + " " + String.join(" ", args);
}
}

@ -21,6 +21,7 @@
package net.william278.uniform.paper;
import lombok.Value;
import net.kyori.adventure.audience.Audience;
import net.william278.uniform.CommandUser;
import net.william278.uniform.Permission;
@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public record LegacyPaperCommandUser(@NotNull CommandSender sender) implements CommandUser {
@Value
public class LegacyPaperCommandUser implements CommandUser {
@NotNull CommandSender sender;
@Override
@NotNull
@ -48,15 +52,15 @@ public record LegacyPaperCommandUser(@NotNull CommandSender sender) implements C
@Override
@Nullable
public UUID getUuid() {
return sender instanceof Player player ? player.getUniqueId() : null;
return sender instanceof Player ? ((Player) sender).getUniqueId() : null;
}
@Override
public boolean checkPermission(@NotNull Permission permission) {
if (sender.isPermissionSet(permission.node())) {
return sender.hasPermission(permission.node());
if (sender.isPermissionSet(permission.getNode())) {
return sender.hasPermission(permission.getNode());
}
return permission.defaultValue().check(
return permission.getDefaultValue().check(
sender.isOp() || sender instanceof ConsoleCommandSender
);
}

@ -21,6 +21,7 @@
package net.william278.uniform.paper;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
@ -38,6 +39,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@SuppressWarnings({"unused", "UnstableApiUsage"})
public class PaperCommand extends BaseCommand<CommandSourceStack> {
@ -92,11 +94,11 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
if (playerName.equals("@a")) {
return Bukkit.getOnlinePlayers();
}
var player = Bukkit.getPlayer(playerName);
Player player = Bukkit.getPlayer(playerName);
if (player == null) {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
}
return List.of(player);
return ImmutableList.of(player);
}, (context, builder) -> {
builder.suggest("@a");
for (Player player : Bukkit.getOnlinePlayers()) {
@ -112,9 +114,9 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
public List<CommandSyntax<CommandSourceStack>> getSyntaxes() {
return super.getSyntaxes().stream().map(
syntax -> new CommandSyntax<>(
syntax.condition(),
syntax.executor(),
syntax.elements().stream()
syntax.getCondition(),
syntax.getExecutor(),
syntax.getElements().stream()
.filter(e -> e instanceof ArgumentElement)
.map(e -> (ArgumentElement<?, ?>) e)
.map(e -> e.custom() ? new ArgumentElement<>(
@ -123,9 +125,9 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
e.suggestionProvider()
) : e)
.map(e -> (CommandElement<CommandSourceStack>) e)
.toList()
.collect(Collectors.toList())
)
).toList();
).collect(Collectors.toList());
}
@Override

@ -22,6 +22,7 @@
package net.william278.uniform.paper;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import lombok.Value;
import net.kyori.adventure.audience.Audience;
import net.william278.uniform.CommandUser;
import net.william278.uniform.Permission;
@ -32,8 +33,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID;
@SuppressWarnings("UnstableApiUsage")
public record PaperCommandUser(@NotNull CommandSourceStack source) implements CommandUser {
@Value
public class PaperCommandUser implements CommandUser {
@NotNull CommandSourceStack source;
@Override
@NotNull
@ -55,10 +57,10 @@ public record PaperCommandUser(@NotNull CommandSourceStack source) implements Co
@Override
public boolean checkPermission(@NotNull Permission permission) {
if (source.getSender().isPermissionSet(permission.node())) {
return source.getSender().hasPermission(permission.node());
if (source.getSender().isPermissionSet(permission.getNode())) {
return source.getSender().hasPermission(permission.getNode());
}
return permission.defaultValue().check(
return permission.getDefaultValue().check(
source.getSender().isOp() || source.getSender() instanceof ConsoleCommandSender
);
}

@ -35,6 +35,7 @@ import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@ -108,7 +109,8 @@ public final class PaperUniform implements Uniform {
// Register with the legacy API
plugin.getServer().getCommandMap().registerAll(
plugin.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""),
s.map(c -> (LegacyPaperCommand) c).map(c -> (org.bukkit.command.Command) c.getImpl(this)).toList()
s.map(c -> (LegacyPaperCommand) c).map(c -> (org.bukkit.command.Command) c.getImpl(this))
.collect(Collectors.toList())
);
}

@ -12,14 +12,14 @@ include(
// Server Plugins
'paper',
'bukkit',
'sponge-11',
// 'sponge-11',
// Proxy Plugins
'velocity',
// Fabric Server-Side Mods
'fabric-1.20.1',
'fabric-1.21',
// 'fabric-1.20.1',
// 'fabric-1.21',
// Example plugin
'example-plugin'

@ -24,6 +24,7 @@ package net.william278.uniform.velocity;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import lombok.Value;
import net.kyori.adventure.audience.Audience;
import net.william278.uniform.CommandUser;
import net.william278.uniform.Permission;
@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public record VelocityCommandUser(CommandSource source) implements CommandUser {
@Value
public class VelocityCommandUser implements CommandUser {
CommandSource source;
@Override
@NotNull
@ -54,10 +58,10 @@ public record VelocityCommandUser(CommandSource source) implements CommandUser {
@Override
public boolean checkPermission(@NotNull Permission permission) {
if (source.getPermissionValue(permission.node()) != Tristate.UNDEFINED) {
return source.getPermissionValue(permission.node()) == Tristate.TRUE;
if (source.getPermissionValue(permission.getNode()) != Tristate.UNDEFINED) {
return source.getPermissionValue(permission.getNode()) == Tristate.TRUE;
}
return permission.defaultValue().check(source.hasPermission(permission.node()));
return permission.getDefaultValue().check(source.hasPermission(permission.getNode()));
}
}

Loading…
Cancel
Save