|
1 | 1 | package net.thenextlvl.gopaint.command; |
2 | 2 |
|
3 | | -import com.mojang.brigadier.Command; |
4 | | -import com.mojang.brigadier.arguments.IntegerArgumentType; |
5 | | -import com.mojang.brigadier.context.CommandContext; |
| 3 | +import com.mojang.brigadier.tree.LiteralCommandNode; |
6 | 4 | import io.papermc.paper.command.brigadier.CommandSourceStack; |
7 | 5 | import io.papermc.paper.command.brigadier.Commands; |
8 | | -import io.papermc.paper.command.brigadier.argument.ArgumentTypes; |
9 | | -import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; |
10 | | -import net.kyori.adventure.key.Key; |
11 | | -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; |
12 | 6 | import net.thenextlvl.gopaint.GoPaintPlugin; |
13 | | -import net.thenextlvl.gopaint.api.brush.PatternBrush; |
14 | 7 | import net.thenextlvl.gopaint.api.model.GoPaintProvider; |
15 | | -import org.bukkit.entity.Player; |
16 | | -import org.bukkit.inventory.ItemStack; |
17 | 8 | import org.jspecify.annotations.NullMarked; |
18 | 9 |
|
19 | | -import java.util.List; |
20 | | - |
21 | 10 | @NullMarked |
22 | 11 | public class GoPaintCommand { |
23 | | - private final GoPaintPlugin plugin; |
24 | | - |
25 | | - public GoPaintCommand(GoPaintPlugin plugin) { |
26 | | - this.plugin = plugin; |
27 | | - } |
28 | | - |
29 | | - public void register() { |
30 | | - var command = Commands.literal("gopaint") |
| 12 | + public static LiteralCommandNode<CommandSourceStack> create(GoPaintPlugin plugin) { |
| 13 | + return Commands.literal("gopaint") |
31 | 14 | .requires(stack -> stack.getSender().hasPermission(GoPaintProvider.USE_PERMISSION)) |
32 | | - .then(Commands.literal("size") |
33 | | - .requires(stack -> stack.getSender() instanceof Player) |
34 | | - .then(Commands.argument("size", IntegerArgumentType.integer(1, 100)) |
35 | | - .executes(this::size))) |
36 | | - .then(Commands.literal("menu") |
37 | | - .requires(stack -> stack.getSender() instanceof Player) |
38 | | - .executes(this::menu)) |
39 | | - .then(Commands.literal("brush") |
40 | | - .then(Commands.argument("brush", ArgumentTypes.key()) |
41 | | - .suggests((context, builder) -> { |
42 | | - plugin.brushRegistry().getBrushes() |
43 | | - .map(PatternBrush::key) |
44 | | - .map(Key::asString) |
45 | | - .filter(key -> key.contains(builder.getRemaining())) |
46 | | - .forEach(builder::suggest); |
47 | | - return builder.buildFuture(); |
48 | | - }) |
49 | | - .requires(stack -> stack.getSender() instanceof Player) |
50 | | - .executes(this::brush))) |
51 | | - .then(Commands.literal("wand") |
52 | | - .requires(stack -> stack.getSender() instanceof Player) |
53 | | - .executes(this::wand)) |
54 | | - .then(Commands.literal("export") |
55 | | - .requires(stack -> stack.getSender() instanceof Player) |
56 | | - .executes(this::exportSettings)) |
57 | | - .then(Commands.literal("import") |
58 | | - .requires(stack -> stack.getSender() instanceof Player) |
59 | | - .executes(this::importSettings)) |
60 | | - .then(Commands.literal("toggle") |
61 | | - .requires(stack -> stack.getSender() instanceof Player) |
62 | | - .executes(this::toggle)) |
63 | | - .then(Commands.literal("reload") |
64 | | - .requires(stack -> stack.getSender().hasPermission(GoPaintProvider.ADMIN_PERMISSION)) |
65 | | - .executes(this::reload)) |
| 15 | + .then(BrushCommand.create(plugin)) |
| 16 | + .then(ExportCommand.create(plugin)) |
| 17 | + .then(ImportCommand.create(plugin)) |
| 18 | + .then(MenuCommand.create(plugin)) |
| 19 | + .then(ReloadCommand.create(plugin)) |
| 20 | + .then(SizeCommand.create(plugin)) |
| 21 | + .then(ToggleCommand.create(plugin)) |
| 22 | + .then(WandCommand.create(plugin)) |
66 | 23 | .build(); |
67 | | - plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS.newHandler(event -> |
68 | | - event.registrar().register(command, List.of("gp")))); |
69 | | - } |
70 | | - |
71 | | - private int exportSettings(CommandContext<CommandSourceStack> context) { |
72 | | - var player = (Player) context.getSource().getSender(); |
73 | | - |
74 | | - var mainHand = player.getInventory().getItemInMainHand(); |
75 | | - var settings = plugin.brushController().getBrushSettings(player); |
76 | | - |
77 | | - plugin.bundle().sendMessage(player, settings.exportSettings(mainHand) ? |
78 | | - "command.gopaint.export.success" : "command.gopaint.export.failed"); |
79 | | - |
80 | | - return Command.SINGLE_SUCCESS; |
81 | | - } |
82 | | - |
83 | | - private int importSettings(CommandContext<CommandSourceStack> context) { |
84 | | - var player = (Player) context.getSource().getSender(); |
85 | | - |
86 | | - var mainHand = player.getInventory().getItemInMainHand(); |
87 | | - var settings = plugin.brushController().getBrushSettings(player); |
88 | | - var parsed = plugin.brushController().parseBrushSettings(mainHand); |
89 | | - |
90 | | - parsed.ifPresent(settings::importSettings); |
91 | | - |
92 | | - plugin.bundle().sendMessage(player, parsed.isPresent() ? |
93 | | - "command.gopaint.import.success" : "command.gopaint.import.failed"); |
94 | | - |
95 | | - return Command.SINGLE_SUCCESS; |
96 | | - } |
97 | | - |
98 | | - private int brush(CommandContext<CommandSourceStack> context) { |
99 | | - var player = (Player) context.getSource().getSender(); |
100 | | - var settings = plugin.brushController().getBrushSettings(player); |
101 | | - var argument = context.getArgument("brush", Key.class); |
102 | | - plugin.brushRegistry().getBrush(argument).ifPresentOrElse(brush -> { |
103 | | - plugin.bundle().sendMessage(player, "brush.set", |
104 | | - Placeholder.component("brush", brush.getName(player))); |
105 | | - settings.setBrush(brush); |
106 | | - }, () -> plugin.bundle().sendMessage(player, "brush.unknown", |
107 | | - Placeholder.parsed("input", argument.asString()))); |
108 | | - return Command.SINGLE_SUCCESS; |
109 | | - } |
110 | | - |
111 | | - private int menu(CommandContext<CommandSourceStack> context) { |
112 | | - var player = (Player) context.getSource().getSender(); |
113 | | - var settings = plugin.brushController().getBrushSettings(player); |
114 | | - settings.getMainMenu().open(); |
115 | | - return Command.SINGLE_SUCCESS; |
116 | | - } |
117 | | - |
118 | | - private int size(CommandContext<CommandSourceStack> context) { |
119 | | - var player = (Player) context.getSource().getSender(); |
120 | | - var settings = plugin.brushController().getBrushSettings(player); |
121 | | - settings.setBrushSize(context.getArgument("size", int.class)); |
122 | | - plugin.bundle().sendMessage(player, "command.gopaint.brush.size", |
123 | | - Placeholder.parsed("size", String.valueOf(settings.getBrushSize()))); |
124 | | - return Command.SINGLE_SUCCESS; |
125 | | - } |
126 | | - |
127 | | - private int toggle(CommandContext<CommandSourceStack> context) { |
128 | | - var player = (Player) context.getSource().getSender(); |
129 | | - var settings = plugin.brushController().getBrushSettings(player); |
130 | | - settings.setEnabled(!settings.isEnabled()); |
131 | | - var message = settings.isEnabled() ? "command.gopaint.brush.enabled" |
132 | | - : "command.gopaint.brush.disabled"; |
133 | | - plugin.bundle().sendMessage(player, message); |
134 | | - return Command.SINGLE_SUCCESS; |
135 | | - } |
136 | | - |
137 | | - private int reload(CommandContext<CommandSourceStack> context) { |
138 | | - var sender = context.getSource().getSender(); |
139 | | - plugin.reloadConfig(); |
140 | | - plugin.bundle().sendMessage(sender, "command.gopaint.reloaded"); |
141 | | - return Command.SINGLE_SUCCESS; |
142 | | - } |
143 | | - |
144 | | - private int wand(CommandContext<CommandSourceStack> context) { |
145 | | - var player = (Player) context.getSource().getSender(); |
146 | | - plugin.bundle().sendMessage(player, giveWand(player) |
147 | | - ? "command.gopaint.wand.success" |
148 | | - : "command.gopaint.wand.failed"); |
149 | | - return Command.SINGLE_SUCCESS; |
150 | | - } |
151 | | - |
152 | | - private boolean giveWand(Player player) { |
153 | | - var type = plugin.config().brushConfig().defaultBrushType(); |
154 | | - |
155 | | - var inventory = player.getInventory(); |
156 | | - var first = inventory.first(type); |
157 | | - |
158 | | - if (first != -1) { |
159 | | - if (inventory.getHeldItemSlot() == first) return true; |
160 | | - |
161 | | - if (first >= 0 && first <= 8) { |
162 | | - inventory.setHeldItemSlot(first); |
163 | | - return true; |
164 | | - } |
165 | | - |
166 | | - var item = inventory.getItem(first); |
167 | | - |
168 | | - inventory.setItem(first, inventory.getItemInMainHand()); |
169 | | - inventory.setItemInMainHand(item); |
170 | | - |
171 | | - return true; |
172 | | - } |
173 | | - |
174 | | - if (inventory.getItemInMainHand().isEmpty()) { |
175 | | - inventory.setItemInMainHand(new ItemStack(type)); |
176 | | - return true; |
177 | | - } |
178 | | - |
179 | | - var empty = inventory.firstEmpty(); |
180 | | - if (empty == -1) return false; |
181 | | - |
182 | | - inventory.setItem(empty, inventory.getItemInMainHand()); |
183 | | - inventory.setItemInMainHand(new ItemStack(type)); |
184 | | - return true; |
185 | 24 | } |
186 | 25 | } |
0 commit comments