diff --git a/bukkit/src/main/java/me/lucko/luckperms/CommandManagerBukkit.java b/bukkit/src/main/java/me/lucko/luckperms/CommandManagerBukkit.java index 26e56379..592889a7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/CommandManagerBukkit.java +++ b/bukkit/src/main/java/me/lucko/luckperms/CommandManagerBukkit.java @@ -6,6 +6,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import java.lang.ref.WeakReference; import java.util.Arrays; class CommandManagerBukkit extends CommandManager implements CommandExecutor { @@ -15,15 +16,20 @@ class CommandManagerBukkit extends CommandManager implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + final WeakReference cs = new WeakReference<>(sender); return onCommand(new Sender() { @Override public void sendMessage(String s) { - sender.sendMessage(s); + CommandSender c = cs.get(); + if (c != null) { + c.sendMessage(s); + } } @Override public boolean hasPermission(String node) { - return sender.hasPermission(node); + CommandSender c = cs.get(); + return c != null && c.hasPermission(node); } }, Arrays.asList(args)); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index 3dfc7f45..ff523f88 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -123,8 +123,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { @Override public String getPlayerStatus(UUID uuid) { - if (getServer().getPlayer(uuid) != null) return "&aOnline"; - return "&cOffline"; + return getServer().getPlayer(uuid) != null ? "&aOnline" : "&cOffline"; } @Override diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java index 3369205c..3a8c2ab7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java @@ -40,15 +40,11 @@ public class BukkitUser extends User { } // Clear existing permissions - for (String p : attachment.getPermissions().keySet()) { - attachment.setPermission(p, false); - } + attachment.getPermissions().keySet().stream().forEach(p -> attachment.setPermission(p, false)); // Re-add all defined permissions for the user Map local = getLocalPermissions(getPlugin().getConfiguration().getServer(), null); - for (String node : local.keySet()) { - attachment.setPermission(node, local.get(node)); - } + local.entrySet().stream().forEach(e -> attachment.setPermission(e.getKey(), e.getValue())); }); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java index c6574051..9b06cab9 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java @@ -54,8 +54,6 @@ public class BukkitUserManager extends UserManager { @Override public void updateAllUsers() { - for (Player p : plugin.getServer().getOnlinePlayers()) { - plugin.getDatastore().loadUser(p.getUniqueId()); - } + plugin.getServer().getOnlinePlayers().stream().map(Player::getUniqueId).forEach(u -> plugin.getDatastore().loadUser(u)); } } diff --git a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java index 1a088c1e..8272595d 100644 --- a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java @@ -77,8 +77,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { @Override public String getPlayerStatus(UUID uuid) { - if (getProxy().getPlayer(uuid) != null) return "&aOnline"; - return "&cOffline"; + return getProxy().getPlayer(uuid) != null ? "&aOnline" : "&cOffline"; } @Override diff --git a/bungee/src/main/java/me/lucko/luckperms/MainCommand.java b/bungee/src/main/java/me/lucko/luckperms/MainCommand.java index c54c0d03..f27720d1 100644 --- a/bungee/src/main/java/me/lucko/luckperms/MainCommand.java +++ b/bungee/src/main/java/me/lucko/luckperms/MainCommand.java @@ -6,6 +6,7 @@ import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.plugin.Command; +import java.lang.ref.WeakReference; import java.util.Arrays; class MainCommand extends Command { @@ -19,15 +20,20 @@ class MainCommand extends Command { @Override public void execute(CommandSender sender, String[] args) { + final WeakReference cs = new WeakReference<>(sender); manager.onCommand(new Sender() { @Override public void sendMessage(String s) { - sender.sendMessage(new TextComponent(s)); + final CommandSender c = cs.get(); + if (c != null) { + c.sendMessage(new TextComponent(s)); + } } @Override public boolean hasPermission(String node) { - return sender.hasPermission(node); + final CommandSender c = cs.get(); + return c != null && c.hasPermission(node); } }, Arrays.asList(args)); } diff --git a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUser.java b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUser.java index d2b7d589..b5569a75 100644 --- a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUser.java +++ b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUser.java @@ -29,14 +29,10 @@ public class BungeeUser extends User { // Clear existing permissions Collection perms = new ArrayList<>(player.getPermissions()); - for (String p : perms) { - player.setPermission(p, false); - } + perms.stream().forEach(p -> player.setPermission(p, false)); // Re-add all defined permissions for the user Map local = getLocalPermissions(getPlugin().getConfiguration().getServer(), null); - for (String node : local.keySet()) { - player.setPermission(node, local.get(node)); - } + local.entrySet().stream().forEach(e -> player.setPermission(e.getKey(), e.getValue())); } } diff --git a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java index 4bdf5657..94226c04 100644 --- a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java +++ b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java @@ -41,8 +41,6 @@ public class BungeeUserManager extends UserManager { @Override public void updateAllUsers() { - for (ProxiedPlayer p : plugin.getProxy().getPlayers()) { - plugin.getDatastore().loadUser(p.getUniqueId()); - } + plugin.getProxy().getPlayers().stream().map(ProxiedPlayer::getUniqueId).forEach(u -> plugin.getDatastore().loadUser(u)); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java index c034af28..c7853cd7 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java @@ -15,6 +15,7 @@ import me.lucko.luckperms.commands.user.subcommands.*; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.logging.Level; public class CommandManager { @@ -66,40 +67,34 @@ public class CommandManager { */ public boolean onCommand(Sender sender, List args) { if (args.size() == 0) { - Util.sendPluginMessage(sender, "&6Running &bLuckPerms " + plugin.getVersion() + "&6."); - - mainCommands.stream() - .filter(c -> c.canUse(sender)) - .forEach(c -> Util.sendPluginMessage(sender, "&e-> &d" + c.getUsage())); - - } else { - String c = args.get(0); - MainCommand main = null; - - for (MainCommand mainCommand : mainCommands) { - if (mainCommand.getName().equalsIgnoreCase(c)) { - main = mainCommand; - break; - } - } - - if (main == null) { - Util.sendPluginMessage(sender, "Command not recognised."); - return true; - } - - if (main.getRequiredArgsLength() == 0) { - main.execute(plugin, sender, null); - return true; - } - - if (args.size() == 1) { - main.sendUsage(sender); - return true; - } - - main.execute(plugin, sender, new ArrayList<>(args.subList(1, args.size()))); + sendCommandUsage(sender); + return true; } + + Optional o = mainCommands.stream().filter(m -> m.getName().equalsIgnoreCase(args.get(0))).limit(1).findAny(); + + if (!o.isPresent()) { + sendCommandUsage(sender); + return true; + } + + final MainCommand main = o.get(); + if (!main.canUse(sender)) { + sendCommandUsage(sender); + return true; + } + + if (main.getRequiredArgsLength() == 0) { + main.execute(plugin, sender, null); + return true; + } + + if (args.size() == 1) { + main.sendUsage(sender); + return true; + } + + main.execute(plugin, sender, new ArrayList<>(args.subList(1, args.size()))); return true; } @@ -109,4 +104,11 @@ public class CommandManager { mainCommands.add(command); } + private void sendCommandUsage(Sender sender) { + Util.sendPluginMessage(sender, "&6Running &bLuckPerms " + plugin.getVersion() + "&6."); + + mainCommands.stream() + .filter(c -> c.canUse(sender)) + .forEach(c -> Util.sendPluginMessage(sender, "&e-> &d" + c.getUsage())); + } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/MainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/MainCommand.java index a34ecfa1..480c04ae 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/MainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/MainCommand.java @@ -1,32 +1,29 @@ package me.lucko.luckperms.commands; +import lombok.AllArgsConstructor; import lombok.Getter; import me.lucko.luckperms.LuckPermsPlugin; import java.util.List; import java.util.stream.Collectors; +@Getter +@AllArgsConstructor public abstract class MainCommand { - @Getter private final String name; - - @Getter private final String usage; - - @Getter private final int requiredArgsLength; - protected MainCommand(String name, String usage, int requiredArgsLength) { - this.name = name; - this.usage = usage; - this.requiredArgsLength = requiredArgsLength; - } - protected abstract void execute(LuckPermsPlugin plugin, Sender sender, List args); public abstract List getSubCommands(); protected void sendUsage(Sender sender) { + if (getSubCommands().isEmpty()) { + Util.sendPluginMessage(sender, "&e-> &d" + getUsage()); + return; + } + List subs = getSubCommands().stream().filter(s -> s.isAuthorized(sender)).collect(Collectors.toList()); if (subs.size() > 0) { Util.sendPluginMessage(sender, "&e" + getName() + " Sub Commands:"); @@ -41,11 +38,6 @@ public abstract class MainCommand { } protected boolean canUse(Sender sender) { - for (SubCommand sc : getSubCommands()) { - if (sc.isAuthorized(sender)) { - return true; - } - } - return false; + return getSubCommands().stream().filter(sc -> sc.isAuthorized(sender)).count() != 0; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/Permission.java b/common/src/main/java/me/lucko/luckperms/commands/Permission.java new file mode 100644 index 00000000..b72e3830 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/Permission.java @@ -0,0 +1,65 @@ +package me.lucko.luckperms.commands; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +public enum Permission { + + SYNC("sync", null), + INFO("info", null), + DEBUG("debug", null), + + CREATE_GROUP("creategroup", null), + DELETE_GROUP("deletegroup", null), + LIST_GROUPS("listgroups", null), + + USER_INFO("info", PermissionGroup.USER), + USER_GETUUID("getuuid", PermissionGroup.USER), + USER_LISTNODES("listnodes", PermissionGroup.USER), + USER_HASPERMISSION("haspermission", PermissionGroup.USER), + USER_SETPERMISSION("setpermission", PermissionGroup.USER), + USER_UNSETPERMISSION("unsetpermission", PermissionGroup.USER), + USER_ADDGROUP("addgroup", PermissionGroup.USER), + USER_REMOVEGROUP("removegroup", PermissionGroup.USER), + USER_SETPRIMARYGROUP("setprimarygroup", PermissionGroup.USER), + USER_CLEAR("clear", PermissionGroup.USER), + + GROUP_INFO("info", PermissionGroup.GROUP), + GROUP_LISTNODES("listnodes", PermissionGroup.GROUP), + GROUP_HASPERMISSION("haspermission", PermissionGroup.GROUP), + GROUP_SETPERMISSION("setpermission", PermissionGroup.GROUP), + GROUP_UNSETPERMISSION("unsetpermission", PermissionGroup.GROUP), + GROUP_SETINHERIT("setinherit", PermissionGroup.GROUP), + GROUP_UNSETINHERIT("unsetinherit", PermissionGroup.GROUP), + GROUP_CLEAR("clear", PermissionGroup.GROUP); + + private String node; + private PermissionGroup group; + + public boolean isAuthorized(Sender sender) { + if (sender.hasPermission("luckperms.*")) { + return true; + } + + if (group != null) { + return group.isAuthorized(sender) || sender.hasPermission("luckperms." + group.getNode() + "." + node); + } + + return sender.hasPermission("luckperms." + node); + } + + @Getter + @AllArgsConstructor + private enum PermissionGroup { + USER("user"), + GROUP("group"); + + private String node; + + private boolean isAuthorized(Sender sender) { + return sender.hasPermission("luckperms." + node + ".*"); + } + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/commands/SubCommand.java b/common/src/main/java/me/lucko/luckperms/commands/SubCommand.java index b1e77ed1..62bfe0d5 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/SubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/SubCommand.java @@ -1,30 +1,18 @@ package me.lucko.luckperms.commands; +import lombok.AllArgsConstructor; import lombok.Getter; +@Getter +@AllArgsConstructor public abstract class SubCommand { - - @Getter private final String name; - - @Getter private final String description; - - @Getter private final String usage; - - @Getter - private final String permission; - - protected SubCommand(String name, String description, String usage, String permission) { - this.name = name; - this.description = description; - this.usage = usage; - this.permission = permission; - } + private final Permission permission; public boolean isAuthorized(Sender sender) { - return sender.hasPermission(permission) || sender.hasPermission("luckperms.*"); + return permission.isAuthorized(sender); } public void sendUsage(Sender sender) { diff --git a/common/src/main/java/me/lucko/luckperms/commands/Util.java b/common/src/main/java/me/lucko/luckperms/commands/Util.java index 7289b588..80525b58 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/Util.java +++ b/common/src/main/java/me/lucko/luckperms/commands/Util.java @@ -20,8 +20,8 @@ public class Util { // Stolen from Bukkit :> char[] b = textToTranslate.toCharArray(); - for(int i = 0; i < b.length - 1; ++i) { - if(b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) { + for (int i = 0; i < b.length - 1; ++i) { + if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) { b[i] = 167; b[i + 1] = Character.toLowerCase(b[i + 1]); } @@ -42,11 +42,7 @@ public class Util { if (strings.isEmpty()) return "&6None"; StringBuilder sb = new StringBuilder(); - - for (String s : strings) { - sb.append("&6").append(s).append("&7, "); - } - + strings.stream().forEach(s -> sb.append("&6").append(s).append("&7, ")); return sb.delete(sb.length() - 2, sb.length()).toString(); } @@ -55,11 +51,11 @@ public class Util { StringBuilder sb = new StringBuilder(); - for (String node : nodes.keySet()) { - if (nodes.get(node)) { - sb.append("&a").append(node).append("&7, "); + for (Map.Entry e : nodes.entrySet()) { + if (e.getValue()) { + sb.append("&a").append(e.getKey()).append("&7, "); } else { - sb.append("&c").append(node).append("&7, "); + sb.append("&c").append(e.getKey()).append("&7, "); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/CreateGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/CreateGroupCommand.java index acdedbf1..2fe8fbaa 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/CreateGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/CreateGroupCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.group; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import java.util.ArrayList; import java.util.List; @@ -16,18 +13,12 @@ public class CreateGroupCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.creategroup")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - if (args.size() == 0) { sendUsage(sender); return; } String groupName = args.get(0).toLowerCase(); - plugin.getDatastore().loadGroup(groupName, success -> { if (success) { Util.sendPluginMessage(sender, "That group already exists!"); @@ -51,6 +42,6 @@ public class CreateGroupCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.creategroup") || sender.hasPermission("luckperms.*"); + return Permission.CREATE_GROUP.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/DeleteGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/DeleteGroupCommand.java index 7c98e5a9..7d33fab3 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/DeleteGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/DeleteGroupCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.group; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import me.lucko.luckperms.groups.Group; import java.util.ArrayList; @@ -17,11 +14,6 @@ public class DeleteGroupCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.deletegroup")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - if (args.size() == 0) { sendUsage(sender); return; @@ -41,7 +33,7 @@ public class DeleteGroupCommand extends MainCommand { Group group = plugin.getGroupManager().getGroup(groupName); if (group == null) { - Util.sendPluginMessage(sender, "An unexpected error occurred."); + Util.sendPluginMessage(sender, "An unexpected error occurred. Group not loaded."); } else { plugin.getDatastore().deleteGroup(group, success1 -> { if (!success1) { @@ -63,6 +55,6 @@ public class DeleteGroupCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.deletegroup") || sender.hasPermission("luckperms.*"); + return Permission.DELETE_GROUP.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java index 0b146161..3f893d02 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java @@ -9,7 +9,7 @@ import me.lucko.luckperms.groups.Group; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; +import java.util.Optional; public class GroupMainCommand extends MainCommand { @@ -21,48 +21,37 @@ public class GroupMainCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (args.size() <= 1) { + if (args.size() < 2) { sendUsage(sender); return; } + Optional o = subCommands.stream().filter(s -> s.getName().equalsIgnoreCase(args.get(1))).limit(1).findAny(); + + if (!o.isPresent()) { + Util.sendPluginMessage(sender, "Command not recognised."); + return; + } + + final GroupSubCommand sub = o.get(); + if (!sub.isAuthorized(sender)) { + Util.sendPluginMessage(sender, "You do not have permission to use this command!"); + return; + } + List strippedArgs = new ArrayList<>(); if (args.size() > 2) { strippedArgs.addAll(args.subList(2, args.size())); } - String c = args.get(1); - GroupSubCommand tempSub = null; - - for (GroupSubCommand s : subCommands) { - if (s.getName().equalsIgnoreCase(c)) { - tempSub = s; - break; - } - } - - final GroupSubCommand sub = tempSub; - - if (sub == null) { - Util.sendPluginMessage(sender, "Command not recognised."); - return; - } - - - if (!sub.isAuthorized(sender)) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - - String g = args.get(0).toLowerCase(); - - plugin.getDatastore().loadGroup(g, success -> { + final String groupName = args.get(0).toLowerCase(); + plugin.getDatastore().loadGroup(groupName, success -> { if (!success) { Util.sendPluginMessage(sender, "&eGroup could not be found."); return; } - Group group = plugin.getGroupManager().getGroup(g); + Group group = plugin.getGroupManager().getGroup(groupName); if (group == null) { Util.sendPluginMessage(sender, "&eGroup could not be found."); return; @@ -86,18 +75,4 @@ public class GroupMainCommand extends MainCommand { subCommands.add(subCommand); } - @Override - protected void sendUsage(Sender sender) { - List subs = getSubCommands().stream().filter(s -> s.isAuthorized(sender)).collect(Collectors.toList()); - if (subs.size() > 0) { - Util.sendPluginMessage(sender, "&e" + getName() + " Sub Commands:"); - - for (SubCommand s : subs) { - s.sendUsage(sender); - } - - } else { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - } - } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/GroupSubCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/GroupSubCommand.java index 60583513..21d736e1 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/GroupSubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/GroupSubCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.commands.Util; @@ -9,17 +10,12 @@ import me.lucko.luckperms.groups.Group; import java.util.List; public abstract class GroupSubCommand extends SubCommand { - protected GroupSubCommand(String name, String description, String usage, String permission) { + protected GroupSubCommand(String name, String description, String usage, Permission permission) { super(name, description, usage, permission); } protected abstract void execute(LuckPermsPlugin plugin, Sender sender, Group group, List args); - @Override - public boolean isAuthorized(Sender sender) { - return sender.hasPermission(getPermission()) || sender.hasPermission("luckperms.group.*") || sender.hasPermission("luckperms.*"); - } - protected void saveGroup(Group group, Sender sender, LuckPermsPlugin plugin) { plugin.getDatastore().saveGroup(group, success -> { if (success) { diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/ListGroupsCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/ListGroupsCommand.java index d25dafb5..0fe0a3ff 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/ListGroupsCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/ListGroupsCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.group; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import java.util.ArrayList; import java.util.List; @@ -16,16 +13,12 @@ public class ListGroupsCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.listgroups")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - plugin.getDatastore().loadAllGroups(success -> { if (!success) { Util.sendPluginMessage(sender, "Unable to load all groups."); } else { - Util.sendPluginMessage(sender, "&aGroups: " + Util.listToCommaSep(new ArrayList<>(plugin.getGroupManager().getGroups().keySet()))); + Util.sendPluginMessage(sender, "&aGroups: " + + Util.listToCommaSep(new ArrayList<>(plugin.getGroupManager().getGroups().keySet()))); } }); } @@ -37,6 +30,6 @@ public class ListGroupsCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.listgroups") || sender.hasPermission("luckperms.*"); + return Permission.LIST_GROUPS.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupClearCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupClearCommand.java index 1bb30b49..d04d0aae 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupClearCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupClearCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -10,8 +11,7 @@ import java.util.List; public class GroupClearCommand extends GroupSubCommand { public GroupClearCommand() { - super("clear", "Clears a groups permissions", - "/perms group clear", "luckperms.group.clear"); + super("clear", "Clears a groups permissions", "/perms group clear", Permission.GROUP_CLEAR); } @Override @@ -24,6 +24,6 @@ public class GroupClearCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return true; + return false; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupHasPermCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupHasPermCommand.java index 886bfd21..25debbff 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupHasPermCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupHasPermCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -11,13 +12,13 @@ import java.util.List; public class GroupHasPermCommand extends GroupSubCommand { public GroupHasPermCommand() { super("haspermission", "Checks to see if a group has a certain permission node", - "/perms group haspermission [server]", "luckperms.group.haspermission"); + "/perms group haspermission [server]", Permission.GROUP_HASPERMISSION); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, Group group, List args) { - if (args.size() >= 2) { - Util.sendBoolean(sender, args.get(0), group.hasPermission(args.get(0), true, args.get(1))); + if (args.size() == 2) { + Util.sendBoolean(sender, args.get(0), group.hasPermission(args.get(0), true, args.get(1).toLowerCase())); } else { Util.sendBoolean(sender, args.get(0), group.hasPermission(args.get(0), true, "global")); } @@ -25,6 +26,6 @@ public class GroupHasPermCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupInfoCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupInfoCommand.java index 0c40c558..11e1b644 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupInfoCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupInfoCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -10,18 +11,16 @@ import java.util.List; public class GroupInfoCommand extends GroupSubCommand { public GroupInfoCommand() { - super("info", "Gives info about the group", - "/perms group info", "luckperms.group.info"); + super("info", "Gives info about the group", "/perms group info", Permission.GROUP_INFO); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, Group group, List args) { - final String prefix = Util.PREFIX; - String sb = prefix + "&d-> &eGroup: &6" + group.getName() + "\n" + - prefix + "&d-> &ePermissions: &6" + group.getNodes().keySet().size() + "\n" + - prefix + "&d-> &bUse &a/perms group " + group.getName() + " listnodes &bto see all permissions."; - - sender.sendMessage(Util.color(sb)); + sender.sendMessage(Util.color( + Util.PREFIX + "&d-> &eGroup: &6" + group.getName() + "\n" + + Util.PREFIX + "&d-> &ePermissions: &6" + group.getNodes().keySet().size() + "\n" + + Util.PREFIX + "&d-> &bUse &a/perms group " + group.getName() + " listnodes &bto see all permissions." + )); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupListNodesCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupListNodesCommand.java index 05519cc1..177a1128 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupListNodesCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupListNodesCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -10,8 +11,8 @@ import java.util.List; public class GroupListNodesCommand extends GroupSubCommand { public GroupListNodesCommand() { - super("listnodes", "Lists the permission nodes the group has", - "/perms group listnodes", "luckperms.group.listnodes"); + super("listnodes", "Lists the permission nodes the group has", "/perms group listnodes", + Permission.GROUP_LISTNODES); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetInheritCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetInheritCommand.java index f0c6643a..d0499624 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetInheritCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetInheritCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -12,7 +13,7 @@ import java.util.List; public class GroupSetInheritCommand extends GroupSubCommand { public GroupSetInheritCommand() { super("setinherit", "Sets another group for this group to inherit permissions from", - "/perms group setinherit [server]", "luckperms.group.setinherit"); + "/perms group setinherit [server]", Permission.GROUP_SETINHERIT); } @Override @@ -23,16 +24,16 @@ public class GroupSetInheritCommand extends GroupSubCommand { if (!success) { Util.sendPluginMessage(sender, groupName + " does not exist!"); } else { - final String node = "group." + groupName; - try { if (args.size() == 2) { final String server = args.get(1).toLowerCase(); - group.setPermission(node, true, server); - Util.sendPluginMessage(sender, "&b" + group.getName() + "&a now inherits permissions from &b" + groupName + "&a on server &b" + server + "&a."); + group.setPermission("group." + groupName, true, server); + Util.sendPluginMessage(sender, "&b" + group.getName() + "&a now inherits permissions from &b" + + groupName + "&a on server &b" + server + "&a."); } else { - group.setPermission(node, true); - Util.sendPluginMessage(sender, "&b" + group.getName() + "&a now inherits permissions from &b" + groupName + "&a."); + group.setPermission("group." + groupName, true); + Util.sendPluginMessage(sender, "&b" + group.getName() + "&a now inherits permissions from &b" + + groupName + "&a."); } saveGroup(group, sender, plugin); @@ -45,6 +46,6 @@ public class GroupSetInheritCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermissionCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermissionCommand.java index ab3064d1..f8284d68 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermissionCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermissionCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -11,8 +12,8 @@ import java.util.List; public class GroupSetPermissionCommand extends GroupSubCommand { public GroupSetPermissionCommand() { - super("set", "Sets a permission for a group", - "/perms group set [server]", "luckperms.group.setpermission"); + super("set", "Sets a permission for a group", "/perms group set [server]", + Permission.GROUP_SETPERMISSION); } @Override @@ -41,7 +42,8 @@ public class GroupSetPermissionCommand extends GroupSubCommand { if (args.size() == 3) { final String server = args.get(2).toLowerCase(); group.setPermission(node, b, server); - Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + bool + "&a for &b" + group.getName() + "&a on server &b" + server + "&a."); + Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + bool + "&a for &b" + group.getName() + + "&a on server &b" + server + "&a."); } else { group.setPermission(node, b); Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to " + bool + " for &b" + group.getName() + "&a."); @@ -55,6 +57,6 @@ public class GroupSetPermissionCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength < 2; + return argLength != 2 && argLength != 3; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermissionCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermissionCommand.java index 7e20474b..0f24f71c 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermissionCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermissionCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -12,7 +13,7 @@ import java.util.List; public class GroupUnSetPermissionCommand extends GroupSubCommand { public GroupUnSetPermissionCommand() { super("unset", "Unsets a permission for a group", - "/perms group unset [server]", "luckperms.group.unsetpermission"); + "/perms group unset [server]", Permission.GROUP_UNSETPERMISSION); } @Override @@ -33,7 +34,8 @@ public class GroupUnSetPermissionCommand extends GroupSubCommand { if (args.size() == 2) { final String server = args.get(1).toLowerCase(); group.unsetPermission(node, server); - Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + group.getName() + "&a on server &b" + server + "&a."); + Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + group.getName() + "&a on server &b" + + server + "&a."); } else { group.unsetPermission(node); Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + group.getName() + "&a."); @@ -47,6 +49,6 @@ public class GroupUnSetPermissionCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetInheritCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetInheritCommand.java index b1bfd6b4..00644e3c 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetInheritCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetInheritCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.group.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.group.GroupSubCommand; @@ -12,23 +13,25 @@ import java.util.List; public class GroupUnsetInheritCommand extends GroupSubCommand { public GroupUnsetInheritCommand() { super("unsetinherit", "Unsets another group for this group to inherit permissions from", - "/perms group unsetinherit [server]", "luckperms.group.unsetinherit"); + "/perms group unsetinherit [server]", Permission.GROUP_UNSETINHERIT); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, Group group, List args) { String groupName = args.get(0).toLowerCase(); - String server; - if (args.size() == 2) { - server = args.get(1).toLowerCase(); - } else { - server = "global"; - } - try { - group.unsetPermission("group." + groupName, server); - Util.sendPluginMessage(sender, "&b" + group.getName() + "&a no longer inherits permissions from &b" + groupName + "&a on server &b" + server + "&a."); + if (args.size() == 2) { + final String server = args.get(1).toLowerCase(); + group.unsetPermission("group." + groupName, server); + Util.sendPluginMessage(sender, "&b" + group.getName() + "&a no longer inherits permissions from &b" + + groupName + "&a on server &b" + server + "&a."); + } else { + group.unsetPermission("group." + groupName); + Util.sendPluginMessage(sender, "&b" + group.getName() + "&a no longer inherits permissions from &b" + + groupName + "&a."); + } + saveGroup(group, sender, plugin); } catch (ObjectLacksPermissionException e) { Util.sendPluginMessage(sender, "That group does not inherit '" + groupName + "'."); @@ -37,6 +40,6 @@ public class GroupUnsetInheritCommand extends GroupSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/misc/DebugCommand.java b/common/src/main/java/me/lucko/luckperms/commands/misc/DebugCommand.java index bab6fc34..c89867df 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/misc/DebugCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/misc/DebugCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.misc; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import java.util.ArrayList; import java.util.List; @@ -16,11 +13,6 @@ public class DebugCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.debug")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - Util.sendPluginMessage(sender, "&d&l> &dDebug Info"); Util.sendPluginMessage(sender, "&eOnline Players: &6" + plugin.getPlayerCount()); Util.sendPluginMessage(sender, "&eLoaded Users: &6" + plugin.getUserManager().getUsers().size()); @@ -34,6 +26,6 @@ public class DebugCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.debug") || sender.hasPermission("luckperms.*"); + return Permission.DEBUG.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/misc/InfoCommand.java b/common/src/main/java/me/lucko/luckperms/commands/misc/InfoCommand.java index 8d8dd8d5..b572512e 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/misc/InfoCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/misc/InfoCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.misc; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import java.util.ArrayList; import java.util.List; @@ -16,11 +13,6 @@ public class InfoCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.info")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - Util.sendPluginMessage(sender, "&6Running &bLuckPerms " + plugin.getVersion() + "&6."); Util.sendPluginMessage(sender, "&eAuthor: &6Luck"); Util.sendPluginMessage(sender, "&eStorage Method: &6" + plugin.getDatastore().getName()); @@ -33,6 +25,6 @@ public class InfoCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.info") || sender.hasPermission("luckperms.*"); + return Permission.INFO.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/misc/SyncCommand.java b/common/src/main/java/me/lucko/luckperms/commands/misc/SyncCommand.java index 98364d41..2b0264bd 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/misc/SyncCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/misc/SyncCommand.java @@ -1,10 +1,7 @@ package me.lucko.luckperms.commands.misc; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.commands.MainCommand; -import me.lucko.luckperms.commands.Sender; -import me.lucko.luckperms.commands.SubCommand; -import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.commands.*; import java.util.ArrayList; import java.util.List; @@ -16,11 +13,6 @@ public class SyncCommand extends MainCommand { @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (!sender.hasPermission("luckperms.sync")) { - Util.sendPluginMessage(sender, "You do not have permission to use this command!"); - return; - } - Util.sendPluginMessage(sender, "&bRunning update task for all online users."); plugin.runUpdateTask(); } @@ -32,6 +24,6 @@ public class SyncCommand extends MainCommand { @Override protected boolean canUse(Sender sender) { - return sender.hasPermission("luckperms.sync") || sender.hasPermission("luckperms.*"); + return Permission.SYNC.isAuthorized(sender); } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java index 8d7fccee..e24dbe68 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java @@ -9,6 +9,7 @@ import me.lucko.luckperms.users.User; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.UUID; public class UserMainCommand extends MainCommand{ @@ -21,31 +22,19 @@ public class UserMainCommand extends MainCommand{ @Override protected void execute(LuckPermsPlugin plugin, Sender sender, List args) { - if (args.size() <= 1) { + if (args.size() < 2) { sendUsage(sender); return; } - // The first argument is the name of the user, the second is the command - String command = args.get(1); - UserSubCommand tempSub = null; + Optional o = subCommands.stream().filter(s -> s.getName().equalsIgnoreCase(args.get(1))).limit(1).findAny(); - // Try to identify the command used - for (UserSubCommand s : subCommands) { - if (s.getName().equalsIgnoreCase(command)) { - tempSub = s; - break; - } - } - - // The command the sender used - final UserSubCommand sub = tempSub; - - if (sub == null) { + if (!o.isPresent()) { Util.sendPluginMessage(sender, "Command not recognised."); return; } + final UserSubCommand sub = o.get(); if (!sub.isAuthorized(sender)) { Util.sendPluginMessage(sender, "You do not have permission to use this command!"); return; @@ -57,8 +46,7 @@ public class UserMainCommand extends MainCommand{ strippedArgs.addAll(args.subList(2, args.size())); } - String user = args.get(0); - + final String user = args.get(0); UUID u = Util.parseUuid(user); if (u != null) { runSub(plugin, sender, u, sub, strippedArgs); @@ -88,9 +76,8 @@ public class UserMainCommand extends MainCommand{ return; } - User user1 = plugin.getUserManager().getUser(uuid); - - if (user1 == null) { + User user = plugin.getUserManager().getUser(uuid); + if (user == null) { Util.sendPluginMessage(sender, "&eUser could not be found."); } @@ -99,8 +86,8 @@ public class UserMainCommand extends MainCommand{ return; } - command.execute(plugin, sender, user1, strippedArgs); - plugin.getUserManager().cleanupUser(user1); + command.execute(plugin, sender, user, strippedArgs); + plugin.getUserManager().cleanupUser(user); }); } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/UserSubCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/UserSubCommand.java index a42ccb2c..f32b71b7 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/UserSubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/UserSubCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.commands.Util; @@ -9,17 +10,12 @@ import me.lucko.luckperms.users.User; import java.util.List; public abstract class UserSubCommand extends SubCommand { - protected UserSubCommand(String name, String description, String usage, String permission) { + protected UserSubCommand(String name, String description, String usage, Permission permission) { super(name, description, usage, permission); } protected abstract void execute(LuckPermsPlugin plugin, Sender sender, User user, List args); - @Override - public boolean isAuthorized(Sender sender) { - return sender.hasPermission(getPermission()) || sender.hasPermission("luckperms.user.*") || sender.hasPermission("luckperms.*"); - } - protected void saveUser(User user, Sender sender, LuckPermsPlugin plugin) { user.refreshPermissions(); diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserAddGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserAddGroupCommand.java index bb40e6c5..92168737 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserAddGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserAddGroupCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -12,34 +13,41 @@ import java.util.List; public class UserAddGroupCommand extends UserSubCommand { public UserAddGroupCommand() { - super("addgroup", "Adds the user to a group", - "/perms user addgroup [server]", "luckperms.user.addgroup"); + super("addgroup", "Adds the user to a group", "/perms user addgroup [server]", Permission.USER_ADDGROUP); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List args) { - String group = args.get(0).toLowerCase(); + String groupName = args.get(0).toLowerCase(); - String server; - if (args.size() != 1) { - server = args.get(1); - } else { - server = "global"; - } + plugin.getDatastore().loadGroup(groupName, success -> { + if (!success) { + Util.sendPluginMessage(sender, groupName + " does not exist!"); + } else { + Group group = plugin.getGroupManager().getGroup(groupName); + if (group == null) { + Util.sendPluginMessage(sender, "That group does not exist!"); + return; + } - Group group1 = plugin.getGroupManager().getGroup(group); - if (group1 == null) { - Util.sendPluginMessage(sender, "That group does not exist!"); - return; - } + try { + if (args.size() == 2) { + final String server = args.get(1).toLowerCase(); + user.addGroup(group, server); + Util.sendPluginMessage(sender, "&b" + user.getName() + "&a successfully added to group &b" + + groupName + "&a on the server &b" + server + "&a."); + } else { + user.addGroup(group); + Util.sendPluginMessage(sender, "&b" + user.getName() + "&a successfully added to group &b" + + groupName + "&a."); + } - try { - user.addGroup(group1, server); - Util.sendPluginMessage(sender, "&b" + user.getName() + "&a successfully added to group &b" + group + "&a on the server &b" + server + "&a."); - } catch (ObjectAlreadyHasException e) { - Util.sendPluginMessage(sender, "The user is already a member of that group."); - } - saveUser(user, sender, plugin); + saveUser(user, sender, plugin); + } catch (ObjectAlreadyHasException e) { + Util.sendPluginMessage(sender, "The user is already a member of that group."); + } + } + }); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java index b1b96fa8..81009a0e 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserClearCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -10,8 +11,7 @@ import java.util.List; public class UserClearCommand extends UserSubCommand { public UserClearCommand() { - super("clear", "Clears a users permissions and groups", - "/perms user clear", "luckperms.user.clear"); + super("clear", "Clears a users permissions and groups", "/perms user clear", Permission.USER_CLEAR); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserGetUUIDCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserGetUUIDCommand.java index 31f3322d..681c4a97 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserGetUUIDCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserGetUUIDCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -10,7 +11,7 @@ import java.util.List; public class UserGetUUIDCommand extends UserSubCommand { public UserGetUUIDCommand() { - super("getuuid", "Get the UUID of a user", "/perms user getuuid", "luckperms.user.getuuid"); + super("getuuid", "Get the UUID of a user", "/perms user getuuid", Permission.USER_GETUUID); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserHasPermCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserHasPermCommand.java index 2d66ec1f..866a202a 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserHasPermCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserHasPermCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -11,7 +12,7 @@ import java.util.List; public class UserHasPermCommand extends UserSubCommand { public UserHasPermCommand() { super("haspermission", "Checks to see if a user has a certain permission node", - "/perms user haspermission [server]", "luckperms.user.haspermission"); + "/perms user haspermission [server]", Permission.USER_HASPERMISSION); } @Override @@ -25,6 +26,6 @@ public class UserHasPermCommand extends UserSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java index 84abcbbf..e2ae7001 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserInfoCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -10,22 +11,20 @@ import java.util.List; public class UserInfoCommand extends UserSubCommand { public UserInfoCommand() { - super("info", "Gives info about the user", - "/perms user info", "luckperms.user.info"); + super("info", "Gives info about the user", "/perms user info", Permission.USER_INFO); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List args) { - final String prefix = Util.PREFIX; - String sb = prefix + "&d-> &eUser: &6" + user.getName() + "\n" + - prefix + "&d-> &eUUID: &6" + user.getUuid() + "\n" + - prefix + "&d-> &eStatus: " + plugin.getPlayerStatus(user.getUuid()) + "\n" + - prefix + "&d-> &eGroups: &6" + Util.listToCommaSep(user.getGroupNames()) + "\n" + - prefix + "&d-> &ePrimary Group: &6" + user.getPrimaryGroup() + "\n" + - prefix + "&d-> &ePermissions: &6" + (user.getNodes().keySet().size() - user.getGroupNames().size()) + "\n" + - prefix + "&d-> &bUse &a/perms user " + user.getName() + " listnodes &bto see all permissions."; - - sender.sendMessage(Util.color(sb)); + sender.sendMessage(Util.color( + Util.PREFIX + "&d-> &eUser: &6" + user.getName() + "\n" + + Util.PREFIX + "&d-> &eUUID: &6" + user.getUuid() + "\n" + + Util.PREFIX + "&d-> &eStatus: " + plugin.getPlayerStatus(user.getUuid()) + "\n" + + Util.PREFIX + "&d-> &eGroups: &6" + Util.listToCommaSep(user.getGroupNames()) + "\n" + + Util.PREFIX + "&d-> &ePrimary Group: &6" + user.getPrimaryGroup() + "\n" + + Util.PREFIX + "&d-> &ePermissions: &6" + (user.getNodes().keySet().size() - user.getGroupNames().size()) + "\n" + + Util.PREFIX + "&d-> &bUse &a/perms user " + user.getName() + " listnodes &bto see all permissions." + )); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserListNodesCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserListNodesCommand.java index 2b073569..8ca5cfdc 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserListNodesCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserListNodesCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -10,8 +11,7 @@ import java.util.List; public class UserListNodesCommand extends UserSubCommand { public UserListNodesCommand() { - super("listnodes", "Lists the permission nodes the user has", - "/perms user listnodes", "luckperms.user.listnodes"); + super("listnodes", "Lists the permission nodes the user has", "/perms user listnodes", Permission.USER_LISTNODES); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java index 2f72babf..fcfb8d19 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserRemoveGroupCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -12,39 +13,47 @@ import java.util.List; public class UserRemoveGroupCommand extends UserSubCommand { public UserRemoveGroupCommand() { - super("removegroup", "Removes a user from a group", - "/perms user removegroup [server]", "luckperms.user.removegroup"); + super("removegroup", "Removes a user from a group", "/perms user removegroup [server]", Permission.USER_REMOVEGROUP); } @Override protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List args) { - String group = args.get(0).toLowerCase(); + String groupName = args.get(0).toLowerCase(); - String server; - if (args.size() != 1) { - server = args.get(1); - } else { - server = "global"; - } + plugin.getDatastore().loadGroup(groupName, success -> { + if (!success) { + Util.sendPluginMessage(sender, groupName + " does not exist!"); + } else { + Group group = plugin.getGroupManager().getGroup(groupName); + if (group == null) { + Util.sendPluginMessage(sender, "That group does not exist!"); + return; + } - Group group1 = plugin.getGroupManager().getGroup(group); - if (group1 == null) { - Util.sendPluginMessage(sender, "That group does not exist!"); - return; - } + if ((args.size() == 1 || (args.size() == 2 && args.get(1).equalsIgnoreCase("global"))) + && user.getPrimaryGroup().equalsIgnoreCase(group.getName())) { + Util.sendPluginMessage(sender, "You cannot remove a user from their primary group."); + return; + } - if (server.equalsIgnoreCase("global") && user.getPrimaryGroup().equalsIgnoreCase(group1.getName())) { - Util.sendPluginMessage(sender, "You cannot remove a user from their primary group."); - return; - } + try { + if (args.size() == 2) { + final String server = args.get(1).toLowerCase(); + user.removeGroup(group, server); + Util.sendPluginMessage(sender, "&b" + user.getName() + "&a was removed from group &b" + + groupName + "&a on server &b" + server + "&a."); + } else { + user.removeGroup(group); + Util.sendPluginMessage(sender, "&b" + user.getName() + "&a was removed from group &b" + + groupName + "&a."); + } - try { - user.removeGroup(group1, server); - Util.sendPluginMessage(sender, "&b" + user.getName() + "&a was removed from group &b" + group + "&a on server &b" + server + "&a."); - } catch (ObjectLacksPermissionException e) { - Util.sendPluginMessage(sender, "The user is not a member of that group."); - } - saveUser(user, sender, plugin); + saveUser(user, sender, plugin); + } catch (ObjectLacksPermissionException e) { + Util.sendPluginMessage(sender, "The user is not a member of that group."); + } + } + }); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermissionCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermissionCommand.java index d7e03393..4c31140c 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermissionCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermissionCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -12,7 +13,7 @@ import java.util.List; public class UserSetPermissionCommand extends UserSubCommand { public UserSetPermissionCommand() { super("set", "Sets a permission for a user", - "/perms user set [server]", "luckperms.user.setpermission"); + "/perms user set [server]", Permission.USER_SETPERMISSION); } @Override @@ -41,20 +42,21 @@ public class UserSetPermissionCommand extends UserSubCommand { if (args.size() == 3) { final String server = args.get(2).toLowerCase(); user.setPermission(node, b, server); - Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to " + bool + " for &b" + user.getName() + "&a on server &b" + server + "&a."); + Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to " + bool + " for &b" + user.getName() + + "&a on server &b" + server + "&a."); } else { user.setPermission(node, b); Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to " + bool + " for &b" + user.getName() + "&a."); } + + saveUser(user, sender, plugin); } catch (ObjectAlreadyHasException e) { Util.sendPluginMessage(sender, "That user already has this permission!"); } - - saveUser(user, sender, plugin); } @Override public boolean isArgLengthInvalid(int argLength) { - return argLength < 2; + return argLength != 2 && argLength != 3; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java index 3f10abdc..cb8e3a76 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPrimaryGroupCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -12,7 +13,7 @@ import java.util.List; public class UserSetPrimaryGroupCommand extends UserSubCommand { public UserSetPrimaryGroupCommand() { super("setprimarygroup", "Sets a users primary group", - "/perms user setprimarygroup ", "luckperms.user.setprimarygroup"); + "/perms user setprimarygroup ", Permission.USER_SETPRIMARYGROUP); } @Override @@ -41,6 +42,6 @@ public class UserSetPrimaryGroupCommand extends UserSubCommand { @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermissionCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermissionCommand.java index 044a546e..bc882044 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermissionCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermissionCommand.java @@ -1,6 +1,7 @@ package me.lucko.luckperms.commands.user.subcommands; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.Permission; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.commands.user.UserSubCommand; @@ -12,7 +13,7 @@ import java.util.List; public class UserUnSetPermissionCommand extends UserSubCommand { public UserUnSetPermissionCommand() { super("unset", "Unsets a permission for a user", - "/perms user unset [server]", "luckperms.user.unsetpermission"); + "/perms user unset [server]", Permission.USER_UNSETPERMISSION); } @Override @@ -33,19 +34,21 @@ public class UserUnSetPermissionCommand extends UserSubCommand { if (args.size() == 2) { final String server = args.get(1).toLowerCase(); user.unsetPermission(node, server); - Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + user.getName() + "&a on server &b" + server + "&a."); + Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + user.getName() + "&a on server &b" + + server + "&a."); } else { user.unsetPermission(node); Util.sendPluginMessage(sender, "&aUnset &b" + node + "&a for &b" + user.getName() + "&a."); } + + saveUser(user, sender, plugin); } catch (ObjectLacksPermissionException e) { Util.sendPluginMessage(sender, "That user does not have this permission set."); } - saveUser(user, sender, plugin); } @Override public boolean isArgLengthInvalid(int argLength) { - return argLength == 0; + return argLength != 1 && argLength != 2; } } diff --git a/common/src/main/java/me/lucko/luckperms/users/UserManager.java b/common/src/main/java/me/lucko/luckperms/users/UserManager.java index 39a76d4f..37b7516f 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -6,6 +6,7 @@ import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.Map; +import java.util.NoSuchElementException; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -40,17 +41,13 @@ public abstract class UserManager { * @param name The name to search by * @return a {@link User} object if the user is loaded, returns null if the user is not loaded */ + @SuppressWarnings("OptionalGetWithoutIsPresent") public User getUser(String name) { - User user = null; - - for (User u : users.values()) { - if (u.getName().equalsIgnoreCase(name)) { - user = u; - break; - } + try { + return users.values().stream().filter(u -> u.getName().equalsIgnoreCase(name)).limit(1).findAny().get(); + } catch (NoSuchElementException e) { + return null; } - - return user; } /** diff --git a/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java b/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java index d7addd7f..af4b2a81 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java +++ b/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java @@ -56,10 +56,7 @@ public abstract class PermissionObject { */ public boolean hasPermission(String node, Boolean b) { if (node.startsWith("global/")) node = node.replace("global/", ""); - if (b) { - return getNodes().containsKey(node) && getNodes().get(node); - } - return getNodes().containsKey(node) && !getNodes().get(node); + return b ? getNodes().containsKey(node) && getNodes().get(node) : getNodes().containsKey(node) && !getNodes().get(node); } /** @@ -108,7 +105,6 @@ public abstract class PermissionObject { if (!getNodes().containsKey(node)) { throw new ObjectLacksPermissionException(); } - getNodes().remove(node); }