21
21
import java .util .concurrent .atomic .AtomicBoolean ;
22
22
import java .util .concurrent .locks .Lock ;
23
23
24
- public class SkriptVariableDumper
24
+ // No reason to expose this class, make it package-private
25
+ final class SkriptVariableDumper
25
26
{
26
27
private SkriptVariableDumper () {}
27
28
28
29
private static final AtomicBoolean IS_DUMPING_VARIABLES = new AtomicBoolean (false );
29
30
30
- private static final @ NullOr Method GET_VARIABLES ;
31
+ private static final @ NullOr Method GET_VARIABLES = method ( "getVariables" ) ;
31
32
32
- static
33
+ private static final @ NullOr Method GET_READ_LOCK = method ("getReadLock" );
34
+
35
+ private static @ NullOr Method method (String declaredMethodName )
33
36
{
34
- @ NullOr Method getVariables = null ;
35
-
36
37
try
37
38
{
38
- getVariables = Variables .class .getDeclaredMethod ("getVariables" );
39
- getVariables .setAccessible (true );
39
+ Method method = Variables .class .getDeclaredMethod (declaredMethodName );
40
+ method .setAccessible (true );
41
+ return method ;
40
42
}
41
- catch (NoSuchMethodException e ) { e .printStackTrace (); }
42
-
43
- GET_VARIABLES = getVariables ;
44
- }
45
-
46
- private static final @ NullOr Method GET_READ_LOCK ;
47
-
48
- static
49
- {
50
- @ NullOr Method getReadLock = null ;
51
-
52
- try
43
+ catch (NoSuchMethodException e )
53
44
{
54
- getReadLock = Variables . class . getDeclaredMethod ( "getReadLock" );
55
- getReadLock . setAccessible ( true ) ;
45
+ e . printStackTrace ( );
46
+ return null ;
56
47
}
57
- catch (NoSuchMethodException e ) { e .printStackTrace (); }
58
-
59
- GET_READ_LOCK = getReadLock ;
60
48
}
61
49
62
50
static boolean isInvalid () { return GET_VARIABLES == null || GET_READ_LOCK == null ; }
63
51
64
52
@ SuppressWarnings ("unchecked" )
65
- private static Map < String , Object > variables ( )
53
+ private static < T > T invoke ( @ NullOr Method method )
66
54
{
67
- if (GET_VARIABLES == null ) { throw new IllegalStateException (); }
68
- try { return (Map < String , Object >) GET_VARIABLES .invoke (null ); }
55
+ if (method == null ) { throw new IllegalArgumentException (); }
56
+ try { return (T ) method .invoke (null ); }
69
57
catch (IllegalAccessException | InvocationTargetException e ) { throw new RuntimeException (e ); }
70
58
}
71
59
72
- private static Lock readLock ()
73
- {
74
- if (GET_READ_LOCK == null ) { throw new IllegalStateException (); }
75
- try { return (Lock ) GET_READ_LOCK .invoke (null ); }
76
- catch (IllegalAccessException | InvocationTargetException e ) { throw new RuntimeException (e ); }
77
- }
60
+ private static Map <String , Object > variables () { return invoke (GET_VARIABLES ); }
61
+
62
+ private static Lock readLock () { return invoke (GET_READ_LOCK ); }
78
63
79
- @ SuppressWarnings ("ConstantConditions" )
80
- private static String key (Map .Entry <String , Object > entry )
64
+ private static String key (@ NullOr String key )
81
65
{
82
- @ NullOr String key = entry .getKey ();
83
66
return (key == null || key .isEmpty ()) ? "<none>" : key ;
84
67
}
85
68
86
- @ SuppressWarnings ("ConstantConditions" )
87
- private static @ NullOr Object value (Map .Entry <String , Object > entry )
69
+ private static @ NullOr Object value (@ NullOr Object value )
88
70
{
89
- @ NullOr Object value = entry .getValue ();
90
71
return (value == null ) ? null : SkriptToYaml .adapt (value );
91
72
}
92
73
@@ -95,8 +76,8 @@ private static void dump(ConfigurationSection section, Map<String, Object> vars)
95
76
{
96
77
for (Map .Entry <String , Object > entry : vars .entrySet ())
97
78
{
98
- String key = key (entry );
99
- @ NullOr Object value = value (entry );
79
+ String key = key (entry . getKey () );
80
+ @ NullOr Object value = value (entry . getValue () );
100
81
101
82
if (value instanceof Map ) { dump (section .createSection (key ), (Map <String , Object >) value ); }
102
83
else { section .set (key , value ); }
@@ -116,9 +97,9 @@ static Runnable task(CommandSender sender)
116
97
{
117
98
return () ->
118
99
{
119
- boolean available = IS_DUMPING_VARIABLES .compareAndSet (false , true );
100
+ boolean isAvailable = IS_DUMPING_VARIABLES .compareAndSet (false , true );
120
101
121
- if (!available )
102
+ if (!isAvailable )
122
103
{
123
104
sender .sendMessage ("Already dumping variables, be patient..." );
124
105
return ;
0 commit comments