作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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!
错误是:
最佳答案
您想要做的是从 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/
我是一名优秀的程序员,十分优秀!