first commit

This commit is contained in:
Ghoti 2019-02-14 00:34:59 -06:00
commit 0343a07dab
17 changed files with 1159 additions and 0 deletions

16
.classpath Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
bin/

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BCraftBot</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

167
dependency-reduced-pom.xml Normal file
View File

@ -0,0 +1,167 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FPMB</groupId>
<artifactId>FPMB</artifactId>
<version>2.2</version>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>soluvas-public-thirdparty</id>
<url>http://nexus.bippo.co.id/nexus/content/repositories/soluvas-public-thirdparty/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.0</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>2.3</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.10-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-config</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-event</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-protocol</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.10-SNAPSHOT</version>
<type>javadoc</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-config</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-event</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-protocol</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

130
pom.xml Normal file
View File

@ -0,0 +1,130 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>FPMB</groupId>
<artifactId>FPMB</artifactId>
<version>2.2</version>
<repositories>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>soluvas-public-thirdparty</id>
<url>http://nexus.bippo.co.id/nexus/content/repositories/soluvas-public-thirdparty/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.5.0_327</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>2.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -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<String> 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<String>();
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<String> ids = new ArrayList<String>();
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<String>();
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<String> ids = new ArrayList<String>();
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;
}
}

View File

@ -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!");
}
}

View File

@ -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);
}

View File

@ -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<String, Command> commands = new HashMap<String, Command>();
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();
}
}

View File

@ -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 <username>";
@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;
}
}

View File

@ -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<String> split = new ArrayList<String>();
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;
}
}
}

View File

@ -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 <username>");
((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 <username>");
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;
}
}

View File

@ -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();
}
}
}
}
}
}

View File

@ -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;
}
}

View File

@ -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 <username>