Skip to content

Commit

Permalink
added a featutes for connecting minecraft/discord accounts (probably …
Browse files Browse the repository at this point in the history
…not working yet)
  • Loading branch information
maramowicz committed Nov 26, 2023
1 parent 1941562 commit 1ca1ec2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/main/java/de/olivermakesco/bta_utils/core/PlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.olivermakesco.bta_utils.core;

import java.io.File;
import java.util.ArrayList;

import com.badlogic.gdx.utils.Array;

public class PlayerManager {
ArrayList<Player> players = new ArrayList<Player>();
public PlayerManager() {
init();
}

public void init() {
File files[] = DataManager.findFilesWithNameRecursively("perPlayer", "data");
for (File file : files) {
Player player = new Player(file);
players.add(player);
}
}

}
//Create new class Player containing username, isConnected and discordID
class Player {
public String username;
public boolean isConnected;
public String discordID;
File file;
public Player(String username, boolean isConnected, String discordID) {
this.username = username;
this.isConnected = isConnected;
this.discordID = discordID;
}
public Player(File file) {
this.file = file;
this.username = DataManager.getString(file, "username");
this.isConnected = stringToBoolean(DataManager.getString(file, "connected"));
this.discordID = DataManager.getString(file, "discordID");
}


boolean stringToBoolean(String s) {
return s.equals("true");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.olivermakesco.bta_utils.server;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import de.olivermakesco.bta_utils.core.DataManager;
import net.dv8tion.jda.api.JDA;
Expand All @@ -14,16 +16,17 @@ public static void initCommands(JDA jda) {
jda.updateCommands()
.addCommands(Commands.slash("ping", "Gives the current ping"))
.addCommands(Commands
.slash("dconnect", "Allow you to connect account on Minecraft server with account on Discord")
.slash("mdpair", "Allow you to connect account on Minecraft server with account on Discord")
.addOption(OptionType.STRING, "key", "connection key", true, false))
.addCommands(Commands.slash("mdunpair", "unpair discord and minecraft accounts"))
.queue();
}

@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if (event.getName().equals("ping")) {
event.reply("Pong! :D").queue();
} else if (event.getName().equals("dconnect")) {
} else if (event.getName().equals("mdpair")) {
String username = event.getUser().getName();
if (username.contains(".") || username.contains(";") || username.contains("/") || username.contains("\\")) {
event.reply(
Expand Down Expand Up @@ -79,6 +82,25 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
event.reply("Confirming the confirmation of your unconfirmed confirmation.").queue();
}
event.reply("Cannot find any request with that ID, did you rewrted your ID correctly?").queue();
} else if (event.getName().equals("mdunpair")) {
String id = event.getUser().getId();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
File[] files = DataManager.findFilesWithNameRecursively("perPlayer", "data");
for (int i = 0; i < files.length; i++) {
if (id.equals(DataManager.getString(files[i], "discordUserID"))) {
DataManager.setString(files[i], "connected", "false");
event.reply("Accounts unpaired!").queue();
return;
}
}
} catch (Exception e) {
event.reply("Error while initiating the connection.").queue();
}
return;
});
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
public class MinecraftDConnect extends ServerCommand {

public MinecraftDConnect(MinecraftServer server) {
super(server, "dconnect", new String[0]);
super(server, "mdpair", new String[0]);
}

@Override
public boolean execute(CommandHandler handler, CommandSender sender, String[] args) {
String username = sender.getPlayer().username;
if (username.contains(".")||username.contains(";")||username.contains("/")||username.contains("\\")) {
sender.sendMessage("Did you know your username is Illegal? Yes, we are not wrong, the only characters you can use in Minecraft username are: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ so first change your username to normal, then try again.");
return false;
}
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
Expand All @@ -45,7 +46,7 @@ public boolean execute(CommandHandler handler, CommandSender sender, String[] ar
}
}, throwable -> sender.sendMessage("Unknown user, did you paste your Discord UserID?"));
} catch (Exception e) {
sender.sendMessage("Error while initiating the connection.");
sender.sendMessage("Usage: /mdpair DiscordID");
}
});
return true;
Expand All @@ -58,7 +59,7 @@ public boolean opRequired(String[] var1) {

@Override
public void sendCommandSyntax(CommandHandler handler, CommandSender sender) {
sender.sendMessage("/dconnect DiscordUserID");
sender.sendMessage("/mdpair DiscordUserID");
}

private String getRandomHexString(int numchars){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.olivermakesco.bta_utils.server;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import de.olivermakesco.bta_utils.core.DataManager;
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
import net.minecraft.core.net.command.ServerCommand;
import net.minecraft.server.MinecraftServer;

public class MinecraftDDisconnect extends ServerCommand {

public MinecraftDDisconnect(MinecraftServer server) {
super(server, "mdunpair", new String[0]);
}

@Override
public boolean execute(CommandHandler handler, CommandSender sender, String[] args) {
String username = sender.getPlayer().username;
if (username.contains(".") || username.contains(";") || username.contains("/") || username.contains("\\")) {
sender.sendMessage(
"Did you know your username is Illegal? Yes, we are not wrong, the only characters you can use in Minecraft username are: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ so first change your username to normal, then try again.");
return true;
}
ExecutorService executor = Executors.newSingleThreadExecutor();
//if there one argument in args and ""confirm".equals(args[0])"
if (args.length == 1 && "confirm".equals(args[0])) {
executor.submit(() -> {
try {
File[] files = DataManager.findFilesWithNameRecursively("perPlayer", "data");
for (int i = 0; i < files.length; i++) {
if (username.equals(DataManager.getString(files[i], "username"))) {
if ("false".equals(DataManager.getString(files[i], "connected"))) {
sender.sendMessage("this account is not paired with any Discord accounts!");
return false;
}
DataManager.setString(files[i], "connected", "false");
sender.sendMessage("Accounts unpaired!");
return false;
}
}
} catch (Exception e) {
sender.sendMessage("Error while initiating the connection.");
}
return false;
});
}
return false;
}

@Override
public boolean opRequired(String[] var1) {
return false;
}

@Override
public void sendCommandSyntax(CommandHandler handler, CommandSender sender) {
sender.sendMessage("/mdunpair confirm");
}
}

0 comments on commit 1ca1ec2

Please sign in to comment.