3
3
import com .earth2me .essentials .Essentials ;
4
4
import com .earth2me .essentials .economy .EconomyLayer ;
5
5
import com .earth2me .essentials .economy .EconomyLayers ;
6
- import com .google .common .collect .ImmutableList ;
7
6
import org .bstats .bukkit .Metrics ;
7
+ import org .bstats .charts .AdvancedBarChart ;
8
+ import org .bstats .charts .CustomChart ;
9
+ import org .bstats .charts .DrilldownPie ;
10
+ import org .bstats .charts .MultiLineChart ;
11
+ import org .bstats .charts .SimplePie ;
8
12
import org .bukkit .Bukkit ;
9
- import org .bukkit .plugin .Plugin ;
10
13
import org .bukkit .plugin .java .JavaPlugin ;
11
14
12
- import java .lang .reflect .Field ;
13
15
import java .util .HashMap ;
14
- import java .util .List ;
15
16
import java .util .Map ;
16
17
17
18
public class MetricsWrapper {
18
19
19
- private static final List <String > KNOWN_FORCED_METRICS = ImmutableList .of (
20
- "ChatControl" ,
21
- "catserver.server.Metrics" );
22
- private static boolean hasWarned = false ;
23
20
private final Essentials ess ;
24
21
private final Metrics metrics ;
22
+ private final JavaPlugin plugin ;
25
23
private final Map <String , Boolean > commands = new HashMap <>();
26
- private final Plugin plugin ;
27
24
28
- public MetricsWrapper (final Plugin plugin , final int pluginId , final boolean includeCommands ) {
25
+ public MetricsWrapper (final JavaPlugin plugin , final int pluginId , final boolean includeCommands ) {
29
26
this .plugin = plugin ;
30
27
this .ess = (Essentials ) Bukkit .getPluginManager ().getPlugin ("Essentials" );
31
28
this .metrics = new Metrics (plugin , pluginId );
32
29
33
- if (metrics .isEnabled ()) {
34
- plugin .getLogger ().info ("Starting Metrics. Opt-out using the global bStats config." );
35
- } else {
36
- plugin .getLogger ().info ("Metrics disabled per bStats config." );
37
- }
30
+ plugin .getLogger ().info ("Starting Metrics. Opt-out using the global bStats config." );
38
31
39
- checkForcedMetrics ();
40
32
addPermsChart ();
41
33
addEconomyChart ();
42
34
addReleaseBranchChart ();
@@ -51,12 +43,12 @@ public void markCommand(final String command, final boolean state) {
51
43
commands .put (command , state );
52
44
}
53
45
54
- public void addCustomChart (final Metrics . CustomChart chart ) {
46
+ public void addCustomChart (final CustomChart chart ) {
55
47
metrics .addCustomChart (chart );
56
48
}
57
49
58
50
private void addPermsChart () {
59
- metrics .addCustomChart (new Metrics . DrilldownPie ("permsPlugin" , () -> {
51
+ metrics .addCustomChart (new DrilldownPie ("permsPlugin" , () -> {
60
52
final Map <String , Map <String , Integer >> result = new HashMap <>();
61
53
final String handler = ess .getPermissionsHandler ().getName ();
62
54
final Map <String , Integer > backend = new HashMap <>();
@@ -67,7 +59,7 @@ private void addPermsChart() {
67
59
}
68
60
69
61
private void addEconomyChart () {
70
- metrics .addCustomChart (new Metrics . DrilldownPie ("econPlugin" , () -> {
62
+ metrics .addCustomChart (new DrilldownPie ("econPlugin" , () -> {
71
63
final Map <String , Map <String , Integer >> result = new HashMap <>();
72
64
final Map <String , Integer > backend = new HashMap <>();
73
65
final EconomyLayer layer = EconomyLayers .getSelectedLayer ();
@@ -83,23 +75,23 @@ private void addEconomyChart() {
83
75
}
84
76
85
77
private void addVersionHistoryChart () {
86
- metrics .addCustomChart (new Metrics . MultiLineChart ("versionHistory" , () -> {
78
+ metrics .addCustomChart (new MultiLineChart ("versionHistory" , () -> {
87
79
final HashMap <String , Integer > result = new HashMap <>();
88
80
result .put (plugin .getDescription ().getVersion (), 1 );
89
81
return result ;
90
82
}));
91
83
}
92
84
93
85
private void addReleaseBranchChart () {
94
- metrics .addCustomChart (new Metrics . SimplePie ("releaseBranch" , ess .getUpdateChecker ()::getVersionBranch ));
86
+ metrics .addCustomChart (new SimplePie ("releaseBranch" , ess .getUpdateChecker ()::getVersionBranch ));
95
87
}
96
88
97
89
private void addCommandsChart () {
98
90
for (final String command : plugin .getDescription ().getCommands ().keySet ()) {
99
91
markCommand (command , false );
100
92
}
101
93
102
- metrics .addCustomChart (new Metrics . AdvancedBarChart ("commands" , () -> {
94
+ metrics .addCustomChart (new AdvancedBarChart ("commands" , () -> {
103
95
final Map <String , int []> result = new HashMap <>();
104
96
for (final Map .Entry <String , Boolean > entry : commands .entrySet ()) {
105
97
if (entry .getValue ()) {
@@ -112,75 +104,4 @@ private void addCommandsChart() {
112
104
}));
113
105
}
114
106
115
- private boolean isForcedMetricsClass (Class <?> bStatsService ) {
116
- for (String identifier : KNOWN_FORCED_METRICS ) {
117
- if (bStatsService .getCanonicalName ().contains (identifier )) {
118
- return true ;
119
- }
120
- }
121
-
122
- final JavaPlugin owningPlugin = getProvidingPlugin (bStatsService );
123
- if (owningPlugin != null && KNOWN_FORCED_METRICS .contains (owningPlugin .getName ())) {
124
- return true ;
125
- }
126
- return false ;
127
- }
128
-
129
- private void checkForcedMetrics () {
130
- if (hasWarned ) return ;
131
- hasWarned = true ;
132
-
133
- Bukkit .getServer ().getScheduler ().scheduleSyncDelayedTask (plugin , () -> {
134
- for (final Class <?> service : Bukkit .getServicesManager ().getKnownServices ()) {
135
- try {
136
- service .getField ("B_STATS_VERSION" ); // Identifies bStats classes
137
-
138
- if (isForcedMetricsClass (service )) {
139
- warnForcedMetrics (service );
140
- } else {
141
- try {
142
- service .getDeclaredField ("pluginId" ); // Only present in recent bStats classes, which should also have the enabled field unless modified
143
- } catch (final NoSuchFieldException e ) {
144
- // Old bStats class found so "enabled" field detection is unreliable.
145
- break ;
146
- }
147
-
148
- try {
149
- service .getDeclaredField ("enabled" ); // In some modified forced metrics classes, this will fail
150
- } catch (final NoSuchFieldException e ) {
151
- warnForcedMetrics (service );
152
- }
153
- }
154
- } catch (final NoSuchFieldException ignored ) {
155
- }
156
- }
157
- });
158
- }
159
-
160
- private void warnForcedMetrics (final Class <?> service ) {
161
- final Plugin servicePlugin = JavaPlugin .getProvidingPlugin (service );
162
- plugin .getLogger ().severe ("WARNING: Potential forced metrics collection by plugin '" + servicePlugin .getName () + "' v" + servicePlugin .getDescription ().getVersion ());
163
- plugin .getLogger ().severe ("Your server is running a plugin that may not respect bStats' opt-out settings." );
164
- plugin .getLogger ().severe ("This may cause data to be uploaded to bStats.org for plugins that use bStats, even if you've opted out in the bStats config." );
165
- plugin .getLogger ().severe ("Please report this to bStats and to the authors of '" + servicePlugin .getName () + "'. (Offending class: " + service .getName () + ")" );
166
- }
167
-
168
- private JavaPlugin getProvidingPlugin (final Class <?> clazz ) {
169
- try {
170
- return JavaPlugin .getProvidingPlugin (clazz );
171
- } catch (final Exception ignored ) {
172
- }
173
-
174
- final ClassLoader parent = clazz .getClassLoader ().getParent ();
175
- if (parent .getClass ().getName ().equals ("org.bukkit.plugin.java.PluginClassLoader" )) {
176
- try {
177
- final Field pluginField = parent .getClass ().getDeclaredField ("plugin" );
178
- pluginField .setAccessible (true );
179
- return (JavaPlugin ) pluginField .get (parent );
180
- } catch (final Exception ignored ) {
181
- }
182
- }
183
-
184
- return null ;
185
- }
186
107
}
0 commit comments