From ae12206958a0ba848594beeb2f804720d1a89d4f Mon Sep 17 00:00:00 2001 From: HackOS <63157139+HackedOS@users.noreply.github.com> Date: Thu, 12 May 2022 22:11:18 +0530 Subject: Add Auto-Toggle and Send Scoreboard/Tab Menu Data (#58) * Add Auto-Toggle * Send Scoreboard data to the Server * Fix Privacy settings and respect collect scoreboard * Move code to EventHandlers.java and collect Tab Manu data * Remove unused import * Refactor for Readability * Bug Fix * Fix bug * Add LocalConfig and Respect AutoStart * Add chat msg when leaving skyblock * Add ChatRegex * Fix bug where purse doesnt update with pigyy bank * Improve Performance * Fix chatregex always being null --- src/main/java/de/torui/coflsky/CoflSky.java | 37 +++- src/main/java/de/torui/coflsky/EventHandler.java | 191 +++++++++++++++++++++ src/main/java/de/torui/coflsky/EventRegistry.java | 54 +++--- .../de/torui/coflsky/commands/CommandType.java | 16 +- .../torui/coflsky/configuration/Configuration.java | 29 ++-- .../torui/coflsky/configuration/LocalConfig.java | 33 ++++ 6 files changed, 324 insertions(+), 36 deletions(-) create mode 100644 src/main/java/de/torui/coflsky/EventHandler.java create mode 100644 src/main/java/de/torui/coflsky/configuration/LocalConfig.java diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 6e7fde7..7ecccf0 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -1,12 +1,18 @@ package de.torui.coflsky; +import java.io.File; +import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import com.google.gson.Gson; +import de.torui.coflsky.configuration.LocalConfig; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import org.lwjgl.input.Keyboard; -import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.network.WSClientWrapper; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.ClientCommandHandler; @@ -27,6 +33,9 @@ public class CoflSky public static KeyBinding[] keyBindings; public static EventRegistry Events; + public static File configFile; + private File coflDir; + public static LocalConfig config; public static final String[] webSocketURIPrefix = new String [] { "wss://sky.coflnet.com/modsocket", @@ -36,7 +45,25 @@ public class CoflSky }; public static String CommandUri = "https://sky-commands.coflnet.com/api/mod/commands"; - + @EventHandler + public void preInit(FMLPreInitializationEvent event) { + String configString = null; + Gson gson = new Gson(); + coflDir = new File(event.getModConfigurationDirectory(), "CoflSky"); + coflDir.mkdirs(); + configFile = new File(coflDir, "config.json"); + try { + if (configFile.isFile()) { + configString = new String(Files.readAllBytes(Paths.get(configFile.getPath()))); + config = gson.fromJson(configString, LocalConfig.class); + } + } catch (IOException e) { + e.printStackTrace(); + } + if (configString == null) { + config = LocalConfig.createDefaultConfig(); + } + } @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException { @@ -62,7 +89,11 @@ public class CoflSky } Events = new EventRegistry(); - MinecraftForge.EVENT_BUS.register(Events); + MinecraftForge.EVENT_BUS.register(Events); + Runtime.getRuntime() + .addShutdownHook( + new Thread( + () -> config.saveConfig(configFile , config))); } diff --git a/src/main/java/de/torui/coflsky/EventHandler.java b/src/main/java/de/torui/coflsky/EventHandler.java new file mode 100644 index 0000000..6fac06a --- /dev/null +++ b/src/main/java/de/torui/coflsky/EventHandler.java @@ -0,0 +1,191 @@ +package de.torui.coflsky; + +import de.torui.coflsky.commands.Command; +import de.torui.coflsky.commands.CommandType; +import de.torui.coflsky.configuration.Configuration; +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.NetworkPlayerInfo; +import net.minecraft.event.ClickEvent; +import net.minecraft.scoreboard.Score; +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.ScorePlayerTeam; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatStyle; +import net.minecraft.util.EnumChatFormatting; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static de.torui.coflsky.CoflSky.config; + +public class EventHandler { + + public static boolean isInSkyblock = false; + public static boolean isInTheCatacombs = false; + private static int purse = 0; + private static int bits = 0; + private static String location = ""; + private static String server = ""; + + public static void TabMenuData() { + if (isInSkyblock && CoflSky.Wrapper.isRunning){ + List tabdata = getTabList(); + int size = tabdata.size() - 1; + for (int i = 0; i < tabdata.size(); i++) { + String line = tabdata.get(size - i).toLowerCase(); + ProcessTabMenu(line); + } + } + } + public static void ScoreboardData() { + String s; + try { + Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard(); + ScoreObjective objective = scoreboard.getObjectiveInDisplaySlot(1); + s = EnumChatFormatting.getTextWithoutFormattingCodes(objective.getDisplayName()); + } catch (Exception e) { + s = ""; + } + checkIfInSkyblock(s); + if (isInSkyblock && CoflSky.Wrapper.isRunning) { + List scoreBoardLines = getScoreboard(); + int size = scoreBoardLines.size() - 1; + boolean hasFoundCatacombs = false; + for (int i = 0; i < scoreBoardLines.size(); i++) { + String line = EnumChatFormatting.getTextWithoutFormattingCodes(scoreBoardLines.get(size - i).toLowerCase()); + if (line.contains("the catacombs")) { + hasFoundCatacombs = true; + } + if (Configuration.getInstance().collectScoreboard) { + ProcessScoreboard(line); + } + + } + if (hasFoundCatacombs && !isInTheCatacombs) { + Command data = new Command<>(CommandType.set, "disableFlips true"); + CoflSky.Wrapper.SendMessage(data); + isInTheCatacombs = true; + } + if (isInTheCatacombs && !hasFoundCatacombs) { + Command data = new Command<>(CommandType.set, "disableFlips false"); + CoflSky.Wrapper.SendMessage(data); + isInTheCatacombs = false; + } + } + } + private static List getScoreboard() { + ArrayList scoreboardAsText = new ArrayList<>(); + if (Minecraft.getMinecraft() == null || Minecraft.getMinecraft().theWorld == null) { + return scoreboardAsText; + } + Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard(); + ScoreObjective sideBarObjective = scoreboard.getObjectiveInDisplaySlot(1); + if (sideBarObjective == null) { + return scoreboardAsText; + } + String scoreboardTitle = sideBarObjective.getDisplayName(); + scoreboardTitle = EnumChatFormatting.getTextWithoutFormattingCodes(scoreboardTitle); + scoreboardAsText.add(scoreboardTitle); + Collection scoreboardLines = scoreboard.getSortedScores(sideBarObjective); + for (Score line : scoreboardLines) { + String playerName = line.getPlayerName(); + if (playerName == null || playerName.startsWith("#")) { + continue; + } + ScorePlayerTeam scorePlayerTeam = scoreboard.getPlayersTeam(playerName); + String lineText = EnumChatFormatting.getTextWithoutFormattingCodes( + ScorePlayerTeam.formatPlayerName(scorePlayerTeam, line.getPlayerName())); + scoreboardAsText.add(lineText.replace(line.getPlayerName(),"")); + } + return scoreboardAsText; + } + + private static List getTabList() { + ArrayList tabListAsString = new ArrayList<>(); + if (Minecraft.getMinecraft() == null || Minecraft.getMinecraft().getNetHandler() == null) { + return tabListAsString; + } + Collection playerInfoMap = Minecraft.getMinecraft().getNetHandler().getPlayerInfoMap(); + for (NetworkPlayerInfo playerInfo : playerInfoMap) { + String toDisplay; + if (playerInfo.getDisplayName() != null) { + toDisplay = playerInfo.getDisplayName().getFormattedText(); + } else { + toDisplay = ScorePlayerTeam.formatPlayerName(playerInfo.getPlayerTeam(), playerInfo.getGameProfile().getName()); + } + tabListAsString.add(EnumChatFormatting.getTextWithoutFormattingCodes(toDisplay)); + } + return tabListAsString; + } + private static void ProcessTabMenu(String line) { + if (line.contains("server:")) { + String server_ = line.split("server: ")[1]; + if (!server.equals(server_)) { + server = server_; + Command data = new Command<>(CommandType.updateServer, server); + CoflSky.Wrapper.SendMessage(data); + } + } else if (line.contains("area:")) { + String location_ = line.split("area: ")[1]; + if (!location.equals(location_)) { + location = location_; + Command data = new Command<>(CommandType.updateLocation, location); + CoflSky.Wrapper.SendMessage(data); + } + } + } + private static void checkIfInSkyblock(String s) { + if (s.contains("SKYBLOCK") && !isInSkyblock) { + if (config.autoStart){ + CoflSky.Wrapper.stop(); + CoflSky.Wrapper.startConnection(); + } + isInSkyblock = true; + } else if (!s.contains("SKYBLOCK") && isInSkyblock) { + if (config.autoStart){ + CoflSky.Wrapper.stop(); + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("connection to ") + .appendSibling(new ChatComponentText("C").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.DARK_BLUE))) + .appendSibling(new ChatComponentText("oflnet").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.GOLD))) + .appendSibling(new ChatComponentText(" has been stopped since you left skyblock.\n To reconnect enter ")) + .appendSibling(new ChatComponentText("\"").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.AQUA))) + .appendSibling(new ChatComponentText("/cofl start")) + .appendSibling(new ChatComponentText("\"").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.AQUA))) + .appendSibling(new ChatComponentText(" or click this message")) + .setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/cofl start"))) + ); + } + isInSkyblock = false; + } + } + private static void ProcessScoreboard(String line){ + if (line.contains("purse") || line.contains("piggy")) { + int purse_ = 0; + try { + purse_ = Integer.parseInt(line.split(": ")[1].replace(",", "")); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + if (purse != purse_) { + purse = purse_; + Command data = new Command<>(CommandType.updatePurse, purse); + CoflSky.Wrapper.SendMessage(data); + } + } else if (line.contains("bits")) { + int bits_ = 0; + try { + bits_ = Integer.parseInt(line.split(": ")[1].replace(",", "")); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + if (bits != bits_) { + bits = bits_; + Command data = new Command<>(CommandType.updateBits, bits); + CoflSky.Wrapper.SendMessage(data); + } + } + } + +} diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index 5a1b58c..3434c1c 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -1,6 +1,11 @@ package de.torui.coflsky; import java.time.LocalDateTime; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import com.mojang.realmsclient.util.Pair; import de.torui.coflsky.FlipHandler.Flip; @@ -8,6 +13,7 @@ import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; import de.torui.coflsky.commands.models.AuctionData; +import de.torui.coflsky.configuration.Configuration; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiChest; @@ -29,8 +35,13 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.common.gameevent.TickEvent; -public class EventRegistry { +import static de.torui.coflsky.EventHandler.ScoreboardData; +import static de.torui.coflsky.EventHandler.TabMenuData; +public class EventRegistry { + public static Pattern chatpattern = Pattern.compile(Configuration.getInstance().chatRegex, Pattern.CASE_INSENSITIVE); + public final ExecutorService chatThreadPool = Executors.newFixedThreadPool(2); + public final ExecutorService tickThreadPool = Executors.newFixedThreadPool(2); @SubscribeEvent public void onDisconnectedFromServerEvent(ClientDisconnectionFromServerEvent event) { if(CoflSky.Wrapper.isRunning) { @@ -123,26 +134,16 @@ public class EventRegistry { @SubscribeEvent public void HandleChatEvent(ClientChatReceivedEvent sce) { - if(CoflSky.Wrapper.isRunning && last.first() != null) { - if(sce.message.getUnformattedText().startsWith("You claimed ")) { - - AuctionData ad = new AuctionData(); - ad.setItemId(last.second().first()); - - if((LastViewAuctionInvocation+60*1000) >= System.currentTimeMillis()) { - ad.setAuctionId(LastViewAuctionUUID); - } else { - ad.setAuctionId(""); + if(CoflSky.Wrapper.isRunning) { + chatThreadPool.submit(() -> { + String msg = sce.message.getUnformattedText(); + Matcher matcher = chatpattern.matcher(msg); + boolean matchFound = matcher.find(); + if (matchFound) { + Command data = new Command<>(CommandType.chatBatch, new String[]{msg}); + CoflSky.Wrapper.SendMessage(data); } - - Command data = new Command<>(CommandType.PurchaseConfirm, ad); - CoflSky.Wrapper.SendMessage(data); - System.out.println("PurchaseConfirm"); - last = EMPTY; - } - else if(last.second().second().plusSeconds(10).isBefore(LocalDateTime.now())) { - last = EMPTY; - } + }); } } @@ -207,4 +208,17 @@ public class EventRegistry { public void OnRenderTick(TickEvent.RenderTickEvent event) { de.torui.coflsky.CountdownTimer.onRenderTick(event); } + + int UpdateThisTick = 0; + @SubscribeEvent(priority = EventPriority.LOWEST) + public void onTick(TickEvent.ClientTickEvent event) { + UpdateThisTick++; + if (UpdateThisTick >= 200) UpdateThisTick = 0; + if (UpdateThisTick == 0) { + tickThreadPool.submit(() -> { + ScoreboardData(); + TabMenuData(); + }); + } + } } diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index ecece3f..533af1e 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -9,6 +9,8 @@ import com.google.gson.annotations.SerializedName; public enum CommandType { @SerializedName("writeToChat") WriteToChat, + @SerializedName("set") + set, @SerializedName("execute") Execute, @@ -38,8 +40,18 @@ public enum CommandType { @SerializedName("privacySettings") PrivacySettings, @SerializedName("countdown") - Countdown, -; + Countdown, + @SerializedName("updatePurse") + updatePurse, + @SerializedName("updateBits") + updateBits, + @SerializedName("updateServer") + updateServer, + @SerializedName("updateLocation") + updateLocation, + @SerializedName("chatBatch") + chatBatch, + ; public static Map data; static { data = new HashMap<>(); diff --git a/src/main/java/de/torui/coflsky/configuration/Configuration.java b/src/main/java/de/torui/coflsky/configuration/Configuration.java index bdd6186..1ef040f 100644 --- a/src/main/java/de/torui/coflsky/configuration/Configuration.java +++ b/src/main/java/de/torui/coflsky/configuration/Configuration.java @@ -1,5 +1,10 @@ package de.torui.coflsky.configuration; +import de.torui.coflsky.CoflSky; +import de.torui.coflsky.EventRegistry; + +import java.util.regex.Pattern; + public class Configuration { public Configuration() { @@ -15,19 +20,21 @@ public class Configuration { public static void setInstance(Configuration config) { instance = config; + CoflSky.config.autoStart = instance.autoStart; + EventRegistry.chatpattern = Pattern.compile(instance.chatRegex, Pattern.CASE_INSENSITIVE);; } - public String ChatRegex; - public boolean CollectChat; - public boolean CollectInventory; - public boolean CollectTab; - public boolean CollectScoreboard; - public boolean AllowProxy; - public boolean CollectInvClick; - public boolean CollectChatClicks; - public boolean CollectLobbyChanges; - public boolean CollectEntities; - + public String chatRegex; + public boolean collectChat; + public boolean collectInventory; + public boolean collectTab; + public boolean collectScoreboard; + public boolean allowProxy; + public boolean collectInvClick; + public boolean collectChatClicks; + public boolean collectLobbyChanges; + public boolean collectEntities; + public boolean autoStart; @Description("Wherever or not to send item descriptions for extending to the server") public boolean ExtendDescriptions; diff --git a/src/main/java/de/torui/coflsky/configuration/LocalConfig.java b/src/main/java/de/torui/coflsky/configuration/LocalConfig.java new file mode 100644 index 0000000..28909c4 --- /dev/null +++ b/src/main/java/de/torui/coflsky/configuration/LocalConfig.java @@ -0,0 +1,33 @@ +package de.torui.coflsky.configuration; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class LocalConfig { + public boolean autoStart; + public LocalConfig(boolean autoStart) { + this.autoStart = autoStart; + } + public static void saveConfig(File file, LocalConfig Config) { + Gson gson = new Gson(); + try { + if (!file.isFile()) { + file.createNewFile(); + } + Files.write(Paths.get(file.getAbsolutePath()), + gson.toJson(Config).getBytes(StandardCharsets.UTF_8)); + }catch (IOException e){ + e.printStackTrace(); + } + } + + public static LocalConfig createDefaultConfig() { + return new LocalConfig(false); + } +} -- cgit