From 0343a07dab78c56f08911473f9be2b444d8d1246 Mon Sep 17 00:00:00 2001 From: thmsdy Date: Thu, 14 Feb 2019 00:34:59 -0600 Subject: [PATCH] first commit --- .classpath | 16 + .gitignore | 2 + .project | 23 ++ .settings/org.eclipse.jdt.core.prefs | 12 + .settings/org.eclipse.m2e.core.prefs | 4 + dependency-reduced-pom.xml | 167 ++++++++++ pom.xml | 130 ++++++++ src/main/java/com/fpghoti/bcraftbot/Main.java | 287 ++++++++++++++++++ .../fpghoti/bcraftbot/bot/BotListener.java | 30 ++ .../com/fpghoti/bcraftbot/bot/Command.java | 11 + .../com/fpghoti/bcraftbot/bot/ServerBot.java | 90 ++++++ .../bcraftbot/bot/command/AddMeCommand.java | 51 ++++ .../bcraftbot/bot/util/CommandParser.java | 41 +++ .../fpghoti/bcraftbot/command/AddExempt.java | 51 ++++ .../bcraftbot/listener/PlayerListener.java | 53 ++++ .../bcraftbot/sql/MySQLConnection.java | 181 +++++++++++ src/main/resources/plugin.yml | 10 + 17 files changed, 1159 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 dependency-reduced-pom.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/fpghoti/bcraftbot/Main.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/bot/BotListener.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/bot/Command.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/bot/util/CommandParser.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/command/AddExempt.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java create mode 100644 src/main/java/com/fpghoti/bcraftbot/sql/MySQLConnection.java create mode 100644 src/main/resources/plugin.yml diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..a6e7863 --- /dev/null +++ b/.classpath @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4df66e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +bin/ \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..3fe99dd --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + BCraftBot + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..df46a9a --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..14b697b --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..0e9ab7e --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,167 @@ + + + 4.0.0 + FPMB + FPMB + 2.2 + + + + true + src/main/resources + + + + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + maven-shade-plugin + 3.1.0 + + + package + + shade + + + true + + + + + + + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + + + jcenter + jcenter-bintray + http://jcenter.bintray.com + + + + false + + soluvas-public-thirdparty + http://nexus.bippo.co.id/nexus/content/repositories/soluvas-public-thirdparty/ + + + + + net.java.dev.jna + jna + 4.5.0 + pom + compile + + + org.json + json + 20171018 + pom + compile + + + net.sf.trove4j + trove4j + 3.0.3 + pom + compile + + + org.apache.commons + commons-collections4 + 4.1 + pom + compile + + + org.apache.commons + commons-lang3 + 3.6 + pom + compile + + + com.squareup.okhttp + okhttp + 2.7.5 + pom + compile + + + com.neovisionaries + nv-websocket-client + 2.3 + pom + compile + + + net.md-5 + bungeecord-api + 1.10-SNAPSHOT + provided + + + bungeecord-chat + net.md-5 + + + bungeecord-config + net.md-5 + + + bungeecord-event + net.md-5 + + + bungeecord-protocol + net.md-5 + + + guava + com.google.guava + + + + + net.md-5 + bungeecord-api + 1.10-SNAPSHOT + javadoc + provided + + + bungeecord-chat + net.md-5 + + + bungeecord-config + net.md-5 + + + bungeecord-event + net.md-5 + + + bungeecord-protocol + net.md-5 + + + guava + com.google.guava + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fc77439 --- /dev/null +++ b/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + FPMB + FPMB + 2.2 + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + + + jcenter + jcenter-bintray + http://jcenter.bintray.com + + + soluvas-public-thirdparty + http://nexus.bippo.co.id/nexus/content/repositories/soluvas-public-thirdparty/ + + false + + + + + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + + + package + + shade + + + true + + + + + + + + + ch.qos.logback + logback-core + 1.2.3 + + + ch.qos.logback + logback-classic + 1.2.3 + + + joda-time + joda-time + 2.3 + + + net.dv8tion + JDA + 3.5.0_327 + + + net.java.dev.jna + jna + 4.5.0 + pom + + + org.json + json + 20171018 + pom + + + net.sf.trove4j + trove4j + 3.0.3 + pom + + + org.apache.commons + commons-collections4 + 4.1 + pom + + + org.apache.commons + commons-lang3 + 3.6 + pom + + + com.squareup.okhttp + okhttp + 2.7.5 + pom + + + com.neovisionaries + nv-websocket-client + 2.3 + pom + + + org.bukkit + bukkit + 1.13.2-R0.1-SNAPSHOT + provided + + + \ No newline at end of file diff --git a/src/main/java/com/fpghoti/bcraftbot/Main.java b/src/main/java/com/fpghoti/bcraftbot/Main.java new file mode 100644 index 0000000..6c072db --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/Main.java @@ -0,0 +1,287 @@ +package com.fpghoti.bcraftbot; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +import com.fpghoti.bcraftbot.bot.ServerBot; +import com.fpghoti.bcraftbot.command.AddExempt; +import com.fpghoti.bcraftbot.listener.PlayerListener; +import com.fpghoti.bcraftbot.sql.MySQLConnection; + +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.User; + +public class Main extends JavaPlugin { + + private String sqlhost; + private String sqluser; + private String sqldatabase; + private String sqlpassword; + private String sqltable; + private String bottoken; + private String kickmsg; + private String rolename; + + private boolean stopTimer = false; + private boolean assignrole; + + private int mysqlTimer = 1140; + + private ArrayList exempt; + + private MySQLConnection sql; + + public static Logger log = Logger.getLogger("Minecraft"); + + ServerBot bot; + + public void onEnable() { + registerConfig(); + messageSet(); + getSettings(); + getServer().getPluginManager().registerEvents(new PlayerListener(this), this); + sql = new MySQLConnection(this,sqlhost,sqluser,sqlpassword,sqldatabase); + sql.connect(); + if(!sql.tableExists(sqltable)){ + log(Level.INFO, "Table not found. Creating new table..."); + sql.update("CREATE TABLE " + sqltable + " (DiscordID NOT NULL CHAR(18), MinecraftName VARCHAR(16), UUID CHAR(36), PRIMARY KEY (DiscordID);"); + log(Level.INFO, "Table created!"); + } + startReconnect(); + bot = new ServerBot(this); + bot.runBot(); + getCommand("AddExempt").setExecutor(new AddExempt(this)); + } + + public void onDisable() { + bot.shutDown(); + stopTimer = true; + sql.disconnect(); + } + + public void startReconnect() { + new BukkitRunnable(){ + public void run() { + if(!stopTimer) { + if(mysqlTimer >= 1200){ + sql.reconnect(); + mysqlTimer = 0; + }else{ + mysqlTimer++; + } + } + } + }.runTaskTimerAsynchronously(this, 1*20, 1*20); + } + + private void registerConfig() { + this.getConfig().options().copyDefaults(true); + config.options().copyHeader(true); + saveConfig(); + } + + public void log(Level level, String msg) { + log.log(level, "[BCraftBot] " + msg.replaceAll("§[0-9A-FK-OR]", "")); + } + + FileConfiguration config = this.getConfig(); + + public void messageSet(){ + if (config.get("Host") == null){ + config.createSection("Host"); + config.set("Host", "0.0.0.0"); + } + if (config.get("User") == null){ + config.createSection("User"); + config.set("User", "username"); + } + if (config.get("Password") == null){ + config.createSection("Password"); + config.set("Password", "pass12345"); + } + if (config.get("Database") == null){ + config.createSection("Database"); + config.set("Database", "dbname"); + } + if (config.get("TableName") == null){ + config.createSection("TableName"); + config.set("TableName", "bcraftbot"); + } + if (config.get("Bot-Token") == null){ + config.createSection("Bot-Token"); + config.set("Bot-Token", "inserttokenhere"); + } + if (config.get("ExemptUsernames") == null){ + config.createSection("ExemptUsernames"); + config.set("ExemptUsernames", "test1,test2,test3"); + } + if (config.get("KickMessage") == null){ + config.createSection("KickMessage"); + config.set("KickMessage", "You are either not a member of the Discord or have not yet verified your account!"); + } + if (config.get("KickMessage") == null){ + config.createSection("KickMessage"); + config.set("KickMessage", "You are either not a member of the Discord or have not yet verified your account!"); + } + if (config.get("AssignRole") == null){ + config.createSection("AssignRole"); + config.set("AssignRole", true); + } + if (config.get("RoleName") == null){ + config.createSection("RoleName"); + config.set("RoleName", "Craftee"); + } + this.saveConfig(); + } + + public void getSettings(){ + sqlhost = config.getString("Host"); + sqluser = config.getString("User"); + sqlpassword = config.getString("Password"); + sqldatabase = config.getString("Database"); + sqltable = config.getString("TableName"); + bottoken = config.getString("Bot-Token"); + setExemptList(config.getString("ExemptUsernames")); + kickmsg = config.getString("KickMessage"); + assignrole = config.getBoolean("AssignRole"); + rolename = config.getString("RoleName"); + } + + public void setExemptList(String s) { + exempt = new ArrayList(); + for(String item: s.split(",")) { + exempt.add(item); + } + } + + public String getTableName() { + return sqltable; + } + + public void addExempt(String username) { + String updated = config.getString("ExemptUsernames") + "," + username; + if (config.get("ExemptUsernames") != null){ + config.set("ExemptUsernames", updated); + this.saveConfig(); + }else { + log.severe("BCraftBot failed to update the exempt user config!"); + } + setExemptList(updated); + } + + public ServerBot getBot() { + return bot; + } + + public boolean isExempt(String name) { + for(String item: exempt) { + if(item.equalsIgnoreCase(name)) { + return true; + } + } + return false; + } + + public boolean assignRole() { + return assignrole; + } + + public String getAssignRoleName() { + return rolename; + } + + public String getKickMessage() { + return kickmsg; + } + + public String getBotToken() { + return bottoken; + } + + public MySQLConnection getSQL() { + return sql; + } + + public boolean isMember(Player p) { + String name = p.getName().toLowerCase(); + ArrayList ids = new ArrayList(); + ResultSet rs = sql.query("SELECT * FROM " + sqltable + " WHERE MinecraftName = " + name.toLowerCase()); + try { + while (rs.next()) { + ids.add(rs.getString("DiscordID")); + } + } catch (SQLException e) { + log(Level.SEVERE, "Error getting Discord IDs from database!"); + e.printStackTrace(); + } + for(String id : ids) { + User user = bot.getJDA().getUserById(id); + if(user != null) { + for(Guild guild : bot.getJDA().getGuilds()) { + if(guild.isMember(user)) { + return true; + } + } + } + } + + String uuid = p.getUniqueId().toString(); + ids = new ArrayList(); + rs = sql.query("SELECT * FROM " + sqltable + " WHERE UUID = " + uuid); + try { + while (rs.next()) { + ids.add(rs.getString("DiscordID")); + } + } catch (SQLException e) { + log(Level.SEVERE, "Error getting Discord IDs from database!"); + e.printStackTrace(); + } + for(String id : ids) { + User user = bot.getJDA().getUserById(id); + if(user != null) { + for(Guild guild : bot.getJDA().getGuilds()) { + if(guild.isMember(user)) { + sql.update("UPDATE " + sqltable + " SET MinecraftName = " + p.getName().toLowerCase() + " WHERE DiscordID = " + id + ";"); + return true; + } + } + } + } + + return false; + } + + public User getDiscordUser(Player p) { + String name = p.getName().toLowerCase(); + ArrayList ids = new ArrayList(); + ResultSet rs = sql.query("SELECT * FROM " + sqltable + " WHERE MinecraftName = " + name.toLowerCase()); + try { + while (rs.next()) { + ids.add(rs.getString("DiscordID")); + } + } catch (SQLException e) { + log(Level.SEVERE, "Error getting Discord IDs from database!"); + e.printStackTrace(); + } + for(String id : ids) { + User user = bot.getJDA().getUserById(id); + if(user != null) { + for(Guild guild : bot.getJDA().getGuilds()) { + if(guild.isMember(user)) { + return user; + } + } + } + } + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/BotListener.java b/src/main/java/com/fpghoti/bcraftbot/bot/BotListener.java new file mode 100644 index 0000000..5014ff2 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/bot/BotListener.java @@ -0,0 +1,30 @@ +package com.fpghoti.bcraftbot.bot; + +import java.util.logging.Level; + +import com.fpghoti.bcraftbot.bot.ServerBot; +import net.dv8tion.jda.core.events.ReadyEvent; +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; +import net.dv8tion.jda.core.hooks.ListenerAdapter; + +public class BotListener extends ListenerAdapter{ + + ServerBot bot; + + public BotListener(ServerBot bot) { + this.bot = bot; + } + + @Override + public void onMessageReceived(MessageReceivedEvent event){ + if(!event.getAuthor().isBot() && event.getMessage().getContentDisplay().startsWith("!") && event.getMessage().getAuthor().getId() != event.getJDA().getSelfUser().getId()){ + bot.handleCommand(bot.getCommandParser().parse(event.getMessage().getContentRaw().toLowerCase(), event)); + } + } + + @Override + public void onReady(ReadyEvent event){ + bot.log(Level.INFO, "Bot is ready!"); + } + +} diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/Command.java b/src/main/java/com/fpghoti/bcraftbot/bot/Command.java new file mode 100644 index 0000000..ec32422 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/bot/Command.java @@ -0,0 +1,11 @@ +package com.fpghoti.bcraftbot.bot; + +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; + +public interface Command { + + public boolean called(String[] args, MessageReceivedEvent event); + public void action(String[] args, MessageReceivedEvent event); + public String help(); + public void executed(boolean success, MessageReceivedEvent event); +} diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java b/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java new file mode 100644 index 0000000..3846263 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java @@ -0,0 +1,90 @@ +package com.fpghoti.bcraftbot.bot; + +import java.util.HashMap; +import java.util.logging.Level; + +import com.fpghoti.bcraftbot.Main; +import com.fpghoti.bcraftbot.bot.BotListener; +import com.fpghoti.bcraftbot.bot.Command; +import com.fpghoti.bcraftbot.bot.command.AddMeCommand; +import com.fpghoti.bcraftbot.bot.util.CommandParser; +import com.fpghoti.bcraftbot.sql.MySQLConnection; + +import net.dv8tion.jda.core.AccountType; +import net.dv8tion.jda.core.JDA; +import net.dv8tion.jda.core.JDABuilder; + +public class ServerBot { + + private Main plugin; + + private JDA jda; + + private CommandParser parser; + + private HashMap commands = new HashMap(); + + public ServerBot(Main plugin) { + this.plugin = plugin; + } + + public void runBot(){ + parser = new CommandParser(); + plugin.log(Level.INFO, "Initializing Discord Bot..."); + String token = plugin.getBotToken(); + plugin.log(Level.INFO, "Connecting bot to Discord..."); + try{ + jda = new JDABuilder(AccountType.BOT).setToken(token).buildBlocking(); + jda.addEventListener(new BotListener(this)); + jda.setAutoReconnect(true); + String link = "https://discordapp.com/oauth2/authorize?&client_id=" + jda.getSelfUser().getId(); + plugin.log(Level.INFO, "Connection successful!"); + plugin.log(Level.INFO, "You can add this bot to Discord using this link: "); + plugin.log(Level.INFO, link); + }catch(Exception e){ + e.printStackTrace(); + plugin.log(Level.SEVERE, "There was an issue connecting to Discord. Bot shutting down!"); + } + + commands.put("addme", new AddMeCommand(this)); + } + + public void handleCommand(CommandParser.CommandContainer cmd){ + if(commands.containsKey(cmd.invoke)){ + boolean safe = commands.get(cmd.invoke).called(cmd.args, cmd.event); + + if(safe){ + commands.get(cmd.invoke).action(cmd.args, cmd.event); + commands.get(cmd.invoke).executed(safe, cmd.event); + }else{ + commands.get(cmd.invoke).executed(safe, cmd.event); + } + } + } + + public MySQLConnection getSQL() { + return plugin.getSQL(); + } + + public String getTableName() { + return plugin.getTableName(); + } + + public void log(Level level, String msg) { + plugin.log(level, msg); + } + + public CommandParser getCommandParser() { + return parser; + } + + public JDA getJDA() { + return jda; + } + + + public void shutDown(){ + jda.shutdown(); + } + +} diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java b/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java new file mode 100644 index 0000000..032f341 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java @@ -0,0 +1,51 @@ +package com.fpghoti.bcraftbot.bot.command; + +import java.util.logging.Level; + +import com.fpghoti.bcraftbot.bot.Command; +import com.fpghoti.bcraftbot.bot.ServerBot; + +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; + +public class AddMeCommand implements Command{ + + private ServerBot bot; + + public AddMeCommand(ServerBot bot) { + this.bot = bot; + } + + private final String HELP = "USAGE: !addme "; + + @Override + public boolean called(String[] args, MessageReceivedEvent event) { + + return true; + } + + @Override + public void action(String[] args, MessageReceivedEvent event) { + if(args.length == 1) { + String name = args[0].toLowerCase(); + bot.log(Level.INFO, event.getAuthor().getName() + " issued a Discord Bot command: -addme " + name); + if(bot.getSQL().itemExists("DiscordID", event.getAuthor().getId(), bot.getTableName())) { + bot.getSQL().set("MinecraftName", name, "DiscordID", "=", event.getAuthor().getId(), bot.getTableName()); + }else { + bot.getSQL().update("INSERT INTO " + bot.getTableName() + " (DiscordID,MinecraftName) VALUES (\'" + event.getAuthor().getId() + "\',\'" + name + "\');"); + } + } + + } + + @Override + public String help() { + + return HELP; + } + + @Override + public void executed(boolean success, MessageReceivedEvent event) { + return; + } + +} diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/util/CommandParser.java b/src/main/java/com/fpghoti/bcraftbot/bot/util/CommandParser.java new file mode 100644 index 0000000..ea84251 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/bot/util/CommandParser.java @@ -0,0 +1,41 @@ +package com.fpghoti.bcraftbot.bot.util; + +import java.util.ArrayList; + +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; + +public class CommandParser { + public CommandContainer parse(String rw, MessageReceivedEvent e){ + ArrayList split = new ArrayList(); + String raw = rw; + String fixed = raw.replaceFirst("-", ""); + String[] splitf = fixed.split(" "); + for(String s: splitf){ + split.add(s); + } + String invoke = split.get(0); + String[] args = new String[split.size() - 1]; + split.subList(1, split.size()).toArray(args); + + return new CommandContainer(raw, fixed, splitf, invoke, args, e); + } + + public class CommandContainer{ + public final String raw; + public final String fixed; + public final String[] splitf; + public final String invoke; + public final String[] args; + public final MessageReceivedEvent event; + + public CommandContainer(String rw, String fixed, String[] splitf, String invoke, String[] args, MessageReceivedEvent e){ + this.raw = rw; + this.fixed = fixed; + this.splitf = splitf; + this.invoke = invoke; + this.args = args; + this.event = e; + + } + } +} diff --git a/src/main/java/com/fpghoti/bcraftbot/command/AddExempt.java b/src/main/java/com/fpghoti/bcraftbot/command/AddExempt.java new file mode 100644 index 0000000..6316374 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/command/AddExempt.java @@ -0,0 +1,51 @@ +package com.fpghoti.bcraftbot.command; + +import java.util.logging.Level; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.fpghoti.bcraftbot.Main; + +public class AddExempt implements CommandExecutor{ + + private Main plugin; + + public AddExempt(Main plugin) { + this.plugin = plugin; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args){ + if(label.equalsIgnoreCase("addexempt")){ + if(sender instanceof Player) { + if(!((Player)sender).hasPermission("bcraftbot.addexempt")) { + ((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] You do not have permission to do this!"); + return true; + } + if(args.length != 1) { + ((Player)sender).sendMessage(ChatColor.GOLD + "[BCraftBot] Usage: /addexempt "); + ((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] There was an issue with your syntax."); + return true; + } + }else { + if(args.length != 1) { + plugin.log(Level.INFO, "Usage: addexempt "); + plugin.log(Level.INFO, "There was an issue with your syntax."); + return true; + } + } + String name = args[0]; + plugin.addExempt(name); + if(sender instanceof Player) { + ((Player)sender).sendMessage(ChatColor.GREEN + "[BCraftBot] Player added to the exempt list!"); + }else { + plugin.log(Level.INFO, "Player added to the exempt list!"); + } + } + return true; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java new file mode 100644 index 0000000..c1ee3e7 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java @@ -0,0 +1,53 @@ +package com.fpghoti.bcraftbot.listener; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; + +import com.fpghoti.bcraftbot.Main; + +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.Role; +import net.dv8tion.jda.core.entities.User; + +public class PlayerListener implements Listener { + + private Main plugin; + + public PlayerListener(Main plugin){ + this.plugin = plugin; + } + + @EventHandler + public void onPlayerLogin(PlayerLoginEvent event){ + Player player = event.getPlayer(); + if(!plugin.isMember(player)) { + event.disallow(Result.KICK_OTHER, plugin.getKickMessage()); + }else { + if(plugin.assignRole()) { + for(Guild guild : plugin.getBot().getJDA().getGuilds()) { + + Role role = null; + for(Role r : guild.getRoles()) { + if(r.getName().equalsIgnoreCase(plugin.getAssignRoleName())) { + role = r; + } + } + + User user = plugin.getDiscordUser(player); + Member mem = guild.getMember(user); + + if(user != null && user.getMutualGuilds().contains(guild) && role != null) { + guild.getController().addRolesToMember(mem, role).queue(); + } + + + } + } + } + } + +} diff --git a/src/main/java/com/fpghoti/bcraftbot/sql/MySQLConnection.java b/src/main/java/com/fpghoti/bcraftbot/sql/MySQLConnection.java new file mode 100644 index 0000000..ca0d4a6 --- /dev/null +++ b/src/main/java/com/fpghoti/bcraftbot/sql/MySQLConnection.java @@ -0,0 +1,181 @@ +package com.fpghoti.bcraftbot.sql; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.logging.Level; + +import com.fpghoti.bcraftbot.Main; + +public class MySQLConnection{ + + private Main plugin; + + private Connection connection; + + private String host; + private String user; + private String password; + private String database; + + public MySQLConnection(Main plugin, String sqlhost, String sqluser, String sqlpassword, String sqldatabase) { + this.plugin = plugin; + this.host = sqlhost; + this.user = sqluser; + this.password = sqlpassword; + this.database = sqldatabase; + } + + public Connection getConnection(){ + return connection; + } + + public void connect(){ + + if (host.equalsIgnoreCase("") || host == null) { + plugin.log(Level.SEVERE, "You have not specified a host in the Main config!"); + } else if (user.equalsIgnoreCase("") || user == null) { + plugin.log(Level.SEVERE, "You have not specified a user in the Main config!"); + } else if (password.equalsIgnoreCase("") || password == null) { + plugin.log(Level.SEVERE, "You have not specified a password in the Main config!"); + } else if (database.equalsIgnoreCase("") || database == null) { + plugin.log(Level.SEVERE, "You have not specified a database in the Main config!"); + } else { + login(); + } + } + + public void disconnect(){ + try{ + if (getConnection() != null){ + connection.close(); + } + else{ + plugin.log(Level.SEVERE, "There was an issue with MySQL: Main is not currently connected to a database."); + } + } + catch (Exception e){ + plugin.log(Level.SEVERE, "There was an issue with MySQL: " + e.getMessage()); + } + connection = null; + } + + public void reconnect(){ + disconnect(); + connect(); + } + + public void login(){ + try{ + if (getConnection() != null){ + connection.close(); + } + } + catch (Exception e){} + connection = null; + try{ + connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database, user, password); + } + catch (Exception e){ + plugin.log(Level.SEVERE, "There was an issue with MySQL: " + e.getMessage()); + } + } + + public ResultSet query(String query){ + if (query == null) { + return null; + } + connect(); + ResultSet results = null; + try{ + Statement statement = getConnection().createStatement(); + results = statement.executeQuery(query); + } + catch (Exception e){ + plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage()); + plugin.log(Level.SEVERE,"Failed Query in MySQL using the following query input:"); + plugin.log(Level.SEVERE, query); + } + return results; + } + + public void update(String input){ + if (input == null){ + return; + } + connect(); + try{ + Statement statement = getConnection().createStatement(); + statement.executeUpdate(input); + statement.close(); + } + catch (Exception e){ + plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage()); + plugin.log(Level.SEVERE,"Failed to update MySQL using the following update input:"); + plugin.log(Level.SEVERE, input); + } + } + + public boolean tableExists(String tablename){ + if (tablename == null) { + return false; + } + try{ + if (getConnection() == null) { + return false; + } + if (getConnection().getMetaData() == null) { + return false; + } + ResultSet results = getConnection().getMetaData().getTables(null, null, tablename, null); + if (results.next()) { + return true; + } + } + catch (Exception localException) {} + return false; + } + + public boolean itemExists(String column, String data, String table){ + if (data != null) { + data = "'" + data + "'"; + } + try{ + ResultSet results = query("SELECT * FROM " + table + " WHERE " + column + "=" + data); + while (results.next()) { + if (results.getString(column) != null) { + return true; + } + } + } + catch (Exception localException) {} + return false; + } + + + public void set(String selected, Object object, String column, String equality, String data, String table){ + if (object != null) { + object = "'" + object + "'"; + } + if (data != null) { + data = "'" + data + "'"; + } + update("UPDATE " + table + " SET " + selected + "=" + object + " WHERE " + column + equality + data + ";"); + } + + public Object get(String selected, String column, String equality, String data, String table){ + if (data != null) { + data = "'" + data + "'"; + } + try{ + ResultSet rs = query("SELECT * FROM " + table + " WHERE " + column + equality + data); + if (rs.next()) { + return rs.getObject(selected); + } + } + catch (Exception localException) {} + return null; + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..8d5ce1a --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,10 @@ +main: com.fpghoti.bcraftbot.Main +version: 1.0 +api-version: 1.13 +name: BCraftBot +descripion: Discord-based whitelisting system for Minecraft +author: Ghoti_Mayo +commands: + AddExempt: + description: Add a player to the exempt list. Exempted users are permanently allowed to join, as long as they remain in the exempted list. + usage: /addexempt \ No newline at end of file