【问题标题】:bukkit - How to use a variable on config.yml?bukkit - 如何在 config.yml 上使用变量?
【发布时间】:2016-12-07 21:14:29
【问题描述】:

我对 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.yaml 字符串中的 %player% 是 sender.getDisplayName() @Kerooker
  • 你写问题的方式,我根本无法理解你想要什么

标签: java yaml minecraft bukkit


【解决方案1】:

您要做的是从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!"

【讨论】:

  • 我会说,将逻辑分步进行,使其更易于他人阅读和理解并在将来维护。如果你愿意,我可以为你编辑你的帖子。
  • 哦,谢谢!我会测试它,然后接受作为答案,谢谢
  • @bramhaag 我应该怎么做才能在该方法中使用替代和颜色代码?
【解决方案2】:

我认为参数调用更好的解决方案,不仅应该通过替换它来使用特定的参数,对于长期的解决方案应该是这样的,并且str表示为来自yaml的参数:

ChatColor.translateAlternateColorCodes('&', str.replace(StringUtils.substringBetween(str, "%", "%"), msg_config.getString(StringUtils.substringBetween(str, "%", "%"))).replace("%",""));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2015-11-13
    相关资源
    最近更新 更多