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

Commit

Permalink
added rank sync option
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed Aug 6, 2022
1 parent 9543f17 commit aeb4983
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 63 deletions.
7 changes: 6 additions & 1 deletion src/main/java/pl/norbit/discordmc/DiscordMc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import pl.norbit.discordmc.bot.DiscordBot;
import pl.norbit.discordmc.bot.builder.BotBuilder;
import pl.norbit.discordmc.bot.commands.CommandManager;
import pl.norbit.discordmc.bot.utils.ChatUtil;
import pl.norbit.discordmc.sync.SyncManager;
import pl.norbit.discordmc.utils.ChatUtil;
import pl.norbit.discordmc.db.PluginDBManager;
import pl.norbit.discordmc.server.config.PluginConfig;
import pl.norbit.discordmc.server.commands.MainCMD;
Expand Down Expand Up @@ -45,6 +46,10 @@ public void onEnable() {
timeServer = System.currentTimeMillis();
PluginDBManager.init(discordBot.getJda(), this);

if(PluginConfig.SYNC_RANK_ENABLE) {
SyncManager.init(discordBot.getJda());
}

//console module
if(PluginConfig.CONSOLE_MODULE){
MessageChannel messageChannel = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.bukkit.plugin.java.JavaPlugin;
import pl.norbit.discordmc.bot.commands.ProfileCommand;
import pl.norbit.discordmc.bot.commands.SyncCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package pl.norbit.discordmc.bot.commands;

import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bson.Document;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import pl.norbit.discordmc.bot.embed.Embed;
import pl.norbit.discordmc.bot.utils.ChatUtil;
import pl.norbit.discordmc.db.GameUser;
import pl.norbit.discordmc.db.MongoDatabase;
import pl.norbit.discordmc.utils.ChatUtil;
import pl.norbit.discordmc.db.PluginDBManager;
import pl.norbit.discordmc.db.objects.DatabaseRecord;
import pl.norbit.discordmc.server.config.PluginConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import pl.norbit.discordmc.bot.embed.Embed;
import pl.norbit.discordmc.bot.utils.ChatUtil;
import pl.norbit.discordmc.utils.ChatUtil;
import pl.norbit.discordmc.server.config.PluginConfig;
import pl.norbit.discordmc.server.enums.Channel;
import pl.norbit.discordmc.server.objects.GamePlayer;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/pl/norbit/discordmc/db/PluginDBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static DatabaseRecord getUser(String discordID){

if (doc != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(doc.getString(GameUser.UUID.name())));
User user = jda.getUserById(doc.getString(GameUser.DISCORD_ID.name()));
User user = jda.retrieveUserById(doc.getString(GameUser.DISCORD_ID.name())).complete();
return new DatabaseRecord(player, user);
}
}else if(PluginConfig.DATABASE_TYPE.equalsIgnoreCase("MYSQL")){
Expand All @@ -80,7 +80,7 @@ public static DatabaseRecord getUser(String discordID){
try {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(user.getString(GameUser.UUID.name())));

User dcUser = jda.getUserById(discordID);
User dcUser = jda.retrieveUserById(discordID).complete();

return new DatabaseRecord(player, dcUser);

Expand All @@ -92,7 +92,7 @@ public static DatabaseRecord getUser(String discordID){
LocalDB.LocalRecord user = LocalDB.getUser(discordID);
if(user != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(user.getPlayerUUID());
User dcUser = jda.getUserById(discordID);
User dcUser = jda.retrieveUserById(discordID).complete();
return new DatabaseRecord(player, dcUser);
}
}
Expand All @@ -104,9 +104,8 @@ public static DatabaseRecord getUser(UUID playerUUID){
if(PluginConfig.DATABASE_TYPE.equalsIgnoreCase("MONGODB")) {
Document doc = MongoDatabase.getUser(GameUser.UUID.name(), playerUUID.toString());
if (doc != null) {

OfflinePlayer player = Bukkit.getOfflinePlayer(playerUUID);
User user = jda.getUserById(doc.getString(GameUser.DISCORD_ID.name()));
User user = jda.retrieveUserById(doc.getString(GameUser.DISCORD_ID.name())).complete();
return new DatabaseRecord(player, user);
}
}else if(PluginConfig.DATABASE_TYPE.equalsIgnoreCase("MYSQL")){
Expand All @@ -129,7 +128,7 @@ public static DatabaseRecord getUser(UUID playerUUID){
LocalDB.LocalRecord user = LocalDB.getUser(playerUUID);
if(user != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(playerUUID);
User dcUser = jda.getUserById(user.getUserID());
User dcUser = jda.retrieveUserById(user.getUserID()).complete();
return new DatabaseRecord(player, dcUser);
}
}
Expand Down
114 changes: 69 additions & 45 deletions src/main/java/pl/norbit/discordmc/server/commands/MainCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import pl.norbit.discordmc.bot.embed.Embed;
import pl.norbit.discordmc.bot.utils.ChatUtil;
import pl.norbit.discordmc.db.objects.DatabaseRecord;
import pl.norbit.discordmc.sync.SyncManager;
import pl.norbit.discordmc.utils.ChatUtil;
import pl.norbit.discordmc.db.PluginDBManager;
import pl.norbit.discordmc.server.config.ConfigManager;
import pl.norbit.discordmc.server.config.PluginConfig;
import pl.norbit.discordmc.server.enums.Channel;
import pl.norbit.discordmc.server.objects.GamePlayer;
import pl.norbit.discordmc.sync.SyncPlayer;
import pl.norbit.discordmc.sync.SyncTimerTask;
import pl.norbit.discordmc.utils.PermissionUtil;

import java.awt.*;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public class MainCMD implements CommandExecutor {

Expand All @@ -37,19 +39,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

if(args.length > 0) {

PermissionUtil permissionUtil = new PermissionUtil(p);

if (args[0].equalsIgnoreCase(PluginConfig.COMMAND_CHAT_CHANGE_ARG)) {
if(PluginConfig.CHAT_MODULE) {
String[] perms = {"discordmc.channel", "discordmc.*", "*"};

AtomicBoolean hasPerm = new AtomicBoolean(false);
p.getEffectivePermissions().forEach(perm -> {
if(perm.getPermission().equalsIgnoreCase("discordmc.channel") ||
perm.getPermission().equalsIgnoreCase("discordmc.*") ||
perm.getPermission().equalsIgnoreCase("*")) {
hasPerm.set(true);
}
});

if(hasPerm.get()){
if(permissionUtil.hasPermission(perms)){
changeChannelCMD(args, p);
}else{
String message = PluginConfig.PERMISSION_MESSAGE;
Expand All @@ -61,17 +57,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

}else if(args[0].equalsIgnoreCase(PluginConfig.SYNC_COMMAND_ARG)){

AtomicBoolean hasPerm = new AtomicBoolean(false);
p.getEffectivePermissions().forEach(perm -> {
if(perm.getPermission().equalsIgnoreCase("discordmc.sync")||
perm.getPermission().equalsIgnoreCase("discordmc.*") ||
perm.getPermission().equalsIgnoreCase("*"))
{
hasPerm.set(true);
}
});
String[] perms = {"discordmc.sync", "discordmc.*", "*"};

if(hasPerm.get()){
if(permissionUtil.hasPermission(perms)){
syncCmd(p);
}else{
String message = PluginConfig.PERMISSION_MESSAGE;
Expand All @@ -80,36 +68,20 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

} else if(args[0].equalsIgnoreCase(PluginConfig.SYNC_COMMAND_CLEAR_ARG)){

AtomicBoolean hasPerm = new AtomicBoolean(false);
p.getEffectivePermissions().forEach(perm -> {
if(perm.getPermission().equalsIgnoreCase("discordmc.syncclear")||
perm.getPermission().equalsIgnoreCase("discordmc.*") ||
perm.getPermission().equalsIgnoreCase("*"))
{
hasPerm.set(true);
}
});
String[] perms = {"discordmc.syncclear", "discordmc.*", "*"};

if(hasPerm.get()){
if(permissionUtil.hasPermission(perms)){
syncClearCMD(p);
}else{
String message = PluginConfig.PERMISSION_MESSAGE;
p.sendMessage(ChatUtil.format(message));
}

}else if(args[0].equalsIgnoreCase("reload")){
AtomicBoolean hasPerm = new AtomicBoolean(false);

p.getEffectivePermissions().forEach(perm -> {
if(perm.getPermission().equalsIgnoreCase("discordmc.reload")||
perm.getPermission().equalsIgnoreCase("discordmc.*") ||
perm.getPermission().equalsIgnoreCase("*"))
{
hasPerm.set(true);
}
});

if(hasPerm.get()){
String[] perms = {"discordmc.reload", "discordmc.*", "*"};

if(permissionUtil.hasPermission(perms)){
ConfigManager.loadConfig(javaPlugin, false);

String message = "&aConfig has been reloaded!";
Expand All @@ -118,6 +90,21 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
String message = PluginConfig.PERMISSION_MESSAGE;
p.sendMessage(ChatUtil.format(message));
}
}else if(args[0].equalsIgnoreCase("rankreload")){

if(PluginConfig.SYNC_RANK_ENABLE) {
String[] perms = {"discordmc.rankreload", "discordmc.*", "*"};

if (permissionUtil.hasPermission(perms)) {
syncRank(p);
} else {
String message = PluginConfig.PERMISSION_MESSAGE;
p.sendMessage(ChatUtil.format(message));
}
}else{
String message = "&cSync rank is disabled!";
p.sendMessage(ChatUtil.format(message));
}
}else {

sendHelpMessage(p);
Expand Down Expand Up @@ -154,6 +141,28 @@ private void sendArgWarnMessage(Player player){

player.sendMessage(ChatUtil.format(message));
}
private void syncRank(Player p){

DatabaseRecord user = PluginDBManager.getUser(p.getUniqueId());

if(user != null){

boolean b = SyncManager.addPlayer(p.getUniqueId(), user.getUser().getId());

if(PluginConfig.SYNC_NAME){
SyncManager.changeToMinecraftName(p, user.getUser().getId());
}

String message = "&aRank reloaded!";

p.sendMessage(ChatUtil.format(message));
}else{

String message = "&cYou are not sync!";

p.sendMessage(ChatUtil.format(message));
}
}

private void syncCmd(Player player){

Expand All @@ -177,12 +186,19 @@ private void syncCmd(Player player){
new Color(
PluginConfig.EMBED_SUCCESS_R,
PluginConfig.EMBED_SUCCESS_G,
PluginConfig.EMBED_SUCCESS_B)
)
PluginConfig.EMBED_SUCCESS_B))
.build();

syncPlayer.getMessageChannel().sendMessageEmbeds(embed).queue();

if(PluginConfig.SYNC_RANK_ENABLE) {
SyncManager.addPlayer(playerUUID, userID);
}

if(PluginConfig.SYNC_NAME){
SyncManager.changeToMinecraftName(player, userID);
}

String mcMessage = PluginConfig.SYNC_SUCCESS_MC
.replace("{DISCORD}",
syncPlayer.getUser().getAsTag())
Expand Down Expand Up @@ -230,10 +246,18 @@ private void changeChannelCMD(String[] args, Player player){
}
}
private void syncClearCMD(Player player){

PluginDBManager.deleteUser(player.getUniqueId());

String message = PluginConfig.SYNC_CLEAR_MESSAGE;
GamePlayer gamePlayer = GamePlayer.getGamePLayer(player);

if(gamePlayer.getDiscordUser() != null) {
String id = gamePlayer.getDiscordUser().getId();
SyncManager.clearRanks(id);
SyncManager.clearName(id);
}

gamePlayer.setDiscordUser(null);

player.sendMessage(ChatUtil.format(message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public static void loadConfig(JavaPlugin javaPlugin, 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_NAME = config.getBoolean("sync-name");

List<Integer> profileRGB = config.getIntegerList("embed-profile-rgb");
PluginConfig.EMBED_PROFILE_R = profileRGB.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class PluginConfig {
NAME_MC_NON_PREMIUM, PLAYER_OFFLINE_STATUS, PLAYER_ONLINE_STATUS, ARG_USER_IS_NOT_SYNC, USER_IS_NOT_SYNC,
DISCORD_USER_IS_SYNC;

public static List<String> COMMAND_ARGS_LIST,EMBED_INFO_ARGS, EMBED_PROFILE, BLOCKED_COMMANDS, IGNORE_FOLDERS;
public static List<String> COMMAND_ARGS_LIST,EMBED_INFO_ARGS, EMBED_PROFILE, BLOCKED_COMMANDS, IGNORE_FOLDERS, SYNC_ROLES;

public static boolean PLUGIN_ENABLE, DATABASE_SSL, CHAT_MODULE, CONSOLE_MODULE, DISCORD_CONSOLE_COMMANDS,
BLOCK_WARN_MESSAGES, BLOCK_ERROR_MESSAGES, DISCORD_INFO_MODULE;
BLOCK_WARN_MESSAGES, BLOCK_ERROR_MESSAGES, DISCORD_INFO_MODULE, SYNC_RANK_ENABLE, SYNC_NAME;

public static int EMBED_DISCORD_R, EMBED_DISCORD_G, EMBED_DISCORD_B, EMBED_MC_R, EMBED_MC_G, EMBED_MC_B, DATABASE_PORT,
EMBED_ERROR_R, EMBED_ERROR_G, EMBED_ERROR_B, EMBED_WARN_R, EMBED_WARN_G, EMBED_WARN_B, EMBED_SUCCESS_R,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import pl.norbit.discordmc.bot.embed.Embed;
import pl.norbit.discordmc.bot.utils.ChatUtil;
import pl.norbit.discordmc.utils.ChatUtil;
import pl.norbit.discordmc.server.config.PluginConfig;
import pl.norbit.discordmc.server.enums.Channel;
import pl.norbit.discordmc.server.objects.GamePlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public User getDiscordUser() {
public void setDiscordUser(String discordID) {

if(discordID != null) {
this.discordUser = jda.getUserById(discordID);
this.discordUser = jda.retrieveUserById(discordID).complete();
}else{
this.discordUser = null;
}
Expand Down
Loading

0 comments on commit aeb4983

Please sign in to comment.