Skip to content

Commit

Permalink
added localization support for permission commands + smol fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 21, 2022
1 parent afd916d commit 07f628b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package fr.xpdustry.distributor.core;

import arc.util.CommandHandler;
import cloud.commandframework.arguments.standard.StringArgument;
import fr.xpdustry.distributor.api.Distributor;
import fr.xpdustry.distributor.api.DistributorProvider;
import fr.xpdustry.distributor.api.command.ArcCommandManager;
Expand Down Expand Up @@ -120,7 +121,7 @@ public void onClientCommandsRegistration(final CommandHandler handler) {
}

private void onSharedCommandsRegistration(final ArcCommandManager<CommandSender> manager) {
// TODO Find a better solution than using 2 annotations parsers
// TODO This is ugly, transition to a functional permission command system

{
final var parser = manager.createAnnotationParser(CommandSender.class);
Expand All @@ -132,6 +133,20 @@ private void onSharedCommandsRegistration(final ArcCommandManager<CommandSender>
final var parser = manager.createAnnotationParser(CommandSender.class);
parser.stringProcessor(input -> input.replace("permissible", "group"));
parser.parse(new GroupPermissibleCommand(this.permissions));

manager.command(manager.commandBuilder("permission")
.literal("create-group")
.argument(StringArgument.of("group"))
.handler(ctx -> {
final var groups = this.permissions.getGroupPermissionManager();
final String group = ctx.get("group");
if (groups.existsById(group)) {
ctx.getSender().sendLocalizedWarning("permission.group.create.already", group);
} else {
groups.save(groups.findOrCreateById(group));
ctx.getSender().sendLocalizedMessage("permission.group.create.success", group);
}
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
public final class GroupPermissibleCommand extends PermissibleCommand<GroupPermissible> {

public GroupPermissibleCommand(final PermissionService service) {
super(service, "group");
super(service);
}

@CommandPermission("distributor.permission.group.weight.info")
@CommandMethod("<group> weight info")
public void getGroupPermissibleWeight(
final CommandSender sender,
final @Argument(value = "group", parserName = "group-parser") GroupPermissible permissible) {
sender.sendMessage("The group " + permissible.getName() + " has a weight of " + permissible.getWeight());
sender.sendLocalizedMessage("permission.group.weight.get", permissible.getName(), permissible.getWeight());
}

@CommandPermission("distributor.permission.group.weight.edit")
Expand All @@ -50,23 +50,11 @@ public void setGroupPermissibleWeight(
final @Argument(value = "group", parserName = "group-parser") GroupPermissible permissible,
final @Argument("weight") @Range(min = "0") int weight) {
if (permissible.getWeight() == weight) {
sender.sendMessage(permissible.getName() + " already have a weight of " + weight);
sender.sendLocalizedMessage("permission.group.weight.set.already", permissible.getName(), weight);
} else {
permissible.setWeight(weight);
this.getManager().save(permissible);
sender.sendMessage("The weight of " + permissible.getName() + " has been set to " + weight);
}
}

// FIXME For some reason, putting this method as the first produce a class cast exception...
@CommandPermission("distributor.permission.group.create")
@CommandMethod("<group> create")
public void createGroupPermissible(final CommandSender sender, final @Argument(value = "group") String group) {
if (this.getManager().existsById(group)) {
sender.sendMessage("The group " + group + " already exists.");
} else {
this.getManager().save(this.getManager().findOrCreateById(group));
sender.sendMessage("The group " + group + " have been created.");
sender.sendLocalizedMessage("permission.group.weight.set.success", permissible.getName(), weight);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
@CommandDescription("Permission management commands.")
public abstract class PermissibleCommand<P extends Permissible> {

private final String category;
private final PermissionService permissions;

public PermissibleCommand(final PermissionService permissions, final String category) {
this.category = category;
public PermissibleCommand(final PermissionService permissions) {
this.permissions = permissions;
}

Expand All @@ -58,7 +56,7 @@ public void getPermissibleInfo(
final @Argument(value = "permissible", parserName = "permissible-parser") P permissible) {
final var permissions = new TreeMap<>(permissible.getPermissions());
if (permissions.isEmpty()) {
sender.sendMessage(permissible.getName() + " have no set permissions.");
sender.sendLocalizedMessage("permission.permissible.permission.list.none", permissible.getName());
} else {
final var builder = new StringBuilder();
final var iterator = permissions.entrySet().iterator();
Expand Down Expand Up @@ -95,12 +93,13 @@ public void setPermissiblePermission(
public void setPermissiblePermission(
final CommandSender sender, final P permissible, final String permission, final Tristate state) {
if (permissible.getPermission(permission) == state) {
sender.sendWarning("The permission is already set to the given state.");
sender.sendLocalizedWarning(
"permission.permissible.permission.set.already", permission, permissible.getName(), state);
} else {
permissible.setPermission(permission, state);
this.getManager().save(permissible);
sender.sendMessage(
"The permission " + permission + " of " + permissible.getName() + " has been set to " + state);
sender.sendLocalizedMessage(
"permission.permissible.permission.set.success", permission, permissible.getName(), state);
}
}

Expand All @@ -110,7 +109,7 @@ public void getPermissibleParentsInfo(
final CommandSender sender,
final @Argument(value = "permissible", parserName = "permissible-parser") P permissible) {
if (permissible.getParentGroups().isEmpty()) {
sender.sendMessage("The player " + permissible.getName() + " has no parent groups.");
sender.sendLocalizedMessage("permission.permissible.parent.list.none", permissible.getName());
} else {
final var builder = new StringBuilder();
final var groups = permissible.getParentGroups();
Expand All @@ -131,11 +130,11 @@ public void addPermissibleParent(
final @Argument(value = "permissible", parserName = "permissible-parser") P permissible,
final @Argument("parent") String parent) {
if (permissible.getParentGroups().contains(parent)) {
sender.sendWarning("The " + this.category + " is already in the group " + parent);
sender.sendLocalizedWarning("permission.permissible.parent.add.already", permissible.getName(), parent);
} else {
permissible.addParent(parent);
this.getManager().save(permissible);
sender.sendMessage("The " + this.category + " has been added to the group " + parent);
sender.sendLocalizedMessage("permission.permissible.parent.add.success", permissible.getName(), parent);
}
}

Expand All @@ -148,9 +147,9 @@ public void removePermissibleParent(
if (permissible.getParentGroups().contains(parent)) {
permissible.removeParent(parent);
this.getManager().save(permissible);
sender.sendMessage("The " + this.category + " has been removed from the group " + parent);
sender.sendLocalizedMessage("permission.permissible.parent.remove.success", permissible.getName(), parent);
} else {
sender.sendWarning("The " + this.category + " is not in the group " + parent);
sender.sendLocalizedWarning("permission.permissible.parent.remove.already", permissible.getName(), parent);
}
}

Expand All @@ -160,10 +159,10 @@ public void deletePermissionPermissions(
final CommandSender sender,
final @Argument(value = "permissible", parserName = "permissible-parser") P permissible) {
if (this.getManager().exists(permissible)) {
sender.sendMessage("No permission data is attached to this " + this.category + ".");
} else {
this.getManager().delete(permissible);
sender.sendMessage("All permission data of " + permissible.getName() + " have been deleted.");
sender.sendLocalizedMessage("permission.permissible.delete.success", permissible.getName());
} else {
sender.sendLocalizedWarning("permission.permissible.delete.already", permissible.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public final class PlayerPermissibleCommand extends PermissibleCommand<PlayerPermissible> {

public PlayerPermissibleCommand(final PermissionService service) {
super(service, "player");
super(service);
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions distributor-core/src/main/resources/bundles/bundle_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ command.invalid.syntax=Invalid input, the correct syntax is ''{0}''.
command.invalid.permission=You do not have the required permissions to perform this action.
command.failure.no_such_command=The command ''{0}'' does not exists.
command.failure.execution=An unexpected problem occurred while executing the command.

permission.permissible.permission.list.none=''{0}'' have no set permissions.
permission.permissible.permission.set.already=The permission ''{0}'' of ''{1}'' is already set to ''{2}''.
permission.permissible.permission.set.success=The permission ''{0}'' of ''{1}'' has been set to ''{2}''.
permission.permissible.parent.list.none=''{0}'' has no parent groups.
permission.permissible.parent.add.already=''{0}'' is already in the group ''{1}''.
permission.permissible.parent.add.success=''{0}'' has been added to the group ''{1}''.
permission.permissible.parent.remove.already=''{0}'' is not in the group ''{1}''.
permission.permissible.parent.remove.success=''{0}'' has been removed from the group ''{1}''.
permission.permissible.delete.already=There is no permission data attached to ''{0}''.
permission.permissible.delete.success=All permission data of ''{0}'' has been deleted.
permission.group.create.already=The group ''{0}'' already exists.
permission.group.create.success=The group ''{0}'' has been created.
permission.group.weight.get=The weight of ''{0}'' is {1}.
permission.group.weight.set.already=The weight of ''{0}'' is already set to {1}.
permission.group.weight.set.success=The weight of ''{0}'' has been set to ''{1}''.

0 comments on commit 07f628b

Please sign in to comment.