Skip to content

Instantly share code, notes, and snippets.

@noobmobile
Created October 12, 2019 01:21
Show Gist options
  • Save noobmobile/faac2d5202a394bb3efd3ec79ed7290e to your computer and use it in GitHub Desktop.
Save noobmobile/faac2d5202a394bb3efd3ec79ed7290e to your computer and use it in GitHub Desktop.
Evento CommandManager, sistema de top 10 comandos mais executados
package com.dont.commandmanager;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements CommandExecutor, Listener{
private final HashMap<String, Integer> statistics = new HashMap<>();
public void onEnable() {
saveDefaultConfig();
getCommand("topcommands").setExecutor(this);
Bukkit.getPluginManager().registerEvents(this, this);
for (String data : getConfig().getStringList("Data")) { // formato na config: /comando:quantidade
String[] splittedData = data.split(":");
statistics.put(splittedData[0], Integer.parseInt(splittedData[1]));
}
}
public void onDisable() {
getConfig().set("Data", statistics.entrySet().stream().map(entry -> entry.getKey()+":"+entry.getValue()).collect(Collectors.toList())); // salva as estatisticas com o formato de: /comando:quantidade
saveConfig();
}
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent e) {
String command = e.getMessage().contains(" ") ? e.getMessage().toLowerCase().split(" ")[0].trim() : e.getMessage().toLowerCase().trim(); // pega sempre o comando (caso tenha argumentos)
statistics.put(command, statistics.getOrDefault(command, 0) + 1);
}
@EventHandler
public void onCommand(ServerCommandEvent e) {
String command = e.getCommand().contains(" ") ? e.getCommand().toLowerCase().split(" ")[0].trim() : e.getCommand().toLowerCase().trim(); // pega sempre o comando (caso tenha argumentos)
command = "/" + command;
statistics.put(command, statistics.getOrDefault(command, 0) + 1);
}
private final int TOP = 10;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
LinkedHashMap<String, Integer> sorted = statistics.entrySet().stream()
.sorted(Entry.comparingByValue(Comparator.reverseOrder()))
.limit(TOP)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (cmd, qntd) -> {throw new IllegalStateException();}, LinkedHashMap::new));
AtomicInteger i = new AtomicInteger(0);
sender.sendMessage("");
sender.sendMessage("§eTop comandos: ");
sorted.forEach((cmd, qntd) -> {
sender.sendMessage("§e"+i.incrementAndGet()+"º "+cmd+": §f"+qntd);
});
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment