From a4d8abf48c9d769d4cbc7b58e254721491807d37 Mon Sep 17 00:00:00 2001 From: victormylle Date: Thu, 22 Oct 2020 22:39:56 +0200 Subject: [PATCH] Added unlink support --- DiscordVerifier/src/Main.java | 54 ++++++++++++++++++++++++++-- cogs/playerlink.py | 68 ++++++++++++++++++++++++++++------- requirements.txt | 1 + 3 files changed, 109 insertions(+), 14 deletions(-) mode change 100644 => 100755 cogs/playerlink.py diff --git a/DiscordVerifier/src/Main.java b/DiscordVerifier/src/Main.java index 228a4e5..e72bfef 100755 --- a/DiscordVerifier/src/Main.java +++ b/DiscordVerifier/src/Main.java @@ -1,3 +1,6 @@ +// 25216 --> uuid server +// 25224 --> send chat server + import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -5,25 +8,65 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.util.UUID; public class Main extends JavaPlugin { Thread serversocket; + Thread uuidServerThread; + ServerSocket uuidServerSocket; + ServerSocket chatServerSocket; public void onEnable() { getLogger().info("DiscordVerifier Plugin enabled"); PluginManager pm = getServer().getPluginManager(); start_socketserver(); + start_uuid_server(); } + public void start_uuid_server(){ + uuidServerThread = new Thread(() -> { + try{ + uuidServerSocket = new ServerSocket(25216); + while (true) { + Socket s = uuidServerSocket.accept(); + PrintWriter out = + new PrintWriter(s.getOutputStream(), true); + BufferedReader in = new BufferedReader( + new InputStreamReader(s.getInputStream())); + String inputLine; + inputLine = in.readLine(); + if (inputLine != null) { + System.out.println("minecraftname: " + inputLine); + Player player = getServer().getPlayer(inputLine); + if (player == null){ + out.println("PlayerError"); + }else { + System.out.println(player.getUniqueId().toString()); + UUID uuid = player.getUniqueId(); + out.println(uuid.toString()); + + } + } + } + + } catch (Exception e) { + System.out.println(e); + } + }); + + uuidServerThread.start(); + } + + public void start_socketserver() { serversocket = new Thread(() -> { try{ - ServerSocket ss = new ServerSocket(3333); + chatServerSocket = new ServerSocket(25224); while (true) { - Socket s = ss.accept(); + Socket s = chatServerSocket.accept(); PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader in = new BufferedReader( @@ -59,6 +102,13 @@ public class Main extends JavaPlugin { } public void onDisable() { + try { + chatServerSocket.close(); + uuidServerSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + uuidServerThread.stop(); serversocket.stop(); getLogger().info("Plugin disabled"); } diff --git a/cogs/playerlink.py b/cogs/playerlink.py old mode 100644 new mode 100755 index fc2663e..2927fae --- a/cogs/playerlink.py +++ b/cogs/playerlink.py @@ -6,6 +6,10 @@ import socket import asyncio import threading from data import constants +from data.DatabaseConnection import * + +# 25216 --> uuid server +# 25224 --> send chat server def get_random_string(length): # Random string with the combination of lower and upper case @@ -18,9 +22,22 @@ class PlayerError(Exception): class WrongCodeError(Exception): pass +def get_player_uuid(minecraftname): + HOST = '192.168.1.214' # The server's hostname or IP address + PORT = 25216 # The port used by the server + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + s.send(bytes("{}\n".format(minecraftname), encoding="ascii")) + data = s.recv(1024) + data_string = data.decode("utf-8").strip('\n') + if data_string == "PlayerError": + raise PlayerError() + return data_string + def send_chat(discordname, minecraftname, code): - HOST = '127.0.0.1' # The server's hostname or IP address - PORT = 3333 # The port used by the server + HOST = '192.168.1.214' # The server's hostname or IP address + PORT = 25224 # The port used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) @@ -29,7 +46,7 @@ def send_chat(discordname, minecraftname, code): data_string = data.decode("utf-8").strip('\n') if data_string == "PlayerError": raise PlayerError() - return True + return data_string class PlayerLink(commands.Cog): def __init__(self, client): @@ -46,6 +63,7 @@ class PlayerLink(commands.Cog): authorid = ctx.author.id code = get_random_string(8) + def check(message: discord.Message): return message.channel.id == channelid and message.author.id == authorid @@ -56,15 +74,18 @@ class PlayerLink(commands.Cog): embed.add_field(name="Minecraft Name:", value=f"{minecraftname}", inline=False) embed.add_field(name="Discord Name:", value=f"{discord_author.mention}", inline=False) + + # use dictionary and set color string before the message: example => r(ed):message + if isinstance(error, WrongCodeError): embed.add_field(name="Error", value=f"The code is wrong!", inline=False) embed.colour = discord.Colour.red() - elif (timer.ended): - embed.add_field(name="Timer", value=f"The code is expired!", inline=False) - embed.colour = discord.Colour.red() elif (success == True): embed.add_field(name="Success", value=f"The link was successfull!", inline=False) embed.colour = discord.Colour.green() + elif (timer.ended): + embed.add_field(name="Timer", value=f"The code is expired!", inline=False) + embed.colour = discord.Colour.red() elif (timer.minutes == 2): embed.add_field(name="Timer", value=f"The code will expire in { str(timer.minutes)} minutes", inline=False) embed.colour = discord.Colour.orange() @@ -86,6 +107,8 @@ class PlayerLink(commands.Cog): try: + + uuid = get_player_uuid(arg) send_chat(ctx.author.name, arg, code) #message_send_time = timer = Timer() @@ -98,9 +121,17 @@ class PlayerLink(commands.Cog): msg = await self.client.wait_for('message', check=check) if msg.content == code: - await ctx.author.add_roles(self.get_linked_role()) - timer.ended = True - await message.edit(embed=create_embed(ctx.author, arg, timer, success=True)) + + dbLinker = PlayerDBLinker() + try: + dbLinker.linkPlayer(uuid, authorid) + await ctx.author.add_roles(self.get_linked_role()) + timer.ended = True + await message.edit(embed=create_embed(ctx.author, arg, timer, success=True)) + except SQLInsertError: + await message.edit(embed=create_embed(ctx.author, arg, timer)) + finally: + dbLinker.closeconnections() else: # this stops the timer task task.cancel() @@ -113,9 +144,22 @@ class PlayerLink(commands.Cog): # await ctx.send("Something went wrong") @commands.command(name="Unlink") - async def unlink(self, ctx): - await ctx.author.remove_roles(self.get_linked_role()) - await ctx.send("Unlinked your account") + async def unlink(self, ctx, arg): + authorid = ctx.author.id + dbLinker = PlayerDBLinker() + try: + dbLinker.unlinkPlayer(arg, authorid) + await ctx.author.remove_roles(self.get_linked_role()) + await ctx.send("Unlinked your account") + except UnLinkError: + await ctx.send("The unlink was unsuccessfull") + except WrongMinecraftName: + await ctx.send("Wrong minecraft name!") + except PlayerNotLinked: + await ctx.send("Player not linked!") + finally: + dbLinker.closeconnections() + class Timer(): def __init__(self): diff --git a/requirements.txt b/requirements.txt index 8532548..a0ea04f 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ discord.py==1.5.0 mcstatus==4.0.0 +mysql-connector-python==8.0.21