Created
August 22, 2016 18:33
-
-
Save MusicalCreeper01/de2ea8a77702e083f3b800bdb5f6ac73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.icodethings.scoreboarddeathcounter; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.PlayerDeathEvent; | |
import org.bukkit.event.player.PlayerJoinEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.scoreboard.*; | |
import java.util.*; | |
public class ScoreboardKillCounter extends JavaPlugin implements Listener { | |
// you MUST use a LinkedHashMap, otherwise sorting won't work! | |
// you use it the same way as you would a normal hashmap | |
public LinkedHashMap<String, Integer> playerKills = new LinkedHashMap<>(); | |
ScoreboardManager manager; | |
Scoreboard board; | |
Objective objective; | |
@Override | |
public void onEnable() { | |
// register the main class as an event listener | |
// Register the listener in the main class for the sake of simplicity in this example | |
getServer().getPluginManager().registerEvents(this, this); | |
//Create the scoreboard and objective | |
System.out.println("Creating scoreboard.."); | |
manager = Bukkit.getScoreboardManager(); | |
board = manager.getNewScoreboard(); | |
objective = board.registerNewObjective("kills", "dummy"); | |
objective.setDisplaySlot(DisplaySlot.SIDEBAR); | |
objective.setDisplayName(ChatColor.GREEN + "*= Player Kills =*"); | |
System.out.println("Scoreboard created!"); | |
} | |
@EventHandler | |
public void onPlayerDeath(PlayerDeathEvent event) { | |
Player player = event.getEntity().getPlayer(); // The player who died | |
Entity e = player.getKiller(); | |
if (e instanceof Player) { // check if the killer was a player | |
Player killer = (Player) e; // Get the killer player | |
System.out.println("Player: " + player.getDisplayName() + " was killed by " + killer.getDisplayName()); | |
if (playerKills.containsKey(killer.getDisplayName())) // IF the player is already in the map, add 1 to their score | |
playerKills.put(killer.getDisplayName(), playerKills.get(killer.getDisplayName()) + 1); | |
else // Otherwise add them to the hashmap | |
playerKills.put(killer.getDisplayName(), 1); | |
//Update the scoreboard | |
updateScoreboard(); | |
} | |
} | |
@EventHandler | |
public void onPlayerJoin(PlayerJoinEvent event) { | |
//Add the scoreboard to the player | |
event.getPlayer().setScoreboard(board); | |
} | |
public void updateScoreboard() { | |
//Sort the kills hashmap | |
LinkedHashMap<String, Integer> sorted = sortHashMapByValues(playerKills); | |
//Display them on the scoreboard | |
Integer playersToShow = 5; | |
Integer i = 0; | |
for (Map.Entry<String, Integer> entry : sorted.entrySet()) { // Loop over all the key-values in the hashmap | |
String playername = entry.getKey(); // get the name for the current player | |
Integer kills = entry.getValue(); // get the kills for the current player | |
//Add the player's name to the scoreboard | |
Score score = objective.getScore(playername); | |
//Set the score to their kills | |
score.setScore(kills); | |
++i; | |
if (i == playersToShow) // if we've gotten all the players we wanted, stop looping | |
break; | |
} | |
} | |
@Override | |
public void onDisable() { | |
//Fired when the server stops and disables all plugins | |
} | |
/* | |
Modified function for sorting the HashMap's Integer values, original from: | |
http://stackoverflow.com/questions/8119366/sorting-hashmap-by-values/8119406#8119406 | |
*/ | |
public LinkedHashMap<String, Integer> sortHashMapByValues(HashMap<String, Integer> passedMap) { | |
List<String> mapKeys = new ArrayList<>(passedMap.keySet()); | |
List<Integer> mapValues = new ArrayList<>(passedMap.values()); | |
Collections.sort(mapValues); | |
Collections.sort(mapKeys); | |
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>(); | |
Iterator<Integer> valueIt = mapValues.iterator(); | |
while (valueIt.hasNext()) { | |
Integer val = valueIt.next(); | |
Iterator<String> keyIt = mapKeys.iterator(); | |
while (keyIt.hasNext()) { | |
String key = keyIt.next(); | |
Integer comp1 = passedMap.get(key); | |
Integer comp2 = val; | |
if (comp1.equals(comp2)) { | |
keyIt.remove(); | |
sortedMap.put(key, val); | |
break; | |
} | |
} | |
} | |
return sortedMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment