Skip to content

Commit 604a3ed

Browse files
committed
Refactor commands into separate classes for maintainability
Commands such as "size", "menu", "toggle", and others were moved to dedicated classes, improving modularity and readability. The main "GoPaintCommand" class was streamlined to aggregate these subcommands. This change enhances the structure, making it easier to extend and maintain the code.
1 parent 9b32fbe commit 604a3ed

File tree

11 files changed

+321
-173
lines changed

11 files changed

+321
-173
lines changed

src/main/java/net/thenextlvl/gopaint/GoPaintPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import core.io.IO;
88
import core.paper.adapters.inventory.MaterialAdapter;
99
import core.paper.adapters.key.KeyAdapter;
10+
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
1011
import net.kyori.adventure.key.Key;
1112
import net.kyori.adventure.text.minimessage.MiniMessage;
1213
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
@@ -105,7 +106,8 @@ private void registerListeners() {
105106
}
106107

107108
private void registerCommands() {
108-
new GoPaintCommand(this).register();
109+
getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS.newHandler(event ->
110+
event.registrar().register(GoPaintCommand.create(this), List.of("gp"))));
109111
}
110112

111113
public PluginConfig config() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package net.thenextlvl.gopaint.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import io.papermc.paper.command.brigadier.CommandSourceStack;
7+
import io.papermc.paper.command.brigadier.Commands;
8+
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
9+
import net.kyori.adventure.key.Key;
10+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
11+
import net.thenextlvl.gopaint.GoPaintPlugin;
12+
import net.thenextlvl.gopaint.command.suggestion.BrushSuggestionProvider;
13+
import org.bukkit.entity.Player;
14+
import org.jspecify.annotations.NullMarked;
15+
16+
@NullMarked
17+
class BrushCommand {
18+
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
19+
return Commands.literal("brush")
20+
.then(Commands.argument("brush", ArgumentTypes.key())
21+
.suggests(new BrushSuggestionProvider<>(plugin))
22+
.requires(stack -> stack.getSender() instanceof Player)
23+
.executes(context -> brush(context, plugin)));
24+
}
25+
26+
private static int brush(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
27+
var player = (Player) context.getSource().getSender();
28+
var settings = plugin.brushController().getBrushSettings(player);
29+
var argument = context.getArgument("brush", Key.class);
30+
plugin.brushRegistry().getBrush(argument).ifPresentOrElse(brush -> {
31+
plugin.bundle().sendMessage(player, "brush.set",
32+
Placeholder.component("brush", brush.getName(player)));
33+
settings.setBrush(brush);
34+
}, () -> plugin.bundle().sendMessage(player, "brush.unknown",
35+
Placeholder.parsed("input", argument.asString())));
36+
return Command.SINGLE_SUCCESS;
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.thenextlvl.gopaint.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import io.papermc.paper.command.brigadier.CommandSourceStack;
7+
import io.papermc.paper.command.brigadier.Commands;
8+
import net.thenextlvl.gopaint.GoPaintPlugin;
9+
import org.bukkit.entity.Player;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
@NullMarked
13+
class ExportCommand {
14+
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15+
return Commands.literal("export")
16+
.requires(stack -> stack.getSender() instanceof Player)
17+
.executes(context -> exportSettings(context, plugin));
18+
}
19+
20+
private static int exportSettings(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
21+
var player = (Player) context.getSource().getSender();
22+
23+
var mainHand = player.getInventory().getItemInMainHand();
24+
var settings = plugin.brushController().getBrushSettings(player);
25+
26+
plugin.bundle().sendMessage(player, settings.exportSettings(mainHand) ?
27+
"command.gopaint.export.success" : "command.gopaint.export.failed");
28+
29+
return Command.SINGLE_SUCCESS;
30+
}
31+
}
Lines changed: 11 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,25 @@
11
package net.thenextlvl.gopaint.command;
22

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;
64
import io.papermc.paper.command.brigadier.CommandSourceStack;
75
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;
126
import net.thenextlvl.gopaint.GoPaintPlugin;
13-
import net.thenextlvl.gopaint.api.brush.PatternBrush;
147
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
15-
import org.bukkit.entity.Player;
16-
import org.bukkit.inventory.ItemStack;
178
import org.jspecify.annotations.NullMarked;
189

19-
import java.util.List;
20-
2110
@NullMarked
2211
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")
3114
.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))
6623
.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;
18524
}
18625
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.thenextlvl.gopaint.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import io.papermc.paper.command.brigadier.CommandSourceStack;
7+
import io.papermc.paper.command.brigadier.Commands;
8+
import net.thenextlvl.gopaint.GoPaintPlugin;
9+
import org.bukkit.entity.Player;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
@NullMarked
13+
class ImportCommand {
14+
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15+
return Commands.literal("import")
16+
.requires(stack -> stack.getSender() instanceof Player)
17+
.executes(context -> importSettings(context, plugin));
18+
}
19+
20+
private static int importSettings(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
21+
var player = (Player) context.getSource().getSender();
22+
23+
var mainHand = player.getInventory().getItemInMainHand();
24+
var settings = plugin.brushController().getBrushSettings(player);
25+
var parsed = plugin.brushController().parseBrushSettings(mainHand);
26+
27+
parsed.ifPresent(settings::importSettings);
28+
29+
plugin.bundle().sendMessage(player, parsed.isPresent() ?
30+
"command.gopaint.import.success" : "command.gopaint.import.failed");
31+
32+
return Command.SINGLE_SUCCESS;
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.thenextlvl.gopaint.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import io.papermc.paper.command.brigadier.CommandSourceStack;
7+
import io.papermc.paper.command.brigadier.Commands;
8+
import net.thenextlvl.gopaint.GoPaintPlugin;
9+
import org.bukkit.entity.Player;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
@NullMarked
13+
class MenuCommand {
14+
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15+
return Commands.literal("menu")
16+
.requires(stack -> stack.getSender() instanceof Player)
17+
.executes(context -> menu(context, plugin));
18+
}
19+
20+
private static int menu(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
21+
var player = (Player) context.getSource().getSender();
22+
var settings = plugin.brushController().getBrushSettings(player);
23+
settings.getMainMenu().open();
24+
return Command.SINGLE_SUCCESS;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.thenextlvl.gopaint.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import io.papermc.paper.command.brigadier.CommandSourceStack;
7+
import io.papermc.paper.command.brigadier.Commands;
8+
import net.thenextlvl.gopaint.GoPaintPlugin;
9+
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
@NullMarked
13+
class ReloadCommand {
14+
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15+
return Commands.literal("reload")
16+
.requires(stack -> stack.getSender().hasPermission(GoPaintProvider.ADMIN_PERMISSION))
17+
.executes(context -> reload(context, plugin));
18+
}
19+
20+
private static int reload(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
21+
var sender = context.getSource().getSender();
22+
plugin.reloadConfig();
23+
plugin.bundle().sendMessage(sender, "command.gopaint.reloaded");
24+
return Command.SINGLE_SUCCESS;
25+
}
26+
}

0 commit comments

Comments
 (0)