From 52d0c94e43c9602b5e9ca77f8cf000d14b45e319 Mon Sep 17 00:00:00 2001
From: syeyoung <cyoung06@naver.com>
Date: Tue, 7 Feb 2023 18:42:37 +0900
Subject: - Make Freeze detector a feature

Signed-off-by: syeyoung <cyoung06@naver.com>
---
 .../syeyoung/dungeonsguide/mod/DungeonsGuide.java  |  3 -
 .../mod/events/listener/FreezeListener.java        | 75 ----------------------
 .../mod/features/FeatureRegistry.java              |  2 +-
 .../impl/advanced/FeatureDetectFreeze.java         | 73 +++++++++++++++++++++
 4 files changed, 74 insertions(+), 79 deletions(-)
 delete mode 100644 mod/src/main/java/kr/syeyoung/dungeonsguide/mod/events/listener/FreezeListener.java
 create mode 100644 mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureDetectFreeze.java

(limited to 'mod/src')

diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java
index b6e92feb..3df65e42 100755
--- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java
+++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java
@@ -34,7 +34,6 @@ import kr.syeyoung.dungeonsguide.mod.discord.DiscordIntegrationManager;
 import kr.syeyoung.dungeonsguide.mod.dungeon.DungeonFacade;
 import kr.syeyoung.dungeonsguide.mod.events.annotations.EventHandlerRegistry;
 import kr.syeyoung.dungeonsguide.mod.events.listener.DungeonListener;
-import kr.syeyoung.dungeonsguide.mod.events.listener.FreezeListener;
 import kr.syeyoung.dungeonsguide.mod.events.listener.PacketInjector;
 import kr.syeyoung.dungeonsguide.mod.events.listener.PacketListener;
 import kr.syeyoung.dungeonsguide.mod.features.FeatureRegistry;
@@ -86,7 +85,6 @@ import java.lang.reflect.Method;
 import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 public class DungeonsGuide implements DGInterface {
@@ -172,7 +170,6 @@ public class DungeonsGuide implements DGInterface {
     public void init(File f) {
         ProgressManager.ProgressBar progressbar = ProgressManager.push("DungeonsGuide", 4);
 
-        registerEventsForge(new FreezeListener());
 
         progressbar.step("Creating Configuration");
 
diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/events/listener/FreezeListener.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/events/listener/FreezeListener.java
deleted file mode 100644
index 235b678b..00000000
--- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/events/listener/FreezeListener.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
- * Copyright (C) 2023  cyoung06 (syeyoung)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published
- * by the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-package kr.syeyoung.dungeonsguide.mod.events.listener;
-
-import kr.syeyoung.dungeonsguide.mod.DungeonsGuide;
-import kr.syeyoung.dungeonsguide.mod.features.FeatureRegistry;
-import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
-import net.minecraftforge.fml.common.gameevent.TickEvent;
-
-import javax.swing.*;
-import java.awt.*;
-import java.awt.datatransfer.Clipboard;
-import java.awt.datatransfer.StringSelection;
-import java.lang.management.ManagementFactory;
-import java.lang.management.ThreadInfo;
-import java.lang.management.ThreadMXBean;
-import java.util.Arrays;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-public class FreezeListener implements Runnable {
-    public FreezeListener() {
-        t.start();
-    }
-    private volatile long lastTick = Long.MAX_VALUE;
-
-    @SubscribeEvent
-    public void onTick(TickEvent.ClientTickEvent tickEvent) {
-        lastTick = System.currentTimeMillis() + 5000;
-
-    }
-
-
-    Thread t = new Thread(DungeonsGuide.THREAD_GROUP,this);
-
-    @Override
-    public void run() {
-        while(!t.isInterrupted()) {
-            if (lastTick < System.currentTimeMillis() && FeatureRegistry.FREEZE_DETECTOR.isEnabled()) {
-                ThreadMXBean bean = ManagementFactory.getThreadMXBean();
-                ThreadInfo[] infos = bean.dumpAllThreads(true, true);
-                String stacktrace = Arrays.stream(infos).map(Object::toString)
-                        .collect(Collectors.joining());
-                System.out.println(stacktrace);
-
-                StringSelection selection = new StringSelection(stacktrace);
-                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
-                clipboard.setContents(selection, selection);
-
-                JOptionPane.showMessageDialog(null, "Your Minecraft Seems to be frozen!\nThreadump has been copied into your clipboard!", "DG Freeze Alert", JOptionPane.INFORMATION_MESSAGE);
-            }
-            try {
-                Thread.sleep(16);
-            } catch (InterruptedException e) {
-                break;
-            }
-        }
-    }
-}
diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java
index b30d0a67..1c0bc38e 100644
--- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java
+++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java
@@ -99,7 +99,7 @@ public class FeatureRegistry {
     public static final FeatureHideNameTags DUNGEON_HIDENAMETAGS = register(new FeatureHideNameTags());
     public static final FeaturePlayerESP DUNGEON_PLAYERESP = register(new FeaturePlayerESP());
     public static final SimpleFeature DUNGEON_INTERMODCOMM = register(new SimpleFeature("Dungeon.Teammates", "Communicate With Other's Dungeons Guide", "Sends total secret in the room to others\nSo that they can use the data to calculate total secret in dungeon run\n\nThis automates player chatting action, (chatting data) Thus it might be against hypixel's rules.\nBut mods like auto-gg which also automate player action and is kinda allowed mod exist so I'm leaving this feature.\nThis option is use-at-your-risk and you'll be responsible for ban if you somehow get banned because of this feature\n(Although it is not likely to happen)\nDefaults to off", "dungeon.intermodcomm", false));
-    public static final SimpleFeature FREEZE_DETECTOR = register(new SimpleFeature("Misc", "Freeze Detector", "Detect freezes, and when mc freezes for more than 5s, copy threadump and show you a popup", "misc.freezedetect", false));
+    public static final FeatureDetectFreeze FREEZE_DETECTOR = register(new FeatureDetectFreeze());
     public static final FeatureWarnLowHealth DUNGEON_LOWHEALTH_WARN = register(new FeatureWarnLowHealth());
     public static final FeatureDungeonScore DUNGEON_SCORE = register(new FeatureDungeonScore());
     public static final FeatureDungeonTombs DUNGEON_TOMBS = register(new FeatureDungeonTombs());
diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureDetectFreeze.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureDetectFreeze.java
new file mode 100644
index 00000000..14bb73b4
--- /dev/null
+++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureDetectFreeze.java
@@ -0,0 +1,73 @@
+/*
+ * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
+ * Copyright (C) 2023  cyoung06 (syeyoung)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package kr.syeyoung.dungeonsguide.mod.features.impl.advanced;
+
+import kr.syeyoung.dungeonsguide.mod.DungeonsGuide;
+import kr.syeyoung.dungeonsguide.mod.events.annotations.DGEventHandler;
+import kr.syeyoung.dungeonsguide.mod.features.FeatureRegistry;
+import kr.syeyoung.dungeonsguide.mod.features.SimpleFeature;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.StringSelection;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.util.Arrays;
+import java.util.stream.Collectors;
+
+public class FeatureDetectFreeze extends SimpleFeature {
+    public FeatureDetectFreeze() {
+        super("Misc", "Freeze Detector", "Detect freezes, and when mc freezes for more than 3s, copy threadump and show you a popup", "misc.freezedetect", false);
+        t.start();
+    }
+
+    @DGEventHandler(ignoreDisabled = true, triggerOutOfSkyblock = true)
+    public void onTick() {
+        lastTick = System.currentTimeMillis() + 3000;
+    }
+
+
+    private long lastTick = Long.MAX_VALUE;
+    private Thread t = new Thread(DungeonsGuide.THREAD_GROUP,this::run);
+
+    public void run() {
+        while(!t.isInterrupted()) {
+            if (lastTick < System.currentTimeMillis() && FeatureRegistry.FREEZE_DETECTOR.isEnabled()) {
+                ThreadMXBean bean = ManagementFactory.getThreadMXBean();
+                ThreadInfo[] infos = bean.dumpAllThreads(true, true);
+                String stacktrace = Arrays.stream(infos).map(Object::toString)
+                        .collect(Collectors.joining());
+                System.out.println(stacktrace);
+
+                StringSelection selection = new StringSelection(stacktrace);
+                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+                clipboard.setContents(selection, selection);
+
+                JOptionPane.showMessageDialog(null, "Your Minecraft Seems to be frozen!\nThreadump has been copied into your clipboard!", "DG Freeze Alert", JOptionPane.INFORMATION_MESSAGE);
+            }
+            try {
+                Thread.sleep(16);
+            } catch (InterruptedException e) {
+                break;
+            }
+        }
+    }
+}
-- 
cgit