Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Metrics: simplify getPlayerAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Nov 7, 2023
1 parent 0f3615e commit 2a9af95
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions src/main/java/net/flectone/chat/model/metric/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
Expand Down Expand Up @@ -114,18 +112,7 @@ private void appendServiceData(JsonObjectBuilder builder) {
}

private int getPlayerAmount() {
try {
// Around MC 1.8 the return type was changed from an array to a collection,
// This fixes java.lang.NoSuchMethodError:
// org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
return onlinePlayersMethod.getReturnType().equals(Collection.class)
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
} catch (Exception e) {
// Just use the new method if the reflection failed
return Bukkit.getOnlinePlayers().size();
}
return Bukkit.getOnlinePlayers().size();
}

public static class MetricsBase {
Expand Down

0 comments on commit 2a9af95

Please sign in to comment.