mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-18 09:54:59 +01:00
Custom Permission Handler (#1282)
Co-authored-by: Melledy <52122272+Melledy@users.noreply.github.com>
This commit is contained in:
@@ -14,6 +14,7 @@ public final class CommandMap {
|
||||
private final Map<String, Command> annotations = new HashMap<>();
|
||||
private final Map<String, Integer> targetPlayerIds = new HashMap<>();
|
||||
private static final String consoleId = "console";
|
||||
|
||||
public CommandMap() {
|
||||
this(false);
|
||||
}
|
||||
@@ -202,21 +203,9 @@ public final class CommandMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Check for permission.
|
||||
if (player != null) {
|
||||
String permissionNode = this.annotations.get(label).permission();
|
||||
String permissionNodeTargeted = this.annotations.get(label).permissionTargeted();
|
||||
Account account = player.getAccount();
|
||||
if (player != targetPlayer) { // Additional permission required for targeting another player
|
||||
if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) {
|
||||
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
|
||||
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
|
||||
return;
|
||||
}
|
||||
// Check for permissions.
|
||||
if (!Grasscutter.getPermissionHandler().checkPermission(player, targetPlayer, this.annotations.get(label).permission(), this.annotations.get(label).permissionTargeted())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if command has unfulfilled constraints on targetPlayer
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package emu.grasscutter.command;
|
||||
|
||||
import emu.grasscutter.game.Account;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
|
||||
public class DefaultPermissionHandler implements PermissionHandler {
|
||||
@Override
|
||||
public boolean EnablePermissionCommand() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted) {
|
||||
if(player == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Account account = player.getAccount();
|
||||
if (player != targetPlayer) { // Additional permission required for targeting another player
|
||||
if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) {
|
||||
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
|
||||
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package emu.grasscutter.command;
|
||||
|
||||
import emu.grasscutter.game.player.Player;
|
||||
|
||||
public interface PermissionHandler {
|
||||
public boolean EnablePermissionCommand();
|
||||
public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted);
|
||||
}
|
||||
@@ -20,6 +20,11 @@ public final class PermissionCommand implements CommandHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!Grasscutter.getPermissionHandler().EnablePermissionCommand()) {
|
||||
CommandHandler.sendTranslatedMessage(sender, "commands.generic.permission_error");
|
||||
return;
|
||||
}
|
||||
|
||||
String action = args.get(0);
|
||||
String permission = args.get(1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user