11package one .armelin .distale ;
22
33import com .hypixel .hytale .logger .HytaleLogger ;
4+ import com .hypixel .hytale .logger .backend .HytaleLoggerBackend ;
45import com .hypixel .hytale .protocol .PlayerSkin ;
56import com .hypixel .hytale .server .core .command .system .CommandManager ;
67import com .hypixel .hytale .server .core .plugin .JavaPlugin ;
78import com .hypixel .hytale .server .core .plugin .JavaPluginInit ;
89import com .hypixel .hytale .server .core .universe .Universe ;
10+ import com .hypixel .hytale .server .core .universe .world .World ;
11+ import com .hypixel .hytale .server .core .universe .world .WorldConfig ;
912import net .dv8tion .jda .api .JDA ;
1013import net .dv8tion .jda .api .JDABuilder ;
1114import net .dv8tion .jda .api .entities .Activity ;
1215import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
1316import net .dv8tion .jda .api .exceptions .InvalidTokenException ;
1417import net .dv8tion .jda .api .requests .GatewayIntent ;
18+ import net .dv8tion .jda .api .utils .MarkdownSanitizer ;
1519import net .dv8tion .jda .api .utils .MemberCachePolicy ;
1620import okhttp3 .OkHttpClient ;
1721import okhttp3 .Protocol ;
1822import one .armelin .distale .commands .ShrugCommand ;
1923import one .armelin .distale .listeners .*;
2024import one .armelin .distale .listeners .systems .BeforeGatherMemoriesSystem ;
2125import one .armelin .distale .listeners .systems .PlayerDeathSystem ;
26+ import one .armelin .distale .utils .ObservableCopyOnWriteArrayList ;
2227
2328import java .nio .file .Files ;
2429import java .util .Collections ;
2530import java .util .HashMap ;
2631import java .util .Map ;
2732import java .util .UUID ;
33+ import java .util .logging .Level ;
34+ import java .util .logging .LogRecord ;
2835import java .util .regex .Matcher ;
2936import java .util .regex .Pattern ;
3037
@@ -53,6 +60,43 @@ public class DisTale extends JavaPlugin {
5360
5461 private static DisTale instance ;
5562
63+ private static final ObservableCopyOnWriteArrayList <LogRecord > logWatcher = new ObservableCopyOnWriteArrayList <>(record -> {
64+ if (jda == null || stop || !config .announceCrashes ) return ;
65+ if (record .getLevel () == Level .SEVERE && record .getThrown () != null ){
66+ if (record .getLoggerName ().equals ("Hytale" )){
67+ Pattern pattern = Pattern .compile ("Exception in thread Thread\\ [#\\ d+,([^,]+),[^\\ ]]*\\ ](?: potentially caused by (.+))?" );
68+ Matcher matcher = pattern .matcher (record .getMessage ());
69+ if (matcher .find ()) {
70+ String threadName = matcher .group (1 );
71+ String cause = matcher .group (2 );
72+ if (threadName .startsWith ("WorldThread" )){
73+ String worldName = threadName .split (" - " )[1 ];
74+ Pattern uuidPattern = Pattern .compile ("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" );
75+ Matcher uuidMatcher = uuidPattern .matcher (worldName );
76+ if (uuidMatcher .find ()) {
77+ World world = universe .getWorld (matcher .group ());
78+ if (world != null ){
79+ worldName = world .getWorldConfig ().getDisplayName () != null ? world .getWorldConfig ().getDisplayName () : WorldConfig .formatDisplayName (world .getName ());
80+ }
81+ }
82+ String msg ;
83+ if (cause != null ){
84+ msg = DisTale .config .texts .worldCrashWithCauseMessage
85+ .replace ("%worldname%" , worldName )
86+ .replace ("%crashdescription%" , MarkdownSanitizer .escape (record .getThrown ().getMessage ()))
87+ .replace ("%cause%" , MarkdownSanitizer .escape (cause ));
88+ } else {
89+ msg = DisTale .config .texts .worldCrashMessage
90+ .replace ("%worldname%" , worldName )
91+ .replace ("%crashdescription%" , MarkdownSanitizer .escape (record .getThrown ().getMessage ()));
92+ }
93+ textChannel .sendMessage (msg ).queue ();
94+ }
95+ }
96+ }
97+ }
98+ });
99+
56100 public DisTale (JavaPluginInit init ) {
57101 super (init );
58102 instance = this ;
@@ -70,6 +114,8 @@ public DisTale(JavaPluginInit init) {
70114 }
71115 config = Configuration .getConfig (getDataDirectory ());
72116
117+ HytaleLoggerBackend .subscribe (logWatcher );
118+
73119 LOGGER .atInfo ().log ("DisTale initialized successfully!" );
74120 }
75121
0 commit comments