Skip to content

Instantly share code, notes, and snippets.

@Bastian
Last active May 13, 2021 17:43
Show Gist options
  • Save Bastian/b47279b4ff792153480b50ad0945c963 to your computer and use it in GitHub Desktop.
Save Bastian/b47279b4ff792153480b50ad0945c963 to your computer and use it in GitHub Desktop.
Advanced Pie example
@Override
public void onEnable() {
Metrics metrics = new Metrics(this);
metrics.addCustomChart(new AdvancedPie("most_famous_foods", new Callable<Map<String, Integer>>() {
@Override
public Map<String, Integer> call() throws Exception {
Map<String, Integer> valueMap = new HashMap<>();
valueMap.put("Apple", getPlayersWithFood(Material.APPLE));
valueMap.put("Bread", getPlayersWithFood(Material.BREAD));
return valueMap;
}
private int getPlayersWithFood(Material food) {
int counter = 0;
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getInventory().contains(food)) {
counter++;
}
}
return counter;
}
}));
}
// If you use the Copy & Paste Metrics classes, use `Metrics.AdvancedPie` instead
@Andre601
Copy link

This could be simplified to my knowledge by using anonymous lambda (Or what you call those)

Example:

@Override
public void onEnable() {
    Metrics metrics = new Metrics(this);
    metrics.addCustomChart(new Metrics.AdvancedPie("most_famous_foods", () -> {
        Map<String, Integer> values = new HashMap<>();
        
        values.put("Apple", getPlayersWithFood(Material.APPLE));
        values.put("Bread", getPlayersWithFood(Material.BREAD));
        
        private int getPlayersWithFood(Material food) {
            int counter = 0;
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (player.getInventory().contains(food)) {
                    counter++;
                }
            }
            return counter;
        }

        return values;
    }));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment