gpt4 book ai didi

java - bukkit - 如何在 config.yml 上使用变量?

转载 作者:行者123 更新时间:2023-11-30 07:00:24 25 4
gpt4 key购买 nike

我对 Bukkit 的配置 API 还很陌生,所以我有一个问题:如何在 YAML 文件上设置一个包含 %player% 之类的字符串来代表玩家(例如,当有人使用包含以下内容的命令时进行广播)可以配置的发件人姓名)。如何将sender.getDisplayName()解析为config.yml中的%player%?

我现在的代码是:

String message = core.getConfig().getString("message"); //Get the message from the config file
message = message.replace("%player%", player.getDisplayName()); //Replace %player% with the player's name.
ChatColor.translateAlternateColorCodes('&', message);
Bukkit.broadcastMessage(message);

我的 config.yml 是:

message: &6%player% just launched the cow party!

错误是:

http://pastebin.com/8AdA5Xe1 (这么久)

最佳答案

您想要做的是从 config.yml 文件中获取字符串,然后您需要将 %player% 替换为玩家的名称或显示名称。

像这样的东西会起作用;

String message = getConfig().getString("path.to.string"); //Get the message from the config file
message = message.replace("%player%", player.getName()); //Replace %player% with the player's name.
ChatColor.translateAlternateColorCodes('&', message); //Translate colors to color codes
player.sendMessage(message); //Send the message

您还可以使用这一行:

player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("path.to.message").replace("%player%", player.getName()));

当然,您需要将 player 替换为您的有效播放器。

关于您的错误:

您需要在消息两边加上引号 ("),因此请更改

message: &6%player% just launched the cow party!

进入

message: "&6%player% just launched the cow party!"

关于java - bukkit - 如何在 config.yml 上使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41027495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com