Skip to content

Commit

Permalink
finally
Browse files Browse the repository at this point in the history
  • Loading branch information
NazrinNya committed Jul 5, 2023
1 parent 8c6abf6 commit 5a40102
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 76 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compileOnly "com.github.Anuken.Mindustry:core:$mindustryVersion"
compileOnly "com.github.Anuken.Mindustry:server:$mindustryVersion"
implementation "com.github.xzxadixzx.useful-stuffs:bundle:3c4241ca6d"
implementation "com.github.xzxadixzx.useful-stuffs:menu:3c4241ca6d"
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-sync:4.10.1"
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/plugin/ConfigJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ConfigJson {
public static String banlogchannelid;
public static String logchannelid;
public static String mongodburl;
public static String discordurl;
// reads variables from config.json
public static void read() throws IOException, ParseException {
try {
Expand All @@ -25,6 +26,7 @@ public static void read() throws IOException, ParseException {
logchannelid = (String) object.get("logchannelid");
banlogchannelid = (String) object.get("banlogchannelid");
mongodburl = (String) object.get("mongodburl");
discordurl = (String) object.get("discordurl");
}catch(Exception e){
Log.err("Skill issue: " + e);
}
Expand Down
112 changes: 47 additions & 65 deletions src/main/java/plugin/Ploogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import mindustry.Vars;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.gen.Call;
import mindustry.gen.Groups;
import mindustry.mod.Plugin;
import mindustry.gen.Player;
import org.bson.Document;
import org.bson.conversions.Bson;
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 useful.Bundle;


import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import static com.mongodb.client.model.Filters.eq;
import static mindustry.Vars.*;

import static plugin.commands.BanMenu.loadBanMenu;
import static plugin.functions.MongoDB.MongoDbPlayerCreation;
import static plugin.functions.MongoDB.MongoDbPlayerRankCheck;


public class Ploogin extends Plugin implements ApplicationListener{
Expand All @@ -40,6 +35,7 @@ public class Ploogin extends Plugin implements ApplicationListener{
public static Player victim;
public static String reason;
public static Player moderator;

public static long time;

// loads bot and other shit
Expand All @@ -50,37 +46,32 @@ public Ploogin() throws IOException, ParseException {
db = mongoClient.getDatabase("mindustry");
playerCollection = db.getCollection("players");
}
public void MongoDbPlayerCreation(Player eventPlayer){
var id = new ObjectId();
Document plrDoc = new Document("_id", id);
plrDoc.append("id", playerCollection.countDocuments());
plrDoc.append("uuid", eventPlayer.uuid());
plrDoc.append("lastBan", 0);
Document chk = playerCollection.find(Filters.eq("uuid", eventPlayer.uuid())).first();
if (chk == null){
playerCollection.insertOne(plrDoc);
} else {
return;
}
}

// starts once plugin is started
public void init() {
loadBanMenu();
Log.info("Plugin started!");
Bundle.defaultLocale = new Locale("en");
Events.on(EventType.PlayerJoin.class, event ->
MongoDbPlayerCreation(event.player)
);
Bundle.load(Ploogin.class);
Events.on(EventType.PlayerJoin.class, event -> {
Player plr = event.player;
MongoDbPlayerCreation(plr);
MongoDbPlayerRankCheck(plr);
});
Events.on(EventType.PlayerConnect.class, event -> {
Document user = playerCollection.find(Filters.eq("uuid", event.player.uuid())).first();
if (user == null){
return;
}
long lastBan = user.getLong("lastBan");
Date date = new Date();
if (lastBan > date.getTime()) {
String timeUntilUnban = Bundle.formatDate(lastBan - date.getTime());
String timeUntilUnban = Bundle.formatDuration(lastBan - date.getTime());
event.player.con.kick("You have been banned! Wait " + timeUntilUnban + " more for unban!", 0);
}
});
}
// registers commands for client such as /ping


@Override
public void registerClientCommands(CommandHandler handler){
handler.<Player>register("announce", "<text...>", "calls an announce", (args, player)->{
Expand All @@ -94,13 +85,19 @@ public void registerClientCommands(CommandHandler handler){
if (!player.admin) {
player.sendMessage("[red]You do not have enough permissions!");
} else {
Events.fire(new EventType.GameOverEvent(Team.derelict));
Events.fire(new EventType.GameOverEvent(Team.derelict));
}
});
handler.<Player>register("list", "Lists all players on the server", (args, player) -> {
Groups.player.each(player1 -> player.sendMessage("Name: " + player1.name + "; [white]id: " + player1.id()));
StringBuilder list = new StringBuilder();
for (Player plr : Groups.player){
Document user = playerCollection.find(Filters.eq("uuid", plr.uuid())).first();
int id = user.getInteger("id");
list.append(plr.name()+ "; [white]ID: " + id + "\n");
}
player.sendMessage(String.valueOf(list));
});
handler.<Player>register("js", "<code...>", "Execute JavaScript code.", (args, player) -> {
/*handler.<Player>register("js", "<code...>", "Execute JavaScript code.", (args, player) -> {
if (player.admin()) {
try {
String output = mods.getScripts().runConsole(args[0]);
Expand All @@ -112,36 +109,7 @@ public void registerClientCommands(CommandHandler handler){
} else {
player.sendMessage("[scarlet]You must be admin to use this command.");
}
});
handler.<Player>register("ban", "<player> <seconds> <reason...>", "Bans the players", (args,player) -> {
String id = args[0];
reason = args[2];
victim = Utilities.findPlayerByName(id);
moderator = player;
time = Long.parseLong(args[1]);
if (victim == null){
player.sendMessage("[red]This player doesnt exist!");
return;
}
if (victim.admin()){
player.sendMessage("[red]You cant ban an admin!");
return;
}
if (victim == player){
player.sendMessage("[red]You cant ban yourself!");
return;
}
if (player.admin()){
Call.menu(player.con, plugin.utils.MenuHandler.banMenu, "Ban", "Are you sure you want to ban " + victim.plainName() + "?", new String[][]{{"Confirm ", "Cancel"}});
} else {
player.sendMessage("[red]Not enough permissions!");
}
});
}
// i dont know why it even exists
@Override
public void dispose(){
exit();
});*/
}
// shit to register console commands because yes
@Override
Expand All @@ -153,5 +121,19 @@ public void registerServerCommands(CommandHandler handler){
System.exit(0);
}, 1f);
});
handler.register("setrank", "<id> <rank>", "Sets rank to player", (args, params) -> {
int id = Integer.parseInt(args[0]);
int rankid = Integer.parseInt(args[1]);
Document user = playerCollection.find(Filters.eq("id", id)).first();
if (user == null){
Log.warn("This user doesnt exist!");
return;
}
Bson updates = Updates.combine(
Updates.set("rank", rankid)
);
playerCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
Log.info("Rank has been given!");
});
}
}
133 changes: 133 additions & 0 deletions src/main/java/plugin/commands/BanMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package plugin.commands;

import arc.Events;
import arc.util.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import mindustry.game.EventType.AdminRequestEvent;
import mindustry.game.Team;
import mindustry.gen.*;
import mindustry.net.Administration.TraceInfo;
import mindustry.net.Packets.KickReason;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import plugin.Ploogin;
import plugin.discord.Bot;
import useful.*;
import useful.text.TextInput;
import useful.State.StateKey;

import java.time.Duration;
import java.util.Date;

import static mindustry.Vars.*;

public class BanMenu {

public static final StateKey<Player> TARGET = new StateKey<>("target");
public static final StateKey<Integer> DURATION = new StateKey<>("duration");
public static Player moderator = Ploogin.moderator;

public static final TextInput
durationInput = new TextInput(),
reasonInput = new TextInput();

public static void loadBanMenu() {
net.handleServer(AdminRequestCallPacket.class, (con, packet) -> {
var player = con.player;
var other = packet.other;

if (!player.admin || other == null || (other.admin && other != player)) {
return;
}

var action = packet.action;
var params = packet.params;

Events.fire(new AdminRequestEvent(player, other, action));

switch (action) {
case wave -> {
logic.skipWave();
Log.info("&lc@ &fi&lk[&lb@&fi&lk]&fb has skipped the wave.", player.plainName(), player.uuid());
}

case ban -> durationInput.show(player, TARGET, other);

case kick -> {
other.kick(KickReason.kick);
Log.info("&lc@ &fi&lk[&lb@&fi&lk]&fb has kicked @ &fi&lk[&lb@&fi&lk]&fb.", player.plainName(), player.uuid(), other.plainName(), other.uuid());
}

case trace -> {
Call.traceInfo(player.con, other, new TraceInfo(
other.ip(),
other.uuid(),
other.con.modclient,
other.con.mobile,
other.getInfo().timesJoined,
other.getInfo().timesKicked,
other.getInfo().ips.toArray(String.class),
other.getInfo().names.toArray(String.class)
));

Log.info("&lc@ &fi&lk[&lb@&fi&lk]&fb has requested trace info of @ &fi&lk[&lb@&fi&lk]&fb.", player.plainName(), player.uuid(), other.plainName(), other.uuid());
}

case switchTeam -> {
if (params instanceof Team team)
other.team(team);
}
}
});

durationInput.transform(input -> {
input.title("Ban duration (in days)");
input.content("Write ban duration (in days)");

input.textLength(4);

input.result((view, text) -> {
if (!Strings.canParsePositiveInt(text)) {
Action.show().get(view);
return;
}

Action2.openWith(reasonInput, DURATION).get(view, Strings.parseInt(text));
});
});

reasonInput.transform(input -> {
input.title("Ban reason");
input.content("Write ban reason:");

input.textLength(64);
input.defaultText("No reason");

input.closed(Action.back());
input.result((view, text) -> {
var target = view.state.get(TARGET);
long duration = view.state.get(DURATION);
Document usr = Ploogin.playerCollection.find(Filters.eq("uuid", target.uuid())).first();
Date date = new Date();
long banTime = date.getTime() + duration*86400000;
String timeUntilUnban = Bundle.formatDuration(Duration.ofDays(duration));
target.con.kick("You have been banned for: " + text + ". Wait " + timeUntilUnban + " until unban!", 0);
Call.sendMessage(target.plainName() + " has been banned for: " + text);
Bson updates = Updates.combine(
Updates.set("lastBan", banTime)
);
Ploogin.playerCollection.updateOne(usr, updates, new UpdateOptions().upsert(true));
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Ban event")
.setDescription(target.plainName() + " has been banned for: " + text)
.addField("Moderator", moderator.plainName());
Bot.banchannel.sendMessage(embed);


});
});
}
}
14 changes: 7 additions & 7 deletions src/main/java/plugin/discord/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import arc.Core;
import arc.Events;
import arc.util.Log;
import com.mongodb.client.model.Filters;
import mindustry.Vars;
import mindustry.content.Blocks;
import mindustry.core.GameState;
Expand All @@ -12,6 +13,7 @@
import mindustry.gen.Groups;
import mindustry.maps.Map;
import mindustry.net.Packets;
import org.bson.Document;
import org.javacord.api.*;
import mindustry.mod.*;
import org.javacord.api.entity.channel.TextChannel;
Expand All @@ -22,15 +24,11 @@
import org.javacord.api.event.message.MessageCreateEvent;
import org.javacord.api.interaction.*;
import plugin.utils.Utilities;
import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.Collections;
import mindustry.gen.Player;

import plugin.ConfigJson;

import static arc.util.Strings.stripColors;
import static mindustry.Vars.*;
import static plugin.Ploogin.playerCollection;

public class Bot {
// variables for load function
Expand Down Expand Up @@ -144,10 +142,12 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
StringBuilder list = new StringBuilder();
list.append("```Players online: ").append(Groups.player.size()).append("\n\n");
for (Player player : Groups.player){
Document user = playerCollection.find(Filters.eq("uuid", player.uuid())).first();
int id = user.getInteger("id");
if (player.admin()){
list.append("# [A] " + player.plainName()).append("; ID: " + player.id()).append("\n");
list.append("# [A] " + player.plainName()).append("; ID: " + id).append("\n");
} else {
list.append("# " + player.plainName()).append("; ID: " + player.id()).append("\n");
list.append("# " + player.plainName()).append("; ID: " + id).append("\n");
}
}
list.append("```");
Expand Down
Loading

0 comments on commit 5a40102

Please sign in to comment.