diff --git a/build.gradle b/build.gradle index c9dccad..0336da2 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,7 @@ dependencies { compileOnly "com.github.Anuken.Mindustry:server:$mindustryVersion" implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' implementation "org.javacord:javacord:3.8.0" + implementation "org.mongodb:mongodb-driver:3.12.7" //implementation 'org.mariadb.jdbc:mariadb-java-client:3.1.2' //implementation 'com.theokanning.openai-gpt3-java:service:0.11.1' //implementation 'org.springframework.boot:spring-boot-starter-web' diff --git a/configTemplate/config.json b/configTemplate/config.json index 1b6eac8..2acb9e1 100644 --- a/configTemplate/config.json +++ b/configTemplate/config.json @@ -1,5 +1,6 @@ { "token": "bot token", "logchannelid": "channel where messages will be logged", - "banlogchannelid": "channel where bans will be logged" + "banlogchannelid": "channel where bans will be logged", + "mongodburl": "Mongo Database URL connection" } diff --git a/src/main/java/plugin/ConfigJson.java b/src/main/java/plugin/ConfigJson.java index 4caa0bd..a300067 100644 --- a/src/main/java/plugin/ConfigJson.java +++ b/src/main/java/plugin/ConfigJson.java @@ -15,6 +15,7 @@ public class ConfigJson { public static String token; public static String banlogchannelid; public static String logchannelid; + public static String mongodburl; // reads variables from config.json public static void read() throws IOException, ParseException { try { @@ -23,6 +24,7 @@ public static void read() throws IOException, ParseException { token = (String) object.get("token"); logchannelid = (String) object.get("logchannelid"); banlogchannelid = (String) object.get("banlogchannelid"); + mongodburl = (String) object.get("mongodburl"); }catch(Exception e){ Log.err("Skill issue: " + e); } diff --git a/src/main/java/plugin/DataHandler.java b/src/main/java/plugin/DataHandler.java new file mode 100644 index 0000000..1c3022a --- /dev/null +++ b/src/main/java/plugin/DataHandler.java @@ -0,0 +1,18 @@ +package plugin; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +public class DataHandler { + public static MongoClient mongoClient; + public static void mongodb() { + mongoClient = MongoClients.create(ConfigJson.mongodburl); + MongoDatabase db = mongoClient.getDatabase("mindustry"); + MongoCollection playerCollection = db.getCollection("players"); + } + + +} diff --git a/src/main/java/plugin/Ploogin.java b/src/main/java/plugin/Ploogin.java index ae7ff97..4afe1fd 100644 --- a/src/main/java/plugin/Ploogin.java +++ b/src/main/java/plugin/Ploogin.java @@ -2,26 +2,36 @@ import arc.*; import arc.util.*; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Filters; import mindustry.Vars; import mindustry.game.*; import mindustry.gen.*; import mindustry.mod.Plugin; import mindustry.gen.Player; +import org.bson.Document; +import org.bson.types.ObjectId; import org.javacord.api.*; import org.javacord.api.entity.intent.Intent; import org.json.simple.parser.ParseException; import plugin.discord.Bot; import plugin.ConfigJson; import plugin.utils.Utilities; +import plugin.DataHandler.*; import java.io.IOException; +import java.util.Map; -import static mindustry.Vars.mods; -import static mindustry.Vars.player; +import static com.mongodb.client.model.Filters.eq; +import static mindustry.Vars.*; +import static plugin.DataHandler.mongoClient; +import static plugin.DataHandler.mongodb; public class Ploogin extends Plugin implements ApplicationListener{ - + MongoDatabase db = mongoClient.getDatabase("mindustry"); + MongoCollection playerCollection = db.getCollection("players"); public static Player victim; public static String reason; public static Player moderator; @@ -30,10 +40,26 @@ public class Ploogin extends Plugin implements ApplicationListener{ public Ploogin() throws IOException, ParseException { ConfigJson.read(); Bot.load(); + mongodb(); + } + public void MongoDbPlayerCreation(Player eventPlayer){ + var id = new ObjectId(); + Document plrDoc = new Document("_id", id); + plrDoc.append("uuid", eventPlayer.uuid()); + plrDoc.append("lastBan", "0"); + Document chk = playerCollection.find(Filters.eq(eventPlayer.uuid())).first(); + if (chk == null){ + playerCollection.insertOne(plrDoc); + } else { + return; + } } // starts once plugin is started public void init() { Log.info("Plugin started!"); + Events.on(EventType.PlayerJoin.class, event -> + MongoDbPlayerCreation(event.player) + ); } // registers commands for client such as /ping @Override