Skip to content

Commit e19ed05

Browse files
committed
java 21, switch to papermc's command api
1 parent b4eda8d commit e19ed05

File tree

5 files changed

+140
-264
lines changed

5 files changed

+140
-264
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<groupId>gg.quartzdev</groupId>
88
<artifactId>qlib-paper</artifactId>
9-
<version>1.0.0-beta4</version>
9+
<version>1.0.0-beta5</version>
1010
<packaging>jar</packaging>
1111

1212
<name>qlib-paper</name>
1313

1414
<properties>
15-
<java.version>17</java.version>
15+
<java.version>21</java.version>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
</properties>
1818

@@ -90,7 +90,7 @@
9090
<dependency>
9191
<groupId>io.papermc.paper</groupId>
9292
<artifactId>paper-api</artifactId>
93-
<version>1.21.1-R0.1-SNAPSHOT</version>
93+
<version>1.21.3-R0.1-SNAPSHOT</version>
9494
<scope>provided</scope>
9595
</dependency>
9696
<dependency>
Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,47 @@
11
package gg.quartzdev.mc.lib.qlibpaper.commands;
22

3-
import gg.quartzdev.mc.lib.qlibpaper.Sender;
4-
import gg.quartzdev.mc.lib.qlibpaper.lang.GenericMessages;
5-
import gg.quartzdev.mc.lib.qlibpaper.lang.QPlaceholder;
6-
import org.bukkit.Bukkit;
7-
import org.bukkit.command.Command;
8-
import org.bukkit.command.CommandSender;
9-
import org.bukkit.util.StringUtil;
10-
import org.jetbrains.annotations.NotNull;
3+
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
4+
import org.bukkit.plugin.java.JavaPlugin;
5+
import org.jetbrains.annotations.Nullable;
116

12-
import java.util.ArrayList;
13-
import java.util.Collections;
7+
import java.util.Collection;
148
import java.util.HashMap;
15-
import java.util.HashSet;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Set;
199

20-
public class CommandManager extends Command
10+
public class CommandManager
2111
{
22-
HashMap<String, QCommand> subCommands;
2312

24-
public CommandManager(String name, @NotNull List<String> aliases)
25-
{
26-
super(name);
27-
super.setAliases(aliases);
28-
subCommands = new HashMap<>();
29-
Bukkit.getCommandMap().register(name, this);
30-
}
31-
32-
public void addSubCommand(String label, QCommand subCommand)
33-
{
34-
subCommands.put(label, subCommand);
35-
}
13+
private final HashMap<String, QCMD> commands = new HashMap<>();
14+
private final JavaPlugin plugin;
3615

37-
@Override
38-
public boolean execute(@NotNull CommandSender sender, @NotNull String labelOrAlias, @NotNull String[] args)
39-
{
40-
// Send base command
41-
if (args.length == 0)
42-
{
43-
return subCommands.get("").run(sender, labelOrAlias, args);
44-
}
4516

46-
// Get subcommand from args
47-
QCommand cmd = subCommands.get(args[0]);
48-
49-
if (cmd == null)
50-
{
51-
Sender.message(sender, GenericMessages.CMD_NOT_FOUND.parse(QPlaceholder.COMMAND, args[0]));
52-
return false;
53-
}
54-
55-
// Run the command
56-
return cmd.run(sender, labelOrAlias, args);
17+
public CommandManager(JavaPlugin plugin) {
18+
this.plugin = plugin;
5719
}
5820

59-
@Override
60-
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String labelOrAlias, String[] args) throws IllegalArgumentException
61-
{
62-
List<String> completions = new ArrayList<>();
21+
/**
22+
* Adds a command along with its aliases. After adding all commands call {@link #registerCommands()} to register them.
23+
* @param command the command to add
24+
* @param aliases the aliases for the command
25+
*/
26+
public void add(QCMD command, @Nullable Collection<String> aliases) {
27+
if(aliases != null) command.aliases(aliases);
28+
commands.put(command.label(), command);
29+
}
6330

64-
// Only tab complete a sub command if the player has permission
65-
if (args.length == 1)
66-
{
67-
Set<String> allowedSubCommands = new HashSet<>();
68-
for (Map.Entry<String, QCommand> entry : subCommands.entrySet())
69-
{
70-
String commandName = entry.getKey();
71-
QCommand cmd = entry.getValue();
72-
if (cmd.hasPermission(sender))
31+
@SuppressWarnings("UnstableApiUsage")
32+
public void registerCommands(){
33+
plugin.getLifecycleManager().registerEventHandler(
34+
LifecycleEvents.COMMANDS,
35+
event ->
7336
{
74-
allowedSubCommands.add(commandName);
37+
for(QCMD command : commands.values()){
38+
event.registrar().register(command.label(), command.description(), command.aliases(), command);
39+
}
7540
}
76-
}
77-
StringUtil.copyPartialMatches(args[0], allowedSubCommands, completions);
78-
}
79-
80-
// Let the subcommand handle tab completion
81-
if (args.length > 1)
82-
{
83-
QCommand cmd = subCommands.get(args[0]);
84-
85-
if (cmd == null)
86-
{
87-
return completions;
88-
}
89-
90-
Iterable<String> rawCompletions = cmd.getTabCompletions(sender, args);
91-
if (rawCompletions != null)
92-
{
93-
StringUtil.copyPartialMatches(args[args.length - 1], rawCompletions, completions);
94-
}
95-
}
96-
97-
Collections.sort(completions);
98-
return completions;
41+
);
9942
}
10043

101-
}
102-
44+
public void unregister(QCMD command) {
45+
commands.remove(command.label());
46+
}
47+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package gg.quartzdev.mc.lib.qlibpaper.commands;
2+
3+
import io.papermc.paper.command.brigadier.BasicCommand;
4+
import io.papermc.paper.command.brigadier.CommandSourceStack;
5+
import org.bukkit.command.CommandSender;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
12+
@SuppressWarnings({"unused", "UnstableApiUsage"})
13+
public abstract class QCMD implements BasicCommand
14+
{
15+
/*
16+
the command label
17+
ie: /<label>
18+
*/
19+
private final String label;
20+
21+
/*
22+
bukkit command description
23+
*/
24+
private final String description;
25+
26+
/*
27+
whether a player needs to have the permission to use the command
28+
a player with the permission set to false will still not be able to use the command regardless of this value
29+
*/
30+
private boolean requiresPermission = true;
31+
32+
/*
33+
permission node to use the command
34+
*/
35+
private final String permission;
36+
37+
/*
38+
alternative permission node to use the command thats typically shared by many commands
39+
*/
40+
private final String permissionPackage;
41+
42+
/*
43+
alias labels for the command
44+
*/
45+
private Collection<String> aliases;
46+
47+
public QCMD(@NotNull String label, @Nullable String description, @NotNull String permission, @Nullable String permissionPackage, boolean requiresPermission){
48+
this.label = label;
49+
this.description = description;
50+
this.permission = permission;
51+
this.permissionPackage = permissionPackage == null ? permission : permissionPackage;
52+
this.requiresPermission = requiresPermission;
53+
this.aliases = new ArrayList<>();
54+
}
55+
56+
@Override
57+
public abstract void execute(@NotNull CommandSourceStack commandSourceStack, @NotNull String[] args);
58+
59+
@Override
60+
public abstract @NotNull Collection<String> suggest(@NotNull CommandSourceStack commandSourceStack, @NotNull String[] args);
61+
62+
@Override
63+
public boolean canUse(@NotNull CommandSender sender)
64+
{
65+
// if we don't require permission, we still want to prevent the sender if their permission is set to false
66+
if(!requiresPermission)
67+
return !sender.isPermissionSet(permission()) || sender.hasPermission(permission());
68+
69+
// if the player has the permission or the permission package, they can use the command
70+
return sender.hasPermission(permission()) || sender.hasPermission(permissionPackage());
71+
}
72+
73+
@Override
74+
public @NotNull String permission()
75+
{
76+
return permission;
77+
}
78+
79+
public @NotNull String permissionPackage()
80+
{
81+
return permissionPackage;
82+
}
83+
84+
public @NotNull String label()
85+
{
86+
return label;
87+
}
88+
89+
public @Nullable String description()
90+
{
91+
return description;
92+
}
93+
94+
public @NotNull Collection<String> aliases(){
95+
return aliases;
96+
}
97+
98+
public void aliases(@NotNull Collection<String> aliases){
99+
this.aliases = aliases;
100+
}
101+
102+
public void requiresPermission(boolean requiresPermission)
103+
{
104+
this.requiresPermission = requiresPermission;
105+
}
106+
}

src/main/java/gg/quartzdev/mc/lib/qlibpaper/commands/QCommand.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)