From c01094290886cd6974e5f7cbdad5ff9e43554c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84kwav?= Date: Wed, 26 Jun 2024 14:01:09 +0200 Subject: [PATCH] Added more fixes * replaced help text * using sha256 for file (rat) checks * Send purse as -1 outside of skyblock --- .../java/de/torui/coflsky/CoflSkyCommand.java | 26 +--- .../de/torui/coflsky/WSCommandHandler.java | 2 +- .../torui/coflsky/handlers/EventHandler.java | 17 ++- .../coflsky/network/QueryServerCommands.java | 143 ------------------ .../de/torui/coflsky/utils/FileUtils.java | 9 +- 5 files changed, 23 insertions(+), 174 deletions(-) delete mode 100644 src/main/java/de/torui/coflsky/network/QueryServerCommands.java diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index 00832a2..41dc657 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -52,17 +52,10 @@ public List getCommandAliases() { @Override public String getCommandUsage(ICommandSender sender) { - return HelpText; + return "Sends sub-arguments to the SkyCofl command server\n" + + "§b/cofl §7will request help text with more info\n"; } - public static final String HelpText = "Available local sub-commands:\n" - + "§bstart: §7starts a new connection\n" - + "§bstop: §7stops the connection\n" - + "§bconnect: §7Connects to a different server\n" - + "§breset: §7resets all local session information and stops the connection\n" - + "§bstatus: §7Emits status information\n" - + "§bsetgui: §7Changes the auction purchase GUI\nServer-Only Commands:"; - @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { new Thread(() -> { @@ -74,7 +67,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE //todo: start //possible workaround for https://github.com/Coflnet/SkyblockMod/issues/48 CoflSky.Wrapper.stop(); - sender.addChatMessage(new ChatComponentText("starting connection...")); + sender.addChatMessage(new ChatComponentText("starting SkyCofl connection...")); CoflSky.Wrapper.startConnection(); break; case "stop": @@ -90,12 +83,6 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE .setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl start"))) ); break; - case "debug": - // WSCommandHandler.HandleCommand(new Command(CommandType.Execute, "/me hewwo"), sender.getCommandSenderEntity()); - // WSCommandHandler.HandleCommand(new Command(CommandType.WriteToChat, " {\"type\":\"writeToChat\",\"data\":\"{\\\"text\\\":\\\"\\\\nFLIP: º9Goblin Eg\r\n" - // + "g º87,000 -> 13,999 ºg[BUY]\\\",\\\"onClick\\\":\\\"/viewauction f7d7295ca72f43e9876bf6da7424000c\\\",\\\"hover\\\":\\\"\\\"}\"}"), sender.getCommandSenderEntity()); - //WSCommandHandler.HandleCommand(new Command(CommandType.PlaySound, "{\"name\":\"random.orb\",\"pitch\":0.5}"), sender.getCommandSenderEntity()); - break; case "callback": CallbackCommand(args); break; @@ -247,11 +234,6 @@ private static synchronized void SendAfterStart(ICommandSender sender, RawComman CoflSky.Wrapper.SendMessage(rc); } - public void ListHelp(ICommandSender sender) { - sender.addChatMessage(new ChatComponentText(HelpText)); - sender.addChatMessage(new ChatComponentText(QueryServerCommands.QueryCommands())); - } - public void CallbackCommand(String[] args) { String command = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); @@ -263,7 +245,5 @@ public void CallbackCommand(String[] args) { System.out.println("Sent!"); //}).start(); - } - } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 5bae5b2..b2f2907 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -111,7 +111,7 @@ public static void cacheMods() { for (File mods : modFolder.listFiles()) { modListData.addFilename(mods.getName()); try { - modListData.addFileHashes(FileUtils.getMD5Checksum(mods)); + modListData.addFileHashes(FileUtils.getSha256Checksum(mods)); } catch (Exception exception) { // Highly less likely to happen unless something goes wrong exception.printStackTrace(); diff --git a/src/main/java/de/torui/coflsky/handlers/EventHandler.java b/src/main/java/de/torui/coflsky/handlers/EventHandler.java index 551a983..7477aea 100644 --- a/src/main/java/de/torui/coflsky/handlers/EventHandler.java +++ b/src/main/java/de/torui/coflsky/handlers/EventHandler.java @@ -70,10 +70,12 @@ public static void ScoreboardData() { List scoreBoardLines = getScoreboard(); int size = scoreBoardLines.size() - 1; + boolean foundPurse = false; for (int i = 0; i < scoreBoardLines.size(); i++) { String line = EnumChatFormatting.getTextWithoutFormattingCodes(scoreBoardLines.get(size - i).toLowerCase()); if (Configuration.getInstance().collectScoreboard) { - ProcessScoreboard(line); + if(ProcessScoreboard(line)) + foundPurse = true; } if (line.contains("⏣") && !line.equals(location)) { location = line; @@ -87,6 +89,14 @@ public static void ScoreboardData() { } } } + if(!foundPurse && purse != -1) + { + purse = -1; // no purse found, sync that to server + Command data = new Command<>(CommandType.updatePurse, purse); + CoflSky.Wrapper.SendMessage(data); + UploadScoreboardData(); + UploadTabData(); + } } private static void UploadLocation() { @@ -178,7 +188,7 @@ private static void checkIfInSkyblock(String s) { } } - private static void ProcessScoreboard(String line) { + private static boolean ProcessScoreboard(String line) { if (line.contains("purse") || line.contains("piggy")) { long purse_ = 0; try { @@ -193,6 +203,7 @@ private static void ProcessScoreboard(String line) { CoflSky.Wrapper.SendMessage(data); UploadLocation(); } + return true; } else if (line.contains("bits")) { long bits_ = 0; try { @@ -205,6 +216,8 @@ private static void ProcessScoreboard(String line) { Command data = new Command<>(CommandType.updateBits, bits); CoflSky.Wrapper.SendMessage(data); } + return true; } + return false; } } diff --git a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java b/src/main/java/de/torui/coflsky/network/QueryServerCommands.java deleted file mode 100644 index 42b9f2a..0000000 --- a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java +++ /dev/null @@ -1,143 +0,0 @@ -package de.torui.coflsky.network; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.Arrays; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import de.torui.coflsky.CoflSky; -import de.torui.coflsky.minecraft_integration.CoflSessionManager; -import de.torui.coflsky.minecraft_integration.PlayerDataProvider; - -public class QueryServerCommands { - - private static Gson gson = new GsonBuilder().create(); - - public static String QueryCommands() { - - String queryResult = GetRequest(CoflSky.CommandUri); - - if(queryResult != null) { - CommandInfo[] commands = gson.fromJson(queryResult, CommandInfo[].class); - - System.out.println(">>> "+Arrays.toString(commands)); - - StringBuilder sb = new StringBuilder(); - - if(commands.length>0) { - for(CommandInfo cm : commands) { - sb.append(cm + "\n"); - } - } - return sb.toString().trim(); - - } - - return "§4ERROR: Could not connect to command server!"; - } - - private static class CommandInfo { - - public String subCommand; - public String description; - - public CommandInfo() {} - - public CommandInfo(String subCommand, String description) { - super(); - this.subCommand = subCommand; - this.description = description; - } - - @Override - public String toString() { - return subCommand + ": " + description; - } - - - - } - private static String GetRequest(String uri) { - - try { - System.out.println("Get request"); - URL url = new URL(uri); - HttpURLConnection con; - con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - - //con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - con.setRequestProperty("Accept", "application/json"); - con.setRequestProperty("User-Agent", "CoflMod"); - //con.setDoInput(true); - con.setDoInput(true); - - // ... - - /*OutputStream os = con.getOutputStream(); - byte[] bytes = ("[\"" + getUsername() + "\"]").getBytes("UTF-8"); - os.write(bytes); - os.close(); - */ - System.out.println("InputStream"); - InputStream in = new BufferedInputStream(con.getInputStream()); - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - for (int length; (length = in.read(buffer)) != -1; ) { - result.write(buffer, 0, length); - } - // StandardCharsets.UTF_8.name() > JDK 7 - String resString = result.toString("UTF-8"); - - System.out.println("Result= " + resString); - return resString; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return null; - } - public static String PostRequest(String uri, String data) { - try { - String username = PlayerDataProvider.getUsername(); - URL url = new URL(uri); - HttpURLConnection con; - con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); - - con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - con.setRequestProperty("Accept", "application/json"); - con.setRequestProperty("User-Agent", "CoflMod"); - con.setRequestProperty("conId", CoflSessionManager.GetCoflSession(username).SessionUUID); - con.setRequestProperty("uuid",username); - con.setDoInput(true); - con.setDoOutput(true); - // ... - - OutputStream os = con.getOutputStream(); - byte[] bytes = data.getBytes("UTF-8"); - os.write(bytes); - os.close(); - - InputStream in = new BufferedInputStream(con.getInputStream()); - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - for (int length; (length = in.read(buffer)) != -1; ) { - result.write(buffer, 0, length); - } - // StandardCharsets.UTF_8.name() > JDK 7 - String resString = result.toString("UTF-8"); - - return resString; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return null; - } -} diff --git a/src/main/java/de/torui/coflsky/utils/FileUtils.java b/src/main/java/de/torui/coflsky/utils/FileUtils.java index 1541a75..27e4778 100644 --- a/src/main/java/de/torui/coflsky/utils/FileUtils.java +++ b/src/main/java/de/torui/coflsky/utils/FileUtils.java @@ -7,11 +7,11 @@ public class FileUtils { - public static byte[] createChecksum(File file) throws Exception { + private static byte[] createChecksum(File file) throws Exception { InputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; - MessageDigest complete = MessageDigest.getInstance("MD5"); + MessageDigest complete = MessageDigest.getInstance("SHA-256"); int numRead; do { @@ -25,16 +25,15 @@ public static byte[] createChecksum(File file) throws Exception { return complete.digest(); } - - public static String getMD5Checksum(File file) throws Exception { + public static String getSha256Checksum(File file) throws Exception { byte[] b = createChecksum(file); String result = ""; for (int i=0; i < b.length; i++) { result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); } + System.out.println("MD5: "+result); return result; } - }