11package xyz .webmc .wlib .api ;
22
3+ import xyz .webmc .wlib .api .util .RNGUtil ;
4+
5+ import java .lang .StackWalker .Option ;
6+ import java .lang .StackWalker .StackFrame ;
37import java .util .ArrayList ;
48import java .util .List ;
59import java .util .logging .Logger ;
610
711import dev .colbster937 .reflect .MirrorSafe ;
12+ import org .bukkit .Bukkit ;
13+ import org .bukkit .ChatColor ;
14+ import org .bukkit .entity .Player ;
815import org .bukkit .plugin .Plugin ;
916
1017@ SuppressWarnings ({ "NonConstantLogger" })
1118public final class WLIB {
19+ private static final StackWalker stackWalker = StackWalker .getInstance (Option .RETAIN_CLASS_REFERENCE );
1220 private static final List <Plugin > plugins = new ArrayList <>();
1321 private static Logger logger ;
1422
@@ -34,11 +42,60 @@ public static final List<String> getWLIBPluginNames() {
3442 return ret ;
3543 }
3644
37- public static final Logger getLogger () {
38- return logger ;
45+ public static final void devAlert (final String ... txt ) {
46+ final Class <?> clazz = stackWalker .getCallerClass ();
47+ final String ctx = clazz .getSimpleName ();
48+ final String str = ChatColor .DARK_GREEN + "[" + ChatColor .GREEN + "DEV" + ChatColor .RESET + " - " + ChatColor .GREEN
49+ + ChatColor .DARK_GREEN + ctx + "] " + ChatColor .RESET + String .join (" " , txt );
50+
51+ for (final Player p : Bukkit .getOnlinePlayers ()) {
52+ if (p .hasPermission ("wlib.alerts.dev" ) && !p .hasPermission ("wlib.alerts.dev.muted." + ctx )) {
53+ p .sendMessage (str );
54+ }
55+ }
56+ }
57+
58+ public static final void warnDeprecatedUsage () {
59+ final StackFrame [] frames = stackWalker .walk (s -> s .skip (1 ).toArray (StackFrame []::new ));
60+ if (frames .length > 1 ) {
61+ final StackFrame called = frames [0 ];
62+ final StackFrame caller = frames [1 ];
63+
64+ final StringBuilder sb = new StringBuilder ();
65+
66+ sb .append (caller .getClassName ())
67+ .append ('.' )
68+ .append (caller .getMethodName ())
69+ .append (':' )
70+ .append (caller .getLineNumber ())
71+ .append (" called deprecated method " )
72+ .append (called .getClassName ())
73+ .append ('.' )
74+ .append (called .getMethodName ());
75+
76+ for (int i = 2 ; i < frames .length ; i ++) {
77+ final StackFrame frame = frames [i ];
78+
79+ sb .append ("\n at " )
80+ .append (frame .getClassName ())
81+ .append ('.' )
82+ .append (frame .getMethodName ())
83+ .append ('(' )
84+ .append (frame .getFileName ())
85+ .append (':' )
86+ .append (frame .getLineNumber ())
87+ .append (")" );
88+ }
89+
90+ logger .warning (sb .toString ());
91+ }
3992 }
4093
4194 public static final boolean getIsModernServer () {
4295 return MirrorSafe .getClassExists ("org.bukkit.block.data.BlockData" );
4396 }
97+
98+ public static final Logger getLogger () {
99+ return logger ;
100+ }
44101}
0 commit comments