[Resource] Keeping track of your players name's!

Discussion in 'Resources' started by Deleted user, May 15, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Deleted user

    Alright fellow Bukkiteers. I'm sure we're all well aware of the recent changes to Minecraft, more specifically the integration of player identification based upon UUID instead of a player's name.

    So, basically I've developed a system that will automatically store the current name of a player based upon their UUID. Name changes are detected upon login and previous names are stored. Within this, I've added an easier method to obtain a player without the difficulty of obtaining their UUID. (Please keep in mind you will need to store the data, rather with an ObjectOutputStream or whatever your preferred method of data storage may be.) Lets begin.

    Code:java
    1.  
    2. // Storage for current player names
    3. public static HashMap<UUID, String> players = new HashMap<UUID, String>();
    4. // Storage for previous player names
    5. public static HashMap<UUID, ArrayList<String>> nameChanges = new HashMap<UUID, ArrayList<String>>();
    6.  


    Code:java
    1.  
    2. // This is pretty self-explanatory. When a player successfully connect, obtain some information about said player and call upon another method.
    3. @EventHandler
    4. private void updatePlayerList(PlayerJoinEvent event) {
    5. Player player = event.getPlayer();
    6. UUID ID = player.getUniqueId();
    7. updatePlayerList(ID, player.getName());
    8. }
    9.  


    Code:java
    1.  
    2. public void updatePlayerList(UUID ID, String player) {
    3. if (players.containsKey(ID)) {
    4. String oldName = players.get(ID);
    5. if (players.get(ID).equals(player))
    6. return;
    7. players.put(ID, player);
    8. nameChanges.get(ID).add(player);
    9. Bukkit.getServer().getConsoleSender().sendMessage("Detected name change for " + oldName + ". New name: " + player + ". Player name updated for UUID: " + ID.toString());
    10. } else {
    11. players.put(ID, player);
    12. nameChanges.put(ID, new ArrayList<String>());
    13. nameChanges.get(ID).add(player);
    14. }
    15. }
    16.  


    Code:java
    1.  
    2. public Player getPlayer(String player) {
    3. for (UUID ID : players.keySet()) {
    4. if (players.get(ID).equals(player))
    5. return Bukkit.getServer().getPlayer(ID);
    6. }
    7. return null;
    8. }
    9.  


    Code:java
    1.  
    2. public String getPreviousNames(UUID ID) {
    3. if (nameChanges.get(ID) == null)
    4. return "No previous name changes.";
    5.  
    6. ArrayList<String> previousNames = nameChanges.get(ID);
    7. String names = "";
    8.  
    9. for (String name : previousNames) {
    10. names += name + ", ";
    11. }
    12.  
    13. return names;
    14. }
    15.  
     
    L33m4n123 likes this.
  2. Offline

    Bammerbom

    Eballer48 This will not work after a reload, we need to store the information in a file.
     
  3. Offline

    GermanCoding

    Jhtzb
     
  4. Offline

    Bammerbom

Thread Status:
Not open for further replies.

Share This Page