Skip to content

Commit 8d2dc4c

Browse files
authored
Merge pull request #81 from TheNextLvl-net/command-refactor
Fix brush export bug and refactor commands
2 parents db7f557 + a9f0f8b commit 8d2dc4c

File tree

14 files changed

+331
-186
lines changed

14 files changed

+331
-186
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gameVersions=1.21.3,1.21.4
1+
gameVersions=1.21.4

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

Lines changed: 8 additions & 7 deletions
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;
@@ -25,7 +26,6 @@
2526
import net.thenextlvl.gopaint.version.VersionChecker;
2627
import org.bstats.bukkit.Metrics;
2728
import org.bukkit.Axis;
28-
import org.bukkit.Bukkit;
2929
import org.bukkit.Material;
3030
import org.bukkit.entity.Player;
3131
import org.bukkit.plugin.ServicePriority;
@@ -95,18 +95,19 @@ public void reloadConfig() {
9595
}
9696

9797
private void registerServices() {
98-
Bukkit.getServicesManager().register(BrushController.class, brushController(), this, ServicePriority.Highest);
99-
Bukkit.getServicesManager().register(BrushRegistry.class, brushRegistry(), this, ServicePriority.Highest);
98+
getServer().getServicesManager().register(BrushController.class, brushController(), this, ServicePriority.Highest);
99+
getServer().getServicesManager().register(BrushRegistry.class, brushRegistry(), this, ServicePriority.Highest);
100100
}
101101

102102
private void registerListeners() {
103-
Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
104-
Bukkit.getPluginManager().registerEvents(new InteractListener(this), this);
105-
Bukkit.getPluginManager().registerEvents(new ConnectListener(this), this);
103+
getServer().getPluginManager().registerEvents(new InventoryListener(this), this);
104+
getServer().getPluginManager().registerEvents(new InteractListener(this), this);
105+
getServer().getPluginManager().registerEvents(new ConnectListener(this), this);
106106
}
107107

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

112113
public PluginConfig config() {

src/main/java/net/thenextlvl/gopaint/brush/setting/CraftPlayerBrushSettings.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public PatternBrush getPreviousBrush(@Nullable PatternBrush brush) {
246246
@Override
247247
public boolean exportSettings(ItemStack itemStack) {
248248
if (itemStack.getType().equals(plugin.config().brushConfig().defaultBrushType())) return false;
249+
if (itemStack.getType().isBlock()) return false;
249250

250251
var lines = new ArrayList<Component>();
251252
lines.add(Component.empty());
@@ -302,13 +303,11 @@ public boolean exportSettings(ItemStack itemStack) {
302303
Placeholder.component("brush", getBrush().getName(player))));
303304
itemStack.setData(DataComponentTypes.LORE, ItemLore.lore(lines));
304305

305-
return !itemStack.getType().isBlock() && itemStack.editMeta(itemMeta -> {
306-
var container = itemMeta.getPersistentDataContainer();
307-
306+
return itemStack.editPersistentDataContainer(container -> {
308307
container.set(new NamespacedKey("gopaint", "size"), PersistentDataType.INTEGER, getBrushSize());
309308
container.set(new NamespacedKey("gopaint", "chance"), PersistentDataType.INTEGER, getChance());
310309
container.set(new NamespacedKey("gopaint", "thickness"), PersistentDataType.INTEGER, getThickness());
311-
container.set(new NamespacedKey("gopaint", "fracture_strength"), PersistentDataType.INTEGER, this.getFractureStrength());
310+
container.set(new NamespacedKey("gopaint", "fracture_strength"), PersistentDataType.INTEGER, getFractureStrength());
312311
container.set(new NamespacedKey("gopaint", "angle_distance"), PersistentDataType.INTEGER, getAngleDistance());
313312
container.set(new NamespacedKey("gopaint", "falloff_strength"), PersistentDataType.INTEGER, getFalloffStrength());
314313
container.set(new NamespacedKey("gopaint", "mixing_strength"), PersistentDataType.INTEGER, getMixingStrength());
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+
}

0 commit comments

Comments
 (0)