feat: support 1.17.1+ brigadier on Paper

dependabot/gradle/org.projectlombok-lombok-1.18.34
William 5 months ago
parent 2f3edf0ca1
commit b59b376eb6
No known key found for this signature in database

@ -22,7 +22,7 @@ Uniform _currently_ targets the following platforms:
| Platform | Artifact | Minecraft | Java |
|---------------|--------------------|:----------:|:------:|
| Common | `uniform-common` | - | \>`17` |
| Paper | `uniform-paper` | \>`1.20.6` | \>`21` |
| Paper | `uniform-paper` | \>`1.17.1` | \>`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` |

@ -21,7 +21,6 @@
package net.william278.uniform;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;

@ -4,8 +4,7 @@ plugins {
}
dependencies {
// implementation(project(":paper"))
implementation(project(":bukkit"))
implementation(project(":paper"))
compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT'
}

@ -21,15 +21,15 @@
package net.william278.uniform;
import net.william278.uniform.bukkit.BukkitUniform;
import net.william278.uniform.paper.PaperUniform;
import org.bukkit.plugin.java.JavaPlugin;
@SuppressWarnings("unused")
public class UniformExample extends JavaPlugin {
@Override
public void onLoad() {
BukkitUniform uniform = BukkitUniform.getInstance(this);
public void onEnable() {
PaperUniform uniform = PaperUniform.getInstance(this);
uniform.register(new ExampleCommand());
}

@ -1,4 +1,4 @@
name: UniformExample
version: 1.0
api-version: "1.20"
api-version: "1.19"
main: net.william278.uniform.UniformExample

@ -0,0 +1,86 @@
/*
* 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 com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import lombok.AllArgsConstructor;
import net.william278.uniform.BaseCommand;
import net.william278.uniform.Command;
import net.william278.uniform.CommandUser;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Set;
@SuppressWarnings({"removal", "deprecation", "UnstableApiUsage"})
public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource> {
public LegacyPaperCommand(@NotNull Command command) {
super(command);
}
public LegacyPaperCommand(@NotNull String name, @NotNull List<String> aliases) {
super(name, aliases);
}
public LegacyPaperCommand(@NotNull String name, @NotNull String description, @NotNull List<String> aliases) {
super(name, description, aliases);
}
@Override
@NotNull
protected CommandUser getUser(@NotNull Object user) {
return new LegacyPaperCommandUser((BukkitBrigadierCommandSource) user);
}
@Override
public void addSubCommand(@NotNull Command command) {
addSubCommand(new LegacyPaperCommand(command));
}
@AllArgsConstructor
static class Registrar implements Listener {
@NotNull
private final JavaPlugin plugin;
@NotNull
private final Set<LegacyPaperCommand> commands;
@EventHandler
public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
commands.forEach(command -> {
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build();
event.getRoot().addChild(built);
command.getAliases().forEach(alias -> event.getRoot().addChild(
LiteralArgumentBuilder.<BukkitBrigadierCommandSource>literal(alias).redirect(built).build()
));
});
commands.clear();
}
}
}

@ -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.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;
@SuppressWarnings("removal")
public record LegacyPaperCommandUser(@NotNull BukkitBrigadierCommandSource source) implements CommandUser {
@Override
@NotNull
public Audience getAudience() {
return source.getBukkitSender();
}
@Override
@Nullable
public String getName() {
return source.getBukkitEntity() != null ? source.getBukkitEntity().getName() : null;
}
@Override
@Nullable
public UUID getUuid() {
return source.getBukkitEntity() != null ? source.getBukkitEntity().getUniqueId() : null;
}
}

@ -23,6 +23,7 @@ package net.william278.uniform.paper;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import net.william278.uniform.BaseCommand;
import net.william278.uniform.Command;
import net.william278.uniform.CommandUser;
@ -30,10 +31,12 @@ import net.william278.uniform.element.ArgumentElement;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@SuppressWarnings({"unused", "UnstableApiUsage"})
public class PaperCommand extends BaseCommand<CommandSourceStack> {
@ -50,6 +53,18 @@ public class PaperCommand extends BaseCommand<CommandSourceStack> {
super(name, description, aliases);
}
static void register(@NotNull JavaPlugin plugin, @NotNull Set<PaperCommand> commands) {
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> {
commands.forEach(command -> event.registrar().register(
plugin.getPluginMeta(),
command.build(),
command.getDescription(),
command.getAliases()
));
commands.clear();
});
}
public static ArgumentElement<CommandSourceStack, Material> material(String name) {
return new ArgumentElement<>(name, reader -> {
String materialName = reader.readString();

@ -22,10 +22,7 @@
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;
@ -38,23 +35,30 @@ import java.util.Set;
*
* @since 1.0
*/
@SuppressWarnings("UnstableApiUsage")
public final class PaperUniform implements Uniform<CommandSourceStack, PaperCommand> {
public final class PaperUniform {
private static PaperUniform INSTANCE;
private final Set<LegacyPaperCommand> legacyCommands = Sets.newHashSet();
private final Set<PaperCommand> commands = Sets.newHashSet();
private PaperUniform(@NotNull JavaPlugin plugin) {
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> {
commands.forEach(command -> event.registrar().register(
plugin.getPluginMeta(),
command.build(),
command.getDescription(),
command.getAliases()
));
commands.clear();
});
if (isUseModernApi()) {
PaperCommand.register(plugin, commands);
} else {
plugin.getServer().getPluginManager().registerEvents(
new LegacyPaperCommand.Registrar(plugin, legacyCommands), plugin
);
}
}
private static boolean isUseModernApi() {
try {
Class.forName("io.papermc.paper.command.brigadier.CommandSourceStack");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
/**
@ -75,20 +79,32 @@ public final class PaperUniform implements Uniform<CommandSourceStack, PaperComm
* @param commands The commands to register
* @since 1.0
*/
@Override
public void register(@NotNull PaperCommand... commands) {
public void register(PaperCommand... 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(LegacyPaperCommand... commands) {
Collections.addAll(this.legacyCommands, commands);
}
/**
* Register command(s) to be added to the server's command manager
*
* @param commands The commands to register
* @since 1.0
*/
@Override
public void register(@NotNull Command... commands) {
if (isUseModernApi()) {
register(Arrays.stream(commands).map(PaperCommand::new).toArray(PaperCommand[]::new));
} else {
register(Arrays.stream(commands).map(LegacyPaperCommand::new).toArray(LegacyPaperCommand[]::new));
}
}
}

Loading…
Cancel
Save