gpt4 book ai didi

java - onCommand() 设置正确但根本没有执行

转载 作者:行者123 更新时间:2023-12-02 09:38:20 25 4
gpt4 key购买 nike

我的问题是我有一个简单的 Minecraft 插件,我只想让它执行一个命令。我以前做过命令,所以步骤对我来说很清楚。我的plugin.yml 设置正确(服务器检测我添加的命令并显示它的帮助页面等),并且 onCommand() 函数的设置方式也与我在所有其他插件中的设置方式相同。插件本身可以工作(主要是我测试的 onEnable() 函数),但是 onCommand() 不会被调用。

我已经尝试了不同的plugin.yml 格式以及向onCommand() 添加@Override 注释,但Eclipse 并不真正希望我这样做。我还知道我使用的 API (com.PluginBase) 可以在其他项目中使用它。执行命令时不会产生异常,聊天中只是显示我输入的命令。

这是 Main.java:

package org.Professions;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.material.Command;
import org.bukkit.plugin.java.JavaPlugin;

import com.PluginBase.Chat;

public class Main extends JavaPlugin {

public void onEnable() {
Chat.getInstance().sendErrorMessageToConsole("Professions enabled");
Bukkit.getPluginCommand("profession").setExecutor((CommandExecutor) this);
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

Chat.getInstance().sendErrorMessageToConsole("Got the command: " + label);

/*
* Check if command sender is a player
* and if the command is for this plugin
*/
if
(
sender instanceof Player == false
|| !label.toLowerCase().equals("profession")
) {
return true;
}

// Get the player who send the command
Player player = (Player) sender;

// Check if the player has given the right amount of arguments
if (args.length != 1) {

// Notify the player of invalid argument use
Chat.getInstance().sendMessageToPlayer(player, ChatColor.RED + "Invalid arguments. Usage: /profession <name>");

// Stop executing code after we've determined an incorrect amount of arguments
return true;
}

// Get the players new profession from the first argument he gave for the command
String profession = args[1];

// Set the players name in the playerlist to feature his professions
player.setPlayerListName
(
ChatColor.GREEN + "[" + profession + "] " // the players' profession
+ ChatColor.WHITE + player.getName() // the actual player name
);

// Always return true since if the command wasn't for this plugin we return false earlier
return true;
}
}

这是我的plugin.yml:

name: Professions
main: org.Professions.Main
version: 1.0
api-version: 1.13
depend: [PluginBase]
commands:
profession:
description: Change your profession
usage: /<command>
aliases: [p]

最佳答案

您忘记实现 CommandExecutor 并注册命令。

您的代码应如下所示:

public class Main extends JavaPlugin implements CommandExecutor{

@Override
public void onEnable(){
//...
getCommand("profession").setExecutor(this);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

//...
}
}

另外,我想给你一些关于 Minecraft 插件编码的建议。您不应该在主类中发出命令,它们应该属于自己的类。例如,您的 profession 命令将进入 ProfessionCmd 类及其所有参数(例如,/profession Give 也会在那里)。

关于java - onCommand() 设置正确但根本没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57325101/

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