Finalized the Discord module
This commit is contained in:
@@ -1,118 +1,117 @@
|
|||||||
package com.redstoner.modules.discord;
|
package com.redstoner.modules.discord;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.IOException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import net.nemez.chatapi.click.Message;
|
|
||||||
|
|
||||||
import com.nemez.cmdmgr.Command;
|
import com.nemez.cmdmgr.Command;
|
||||||
import com.redstoner.annotations.Commands;
|
import com.redstoner.annotations.Commands;
|
||||||
import com.redstoner.annotations.Version;
|
import com.redstoner.annotations.Version;
|
||||||
import com.redstoner.misc.CommandHolderType;
|
import com.redstoner.misc.CommandHolderType;
|
||||||
|
import com.redstoner.misc.mysql.Config;
|
||||||
|
import com.redstoner.misc.mysql.MysqlHandler;
|
||||||
|
import com.redstoner.misc.mysql.elements.ConstraintOperator;
|
||||||
|
import com.redstoner.misc.mysql.elements.MysqlConstraint;
|
||||||
|
import com.redstoner.misc.mysql.elements.MysqlDatabase;
|
||||||
|
import com.redstoner.misc.mysql.elements.MysqlField;
|
||||||
|
import com.redstoner.misc.mysql.elements.MysqlTable;
|
||||||
|
import com.redstoner.misc.mysql.types.text.VarChar;
|
||||||
import com.redstoner.modules.Module;
|
import com.redstoner.modules.Module;
|
||||||
import com.redstoner.misc.JsonManager;
|
|
||||||
import com.redstoner.misc.Main;
|
import net.nemez.chatapi.click.Message;
|
||||||
|
|
||||||
@Commands(CommandHolderType.File)
|
@Commands(CommandHolderType.File)
|
||||||
@Version(major = 4, minor = 0, revision = 0, compatible = 4)
|
@Version(major = 4, minor = 0, revision = 0, compatible = 4)
|
||||||
public class Discord implements Module {
|
public class Discord implements Module {
|
||||||
|
private MysqlTable table;
|
||||||
private final String FILENAME = "discordTokens.json";
|
|
||||||
private final String DNE_LINK = "dne://";
|
private String inviteLink;
|
||||||
|
|
||||||
private JSONObject discordTables;
|
|
||||||
private JSONObject byToken;
|
|
||||||
private JSONObject byUUID;
|
|
||||||
|
|
||||||
private String joinLink;
|
|
||||||
private File savefile;
|
|
||||||
|
|
||||||
private final String tokenCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
private final String tokenCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
private SecureRandom rnd = new SecureRandom();
|
private SecureRandom rnd = new SecureRandom();
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onEnable() {
|
public boolean onEnable() {
|
||||||
savefile = new File(Main.plugin.getDataFolder(), FILENAME);
|
Config config;
|
||||||
|
try {
|
||||||
discordTables = JsonManager.getObject(savefile);
|
config = Config.getConfig("Discord.json");
|
||||||
|
} catch (IOException | org.json.simple.parser.ParseException e1) {
|
||||||
if (discordTables == null) {
|
e1.printStackTrace();
|
||||||
discordTables = new JSONObject();
|
|
||||||
discordTables.put("joinLink", DNE_LINK);
|
|
||||||
save();
|
|
||||||
}
|
|
||||||
|
|
||||||
Object joinLinkObject = discordTables.get("joinLink");
|
|
||||||
|
|
||||||
if (joinLinkObject == null || ((String) joinLinkObject).equals(DNE_LINK)) {
|
|
||||||
getLogger().error("Missing Join Link. Set: \"joinLink\" to \"_joinLink_\" in the discordTokens.json file.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
joinLink = (String) joinLinkObject;
|
if (config == null || !config.containsKey("database") || !config.containsKey("table")
|
||||||
|
|| !config.containsKey("inviteLink")) {
|
||||||
Object byTokenObject = discordTables.get("byToken");
|
getLogger().error("Could not load the Discord config file, disabling!");
|
||||||
|
config.put("database", "redstoner");
|
||||||
if (byTokenObject == null) {
|
config.put("table", "discord");
|
||||||
discordTables.put("byToken", new JSONObject());
|
config.put("inviteLink", "https://discord.gg/example");
|
||||||
discordTables.put("byUUID", new JSONObject());
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
byToken = (JSONObject) discordTables.get("byToken");
|
try {
|
||||||
byUUID = (JSONObject) discordTables.get("byUUID");
|
MysqlDatabase database = MysqlHandler.INSTANCE.getDatabase(config.get("database") + "?autoReconnect=true");
|
||||||
|
MysqlField uuid = new MysqlField("uuid", new VarChar(36), false);
|
||||||
|
MysqlField pass = new MysqlField("token", new VarChar(8), false);
|
||||||
|
database.createTableIfNotExists((String) config.get("table"), uuid, pass);
|
||||||
|
table = database.getTable(config.get("table"));
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
getLogger().error("Could not use the Discord config, aborting!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Command(hook = "discord")
|
@Command(hook = "discord")
|
||||||
public void discord(CommandSender sender) {
|
public void discord(CommandSender sender) {
|
||||||
Player p = (Player) sender;
|
Player p = (Player) sender;
|
||||||
String pUUID = p.getUniqueId().toString();
|
String pUUID = p.getUniqueId().toString().replaceAll("-", "");
|
||||||
|
|
||||||
Object tokenObject = byUUID.get(pUUID);
|
String token = null;
|
||||||
String token = tokenObject == null? null : (String) tokenObject;
|
int tries = 0;
|
||||||
|
|
||||||
if (token == null) {
|
while (token == null) {
|
||||||
|
|
||||||
token = randomToken(8);
|
token = randomToken(8);
|
||||||
Object UUIDObject = byToken.get(token);
|
Object[] results = table.get("token", new MysqlConstraint("token", ConstraintOperator.EQUAL, token));
|
||||||
|
|
||||||
while (UUIDObject != null) {
|
if (results.length > 0) {
|
||||||
token = randomToken(8);
|
token = null;
|
||||||
UUIDObject = byToken.get(token);
|
tries++;
|
||||||
}
|
}
|
||||||
byUUID.put(pUUID, token);
|
|
||||||
byToken.put(token, pUUID);
|
if (tries > 10)
|
||||||
save();
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (token == null) {
|
||||||
|
// Someone please check my math and remove this comment lmao
|
||||||
|
new Message(sender, null).appendText(
|
||||||
|
"\n&4Could not find an unused token in 10 tries (a 1 in 2.462267087×10^143 chance)! Please try running this command again!")
|
||||||
|
.send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.delete(new MysqlConstraint("uuid", ConstraintOperator.EQUAL, pUUID));
|
||||||
|
table.insert(pUUID, token);
|
||||||
|
|
||||||
new Message(sender, null).appendText("\n&cRedstoner&7 has a &2Discord&7 Now! \nClick ")
|
new Message(sender, null).appendText("\n&cRedstoner&7 has a &2Discord&7 Now! \nClick ")
|
||||||
.appendLinkHover("&e" + joinLink, joinLink, "&aClick to Join")
|
.appendLinkHover("&e" + inviteLink, inviteLink, "&aClick to Join")
|
||||||
.appendText("&7 to join. \n\nTo sync you rank, copy ")
|
.appendText("&7 to join. \n\nTo sync you rank, copy ")
|
||||||
.appendSuggestHover("&e" + token, token, "&aClick to Copy")
|
.appendSuggestHover("&e" + token, token, "&aClick to Copy")
|
||||||
.appendText("&7 into &3#rank-sync&7.\n")
|
.appendText("&7 into &3#rank-sync&7.\n")
|
||||||
.send();
|
.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String randomToken(int length){
|
private String randomToken(int length) {
|
||||||
StringBuilder sb = new StringBuilder( length );
|
StringBuilder sb = new StringBuilder(length);
|
||||||
for( int i = 0; i < length; i++ )
|
|
||||||
sb.append( tokenCharacters.charAt( rnd.nextInt(tokenCharacters.length()) ) );
|
for (int i = 0; i < length; i++) {
|
||||||
return sb.toString();
|
sb.append(tokenCharacters.charAt(rnd.nextInt(tokenCharacters.length())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
return sb.toString();
|
||||||
private void save() {
|
|
||||||
|
|
||||||
discordTables.put("byToken", byToken);
|
|
||||||
discordTables.put("byUUID", byUUID);
|
|
||||||
JsonManager.save(discordTables, savefile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user