diff --git a/src/main/java/pl/norbit/discordmc/config/ConfigManager.java b/src/main/java/pl/norbit/discordmc/config/ConfigManager.java index efb1161..9bf025e 100644 --- a/src/main/java/pl/norbit/discordmc/config/ConfigManager.java +++ b/src/main/java/pl/norbit/discordmc/config/ConfigManager.java @@ -45,10 +45,16 @@ public static void loadConfig(boolean start){ PluginConfig.USER_IS_NOT_SYNC = config.getString("user-in-not-sync"); PluginConfig.ARG_USER_IS_NOT_SYNC = config.getString("arg-user-in-not-sync"); PluginConfig.DISCORD_USER_IS_SYNC = config.getString("discord-user-is-sync"); - PluginConfig.SYNC_ROLES = config.getStringList("sync-roles"); - PluginConfig.SYNC_RANK_ENABLE = config.getBoolean("sync-rank-enable"); + + PluginConfig.SYNC_PERMS_MC = config.getStringList("sync-mc-permissions"); + PluginConfig.SYNC_ROLES_DISCORD = config.getStringList("sync-discord-roles"); + PluginConfig.SYNC_PERM_ENABLE = config.getBoolean("sync-mc-permissions-enable"); + PluginConfig.SYNC_ROLES_ENABLE = config.getBoolean("sync-discord-roles-enable"); PluginConfig.SYNC_NAME = config.getBoolean("sync-name"); + PluginConfig.PERM_COMMAND_SET = config.getString("command-to-sync-permission"); + PluginConfig.PERM_COMMAND_UNSET = config.getString("command-to-un-sync-permission"); + List profileRGB = config.getIntegerList("embed-profile-rgb"); PluginConfig.EMBED_PROFILE_R = profileRGB.get(0); PluginConfig.EMBED_PROFILE_G = profileRGB.get(1); diff --git a/src/main/java/pl/norbit/discordmc/config/PluginConfig.java b/src/main/java/pl/norbit/discordmc/config/PluginConfig.java index 2b056d9..9810341 100644 --- a/src/main/java/pl/norbit/discordmc/config/PluginConfig.java +++ b/src/main/java/pl/norbit/discordmc/config/PluginConfig.java @@ -12,13 +12,14 @@ public class PluginConfig { SYNC_COMMAND_ARG, EMBED_INFO_TITTLE, EMBED_INFO_DESC, FALSE_INFO, TRUE_INFO, CHANNEL_INFO_ID, DATABASE_TYPE, SYNC_COMMAND_CLEAR_ARG, SYNC_CLEAR_MESSAGE, MODULE_OFF_MESSAGE, PERMISSION_MESSAGE, EMBED_PROFILE_TITLE, ARG_USER_IS_NOT_SYNC, USER_IS_NOT_SYNC, DISCORD_USER_IS_SYNC, RANK_RELOAD_COMMAND_ARG, PLAYER_IS_NOT_SYNC, - RANK_RELOAD_MESSAGE; + RANK_RELOAD_MESSAGE, PERM_COMMAND_SET, PERM_COMMAND_UNSET; public static List COMMAND_ARGS_LIST,EMBED_INFO_ARGS, EMBED_PROFILE_ONLINE, BLOCKED_COMMANDS, - IGNORE_FOLDERS, SYNC_ROLES, EMBED_PROFILE_OFFLINE, COMMANDS_ON_SYNC, COMMANDS_ON_SYNC_CLEAR; + IGNORE_FOLDERS, SYNC_PERMS_MC, EMBED_PROFILE_OFFLINE, COMMANDS_ON_SYNC, COMMANDS_ON_SYNC_CLEAR, + SYNC_ROLES_DISCORD; public static boolean PLUGIN_ENABLE, DATABASE_SSL, CHAT_MODULE, CONSOLE_MODULE, DISCORD_CONSOLE_COMMANDS, - BLOCK_WARN_MESSAGES, BLOCK_ERROR_MESSAGES, DISCORD_INFO_MODULE, SYNC_RANK_ENABLE, SYNC_NAME, + BLOCK_WARN_MESSAGES, BLOCK_ERROR_MESSAGES, DISCORD_INFO_MODULE, SYNC_PERM_ENABLE,SYNC_ROLES_ENABLE, SYNC_NAME, DISCORD_CONSOLE_DISPLAY, PLACEHOLDER_API_EXIST, EXECUTE_COMMAND_ON_SYNC, EXECUTE_COMMAND_ON_SYNC_CLEAR; public static int EMBED_DISCORD_R, EMBED_DISCORD_G, EMBED_DISCORD_B, EMBED_MC_R, EMBED_MC_G, EMBED_MC_B, DATABASE_PORT, diff --git a/src/main/java/pl/norbit/discordmc/discord/DiscordUserService.java b/src/main/java/pl/norbit/discordmc/discord/DiscordUserService.java index 70fc450..24feffa 100644 --- a/src/main/java/pl/norbit/discordmc/discord/DiscordUserService.java +++ b/src/main/java/pl/norbit/discordmc/discord/DiscordUserService.java @@ -8,7 +8,9 @@ import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import pl.norbit.discordmc.DiscordMc; +import pl.norbit.discordmc.api.events.SyncClearEvent; import pl.norbit.discordmc.config.PluginConfig; +import pl.norbit.discordmc.utils.ConsoleUtil; import pl.norbit.discordmc.utils.PermissionUtil; import java.util.ArrayList; @@ -16,65 +18,87 @@ import java.util.UUID; public class DiscordUserService { - public final static List configRanks= new ArrayList<>(); + public final static List configMcRanks = new ArrayList<>(); + public final static List configDiscordRanks = new ArrayList<>(); private static JDA jda; - public static void init() { + public static class Rank { + private final String permission; + private final Role role; + + public Rank(String permission, String ID, RoleType roleType) { + this.permission = permission; + Guild guildById = null; + + try { + guildById = jda.awaitReady().getGuildById(PluginConfig.SERVER_ID); + } catch (InterruptedException ignored) { + + } + assert guildById != null; + role = guildById.getRoleById(ID); + + if(role != null){ + if(roleType == RoleType.MC) configMcRanks.add(this); + else configDiscordRanks.add(this); + }else{ + DiscordMc.sendMessage("&cPermission: &4" + permission + " &cwith &4" + ID + " &chas not been loaded!" + + " This User ID does not exist!"); + } + } + } + public static void init(){ DiscordUserService.jda = DiscordMc.getJda(); - if(!PluginConfig.SYNC_RANK_ENABLE) return; + if(PluginConfig.SYNC_PERM_ENABLE) formatConfig(RoleType.MC); - List configList = PluginConfig.SYNC_ROLES; + if(PluginConfig.SYNC_ROLES_ENABLE) formatConfig(RoleType.DISCORD); + } - for (String perm : configList) { + private static void formatConfig(RoleType roleType){ + List configList; + + if(roleType == RoleType.MC) configList = PluginConfig.SYNC_PERMS_MC; + else configList = PluginConfig.SYNC_ROLES_DISCORD; + + for (String roleString : configList) { + + String[] split = roleString.split(":"); + + if(split.length < 2) continue; - String[] split = perm.split(":"); String permission = split[0].replaceAll(" ", ""); String id = split[1].replaceAll(" ", ""); - new Rank(permission, id); + new Rank(permission, id, roleType); } } private static Rank getRankByPerm(String perm){ - for (Rank configRank : configRanks) { - - if(configRank.permission.equals(perm)){ - return configRank; - } + for (Rank configRank : configMcRanks) { + if(configRank.permission.equals(perm)) return configRank; } return null; } - public static void changeToMinecraftName(Player p, String discordUUID){ - + public static void changeToMinecraftName(Player p, Member member){ String minecraftNick = p.getName(); - Guild guild = jda.getGuildById(PluginConfig.SERVER_ID); - - Member member = guild.retrieveMemberById(discordUUID).complete(); - if(!member.isOwner()) member.modifyNickname(minecraftNick).queue(); } private static void clearName(String discordUUID){ - - Guild guild = jda.getGuildById(PluginConfig.SERVER_ID); - - Member member = guild.retrieveMemberById(discordUUID).complete(); + Member member = DiscordMc.getGuild().retrieveMemberById(discordUUID).complete(); if(!member.isOwner()) member.modifyNickname(member.getUser().getName()).queue(); } private static void roleDeleteFromUser(Member member, Role role){ - - configRanks.forEach(rank -> { - - if(rank.role.equals(role)) - jda.getGuildById(PluginConfig.SERVER_ID).removeRoleFromMember(member.getUser(), role).queue(); + configMcRanks.forEach(rank -> { + if(rank.role.equals(role)) DiscordMc.getGuild().removeRoleFromMember(member.getUser(), role).queue(); }); } private static void clearRanks(String userID){ - Member memberById = jda.getGuildById(PluginConfig.SERVER_ID).retrieveMemberById(userID).complete(); + Member memberById = DiscordMc.getGuild().retrieveMemberById(userID).complete(); if(memberById == null) return; @@ -83,36 +107,64 @@ private static void clearRanks(String userID){ roles.forEach(role -> roleDeleteFromUser(memberById, role)); } - private static List getPermissionRanks(){ - List permList = new ArrayList<>(); + private static void clearPermissions(UUID playerUUID){ + Player p = Bukkit.getPlayer(playerUUID); - configRanks.forEach(rank -> permList.add(rank.permission)); + if(p == null) return; - return permList; - } - public static class Rank { - private final String permission; - private final Role role; + PermissionUtil perms = new PermissionUtil(p); - public Rank(String permission, String ID) { - this.permission = permission; - Guild guildById = null; + configDiscordRanks.forEach(rank -> { + if(!perms.hasPermission(rank.permission)) return; - try { - guildById = jda.awaitReady().getGuildById(PluginConfig.SERVER_ID); - } catch (InterruptedException ignored) { + String cmd = PluginConfig.PERM_COMMAND_UNSET + .replace("{PLAYER}", p.getName()) + .replace("{PERM}", rank.permission); + new BukkitRunnable() { + @Override + public void run() { + ConsoleUtil.executeCommand(p, cmd); + } + }.runTaskLater(DiscordMc.getInstance(), 0); + }); + } + private static void addPermission(Player p, String perm){ + new BukkitRunnable() { + @Override + public void run() { + String cmd = PluginConfig.PERM_COMMAND_SET + .replace("{PLAYER}", p.getName()) + .replace("{PERM}", perm); + + ConsoleUtil.executeCommand(p, cmd); } - assert guildById != null; - role = guildById.getRoleById(ID); + }.runTaskLater(DiscordMc.getInstance(), 0); + } - if(role != null){ - configRanks.add(this); - }else{ - DiscordMc.sendMessage("&cPermission: &4" + permission + " &cwith &4" + ID + " &chas not been loaded!" - + " This User ID does not exist!"); + private static void updatePermissions(Player p, Member member){ + PermissionUtil perms = new PermissionUtil(p); + + configDiscordRanks.forEach(rank -> { + + if(perms.hasPermission(rank.permission)) return; + + for (Role role : member.getRoles()) { + + if(!rank.role.getId().equals(role.getId())) continue; + + addPermission(p, rank.permission); + break; } - } + }); + } + + private static List getPermissionRanks(){ + List permList = new ArrayList<>(); + + configMcRanks.forEach(rank -> permList.add(rank.permission)); + + return permList; } public static void updateDiscordUser(UUID playerUUID, String userID){ @@ -120,18 +172,26 @@ public static void updateDiscordUser(UUID playerUUID, String userID){ if(player == null) return; - PermissionUtil permissionUtil = new PermissionUtil(player); + Member memberById = DiscordMc.getGuild().retrieveMemberById(userID).complete(); - List samePermissions = permissionUtil.getSamePermissions(getPermissionRanks()); + if(memberById == null) return; - Member memberById = jda.getGuildById(PluginConfig.SERVER_ID).retrieveMemberById(userID).complete(); + if(PluginConfig.SYNC_PERM_ENABLE) DiscordUserService.updatePermToRoles(player, memberById); - if(memberById == null) return; + if(PluginConfig.SYNC_NAME) DiscordUserService.changeToMinecraftName(player, memberById); - List roles = memberById.getRoles(); + if(PluginConfig.SYNC_ROLES_ENABLE) DiscordUserService.updatePermissions(player, memberById); + } + + private static void updatePermToRoles(Player p, Member member){ + List roles = member.getRoles(); + + PermissionUtil permissionUtil = new PermissionUtil(p); + + List samePermissions = permissionUtil.getSamePermissions(getPermissionRanks()); roles.forEach(role -> { - roleDeleteFromUser(memberById, role); + roleDeleteFromUser(member, role); }); if(samePermissions == null) return; @@ -141,24 +201,25 @@ public static void updateDiscordUser(UUID playerUUID, String userID){ public void run() { Rank rank = getRankByPerm(samePermissions.get(0)); - jda.getGuildById(PluginConfig.SERVER_ID).addRoleToMember(memberById.getUser(), rank.role).queue();; + if(rank == null) return; + + Role role = rank.role; + + if(role == null) return; + + DiscordMc.getGuild().addRoleToMember(member.getUser(), role).queue();; } }.runTaskLaterAsynchronously(DiscordMc.getInstance(), 4); - - if(PluginConfig.SYNC_NAME){ - DiscordUserService.changeToMinecraftName(player, userID); - } } - public static void clear(String id){ - if(!PluginConfig.SYNC_RANK_ENABLE) return; + public static void clear(String id, UUID playerUUID) { - if(id == null) return; + if (id == null) return; - DiscordUserService.clearRanks(id); + if (PluginConfig.SYNC_PERM_ENABLE) DiscordUserService.clearRanks(id); - if(!PluginConfig.SYNC_NAME) return; + if (PluginConfig.SYNC_ROLES_ENABLE) DiscordUserService.clearPermissions(playerUUID); - DiscordUserService.clearName(id); + if (PluginConfig.SYNC_NAME) DiscordUserService.clearName(id); } } diff --git a/src/main/java/pl/norbit/discordmc/discord/RoleType.java b/src/main/java/pl/norbit/discordmc/discord/RoleType.java new file mode 100644 index 0000000..3d76bc9 --- /dev/null +++ b/src/main/java/pl/norbit/discordmc/discord/RoleType.java @@ -0,0 +1,5 @@ +package pl.norbit.discordmc.discord; + +public enum RoleType { + DISCORD, MC +} diff --git a/src/main/java/pl/norbit/discordmc/players/DiscordPlayerService.java b/src/main/java/pl/norbit/discordmc/players/DiscordPlayerService.java index f14dbf1..61b3eee 100644 --- a/src/main/java/pl/norbit/discordmc/players/DiscordPlayerService.java +++ b/src/main/java/pl/norbit/discordmc/players/DiscordPlayerService.java @@ -98,7 +98,7 @@ public static void syncClear(UUID playerUUID){ playersListDiscord.remove(discordPlayer.getDiscordId()); DatabaseService.deleteUser(discordPlayer.getPlayerUUID()); - DiscordUserService.clear(discordPlayer.getDiscordId()); + DiscordUserService.clear(discordPlayer.getDiscordId(), discordPlayer.getPlayerUUID()); discordPlayer.syncClear(); } @@ -115,7 +115,7 @@ public static void syncClear(String discordID){ playersListDiscord.remove(discordPlayer.getDiscordId()); DatabaseService.deleteUser(discordPlayer.getPlayerUUID()); - DiscordUserService.clear(discordPlayer.getDiscordId()); + DiscordUserService.clear(discordPlayer.getDiscordId(), discordPlayer.getPlayerUUID()); discordPlayer.syncClear(); } diff --git a/src/main/java/pl/norbit/discordmc/server/commands/MainCMD.java b/src/main/java/pl/norbit/discordmc/server/commands/MainCMD.java index edb922b..a1fc20a 100644 --- a/src/main/java/pl/norbit/discordmc/server/commands/MainCMD.java +++ b/src/main/java/pl/norbit/discordmc/server/commands/MainCMD.java @@ -43,7 +43,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St ServerCommand.execute(CommandType.RELOAD, p); }else if(args[0].equalsIgnoreCase(PluginConfig.RANK_RELOAD_COMMAND_ARG)){ - if(PluginConfig.SYNC_RANK_ENABLE) { + if(PluginConfig.SYNC_PERM_ENABLE) { ServerCommand.execute(CommandType.RANK_RELOAD, p); return true; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 546a7eb..f31f1e8 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -16,7 +16,7 @@ # If you like the plugin, support me with a positive opinion on spigot.org, thanks! <3 #---------------------------------------# -# [bot config] # +# [Bot config] # #---------------------------------------# # configuration: https://github.com/Norbit4/DiscordMc/wiki/Configuration @@ -41,7 +41,15 @@ password: '' database: '' ssl: false +#---------------------------------------# +# [Sync profiles] # +#---------------------------------------# + embed-profile-title: 'Profile' +embed-profile-rgb: + - 154 + - 199 + - 50 # to use placeholders you need install PlaceholderAPI https://www.spigotmc.org/resources/placeholderapi.6245/ # placeholders https://github.com/PlaceholderAPI/PlaceholderAPI/wiki/Placeholders#player @@ -59,6 +67,46 @@ embed-profile-offline: - 'nick:// %player_name%' - 'status:// Offline' +#---------------------------------------# +# [Sync roles/perms] # +#---------------------------------------# + +# wiki sync: https://github.com/Norbit4/DiscordMc/wiki/Rank-sync + +#---------------------------------------# +# [module-1] Sync minecraft perms to discord roles + +sync-mc-permissions-enable: false + +# discord name sync (set discord user mc name) +# this option need enabled 'sync-mc-permissions-enable' +# nick sync does not work for the discord server owner, it is specific to discord servers! +sync-name: false + +# sync minecraft perms to discord roles +# this option need enabled 'sync-mc-permissions-enable' +# only top perm from minecraft will be synchronized with discord role (user can have only one role synced) +sync-mc-permissions: + - 'rank-permission:discord-rank-id' + +#---------------------------------------# +# [module-2] Sync discord roles to minecraft perms + +sync-discord-roles-enable: false + +# all roles from discord will be synchronized with minecraft perms +# this can be used to synchronize the users boosting the server and give them permissions for it on the server +# this option need enabled 'sync-discord-roles-enable' +sync-discord-roles: + - 'rank-permission:discord-rank-id' + +command-to-sync-permission: 'lp user {PLAYER} perm set {PERM}' +command-to-un-sync-permission: 'lp user {PLAYER} perm unset {PERM}' + +#---------------------------------------# +# [Sync commands] # +#---------------------------------------# + # commands support PlaceholdersAPI # commands to execute when the player syncs execute-commands-on-sync: false @@ -72,26 +120,8 @@ commands-on-sync-clear: - 'lp user {PLAYER} perm unset sync.true' - 'say Synced clear!' -# discord rank sync -sync-rank-enable: false -# discord name sync (set discord user mc name) -# this option need enabled 'sync-rank-enabled' -# nick sync does not work for the discord server owner, it is specific to discord servers! -sync-name: false - -# wiki sync roles: https://github.com/Norbit4/DiscordMc/wiki/Rank-sync -sync-roles: - - 'rank-permission:discord-rank-id' - -user-in-not-sync: 'You are not synchronized! Use **/sync** to sync your accounts!' -arg-user-in-not-sync: 'This user is not synchronized!' -embed-profile-rgb: - - 154 - - 199 - - 50 - #---------------------------------------# -# [messages] # +# [Messages] # #---------------------------------------# # discord embeds colors @@ -117,6 +147,8 @@ error-message-rgb: - 50 #discord messages +user-in-not-sync: 'You are not synchronized! Use **/sync** to sync your accounts!' +arg-user-in-not-sync: 'This user is not synchronized!' player-is-sync: 'This player is synchronize!' discord-user-is-sync: 'You are synchronized!' offline-player: 'This player is offline!' @@ -145,7 +177,7 @@ command-args-list: - '' #---------------------------------------# -# [discord-chat-module] # +# [Discord-chat-module] # #---------------------------------------# # help: https://github.com/Norbit4/DiscordMc/wiki/Chat-module @@ -181,7 +213,7 @@ mc-user-rgb: - 99 #---------------------------------------# -# [discord-console-module] # +# [Discord-console-module] # #---------------------------------------# # help: https://github:com/Norbit4/DiscordMc/wiki/Cosole-module @@ -203,7 +235,7 @@ blocked-commands: - pl #---------------------------------------# -# [discord-info-module] # +# [Discord-info-module] # #---------------------------------------# # help: https://github.com/Norbit4/DiscordMc/wiki/Info-module