Last active
August 29, 2015 14:12
-
-
Save njb-said/cec2f56907e8eaa54021 to your computer and use it in GitHub Desktop.
A Bukkit handler for uuid-java
This file contains 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 net.mcapi.uuid.handlers; | |
import java.util.UUID; | |
import java.util.concurrent.TimeUnit; | |
import net.mcapi.uuid.UUIDAPI; | |
import net.mcapi.uuid.UUIDHandler; | |
import net.mcapi.uuid.queries.APIQuery; | |
import net.mcapi.uuid.utils.UUIDUtils; | |
import org.bukkit.Bukkit; | |
/** | |
* A {@link Bukkit} handler | |
* <p> | |
* Checks offline player and online | |
* players before sending a query | |
* | |
* <p> | |
* Licensed under GPL v2 as found <a href="https://github.com/MC-API/uuid-java/blob/master/LICENSE">here</a> | |
* | |
* @author njb_said | |
*/ | |
@SuppressWarnings("deprecation") | |
public class BukkitHandler extends UUIDHandler { | |
@Override | |
public UUID getUUID(String username) { | |
username = username.toLowerCase(); | |
if(Bukkit.getPlayer(username) != null) { | |
return Bukkit.getPlayer(username).getUniqueId(); | |
} | |
if(Bukkit.getOfflinePlayer(username) != null) { | |
return Bukkit.getOfflinePlayer(username).getUniqueId(); | |
} | |
if(uuid_cache.containsKey(username)) { | |
return uuid_cache.get(username); | |
} | |
APIQuery query = new APIQuery(username, "full_uuid", "uuid"); | |
try { | |
UUID uuid = UUID.fromString(query.request()); | |
uuid_cache.put(username, uuid, 1, TimeUnit.HOURS); | |
return uuid; | |
} catch (Exception ex) { | |
System.err.println("[MC-API] Could not lookup '" + username + "', returning null.."); | |
System.err.println("[MC-API] Server: " + UUIDAPI.getRegion().toString() + " (" + UUIDAPI.getRegion().buildURL() + ")"); | |
System.err.println("[MC-API] Error: " + ex.getLocalizedMessage()); | |
return null; | |
} | |
} | |
@Override | |
public String getUUIDString(String username) { | |
UUID UUID = getUUID(username); | |
String uuid = UUID == null ? null : UUID.toString().replace("-", ""); | |
return uuid; | |
} | |
@Override | |
public String getUsername(UUID uuid) { | |
if(Bukkit.getPlayer(uuid) != null) { | |
return Bukkit.getPlayer(uuid).getName(); | |
} | |
if(name_cache.containsKey(uuid)) { | |
return name_cache.get(uuid); | |
} | |
if(Bukkit.getOfflinePlayer(uuid).hasPlayedBefore()) { | |
return Bukkit.getOfflinePlayer(uuid).getName(); | |
} | |
APIQuery query = new APIQuery(uuid.toString().replace("-", ""), "name"); | |
try { | |
String username = query.request(); | |
name_cache.put(uuid, username, 1, TimeUnit.HOURS); | |
return username; | |
} catch (Exception ex) { | |
System.err.println("[MC-API] Could not lookup uuid '" + uuid.toString().replace("-", "") + "', returning null.."); | |
System.err.println("[MC-API] Server: " + UUIDAPI.getRegion().toString() + " (" + UUIDAPI.getRegion().buildURL() + ")"); | |
System.err.println("[MC-API] Error: " + ex.getLocalizedMessage()); | |
return null; | |
} | |
} | |
@Override | |
public String getUsername(String uuid) { | |
return getUsername(UUID.fromString(UUIDUtils.convertUUIDToJava(uuid))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment