From 54b803e09bf52dee6f54391b737d8642e4816e11 Mon Sep 17 00:00:00 2001 From: Taah Date: Mon, 23 May 2022 23:15:12 -0700 Subject: [PATCH] Work on the module list to support permissions --- pom.xml | 6 +++ .../command/FreedomCommand.java | 28 +++++++++-- .../totalfreedommod/config/ConfigEntry.java | 2 + .../httpd/module/Module_list.java | 50 ++++++++++++++----- .../handler/DefaultPermissionHandler.java | 12 +++++ .../handler/IPermissionHandler.java | 5 ++ .../handler/NMPermissionHandler.java | 19 +++++++ .../handler/VaultPermissionHandler.java | 17 +++++++ src/main/resources/config.yml | 1 + 9 files changed, 125 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index efea8ed3..91515e22 100644 --- a/pom.xml +++ b/pom.xml @@ -279,6 +279,12 @@ + + org.json + json + 20220320 + + diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/FreedomCommand.java b/src/main/java/me/totalfreedom/totalfreedommod/command/FreedomCommand.java index 112b349c..b78555c1 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/FreedomCommand.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/FreedomCommand.java @@ -182,8 +182,7 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter noPerms(); } } - - protected void checkRank(String permission) + protected void checkPermissions(String permission) { //TODO: Not bothering with BukkitTelnet now. if (sender instanceof Player player) @@ -195,6 +194,23 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter } } + protected boolean checkPermissionsSilent(String permission) + { + //TODO: Not bothering with BukkitTelnet now. + if (sender instanceof ConsoleCommandSender) + { + return true; + } + if (sender instanceof Player player) + { + if (!plugin.permissionHandler.hasPermission(player, permission)) + { + return false; + } + } + return false; + } + @Deprecated protected void checkRank(Rank rank) { @@ -408,7 +424,13 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter public boolean func2() { - if (!plugin.rm.getRank(sender).isAtLeast(perms.level())) + /*if (!plugin.rm.getRank(sender).isAtLeast(perms.level())) + { + msg(NO_PERMISSION); + return true; + }*/ + + if (!checkPermissionsSilent(perms.permission())) { msg(NO_PERMISSION); return true; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java index c398a6d7..088efa81 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java @@ -76,6 +76,8 @@ public enum ConfigEntry PERMISSIONS_GROUPS_ADMIN(String.class, "permissions.groups.admin"), PERMISSIONS_GROUPS_SENIOR(String.class, "permissions.groups.senior_admin"), + + PERMISSIONS_GROUPS_DEFAULT(String.class, "permissions.groups.default"), DISCORD_TOKEN(String.class, "discord.token"), DISCORD_REPORT_CHANNEL_ID(String.class, "discord.report_channel_id"), DISCORD_CHAT_CHANNEL_ID(String.class, "discord.chat_channel_id"), diff --git a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_list.java b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_list.java index 2857f02f..ae2a54a5 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_list.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_list.java @@ -4,12 +4,15 @@ import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.admin.AdminList; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD; +import me.totalfreedom.totalfreedommod.permissions.handler.DefaultPermissionHandler; +import me.totalfreedom.totalfreedommod.permissions.handler.IPermissionHandler; import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; +import org.json.JSONArray; +import org.json.JSONObject; +import java.util.Arrays; import java.util.Collection; public class Module_list extends HTTPDModule @@ -26,6 +29,29 @@ public class Module_list extends HTTPDModule if (params.get("json") != null && params.get("json").equals("true")) { final JSONObject responseObject = new JSONObject(); + if (!(plugin.permissionHandler instanceof DefaultPermissionHandler)) + { + IPermissionHandler handler = plugin.permissionHandler; + final JSONObject players = new JSONObject(); + + Arrays.stream(handler.getGroups()).forEach(s -> + { + JSONArray array = new JSONArray(); + Bukkit.getOnlinePlayers().stream().filter(player -> !plugin.al.isVanished(player.getName())).filter(player -> handler.getPrimaryGroup(player).equalsIgnoreCase(s)).forEach(player -> + { + array.put(player.getName()); + }); + players.put(s.toLowerCase(), array); + }); + + responseObject.put("players", players); + responseObject.put("online", Bukkit.getOnlinePlayers().stream().filter(player -> !plugin.al.isVanished(player.getName())).count()); + responseObject.put("max", Bukkit.getMaxPlayers()); + + final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString(4)); + response.addHeader("Access-Control-Allow-Origin", "*"); + return response; + } final JSONArray owners = new JSONArray(); final JSONArray executives = new JSONArray(); @@ -44,27 +70,27 @@ public class Module_list extends HTTPDModule if (plugin.pl.getData(player).isMasterBuilder()) { - masterbuilders.add(player.getName()); + masterbuilders.put(player.getName()); } if (FUtil.DEVELOPER_NAMES.contains(player.getName())) { - developers.add(player.getName()); + developers.put(player.getName()); } if (ConfigEntry.SERVER_EXECUTIVES.getList().contains(player.getName()) && !FUtil.DEVELOPERS.contains(player.getName())) { - executives.add(player.getName()); + executives.put(player.getName()); } if (ConfigEntry.SERVER_OWNERS.getList().contains(player.getName())) { - owners.add(player.getName()); + owners.put(player.getName()); } if (!plugin.al.isAdmin(player) && hasSpecialTitle(player)) { - operators.add(player.getName()); + operators.put(player.getName()); } if (hasSpecialTitle(player) && plugin.al.isAdmin(player) && !plugin.al.isVanished(player.getName())) @@ -74,12 +100,12 @@ public class Module_list extends HTTPDModule { case ADMIN: { - admins.add(player.getName()); + admins.put(player.getName()); break; } case SENIOR_ADMIN: { - senioradmins.add(player.getName()); + senioradmins.put(player.getName()); break; } default: @@ -102,11 +128,11 @@ public class Module_list extends HTTPDModule responseObject.put("online", FUtil.getFakePlayerCount()); responseObject.put("max", server.getMaxPlayers()); - final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString()); + + final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString(4)); response.addHeader("Access-Control-Allow-Origin", "*"); return response; - } - else + } else { final StringBuilder body = new StringBuilder(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/DefaultPermissionHandler.java b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/DefaultPermissionHandler.java index 606828ff..8f3ce6df 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/DefaultPermissionHandler.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/DefaultPermissionHandler.java @@ -40,4 +40,16 @@ public class DefaultPermissionHandler implements IPermissionHandler { throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!"); } + + @Override + public String getPrimaryGroup(@NotNull Player player) + { + throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!"); + } + + @Override + public String[] getGroups() + { + throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!"); + } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/IPermissionHandler.java b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/IPermissionHandler.java index 50192b37..8776fe32 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/IPermissionHandler.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/IPermissionHandler.java @@ -17,4 +17,9 @@ public interface IPermissionHandler boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName); boolean inGroup(@NotNull Player player, @Nullable String groupName); + + String getPrimaryGroup(@NotNull Player player); + + + String[] getGroups(); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/NMPermissionHandler.java b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/NMPermissionHandler.java index b6152068..5c00a1ff 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/NMPermissionHandler.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/NMPermissionHandler.java @@ -5,6 +5,7 @@ import me.totalfreedom.totalfreedommod.util.FLog; import net.milkbowl.vault.permission.Permission; import nl.chimpgamer.networkmanager.api.NetworkManagerPlugin; import nl.chimpgamer.networkmanager.api.NetworkManagerProvider; +import nl.chimpgamer.networkmanager.api.models.permissions.Group; import nl.chimpgamer.networkmanager.api.models.permissions.PermissionPlayer; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -92,4 +93,22 @@ public class NMPermissionHandler implements IPermissionHandler { return this.inGroup((OfflinePlayer) player, groupName); } + + @Override + public String[] getGroups() + { + return this.plugin.getPermissionManager().getGroups().values().stream().map(Group::getName).toArray(String[]::new); + } + + @Override + public String getPrimaryGroup(@NotNull Player player) + { + PermissionPlayer permissionPlayer = this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId()); + if (permissionPlayer == null) + { + FLog.warning("NM Perms: Couldn't find player's primary group due to them not be found our list"); + return "default"; + } + return this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId()).getPrimaryGroup().getName(); + } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/VaultPermissionHandler.java b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/VaultPermissionHandler.java index 07ee9e4e..b09c96c6 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/VaultPermissionHandler.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/permissions/handler/VaultPermissionHandler.java @@ -96,6 +96,23 @@ public class VaultPermissionHandler implements IPermissionHandler return this.permissions.playerInGroup(player, groupName); } + @Override + public String[] getGroups() + { + if (this.permissions == null) + { + FLog.debug("Can't use Vault permissions system for group listing, there is no plugin using Vault permissions."); + return new String[0]; + } + return this.permissions.getGroups(); + } + + @Override + public String getPrimaryGroup(@NotNull Player player) + { + return this.permissions.getPrimaryGroup(player); + } + public Permission getPermissions() { return permissions; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 20a55f99..902f8707 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -59,6 +59,7 @@ permissions: groups: admin: "Administrator" senior_admin: "Senior-Administrator" + default: "Guest" # Discord discord: