Skip to content

Commit 23572c4

Browse files
committed
Refactor CommandScopes
Command scopes have been changed from arbitrary strings to proper enum instances. The new CommandScope enum class contains the following values: - PLAYER (Player) - CONSOLE (ConsoleCommandSender) - BLOCK (Command Block) - ANY (any CommandSender)
1 parent 91ec614 commit 23572c4

11 files changed

Lines changed: 62 additions & 30 deletions

File tree

bukkit/src/main/java/cl/bgm/bukkit/util/BukkitCommandsManager.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cl.bgm.bukkit.util;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
34
import cl.bgm.minecraft.util.commands.CommandsManager;
45
import org.bukkit.command.BlockCommandSender;
56
import org.bukkit.command.CommandSender;
@@ -14,13 +15,15 @@ public boolean hasPermission(CommandSender player, String perm) {
1415
}
1516

1617
@Override
17-
public boolean scopeMatches(CommandSender player, String scope) {
18-
switch (scope.toLowerCase()) {
19-
case "player":
18+
public boolean scopeMatches(CommandSender player, CommandScope scope) {
19+
switch (scope) {
20+
case ANY:
21+
return true;
22+
case PLAYER:
2023
return player instanceof Player;
21-
case "console":
24+
case CONSOLE:
2225
return player instanceof ConsoleCommandSender;
23-
case "block":
26+
case BLOCK:
2427
return player instanceof BlockCommandSender;
2528
default:
2629
return false;

bungee/src/main/java/cl/bgm/bungee/util/BungeeCommandsManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cl.bgm.bungee.util;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
34
import cl.bgm.minecraft.util.commands.CommandsManager;
45
import net.md_5.bungee.api.CommandSender;
56

@@ -12,11 +13,13 @@ public boolean hasPermission(CommandSender player, String perm) {
1213
}
1314

1415
@Override
15-
public boolean scopeMatches(CommandSender player, String scope) {
16-
switch (scope.toLowerCase()) {
17-
case "player":
16+
public boolean scopeMatches(CommandSender player, CommandScope scope) {
17+
switch (scope) {
18+
case ANY:
19+
return true;
20+
case PLAYER:
1821
return player instanceof ProxiedPlayer;
19-
case "console":
22+
case CONSOLE:
2023
return !(player instanceof ProxiedPlayer);
2124
default:
2225
return false;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cl.bgm.minecraft.util.commands;
2+
3+
public enum CommandScope {
4+
PLAYER,
5+
CONSOLE,
6+
BLOCK,
7+
8+
ANY,
9+
}

core/src/main/java/cl/bgm/minecraft/util/commands/CommandsManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ protected boolean scopeMatches(Method method, T player) {
659659
return true;
660660
}
661661

662-
for (String scope : scopes.value()) {
662+
for (CommandScope scope : scopes.value()) {
663663
if (scopeMatches(player, scope)) {
664664
return true;
665665
}
@@ -684,7 +684,7 @@ protected boolean scopeMatches(Method method, T player) {
684684
* @param scope the permission
685685
* @return true if the scope matches
686686
*/
687-
public abstract boolean scopeMatches(T player, String scope);
687+
public abstract boolean scopeMatches(T player, CommandScope scope);
688688

689689
/**
690690
* Get the injector used to create new instances. This can be

core/src/main/java/cl/bgm/minecraft/util/commands/annotations/CommandScopes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cl.bgm.minecraft.util.commands.annotations;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
4+
35
import java.lang.annotation.Retention;
46
import java.lang.annotation.RetentionPolicy;
57

@@ -13,5 +15,5 @@
1315
* The command's target scopes, which by default are all possible senders
1416
* @return them
1517
*/
16-
String[] value() default {"console", "player", "block"};
18+
CommandScope[] value() default { CommandScope.ANY };
1719
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package cl.bgm.minecraft.util.commands.exceptions;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
4+
35
/**
46
* Thrown when the command scope doesn't match the sender
57
*/
68
public class ScopeMismatchException extends CommandException {
7-
String[] scopes;
9+
CommandScope[] scopes;
810

9-
public ScopeMismatchException(String message, String[] scopes) {
11+
public ScopeMismatchException(String message, CommandScope[] scopes) {
1012
super(message);
1113
this.scopes = scopes;
1214
}
1315

14-
public String[] getScopes() {
16+
public CommandScope[] getScopes() {
1517
return scopes;
1618
}
1719
}

core/src/main/java/cl/bgm/minecraft/util/commands/wrapped/WrappedCommandsManager.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package cl.bgm.minecraft.util.commands.wrapped;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
34
import cl.bgm.minecraft.util.commands.CommandsManager;
45

6+
import static cl.bgm.minecraft.util.commands.CommandScope.BLOCK;
7+
import static cl.bgm.minecraft.util.commands.CommandScope.CONSOLE;
8+
import static cl.bgm.minecraft.util.commands.CommandScope.PLAYER;
9+
510
public class WrappedCommandsManager extends CommandsManager<WrappedCommandSender> {
611
@Override
712
public boolean hasPermission(WrappedCommandSender player, String perm) {
813
return player.hasPermission(perm);
914
}
1015

1116
@Override
12-
public boolean scopeMatches(WrappedCommandSender player, String scope) {
13-
switch (scope.toLowerCase()) {
14-
case "player":
17+
public boolean scopeMatches(WrappedCommandSender player, CommandScope scope) {
18+
switch (scope) {
19+
case ANY:
20+
return true;
21+
case PLAYER:
1522
return player.getType() == WrappedCommandSender.Type.PLAYER;
16-
case "console":
23+
case CONSOLE:
1724
return player.getType() == WrappedCommandSender.Type.CONSOLE;
18-
case "block":
25+
case BLOCK:
1926
return player.getType() == WrappedCommandSender.Type.BLOCK;
2027
default:
2128
return false; // When unknown

example-commands-bukkit/src/main/java/cl/bgm/example/commands/bukkit/ExampleCommandsBukkit.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import cl.bgm.bukkit.util.CommandsManagerRegistration;
55
import cl.bgm.example.commands.bukkit.commands.ExampleNestedCommand;
66
import cl.bgm.example.commands.bukkit.commands.ExampleCommand;
7+
import cl.bgm.minecraft.util.commands.CommandScope;
8+
import cl.bgm.minecraft.util.commands.annotations.CommandScopes;
79
import cl.bgm.minecraft.util.commands.exceptions.CommandException;
810
import cl.bgm.minecraft.util.commands.exceptions.CommandPermissionsException;
911
import cl.bgm.minecraft.util.commands.exceptions.CommandUsageException;
@@ -31,11 +33,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
3133
try {
3234
this.commandsManager.execute(command.getName(), args, sender, sender);
3335
} catch (ScopeMismatchException exception) {
34-
String[] scopes = exception.getScopes();
35-
if (!Arrays.asList(scopes).contains("player")) {
36-
sender.sendMessage("You must use the console to execute this command.");
37-
} else {
38-
sender.sendMessage("You must be a player to execute this command.");
36+
CommandScope[] scopes = exception.getScopes();
37+
if (!Arrays.asList(scopes).contains(CommandScope.PLAYER)) {
38+
sender.sendMessage("A player cannot execute this command.");
39+
} else if (!Arrays.asList(scopes).contains(CommandScope.CONSOLE)) {
40+
sender.sendMessage("The console cannot execute this command.");
41+
} else if (!Arrays.asList(scopes).contains(CommandScope.BLOCK)) {
42+
sender.sendMessage("A command block cannot execute this command.");
3943
}
4044
} catch (CommandPermissionsException exception) {
4145
sender.sendMessage("You do not have permission.");

example-commands-bukkit/src/main/java/cl/bgm/example/commands/bukkit/commands/ExampleCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cl.bgm.example.commands.bukkit.commands;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
34
import cl.bgm.minecraft.util.commands.annotations.Command;
45
import cl.bgm.minecraft.util.commands.CommandContext;
56
import cl.bgm.minecraft.util.commands.annotations.CommandPermissions;
@@ -20,7 +21,7 @@ public class ExampleCommand {
2021
max = 1
2122
)
2223
@CommandPermissions("example.command")
23-
@CommandScopes({"player"})
24+
@CommandScopes(CommandScope.PLAYER)
2425
public static void example(final CommandContext args, final CommandSender sender) {
2526
if (args.argsLength() == 1) {
2627
sender.sendMessage("I'm a " + args.getString(0));

example-commands-bukkit/src/main/java/cl/bgm/example/commands/bukkit/commands/ExampleNestedCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cl.bgm.example.commands.bukkit.commands;
22

3+
import cl.bgm.minecraft.util.commands.CommandScope;
34
import cl.bgm.minecraft.util.commands.annotations.Command;
45
import cl.bgm.minecraft.util.commands.CommandContext;
56
import cl.bgm.minecraft.util.commands.annotations.CommandPermissions;
@@ -22,7 +23,7 @@ public class ExampleNestedCommand {
2223
max = 1
2324
)
2425
@CommandPermissions("hello.command.me")
25-
@CommandScopes({"player"})
26+
@CommandScopes(CommandScope.PLAYER)
2627
public static void me(final CommandContext args, final CommandSender sender) {
2728
if (args.argsLength() == 0) {
2829
sender.sendMessage("hello me");
@@ -38,7 +39,7 @@ public static void me(final CommandContext args, final CommandSender sender) {
3839
max = 1
3940
)
4041
@CommandPermissions("hello.command.you")
41-
@CommandScopes({"player"})
42+
@CommandScopes(CommandScope.PLAYER)
4243
public static void you(final CommandContext args, final CommandSender sender) {
4344
if (args.argsLength() == 0) {
4445
sender.sendMessage("hello you");
@@ -54,7 +55,7 @@ public static class ExampleNestedCommandNode {
5455
desc = "Hello node command."
5556
)
5657
@CommandPermissions("hello.command")
57-
@CommandScopes({"player"})
58+
@CommandScopes(CommandScope.PLAYER)
5859
@NestedCommand(value = ExampleNestedCommand.class, executeBody = true)
5960
public static void hello(final CommandContext args, final CommandSender sender) {
6061
sender.sendMessage("I'm the method body.");

0 commit comments

Comments
 (0)