2014-07-07 45 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