Initial commit

This commit is contained in:
Scifi9902
2026-10-12 13:57:05 -03:00
commit 4bde3b176a
2576 changed files with 212659 additions and 0 deletions
+85
View File
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ProjectCranked</artifactId>
<groupId>net.crankedpvp</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>DiscordSync</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>false</shadedArtifactAttached>
<relocations>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>shaded.com.google</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_204</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>Utils</artifactId>
<groupId>net.crankedpvp</groupId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,102 @@
package net.crankedpvp.discordsync;
import me.mattstudios.mf.base.CommandManager;
import net.crankedpvp.discordsync.commands.SyncCommand;
import net.crankedpvp.discordsync.commands.UnSyncCommand;
import net.crankedpvp.discordsync.listeners.ChatListener;
import net.crankedpvp.discordsync.listeners.DiscordListener;
import net.crankedpvp.discordsync.managers.DatabaseManager;
import net.crankedpvp.discordsync.managers.SyncManager;
import net.crankedpvp.discordsync.utils.DiscordSettings;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import javax.security.auth.login.LoginException;
import java.sql.SQLException;
public class DiscordSyncPlugin extends JavaPlugin {
private static final String TOKEN = DiscordSettings.DISCORD_BOT_TOKEN;
private JDA jda;
private SyncManager syncManager;
private CommandManager commandManager;
private DatabaseManager databaseManager;
private DiscordListener discordListener;
private ChatListener chatListener;
public SyncManager getSyncManager() {
return this.syncManager;
}
public ChatListener getChatListener() {
return this.chatListener;
}
public JDA getJda() {
return this.jda;
}
public DatabaseManager getDatabaseManager() {
return this.databaseManager;
}
public void onEnable() {
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
try {
this.jda = (new JDABuilder()).setToken(TOKEN).setActivity(Activity.playing("play.crankedpvp.org")).setStatus(OnlineStatus.ONLINE).build();
} catch (LoginException var2) {
Bukkit.getScheduler().runTask(this, () -> {
Bukkit.getPluginManager().disablePlugin(this);
throw new RuntimeException("Shutting Discord Link plugin down, failed to initiate bot!", var2);
});
}
Bukkit.getScheduler().runTask(this, () -> {
this.commandManager = new CommandManager(this, true);
this.syncManager = new SyncManager(this);
this.chatListener = new ChatListener(this);
this.getServer().getPluginManager().registerEvents(this.chatListener, this);
this.commandManager.register(new SyncCommand(this));
this.commandManager.register(new UnSyncCommand(this));
this.discordListener = new DiscordListener(this);
this.jda.addEventListener(this.discordListener);
});
});
this.databaseManager = new DatabaseManager(this);
Bukkit.getScheduler().runTaskAsynchronously(this, ()-> {
try {
DiscordSyncPlugin.this.databaseManager.connect();
DiscordSyncPlugin.this.databaseManager.createTable();
Bukkit.getScheduler().runTaskLater(DiscordSyncPlugin.this, () -> {
DiscordSyncPlugin.this.databaseManager.fetchMap();
}, 5L);
DiscordSyncPlugin.this.databaseManager.autoReconnect(850);
} catch (Exception var2) {
var2.printStackTrace();
}
});
}
public void onDisable() {
this.jda.removeEventListener(this.discordListener);
try {
this.databaseManager.getConnection().close();
} catch (SQLException var2) {
var2.printStackTrace();
}
}
public CommandManager getCommandManager() {
return this.commandManager;
}
public DiscordListener getDiscordListener() {
return this.discordListener;
}
}
@@ -0,0 +1,62 @@
package net.crankedpvp.discordsync.commands;
import me.mattstudios.mf.annotations.Alias;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Default;
import me.mattstudios.mf.base.CommandBase;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.managers.SyncManager;
import net.dv8tion.jda.api.entities.User;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
@Command("sync")
@Alias({"syncaccount", "syncdiscord"})
public class SyncCommand extends CommandBase {
private DiscordSyncPlugin plugin;
private SyncManager syncManager;
public SyncCommand(DiscordSyncPlugin plugin) {
this.plugin = plugin;
this.syncManager = plugin.getSyncManager();
}
@Default
public void onCommand(Player player) {
if (!this.plugin.getDatabaseManager().isConnected()) {
player.sendMessage(CC.translate("&c&l(!)&c Account Sync is currently unavailable right now, please try again in a few."));
} else if (this.syncManager.isAccountLinked(player.getUniqueId())) {
player.sendMessage(CC.translate("&c&l(!) &cYour account is already linked to &f&n" + this.syncManager.getLinkedUser(player.getUniqueId()).getAsTag() + "&c. Type &f/unsync&c to unsync your account."));
} else {
int newCode;
if (this.syncManager.isCurrentlySyncing(player)) {
newCode = this.syncManager.getActiveSyncCode(player);
player.sendMessage(CC.translate("&a&l(!)&f Please send the &bCranked Sync Bot&f this message \"!verify " + newCode + "\"."));
} else {
newCode = this.syncManager.getRandomCode();
this.syncManager.getActiveSyncMap().put(newCode, player.getUniqueId());
List<String> messages = new ArrayList<>();
messages.add("");
messages.add("&b&lDISCORD ACCOUNT SYNC &8(Tutorial)");
messages.add("");
messages.add("&f* &7Join the Cranked /discord and find the &bCranked Sync Bot&7.");
messages.add("&f* &7Send the bot this message: &b'!verify " + newCode + "'&7.");
messages.add("");
messages.add("&7Once you have followed these steps, your account should be linked!");
messages.add("");
for (String message : messages) {
player.sendMessage(CC.translate(message));
}
}
}
}
}
@@ -0,0 +1,52 @@
package net.crankedpvp.discordsync.commands;
import me.mattstudios.mf.annotations.Alias;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Default;
import me.mattstudios.mf.base.CommandBase;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.managers.SyncManager;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.Objects;
@Command("unsync")
@Alias({"unsyncaccount", "unsyncaccount", "unsyncdiscord"})
public class UnSyncCommand extends CommandBase {
private final DiscordSyncPlugin plugin;
private final SyncManager syncManager;
public UnSyncCommand(DiscordSyncPlugin plugin) {
this.plugin = plugin;
this.syncManager = plugin.getSyncManager();
}
@Default
public void onCommand(Player player, String toUnsync) {
if (!player.isOp()) {
player.sendMessage(CC.translate("&c&l(!)&c Only administrators can unsync your account, open a #general-support ticket in the crankedpvp /discord!"));
return;
}
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(toUnsync);
if (!this.plugin.getDatabaseManager().isConnected()) {
player.sendMessage(CC.translate("&c&l(!)&c Account Sync is currently unavailable right now, please try again in a few."));
} else if (!this.syncManager.isAccountLinked(offlinePlayer.getUniqueId())) {
player.sendMessage(CC.translate("&c&l(!) &cTheir account is not synced!"));
} else {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> this.plugin.getDatabaseManager().remove(offlinePlayer.getUniqueId()));
player.sendMessage(CC.translate("&a&l(!) &fYou have &asuccessfully &funsync'ed " + offlinePlayer.getName() + "'s account"));
if (offlinePlayer.isOnline()) {
offlinePlayer.getPlayer().sendMessage(CC.translate("&c&l(!)&c Your account has been &funsync'ed &cby an administrator!"));
}
if (syncManager.isAccountLinked(offlinePlayer.getUniqueId())) {
this.plugin.getJda().getGuildById("470324147411615744").removeRoleFromMember(this.syncManager.getLinkedUser(offlinePlayer.getUniqueId()).getId(), Objects.requireNonNull(this.plugin.getJda().getGuildById("470324147411615744").getRoleById("980116575736696853"))).queue();
}
}
}
}
@@ -0,0 +1,157 @@
package net.crankedpvp.discordsync.listeners;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.utils.DiscordSettings;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.*;
public class ChatListener implements Listener {
public static final String LEADER_BADGE = CC.translate("&9&l❖");
public static final String BOOSTER_BADGE = CC.translate("&d&l✪");
private static final Set<UUID> nitroBoosterList = new HashSet<>();
private static final Set<UUID> factionLeaderList = new HashSet<>();
private static final Set<UUID> youtuberList = new HashSet<>();
private static final Set<UUID> famousList = new HashSet<>();
private static final Map<String, String> ranksAndPrefixes = new HashMap<String, String>() {{
put("", "");
}};
private final Guild crankedDiscord;
private final DiscordSyncPlugin plugin;
public ChatListener(DiscordSyncPlugin plugin) {
this.plugin = plugin;
this.crankedDiscord = plugin.getJda().getGuildById(DiscordSettings.SERVER_ID);
Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, ChatListener.this::updateNitroBoosters, 100L, 300*20);
}
public void updateNitroBoosters() {
if (this.plugin.getDatabaseManager().isConnected()) {
Map<UUID, Long> uuidUserMap = this.plugin.getDatabaseManager().fetchMap();
Set<UUID> toAddBoosters = new HashSet<>();
Set<UUID> toAddLeaders = new HashSet<>();
Set<UUID> toAddYoutubers = new HashSet<>();
Set<UUID> toAddFamous = new HashSet<>();
for (UUID uuid : uuidUserMap.keySet()) {
Long discordId = uuidUserMap.get(uuid);
User user = this.plugin.getJda().getUserById(discordId);
this.plugin.getJda().getGuilds().stream().filter((g) -> g.getName().equals(DiscordSettings.DISCORD_NAME)).findFirst().ifPresent((g) -> {
Member member = this.getMemberById(user, g);
if (user != null && member != null) {
if (member.getRoles().stream().anyMatch((r) -> r.getId().equals(DiscordSettings.NITRO_BOOSTER_ROLE))) {
toAddBoosters.add(uuid);
}
if (member.getRoles().stream().anyMatch((r) -> r.getId().equals(DiscordSettings.FACTION_LEADER_ROLE))) {
toAddLeaders.add(uuid);
}
if (member.getRoles().stream().anyMatch((r) -> r.getId().equals(DiscordSettings.MEDIA_ROLE))) {
toAddYoutubers.add(uuid);
}
if (member.getRoles().stream().anyMatch((r) -> r.getId().equals(DiscordSettings.FAMOUS_ROLE))) {
toAddFamous.add(uuid);
}
}
});
}
Bukkit.getScheduler().runTask(this.plugin, () -> factionLeaderList.addAll(toAddLeaders));
Bukkit.getScheduler().runTask(this.plugin, () -> nitroBoosterList.addAll(toAddBoosters));
Bukkit.getScheduler().runTask(this.plugin, () -> youtuberList.addAll(toAddYoutubers));
Bukkit.getScheduler().runTask(this.plugin, () -> famousList.addAll(toAddFamous));
Bukkit.getScheduler().runTaskLater(this.plugin, () -> {
for (Player player : Bukkit.getOnlinePlayers()) {
checkPermissions(player);
}
}, 20L);
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
checkPermissions(player);
}
private void checkPermissions(Player player) {
if (player.hasPermission("cranked.kit.nitro") && !isNitroBooster(player)) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.nitro false");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.nitro false");
} else if (isNitroBooster(player) && !player.hasPermission("cranked.kit.nitro")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.nitro true");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.nitro true");
}
if (player.hasPermission("cranked.kit.faction") && !isFactionLeader(player)) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.faction false");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.faction false");
} else if (isFactionLeader(player) && !player.hasPermission("cranked.kit.faction")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.faction true");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " permission set cranked.kit.faction true"); }
if (player.hasPermission("cranked.kit.media") && !isYoutuber(player) && !player.isOp()) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " parent remove media");
} else if (isYoutuber(player) && !player.hasPermission("cranked.kit.media") && !player.isOp()) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getUniqueId() + " parent add media");
}
}
public Member getMember(User user) {
try {
return this.crankedDiscord.getMember(user);
} catch (Exception var3) {
return null;
}
}
public Member getMemberById(User user, Guild guild) {
try {
return guild.getMemberById(user.getId());
} catch (Exception var4) {
return null;
}
}
public static boolean isNitroBooster(Player player) {
return nitroBoosterList.contains(player.getUniqueId());
}
public static boolean isYoutuber(Player player) {
return youtuberList.contains(player.getUniqueId());
}
public static boolean isFamous(Player player) {
return famousList.contains(player.getUniqueId());
}
public static boolean isFactionLeader(Player player) {
return factionLeaderList.contains(player.getUniqueId());
}
public Set<UUID> getNitroBoosterList() {
return nitroBoosterList;
}
}
@@ -0,0 +1,57 @@
package net.crankedpvp.discordsync.listeners;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.utils.DiscordSettings;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import java.util.Objects;
import java.util.UUID;
public class DiscordListener implements EventListener {
private final DiscordSyncPlugin plugin;
public DiscordListener(DiscordSyncPlugin plugin) {
this.plugin = plugin;
}
public void onEvent(GenericEvent gEvent) {
if (gEvent instanceof PrivateMessageReceivedEvent) {
PrivateMessageReceivedEvent event = (PrivateMessageReceivedEvent)gEvent;
User user = event.getAuthor();
String message = event.getMessage().getContentStripped();
if (message.startsWith("!verify ")) {
int codeEntered = Integer.parseInt(message.split(" ")[1]);
if (this.plugin.getDatabaseManager().contains(user.getIdLong())) {
event.getChannel().sendMessage("Your discord account is already linked!").queue();
} else if (!this.plugin.getSyncManager().isActiveCode(codeEntered)) {
event.getChannel().sendMessage("You have entered an invalid code!").queue();
} else {
UUID playerId = this.plugin.getSyncManager().getActiveSyncMap().get(codeEntered);
Player player = Bukkit.getPlayer(playerId);
if (player == null) {
event.getChannel().sendMessage("That player isn't online anymore!").queue();
} else {
event.getChannel().sendMessage("Congrats! You have successfully linked your account to " + player.getName() + " on CrankedPvP! It can take up to 5 minutes to apply ingame.").queue();
Objects.requireNonNull(Objects.requireNonNull(this.plugin.getJda().getGuildById(DiscordSettings.SERVER_ID)).getMember(user)).modifyNickname(player.getName()).queue();
this.plugin.getJda().getGuildById(DiscordSettings.SERVER_ID).addRoleToMember(user.getId(), Objects.requireNonNull(this.plugin.getJda().getGuildById(DiscordSettings.SERVER_ID).getRoleById(DiscordSettings.SYNCED_ROLE))).queue();
this.plugin.getSyncManager().getActiveSyncMap().remove(codeEntered, playerId);
player.sendMessage(CC.translate("&a&l(!)&a You have successfully linked your account to &f&n" + user.getAsTag() + "&a."));
player.playSound(player.getLocation(),Sound.ORB_PICKUP, 10, 10);
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
this.plugin.getDatabaseManager().insert(playerId, user.getIdLong());
});
}
}
}
}
}
}
@@ -0,0 +1,190 @@
package net.crankedpvp.discordsync.managers;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.utils.DiscordSettings;
import org.bukkit.scheduler.BukkitRunnable;
import java.sql.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class DatabaseManager {
private static final String TABLE_NAME = "discord_data";
private Connection connection;
private final DiscordSyncPlugin plugin;
private final Map<UUID, Long> cachedResults = new HashMap<>();
public Connection getConnection() {
return this.connection;
}
public DatabaseManager(DiscordSyncPlugin plugin) {
this.plugin = plugin;
}
public void autoReconnect(int interval) {
(new BukkitRunnable() {
public void run() {
System.out.println("Running Auto Reconnect");
try {
if (DatabaseManager.this.plugin.getDatabaseManager().isConnected()) {
System.out.println("DB is currently connected, closing the connection...");
DatabaseManager.this.plugin.getDatabaseManager().getConnection().close();
System.out.println("Disconnected from DB.");
} else {
System.out.println("DB is currently NOT connected, opening new connection.");
}
System.out.println("Opening a new connection.");
DatabaseManager.this.plugin.getDatabaseManager().connect();
System.out.println("New connection opened successfully.");
} catch (Exception var2) {
var2.printStackTrace();
System.out.println("Error in console.");
}
}
}).runTaskTimerAsynchronously(this.plugin, interval * 20L, interval * 20L);
}
public boolean isConnected() {
try {
return this.connection != null && !this.connection.isClosed();
} catch (SQLException var2) {
var2.printStackTrace();
return false;
}
}
public void insert(UUID uuid, long discordId) {
try {
PreparedStatement insert = this.connection.prepareStatement("INSERT INTO discord_data (uuid,discordId) VALUES (?,?)");
insert.setString(1, uuid.toString());
insert.setLong(2, discordId);
insert.executeUpdate();
} catch (Exception var5) {
var5.printStackTrace();
}
}
public void remove(UUID uuid) {
try {
PreparedStatement remove = this.connection.prepareStatement("DELETE FROM discord_data WHERE UUID=?");
remove.setString(1, uuid.toString());
remove.executeUpdate();
this.cachedResults.remove(uuid);
} catch (SQLException var3) {
var3.printStackTrace();
}
}
public Long get(UUID uuid) throws Exception {
if (this.cachedResults.containsKey(uuid)) {
return this.cachedResults.get(uuid);
} else {
PreparedStatement statement = this.connection.prepareStatement("SELECT * FROM discord_data WHERE uuid=?");
statement.setString(1, uuid.toString());
ResultSet results = statement.executeQuery();
if (!results.next()) {
return null;
} else {
Long discordId = results.getLong("discordId");
this.cachedResults.put(uuid, discordId);
return discordId;
}
}
}
public boolean contains(UUID uuid) {
if (this.cachedResults.containsKey(uuid)) {
return true;
} else {
try {
return this.get(uuid) != null;
} catch (Exception var3) {
var3.printStackTrace();
return false;
}
}
}
public boolean contains(Long discordId) {
try {
PreparedStatement statement = this.connection.prepareStatement("SELECT * FROM discord_data WHERE discordId=?");
statement.setLong(1, discordId);
ResultSet resultSet = statement.executeQuery();
return resultSet.next();
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public Map fetchMap() {
try {
PreparedStatement statement = this.connection.prepareStatement("SELECT * FROM discord_data");
ResultSet results = statement.executeQuery();
Map<UUID, Long> fetchedMap = new HashMap<>();
while(results.next()) {
try {
UUID uuid = UUID.fromString(results.getString("uuid"));
Long discordId = results.getLong("discordId");
fetchedMap.put(uuid, discordId);
this.cachedResults.putIfAbsent(uuid, discordId);
} catch (Exception var6) {
System.out.println("error while mapping data");
}
}
return fetchedMap;
} catch (Exception var7) {
var7.printStackTrace();
return Collections.EMPTY_MAP;
}
}
/** @deprecated */
@Deprecated
public ResultSet executeAsync(String sql) {
return null;
}
public void connect() throws Exception {
synchronized(this) {
if (this.getConnection() != null && !this.getConnection().isClosed()) {
System.out.println("Current connect isn't closed!");
} else {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException ignored) {
Class.forName("com.mysql.jdbc.Driver");
}
//this.connection = DriverManager.getConnection("jdbc:mysql://u4_1KRTto7gWF:96+HGu!c8h5Tt0PZ^5Nc=l3+@127.0.0.1:3306/s4_DiscordSync");
this.connection = DriverManager.getConnection("jdbc:mysql://172.18.0.1:3306/s4_discordsync", DiscordSettings.DATABASE_USERNAME, DiscordSettings.DATABASE_PASSWORD);
System.out.println("DB connected!");
}
}
}
public void createTable() throws SQLException {
String sqlCreate = "CREATE TABLE IF NOT EXISTS `discord_data` (\t`uuid` VARCHAR(36),\n\t`discordId` BIGINT\n);";
Statement statement = this.connection.createStatement();
statement.execute(sqlCreate);
statement.close();
}
public DiscordSyncPlugin getPlugin() {
return this.plugin;
}
public Map<UUID, Long> getCachedResults() {
return this.cachedResults;
}
}
@@ -0,0 +1,73 @@
package net.crankedpvp.discordsync.managers;
import net.crankedpvp.discordsync.DiscordSyncPlugin;
import net.crankedpvp.discordsync.utils.DiscordSettings;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class SyncManager {
private final DiscordSyncPlugin plugin;
private final HashMap<Integer, UUID> activeSyncMap = new HashMap<>();
private final Guild crankedDiscord;
public HashMap<Integer, UUID> getActiveSyncMap() {
return this.activeSyncMap;
}
public SyncManager(DiscordSyncPlugin plugin) {
this.plugin = plugin;
this.crankedDiscord = plugin.getJda().getGuildById(DiscordSettings.SERVER_ID);
}
public int getRandomCode() {
int randomCode;
for(randomCode = 0; this.isActiveCode(randomCode) || randomCode == 0; randomCode = ThreadLocalRandom.current().nextInt(10000, 99999)) {
}
return randomCode;
}
public int getActiveSyncCode(Player player) {
return (Integer)this.activeSyncMap.keySet().stream().filter((code) -> ((UUID)this.activeSyncMap.get(code)).equals(player.getUniqueId())).findFirst().orElse(-1);
}
public User getLinkedUser(UUID uuid) {
if (!this.plugin.getDatabaseManager().contains(uuid)) {
return null;
} else {
try {
return this.plugin.getJda().getUserById(this.plugin.getDatabaseManager().get(uuid));
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
}
public boolean isCurrentlySyncing(Player player) {
return this.activeSyncMap.containsValue(player.getUniqueId());
}
public boolean isAccountLinked(UUID uuid) {
return this.getLinkedUser(uuid) != null;
}
public boolean isActiveCode(int code) {
return this.activeSyncMap.containsKey(code);
}
public DiscordSyncPlugin getPlugin() {
return this.plugin;
}
public Guild getCrankedDiscord() {
return this.crankedDiscord;
}
}
@@ -0,0 +1,15 @@
package net.crankedpvp.discordsync.utils;
public class DiscordSettings {
public static final String NITRO_BOOSTER_ROLE = "1020069304965795891";
public static final String FACTION_LEADER_ROLE = "1020057161994346632";
public static final String MEDIA_ROLE = "1020057161994346634";
public static final String FAMOUS_ROLE = "1020057161994346635";
public static final String SYNCED_ROLE = "1020057161948217504";
public static final String DISCORD_NAME = "CrankedPvP - Factions";
public static final String SERVER_ID = "1020057161948217495";
public static final String DISCORD_BOT_TOKEN = "MTAzOTk1NTkzNzA0MzE2MTE3OA.G3pu91.BnteWWBhtiueVMKi0Ydh4hla0gYXVbfXqxaFoI";
public static final String DATABASE_USERNAME = "u4_ViERKcPbaQ";
public static final String DATABASE_PASSWORD = "!Hjzx42w@Of0E0nQUT9oAQ@q";
}
@@ -0,0 +1,7 @@
name: CrankedDiscordSync
main: net.crankedpvp.discordsync.DiscordSyncPlugin
version: ${git.total.commit.count}.0
description: "§fCommit ID: §a${git.commit.id.abbrev}"
website: https://crankedpvp.net/
depend:
- CrankedUtils