From 2a5d79db935ccbf4b58df6ddfba42a00c1b51156 Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 8 Apr 2020 10:58:58 +0200 Subject: [PATCH] Toggle group permission, start edit language and configuration --- .../flifloo/StaffToggle/Commands/Staff.java | 99 ++++++++++++++----- .../Configurations/Configuration.java | 21 ++-- .../StaffToggle/Configurations/Language.java | 6 +- .../Configurations/PlayerConfig.java | 1 + src/fr/flifloo/StaffToggle/Main.java | 8 ++ src/languages/en.yml | 15 ++- 6 files changed, 109 insertions(+), 41 deletions(-) diff --git a/src/fr/flifloo/StaffToggle/Commands/Staff.java b/src/fr/flifloo/StaffToggle/Commands/Staff.java index 7794fc0..5ef8a15 100644 --- a/src/fr/flifloo/StaffToggle/Commands/Staff.java +++ b/src/fr/flifloo/StaffToggle/Commands/Staff.java @@ -1,13 +1,18 @@ package fr.flifloo.StaffToggle.Commands; +import com.mysql.fabric.xmlrpc.base.Array; import fr.flifloo.StaffToggle.Configurations.PlayerConfig; -import org.bukkit.ChatColor; +import net.luckperms.api.model.group.Group; +import net.luckperms.api.model.group.GroupManager; +import net.luckperms.api.model.user.User; +import net.luckperms.api.model.user.UserManager; +import net.luckperms.api.node.NodeType; +import net.luckperms.api.node.types.InheritanceNode; import org.bukkit.GameMode; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Item; import org.bukkit.entity.Player; import fr.flifloo.StaffToggle.Main; @@ -23,35 +28,64 @@ public class Staff implements CommandExecutor { public Staff(Main plugin) { this.plugin = plugin; - language = plugin.language.config; - staffConf = plugin.staffConf.config; + language = plugin.language.getConfig(); + staffConf = plugin.staffConf.getConfig(); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "Command only for players !"); + sender.sendMessage(language.getString("notPlayer")); return false; } Player player = (Player) sender; - String state = "players." + player.getUniqueId() + ".state"; + GroupManager groupManager = plugin.luckPerms.getGroupManager(); + String[] groups = groupManager.getLoadedGroups().stream() + .map(Group::getName) + .filter(t->player.hasPermission("staff.group."+t)) + .toArray(String[]::new); + if (groups.length == 0) { + player.sendMessage(language.getString("premissionDenide")); + return false; + } else if (groups.length > 1) + Arrays.sort(groups, Comparator.comparingInt((String s) -> groupManager.getGroup(s).getWeight().isEmpty() ? 0 : groupManager.getGroup(s).getWeight().getAsInt()).reversed()); - if (!staffConf.isSet(state)) { + String userGroup = groups[0]; + UserManager userManager = plugin.luckPerms.getUserManager(); + User user = userManager.getUser(player.getUniqueId()); + + if (user == null) + player.sendMessage(language.getString("userNull")); + String state = "players." + player.getUniqueId() + PlayerConfig.STATE.getValue(); + + if (!staffConf.isSet(state)) staffConf.set(state, false); - } - if (!staffConf.getBoolean(state)){ staffConf.set(state, true); save(player, "normal"); restore(player, "staff"); - player.sendMessage("Staff mod enabled !"); - + if (user != null) { + Group group = groupManager.getGroup(userGroup); + if (group != null) + user.data().add(InheritanceNode.builder(group).build()); + else + player.sendMessage(language.getString("unknonGroup")); + } + player.sendMessage(language.getString("enable")); } else { staffConf.set(state, false); save(player, "staff"); restore(player, "normal"); - player.sendMessage("Staff mod disabled !"); + if (user != null) + user.getNodes().stream() + .filter(NodeType.INHERITANCE::matches) + .map(NodeType.INHERITANCE::cast) + .filter(t->t.getGroupName().equals(userGroup)) + .forEach(t->user.data().remove(t)); + player.sendMessage(language.getString("disable")); } + if (user != null) + userManager.saveUser(user); return true; } @@ -62,26 +96,45 @@ public class Staff implements CommandExecutor { staffConf.set(playerConf + PlayerConfig.ARMOR.getValue(), playerInventory.getArmorContents()); staffConf.set(playerConf + PlayerConfig.LEVEL.getValue(), player.getLevel()); staffConf.set(playerConf + PlayerConfig.EXP.getValue(), player.getExp()); - staffConf.set(playerConf + PlayerConfig.GAMEMODE.getValue(), player.getGameMode()); + staffConf.set(playerConf + PlayerConfig.GAMEMODE.getValue(), player.getGameMode().getValue()); } private void restore(Player player, String mode) { String playerConf = "players." + player.getUniqueId() + "." + mode; - ItemStack[] inventory = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.INVENTORY.getValue()); - ItemStack[] armor = (ItemStack[]) staffConf.get(playerConf + PlayerConfig.ARMOR.getValue()); + ItemStack[] inventory = getItemStackConfig(playerConf + PlayerConfig.INVENTORY.getValue()); + ItemStack[] armor = getItemStackConfig(playerConf + PlayerConfig.ARMOR.getValue()); int level = staffConf.getInt(playerConf + PlayerConfig.LEVEL.getValue()); Object exp = staffConf.get(playerConf + PlayerConfig.EXP.getValue()); - GameMode gameMode = (GameMode) staffConf.get(playerConf + PlayerConfig.GAMEMODE.getValue()); + GameMode gameMode = GameMode.getByValue(staffConf.getInt(playerConf + PlayerConfig.GAMEMODE.getValue())); PlayerInventory playerInventory = player.getInventory(); - if (inventory != null) - playerInventory.setContents(inventory); - if (armor != null) - playerInventory.setArmorContents(armor); + playerInventory.setContents(inventory); + playerInventory.setArmorContents(armor); + if (exp != null) { + if (exp.getClass() == Double.class) + player.setExp(((Double) exp).floatValue()); + else + player.setExp((float) exp); + } + player.setLevel(level); if (gameMode == null) gameMode = GameMode.SURVIVAL; - if (exp != null) - player.setExp((float) exp); - player.setLevel(level); player.setGameMode(gameMode); } + + private ItemStack[] getItemStackConfig(String config) { + Object itemStackRaw = staffConf.get(config); + ArrayList itemStackArray; + ItemStack[] itemStacks = null; + if (itemStackRaw != null) { + if (itemStackRaw.getClass() == ArrayList.class) { + itemStackArray = (ArrayList) itemStackRaw; + itemStacks = new ItemStack[itemStackArray.size()]; + for (int i = 0; i provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class); + if (provider != null) { + luckPerms = provider.getProvider(); + } registerCommands(); } diff --git a/src/languages/en.yml b/src/languages/en.yml index f5ba280..1cd808f 100644 --- a/src/languages/en.yml +++ b/src/languages/en.yml @@ -1,13 +1,12 @@ language: en unknon: "&cUnknown command !" -staff: - help: "&3=========={Staff help}==========\n +premissionDenide: "&cYou don't have the permission to do this command !" +notPlayer: "&cCommand only for players !" +userNull: "$cCant change permissions !" +unknonGroup: "&cCant find your staff group !" +enable: "Staff mod enabled !" +disable: "Staff mod disabled !" +help: "&3=========={Staff help}==========\n &e/staff modo&9: Toggle moderation\n &e/staff admin&9: Toggle administration\n &3==============================" - modo: - on: "&eModeration on" - off: "&6Moderation off" - admin: - on: "&eAdministration on" - off: "&6Administration off" \ No newline at end of file