Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
sync discord roles to mc perms
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed May 1, 2023
1 parent 863db2a commit 6bf39c0
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 98 deletions.
10 changes: 8 additions & 2 deletions src/main/java/pl/norbit/discordmc/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> profileRGB = config.getIntegerList("embed-profile-rgb");
PluginConfig.EMBED_PROFILE_R = profileRGB.get(0);
PluginConfig.EMBED_PROFILE_G = profileRGB.get(1);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/pl/norbit/discordmc/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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,
Expand Down
195 changes: 128 additions & 67 deletions src/main/java/pl/norbit/discordmc/discord/DiscordUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,97 @@
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;
import java.util.List;
import java.util.UUID;

public class DiscordUserService {
public final static List<Rank> configRanks= new ArrayList<>();
public final static List<Rank> configMcRanks = new ArrayList<>();
public final static List<Rank> 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<String> configList = PluginConfig.SYNC_ROLES;
if(PluginConfig.SYNC_ROLES_ENABLE) formatConfig(RoleType.DISCORD);
}

for (String perm : configList) {
private static void formatConfig(RoleType roleType){
List<String> 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;

Expand All @@ -83,55 +107,91 @@ private static void clearRanks(String userID){
roles.forEach(role -> roleDeleteFromUser(memberById, role));
}

private static List<String> getPermissionRanks(){
List<String> 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<String> getPermissionRanks(){
List<String> permList = new ArrayList<>();

configMcRanks.forEach(rank -> permList.add(rank.permission));

return permList;
}

public static void updateDiscordUser(UUID playerUUID, String userID){
Player player = Bukkit.getPlayer(playerUUID);

if(player == null) return;

PermissionUtil permissionUtil = new PermissionUtil(player);
Member memberById = DiscordMc.getGuild().retrieveMemberById(userID).complete();

List<String> 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<Role> roles = memberById.getRoles();
if(PluginConfig.SYNC_ROLES_ENABLE) DiscordUserService.updatePermissions(player, memberById);
}

private static void updatePermToRoles(Player p, Member member){
List<Role> roles = member.getRoles();

PermissionUtil permissionUtil = new PermissionUtil(p);

List<String> samePermissions = permissionUtil.getSamePermissions(getPermissionRanks());

roles.forEach(role -> {
roleDeleteFromUser(memberById, role);
roleDeleteFromUser(member, role);
});

if(samePermissions == null) return;
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/main/java/pl/norbit/discordmc/discord/RoleType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.norbit.discordmc.discord;

public enum RoleType {
DISCORD, MC
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 6bf39c0

Please sign in to comment.