|
| 1 | +package dev.slne.surf.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.CommandDispatcher; |
| 4 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 5 | +import dev.slne.surf.command.sub.SurfCanvasReloadCommand; |
| 6 | +import io.canvasmc.canvas.command.*; |
| 7 | +import net.kyori.adventure.text.*; |
| 8 | +import net.kyori.adventure.text.event.ClickEvent; |
| 9 | +import net.kyori.adventure.text.event.HoverEvent; |
| 10 | +import net.kyori.adventure.text.format.*; |
| 11 | +import net.minecraft.commands.CommandSourceStack; |
| 12 | +import org.bukkit.command.CommandSender; |
| 13 | +import org.jspecify.annotations.*; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +import static net.minecraft.commands.Commands.literal; |
| 18 | + |
| 19 | +@NullMarked |
| 20 | +public class SurfCanvasRootCommandTree { |
| 21 | + |
| 22 | + private static final TextColor HEADER = TextColor.color(47, 140, 94); |
| 23 | + private static final TextColor PRIMARY = TextColor.color(32, 143, 89); |
| 24 | + private static final TextColor SECONDARY = TextColor.color(20, 123, 116); |
| 25 | + private static final TextColor INFORMATION = TextColor.color(79, 143, 47); |
| 26 | + private static final TextColor LIST = TextColor.color(21, 96, 68); |
| 27 | + private static final TextColor ACCENT = TextColor.color(47, 140, 94); |
| 28 | + private static final TextColor MUTED = TextColor.color(80, 120, 100); |
| 29 | + |
| 30 | + public static final SurfCanvasRootCommandTree INSTANCE; |
| 31 | + |
| 32 | + static { |
| 33 | + INSTANCE = new SurfCanvasRootCommandTree(); |
| 34 | + INSTANCE.register(SurfCanvasReloadCommand.class); |
| 35 | + } |
| 36 | + |
| 37 | + private final List<Command> subCommands = new LinkedList<>(); |
| 38 | + |
| 39 | + private Component buildDetailComponent(Command subCommand) { |
| 40 | + String name = subCommand.getName(); |
| 41 | + String description = subCommand.getDescription(); |
| 42 | + boolean selfCmd = subCommand.isAllowedSelfCommand(); |
| 43 | + |
| 44 | + TextComponent.Builder builder = Component.text() |
| 45 | + .append(Component.text("----", SECONDARY)) |
| 46 | + .append(Component.text("/surfcanvas " + name, HEADER).decorate(TextDecoration.BOLD)) |
| 47 | + .append(Component.text("----", SECONDARY)) |
| 48 | + .appendNewline() |
| 49 | + .appendNewline(); |
| 50 | + |
| 51 | + builder.append(Component.text(" Description ", MUTED).decorate(TextDecoration.BOLD)) |
| 52 | + .append(Component.text(description != null ? description : "No description provided.", ACCENT)) |
| 53 | + .appendNewline(); |
| 54 | + |
| 55 | + builder.append(Component.text(" Permission ", MUTED).decorate(TextDecoration.BOLD)) |
| 56 | + .append(Component.text("surfcanvas.command." + name, ACCENT)) |
| 57 | + .appendNewline(); |
| 58 | + |
| 59 | + builder.append(Component.text(" Standalone ", MUTED).decorate(TextDecoration.BOLD)) |
| 60 | + .append(selfCmd |
| 61 | + ? Component.text("Yes ", TextColor.color(100, 220, 140)).append(Component.text("(/" + name + ", /surfcanvas:" + name + ")", INFORMATION)) |
| 62 | + : Component.text("No", TextColor.color(220, 100, 100))) |
| 63 | + .appendNewline() |
| 64 | + .appendNewline(); |
| 65 | + |
| 66 | + builder.append(Component.text("-----------------------", SECONDARY)); |
| 67 | + |
| 68 | + return builder.build(); |
| 69 | + } |
| 70 | + |
| 71 | + public void build(CommandDispatcher<CommandSourceStack> dispatcher) { |
| 72 | + LiteralArgumentBuilder<CommandSourceStack> root = literal("surfcanvas") |
| 73 | + .requires(source -> source.getSender().isOp() || source.getSender().hasPermission("surfcanvas.command")); |
| 74 | + |
| 75 | + for (Command subCommand : subCommands) { |
| 76 | + String name = subCommand.getName(); |
| 77 | + |
| 78 | + root.then(subCommand.construct(literal(name) |
| 79 | + .requires(source -> source.getSender().isOp() || source.getSender().hasPermission("surfcanvas.command." + name)))); |
| 80 | + |
| 81 | + if (subCommand.isAllowedSelfCommand()) { |
| 82 | + dispatcher.register(subCommand.construct(literal(name) |
| 83 | + .requires(source -> source.getSender().isOp() || source.getSender().hasPermission("surfcanvas.command." + name)))); |
| 84 | + |
| 85 | + dispatcher.register(subCommand.construct(literal("surfcanvas:" + name) |
| 86 | + .requires(source -> source.getSender().isOp() || source.getSender().hasPermission("surfcanvas.command." + name)))); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + root.then(literal("help") |
| 91 | + .requires(source -> source.getSender().isOp() || source.getSender().hasPermission("surfcanvas.command.help")) |
| 92 | + .executes(context -> { |
| 93 | + CommandSender bukkitSender = context.getSource().getBukkitSender(); |
| 94 | + |
| 95 | + TextComponent.Builder builder = Component.text() |
| 96 | + .append(Component.text("----", SECONDARY)) |
| 97 | + .append(Component.text("SurfCanvas Commands", HEADER).decorate(TextDecoration.BOLD)) |
| 98 | + .append(Component.text("----", SECONDARY)) |
| 99 | + .appendNewline(); |
| 100 | + |
| 101 | + for (Command subCommand : subCommands) { |
| 102 | + String name = subCommand.getName(); |
| 103 | + if (!bukkitSender.hasPermission("surfcanvas.command." + name)) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + Component hoverText = Component.text() |
| 108 | + .append(Component.text("Click to view further details", INFORMATION)) |
| 109 | + .build(); |
| 110 | + |
| 111 | + Component detailComponent = buildDetailComponent(subCommand); |
| 112 | + |
| 113 | + Component entry = Component.text() |
| 114 | + .append(Component.text("- ").color(LIST)) |
| 115 | + .append(Component.text("/").color(SECONDARY)) |
| 116 | + .append(Component.text(name, PRIMARY) |
| 117 | + .decorate(TextDecoration.UNDERLINED) |
| 118 | + .hoverEvent(HoverEvent.showText(hoverText)) |
| 119 | + .clickEvent(ClickEvent.callback((audience) -> audience.sendMessage(detailComponent)))) |
| 120 | + .appendNewline() |
| 121 | + .build(); |
| 122 | + |
| 123 | + builder.append(entry); |
| 124 | + } |
| 125 | + |
| 126 | + builder.append(Component.text("-----------------------", SECONDARY)); |
| 127 | + |
| 128 | + bukkitSender.sendMessage(builder.build()); |
| 129 | + return 1; |
| 130 | + })); |
| 131 | + dispatcher.register(root); |
| 132 | + } |
| 133 | + |
| 134 | + public void register(Class<? extends Command> command) { |
| 135 | + try { |
| 136 | + if (command.getDeclaredConstructor().getParameterCount() != 0) { |
| 137 | + throw new IllegalArgumentException("Command must have no-arg constructor"); |
| 138 | + } |
| 139 | + this.subCommands.add( |
| 140 | + command.getDeclaredConstructor().newInstance() |
| 141 | + ); |
| 142 | + } catch (InstantiationException | IllegalAccessException | java.lang.reflect.InvocationTargetException | |
| 143 | + NoSuchMethodException e) { |
| 144 | + throw new RuntimeException(e); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments