Skip to content

Commit 7d2cb28

Browse files
committed
Refactored command classes to extend SimpleCommand
1 parent 89e145d commit 7d2cb28

12 files changed

Lines changed: 164 additions & 65 deletions
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
@@ -9,21 +8,27 @@
98
import net.kyori.adventure.key.Key;
109
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
1110
import net.thenextlvl.gopaint.GoPaintPlugin;
11+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
1212
import net.thenextlvl.gopaint.commands.suggestion.BrushSuggestionProvider;
1313
import org.bukkit.entity.Player;
1414
import org.jspecify.annotations.NullMarked;
1515

1616
@NullMarked
17-
class BrushCommand {
17+
final class BrushCommand extends SimpleCommand {
18+
private BrushCommand(GoPaintPlugin plugin) {
19+
super(plugin, "brush", null);
20+
}
21+
1822
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)));
23+
var command = new BrushCommand(plugin);
24+
return command.create().then(Commands.argument("brush", ArgumentTypes.key())
25+
.suggests(new BrushSuggestionProvider<>(plugin))
26+
.requires(stack -> stack.getSender() instanceof Player)
27+
.executes(command));
2428
}
2529

26-
private static int brush(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
30+
@Override
31+
public int run(CommandContext<CommandSourceStack> context) {
2732
var player = (Player) context.getSource().getSender();
2833
var settings = plugin.brushController().getBrushSettings(player);
2934
var argument = context.getArgument("brush", Key.class);
@@ -33,6 +38,6 @@ private static int brush(CommandContext<CommandSourceStack> context, GoPaintPlug
3338
settings.setBrush(brush);
3439
}, () -> plugin.bundle().sendMessage(player, "brush.unknown",
3540
Placeholder.parsed("input", argument.asString())));
36-
return Command.SINGLE_SUCCESS;
41+
return SINGLE_SUCCESS;
3742
}
3843
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
86
import net.thenextlvl.gopaint.GoPaintPlugin;
7+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
98
import org.bukkit.entity.Player;
109
import org.jspecify.annotations.NullMarked;
1110

1211
@NullMarked
13-
class ExportCommand {
12+
final class ExportCommand extends SimpleCommand {
13+
private ExportCommand(GoPaintPlugin plugin) {
14+
super(plugin, "export", null);
15+
}
16+
1417
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15-
return Commands.literal("export")
18+
var command = new ExportCommand(plugin);
19+
return command.create()
1620
.requires(stack -> stack.getSender() instanceof Player)
17-
.executes(context -> exportSettings(context, plugin));
21+
.executes(command);
1822
}
1923

20-
private static int exportSettings(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
24+
@Override
25+
public int run(CommandContext<CommandSourceStack> context) {
2126
var player = (Player) context.getSource().getSender();
2227

2328
var mainHand = player.getInventory().getItemInMainHand();
@@ -26,6 +31,6 @@ private static int exportSettings(CommandContext<CommandSourceStack> context, Go
2631
plugin.bundle().sendMessage(player, settings.exportSettings(mainHand) ?
2732
"command.gopaint.export.success" : "command.gopaint.export.failed");
2833

29-
return Command.SINGLE_SUCCESS;
34+
return SINGLE_SUCCESS;
3035
}
3136
}

src/main/java/net/thenextlvl/gopaint/commands/GoPaintCommand.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import com.mojang.brigadier.tree.LiteralCommandNode;
44
import io.papermc.paper.command.brigadier.CommandSourceStack;
5-
import io.papermc.paper.command.brigadier.Commands;
65
import net.thenextlvl.gopaint.GoPaintPlugin;
76
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
7+
import net.thenextlvl.gopaint.commands.brigadier.BrigadierCommand;
88
import org.jspecify.annotations.NullMarked;
99

1010
@NullMarked
11-
public class GoPaintCommand {
11+
public final class GoPaintCommand extends BrigadierCommand {
12+
private GoPaintCommand(GoPaintPlugin plugin) {
13+
super(plugin, "gopaint", GoPaintProvider.USE_PERMISSION);
14+
}
15+
1216
public static LiteralCommandNode<CommandSourceStack> create(GoPaintPlugin plugin) {
13-
return Commands.literal("gopaint")
14-
.requires(stack -> stack.getSender().hasPermission(GoPaintProvider.USE_PERMISSION))
17+
return new GoPaintCommand(plugin).create()
1518
.then(BrushCommand.create(plugin))
1619
.then(ExportCommand.create(plugin))
1720
.then(ImportCommand.create(plugin))
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
86
import net.thenextlvl.gopaint.GoPaintPlugin;
7+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
98
import org.bukkit.entity.Player;
109
import org.jspecify.annotations.NullMarked;
1110

1211
@NullMarked
13-
class ImportCommand {
12+
final class ImportCommand extends SimpleCommand {
13+
private ImportCommand(GoPaintPlugin plugin) {
14+
super(plugin, "import", null);
15+
}
16+
1417
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15-
return Commands.literal("import")
18+
var command = new ImportCommand(plugin);
19+
return command.create()
1620
.requires(stack -> stack.getSender() instanceof Player)
17-
.executes(context -> importSettings(context, plugin));
21+
.executes(command);
1822
}
1923

20-
private static int importSettings(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
24+
@Override
25+
public int run(CommandContext<CommandSourceStack> context) {
2126
var player = (Player) context.getSource().getSender();
2227

2328
var mainHand = player.getInventory().getItemInMainHand();
@@ -29,6 +34,6 @@ private static int importSettings(CommandContext<CommandSourceStack> context, Go
2934
plugin.bundle().sendMessage(player, parsed.isPresent() ?
3035
"command.gopaint.import.success" : "command.gopaint.import.failed");
3136

32-
return Command.SINGLE_SUCCESS;
37+
return SINGLE_SUCCESS;
3338
}
3439
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
86
import net.thenextlvl.gopaint.GoPaintPlugin;
7+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
98
import org.bukkit.entity.Player;
109
import org.jspecify.annotations.NullMarked;
1110

1211
@NullMarked
13-
class MenuCommand {
12+
final class MenuCommand extends SimpleCommand {
13+
private MenuCommand(GoPaintPlugin plugin) {
14+
super(plugin, "menu", null);
15+
}
16+
1417
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15-
return Commands.literal("menu")
18+
var command = new MenuCommand(plugin);
19+
return command.create()
1620
.requires(stack -> stack.getSender() instanceof Player)
17-
.executes(context -> menu(context, plugin));
21+
.executes(command);
1822
}
1923

20-
private static int menu(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
24+
@Override
25+
public int run(CommandContext<CommandSourceStack> context) {
2126
var player = (Player) context.getSource().getSender();
2227
var settings = plugin.brushController().getBrushSettings(player);
2328
settings.getMainMenu().open();
24-
return Command.SINGLE_SUCCESS;
29+
return SINGLE_SUCCESS;
2530
}
2631
}

src/main/java/net/thenextlvl/gopaint/commands/ReloadCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
55
import com.mojang.brigadier.context.CommandContext;
66
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
87
import net.thenextlvl.gopaint.GoPaintPlugin;
98
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
9+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
1010
import org.jspecify.annotations.NullMarked;
1111

1212
@NullMarked
13-
class ReloadCommand {
13+
final class ReloadCommand extends SimpleCommand {
14+
private ReloadCommand(GoPaintPlugin plugin) {
15+
super(plugin, "reload", GoPaintProvider.ADMIN_PERMISSION);
16+
}
17+
1418
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));
19+
var command = new ReloadCommand(plugin);
20+
return command.create().executes(command);
1821
}
1922

20-
private static int reload(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
23+
@Override
24+
public int run(CommandContext<CommandSourceStack> context) {
2125
var sender = context.getSource().getSender();
2226
plugin.reloadConfig();
2327
plugin.bundle().sendMessage(sender, "command.gopaint.reloaded");
24-
return Command.SINGLE_SUCCESS;
28+
return SINGLE_SUCCESS;
2529
}
2630
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.arguments.IntegerArgumentType;
54
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
65
import com.mojang.brigadier.context.CommandContext;
76
import io.papermc.paper.command.brigadier.CommandSourceStack;
87
import io.papermc.paper.command.brigadier.Commands;
98
import net.kyori.adventure.text.minimessage.tag.resolver.Formatter;
109
import net.thenextlvl.gopaint.GoPaintPlugin;
10+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
1111
import org.bukkit.entity.Player;
1212
import org.jspecify.annotations.NullMarked;
1313

1414
@NullMarked
15-
class SizeCommand {
15+
final class SizeCommand extends SimpleCommand {
16+
private SizeCommand(GoPaintPlugin plugin) {
17+
super(plugin, "size", null);
18+
}
19+
1620
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
17-
return Commands.literal("size")
21+
var command = new SizeCommand(plugin);
22+
return command.create()
1823
.requires(stack -> stack.getSender() instanceof Player)
19-
.then(Commands.argument("size", IntegerArgumentType.integer(1, 100))
20-
.executes(context -> size(context, plugin)));
24+
.then(Commands.argument("size", IntegerArgumentType.integer(1, 100)).executes(command));
2125
}
2226

23-
private static int size(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
27+
@Override
28+
public int run(CommandContext<CommandSourceStack> context) {
2429
var player = (Player) context.getSource().getSender();
2530
var settings = plugin.brushController().getBrushSettings(player);
2631
settings.setBrushSize(context.getArgument("size", int.class));
2732
plugin.bundle().sendMessage(player, "command.gopaint.brush.size",
2833
Formatter.number("size", settings.getBrushSize()));
29-
return Command.SINGLE_SUCCESS;
34+
return SINGLE_SUCCESS;
3035
}
3136
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
86
import net.thenextlvl.gopaint.GoPaintPlugin;
7+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
98
import org.bukkit.entity.Player;
109
import org.jspecify.annotations.NullMarked;
1110

1211
@NullMarked
13-
class ToggleCommand {
12+
final class ToggleCommand extends SimpleCommand {
13+
private ToggleCommand(GoPaintPlugin plugin) {
14+
super(plugin, "toggle", null);
15+
}
16+
1417
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
15-
return Commands.literal("toggle")
18+
var command = new ToggleCommand(plugin);
19+
return command.create()
1620
.requires(stack -> stack.getSender() instanceof Player)
17-
.executes(context -> toggle(context, plugin));
21+
.executes(command);
1822
}
1923

20-
private static int toggle(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
24+
@Override
25+
public int run(CommandContext<CommandSourceStack> context) {
2126
var player = (Player) context.getSource().getSender();
2227
var settings = plugin.brushController().getBrushSettings(player);
2328
settings.setEnabled(!settings.isEnabled());
2429
var message = settings.isEnabled() ? "command.gopaint.brush.enabled"
2530
: "command.gopaint.brush.disabled";
2631
plugin.bundle().sendMessage(player, message);
27-
return Command.SINGLE_SUCCESS;
32+
return SINGLE_SUCCESS;
2833
}
2934
}

src/main/java/net/thenextlvl/gopaint/commands/WandCommand.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package net.thenextlvl.gopaint.commands;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
54
import com.mojang.brigadier.context.CommandContext;
65
import io.papermc.paper.command.brigadier.CommandSourceStack;
7-
import io.papermc.paper.command.brigadier.Commands;
86
import net.thenextlvl.gopaint.GoPaintPlugin;
7+
import net.thenextlvl.gopaint.commands.brigadier.SimpleCommand;
98
import org.bukkit.entity.Player;
109
import org.bukkit.inventory.ItemStack;
1110
import org.jspecify.annotations.NullMarked;
1211

1312
@NullMarked
14-
class WandCommand {
13+
final class WandCommand extends SimpleCommand {
14+
private WandCommand(GoPaintPlugin plugin) {
15+
super(plugin, "wand", null);
16+
}
17+
1518
public static LiteralArgumentBuilder<CommandSourceStack> create(GoPaintPlugin plugin) {
16-
return Commands.literal("wand")
19+
var command = new WandCommand(plugin);
20+
return command.create()
1721
.requires(stack -> stack.getSender() instanceof Player)
18-
.executes(context -> wand(context, plugin));
22+
.executes(command);
1923
}
2024

21-
private static int wand(CommandContext<CommandSourceStack> context, GoPaintPlugin plugin) {
25+
@Override
26+
public int run(CommandContext<CommandSourceStack> context) {
2227
var player = (Player) context.getSource().getSender();
23-
plugin.bundle().sendMessage(player, giveWand(player, plugin)
28+
plugin.bundle().sendMessage(player, giveWand(player)
2429
? "command.gopaint.wand.success"
2530
: "command.gopaint.wand.failed");
26-
return Command.SINGLE_SUCCESS;
31+
return SINGLE_SUCCESS;
2732
}
2833

29-
private static boolean giveWand(Player player, GoPaintPlugin plugin) {
34+
private boolean giveWand(Player player) {
3035
var type = plugin.config().brushConfig().defaultBrushType();
3136

3237
var inventory = player.getInventory();

0 commit comments

Comments
 (0)