如何向播放器对象添加属性?

时间:2022-10-22 16:44:43

I don't quite know if those are the correct words, but in my plugin, the players have to choose a class and I don't quite know how to separate the classes. Each class gets unique spells/abilities. If I could add a property (e.g. a boolean for each class; true if they are that class, false otherwise) to a player and return the value, that would solve my problem. How would I do this?

我不太清楚这些是不是正确的单词,但在我的插件中,玩家必须选择一个类,我不太清楚如何分类。每个班级都有独特的法术/能力。如果我可以为播放器添加一个属性(例如每个类的布尔值;如果它们是那个类则为true,否则为false)并返回值,这将解决我的问题。我该怎么办?

2 个解决方案

#1


2  

If the property does not need to be maintained across server instances, use the Metadatable interface of Player. However, it is difficult (and slow) to use and is not saved on shut down or reload.

如果不需要跨服务器实例维护该属性,请使用Player的Metadatable接口。但是,使用起来很困难(而且很慢),并且在关闭或重新加载时不会保存。

You can use a Map to map the UUID of each player to the property:

您可以使用Map将每个玩家的UUID映射到该属性:

private static final Map<UUID, Boolean> playerProperties = new HashMap<>();

@Override
public void onEnable() {
    // Load from file / database
}

@Override
public void onDisable() {
    // Save to file / database
}

From there, you can get the property of a player with playerProperties.get(player.getUUID()).

从那里,您可以使用playerProperties.get(player.getUUID())获取玩家的属性。

However, since the property is a boolean value, it is more efficient algorithm-wise to use a Set and store players with the property, checking the property with Set#contains:

但是,由于该属性是一个布尔值,因此使用Set并使用该属性存储播放器在算法方面更有效,使用Set#contains检查属性:

private static final Set<UUID> playerProperties = new HashSet<>();

@Override
public void onEnable() {
    // Load from file / database
}

@Override
public void onDisable() {
    // Save to file / database
}

public boolean getPlayerProperty(Player player) {
    return playerProperties.contains(player.getUUID());
}

public void setPlayerProperty(Player player, boolean newProperty) {
    if (newProperty)
        playerProperties.add(player.getUUID());
    else
        playerProperties.remove(player.getUUID());
}

#2


-1  

I think you can do this with Metadata. Take a look at https://hub.spigotmc.org/javadocs/bukkit/ at Package org.bukkit.metadata

我认为你可以用元数据做到这一点。在包org.bukkit.metadata上查看https://hub.spigotmc.org/javadocs/bukkit/

#1


2  

If the property does not need to be maintained across server instances, use the Metadatable interface of Player. However, it is difficult (and slow) to use and is not saved on shut down or reload.

如果不需要跨服务器实例维护该属性,请使用Player的Metadatable接口。但是,使用起来很困难(而且很慢),并且在关闭或重新加载时不会保存。

You can use a Map to map the UUID of each player to the property:

您可以使用Map将每个玩家的UUID映射到该属性:

private static final Map<UUID, Boolean> playerProperties = new HashMap<>();

@Override
public void onEnable() {
    // Load from file / database
}

@Override
public void onDisable() {
    // Save to file / database
}

From there, you can get the property of a player with playerProperties.get(player.getUUID()).

从那里,您可以使用playerProperties.get(player.getUUID())获取玩家的属性。

However, since the property is a boolean value, it is more efficient algorithm-wise to use a Set and store players with the property, checking the property with Set#contains:

但是,由于该属性是一个布尔值,因此使用Set并使用该属性存储播放器在算法方面更有效,使用Set#contains检查属性:

private static final Set<UUID> playerProperties = new HashSet<>();

@Override
public void onEnable() {
    // Load from file / database
}

@Override
public void onDisable() {
    // Save to file / database
}

public boolean getPlayerProperty(Player player) {
    return playerProperties.contains(player.getUUID());
}

public void setPlayerProperty(Player player, boolean newProperty) {
    if (newProperty)
        playerProperties.add(player.getUUID());
    else
        playerProperties.remove(player.getUUID());
}

#2


-1  

I think you can do this with Metadata. Take a look at https://hub.spigotmc.org/javadocs/bukkit/ at Package org.bukkit.metadata

我认为你可以用元数据做到这一点。在包org.bukkit.metadata上查看https://hub.spigotmc.org/javadocs/bukkit/