2014-07-07 42 views
-3

我正在研究一個bukkit插件,當玩家退出時我需要保存數據。我寫了一個可以正常工作的系統,但是它基於玩家的名字,如果玩家改變他們的名字就會丟失數據。所以我的問題是:每次玩家登錄時是否有任何財產(例如:身份證)女巫是否一樣?存儲玩家特定數據

+2

[第一款谷歌的結果(HTTPS: //www.google.com/search?q=java%20minecraft%20get%20player%20id):[從玩家名稱移至UUID。](https://forums.bukkit.org/threads/tut-moving-from -player-names-to-uuid.241626 /) – BackSlash

回答

1

你可以使用播放器的UUID,或ü niversal ü SER ID entification。要獲得玩家的UUID,您可以使用player.getUniqueId()。您也可以使用OfflinePlayers

用於存儲當玩家通過自己的UUID將退出的實例方法:

@EventHandler 
public void playerQuit(PlayerQuitEvent e){ 
    String uuid = e.getPlayer().getUniqueId().toString(); // get the user's UUID 
    long time = System.currentTimeMillis()/1000; // get the current number of seconds 
    plugin.getConfig().set(uuid, time); // set the UUID in the config to the current # of seconds 
} 

然後,讓他們最後出場的日子:

public Date getLastPlayedDate(String p){ 
    String uuid = Bukkit.getOfflinePlayer(p).getUniqueId().toString(); // get the uuid from the OfflinePlayer 
    long time = plugin.getConfig().getLong(uuid); // get the time associated with the uuid 
    Date date = new Date(time * 1000); // convert the time to the date 
    return date; // return the date 
} 
+0

注意:'UUID'代表__Universally Unique Identifier__。 – Unihedron

+0

@Unihedron它可以代表許多事情。大多數情況下,使用Minecraft玩家時,它是* Universal User Identifier *,因爲它只與用戶打交道。如果我們正在做一些類似Minecraft的小怪,它將是一個*通用唯一標識符*,因爲我們沒有對用戶做任何事情。 – Jojodmo

+0

__Universally唯一標識符___是由OSF創建和設置的術語。如果你能指出我的資源,清楚說明你剛剛說的話,那將是驚人的。否則,[這裏是維基百科條目](http://en.wikipedia.org/wiki/Universally_unique_identifier)。 – Unihedron