我正在尝试制作一个插件,添加"变量"进入命令。你使用/ set(variablename)(value)来设置一个值然后你可以使用var:(varname)的任何命令(例如你可以做/设置foo bar然后做" /说var:foo&# 34;它会说" bar"在聊天中)出于某种原因我的
android.system.ErrnoException: write failed: EPIPE (Broken pipe)
要么永远不会执行,要么总是返回false。为什么会这样,我该如何解决?
主要插件类:
else if(Arrays.toString(args).contains("var:")) {
编辑:我知道的方式是我的其他问题,如果我添加
public class main extends JavaPlugin implements Listener {
List<String> vars = new ArrayList<String>();
public void onEnable()
{
getLogger();
getServer().getPluginManager().registerEvents(this, this);
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Variables Enabled!");
}
public void onDisable()
{
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Variables Disabled!");
}
@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {
if(command.getName().equalsIgnoreCase("set")) {
vars.add(args[0] + ":" + args[1]);
sender.sendMessage(ChatColor.RED + "Variable " + args[0] + " added with the value " + args[1]);
}else if(Arrays.toString(args).contains("var:")) { //Line problem is on
int size = args.length;
for (int i=0; i<size; i++)
{
if(args[i].contains("var:")) {
String[] parts = args[i].split(":");
for (String temp : vars) {
String[] varname = temp.split(":");
if(varname[1].equals(parts[1])) {
args[i] = varname[2];
}
}
}
}
}
return super.onCommand(sender, command, label, args);
}
}
在其他地方,我永远不会收到消息&#34; test&#34;即使我在我的args中有var:
答案 0 :(得分:0)
您的问题似乎不是else if
,而是最内层if
。您将命令的变量名称(foo)与先前存储的值(bar)进行比较。请尝试改为:
if(varname[0].equals(parts[1])) {
args[i] = varname[1];
}
当然,使用更合适的集合类型会更好。例如,vars
可能是Map<String, String>
。
答案 1 :(得分:0)
我解决了自己的问题!问题是onCommand仅在从插件中获取命令时触发。我不得不为此使用playercommandpreprocessevent。
答案 2 :(得分:0)
onCommand()方法不允许您过滤播放器发送的所有命令请求,它只允许您实现plugin.yml中列出的命令。我会使用PlayerCommandPreprocessEvent,它允许你捕捉&#34;玩家在被特定插件处理之前尝试执行的所有命令,并更改命令以及后续参数。
我还会使用HashMap而不是List,它们会将String变量的名称和值相互映射。以下是我测试过的一些示例代码:
//Somewhere else
private HashMap<String, String> variableMap = new HashMap<String, String>();
public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
if (command.getName().equalsIgnoreCase("set")) {
if (args.length == 2) { //With this approach, the first argument is the name, the second the value
variableMap.put(args[0], args[1]);
sender.sendMessage(ChatColor.GREEN + "Variable " + args[0] + " now has value " + args[1] + ".");
}
}
return false;
}
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
String[] args = event.getMessage().split(" "); //Get the message and split it to get the arguments
String newMessage = args[0]; //In this example I preserve the original command, so I'm not changing the first argument
for (int i = 1; i < args.length; i++) {
if (args[i].toLowerCase().startsWith("var:")) { //If it starts with "var:", case insensitive...
String[] parts = args[i].split(":");
if (parts.length == 2 && variableMap.containsKey(parts[1])) { //If it has two parts, and the variable map contains the name of that variable
newMessage += " " + variableMap.get(parts[1]); //Add the variable name and not the original argument
continue;
}
}
newMessage += " " + args[i]; //If the argument is not a variable or it is wrongly formatted, add the original argument to the combined string
}
event.setMessage(newMessage); //Set the new command message or contents
}