gpt4 book ai didi

java - bukkit 命令已识别 n

转载 作者:行者123 更新时间:2023-12-02 02:53:53 26 4
gpt4 key购买 nike

我正在制作一个带有服务器基本命令的插件,并且我已经添加了几个运行良好的事件。但是当我尝试添加命令时,它不起作用。 (在你问之前,我已经用一个简单的 sendMessage 替换了 onCommand 部分中的所有内容,并且它不执行任何操作)。当我执行该命令时,它只是告诉我在plugin.yml 中放入的用法。

这是我的代码:抱歉它真的很长

package com.pepsi.core;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {

@Override
public void onEnable() {

Bukkit.getLogger().info("PepsiCore >> Enabled!");
Bukkit.getServer().getPluginManager().registerEvents(this, this);

getConfig().options().copyDefaults(true);
saveConfig();

}

@Override
public void onDisable() {

Bukkit.getLogger().info("PepsiCore >> Disabled!");

}

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

if (args.length == 0) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help &e to see a list of commands!"));
return false;
}

if (args.length == 1) {

if (args[0].equalsIgnoreCase("help")) {
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
sender.sendMessage(Util.transMsg("&6/pcore help &8>> &eDisplays the help page"));
sender.sendMessage(Util.transMsg("&6/pcore info &8>> &eDisplays the plugin info page"));
sender.sendMessage(Util.transMsg("&6/pcore ranks &8>> &eDisplays all the ranks"));
sender.sendMessage(Util.transMsg("&6/pcore setrank <player> <rank> &8>> &eDisplays all the ranks"));
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
return true;
}

if (args[0].equalsIgnoreCase("info")) {
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
sender.sendMessage(Util.transMsg("&ePepsiCore version &61.0.0-BETA"));
sender.sendMessage(Util.transMsg("&eDeveloped by &6P3pi &efor &6Project: Infernal"));
sender.sendMessage(Util.transMsg("&8--------------------<&6&lPepsiCore&8>--------------------"));
return true;
}

if (args[0].equalsIgnoreCase("ranks")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eRanks: &6guest&e, &6gold&e, &6diamond&e, &6emerald&e, &6yt&e, &6staff&e, &6developer&e, &6owner"));
return false;
}

if (args[0].equalsIgnoreCase("setrank")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}

sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}

if (args.length == 2) {

if (args[0].equalsIgnoreCase("setrank")) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eNot enough arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}

sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}

if (args.length == 3) {

if (args[0].equalsIgnoreCase("setrank")) {

Player t = Bukkit.getServer().getPlayer(args[1]);
if (t == null) {
sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eThe player &6" + args[1] + " &eis not online at the moment!"));
return false;
}

String uuid = t.getUniqueId().toString();
String rank = args[2];

if (rank == ("guest")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("gold")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("diamond")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("emerald")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("yt")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("staff")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("developer")) { getConfig().set("ranks." + uuid, rank); }
else if (rank == ("owner")) { getConfig().set("ranks." + uuid, rank); }
else { sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eThe rank &6" + rank + " &e does not exist! Use &6/pcore ranks &eto see a list of ranks!")); }

}

sender.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eInvalid arguments! Use &6/pcore help&e to see a list of commands!"));
return false;
}

return true;
}

@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {

Player p = e.getPlayer();
String uuid = p.getUniqueId().toString();
String name = p.getName();
String msg = e.getMessage();

String rank = (String) getConfig().get("ranks." + uuid);

if (rank == "guest") { e.setFormat(Util.transMsg("&fGuest &7&l" + name + " &8>> &f" + msg)); }
else if (rank == "gold") { e.setFormat(Util.transMsg("&eGold &6&l" + name + " &8>> &e" + msg)); }
else if (rank == "diamond") { e.setFormat(Util.transMsg("&bDiamond &3&l" + name + " &8>> &b" + msg)); }
else if (rank == "emerald") { e.setFormat(Util.transMsg("&aEmerald &2&l" + name + " &8>> &a" + msg)); }
else if (rank == "yt") { e.setFormat(Util.transMsg("&dYouTuber &5&l" + name + " &8>> &d" + msg)); }
else if (rank == "staff") { e.setFormat(Util.transMsg("&9Staff &1&l" + name + " &8>> &9" + msg)); }
else if (rank == "developer") { e.setFormat(Util.transMsg("&cDeveloper &4&l" + name + " &8>> &c" + msg)); }
else if (rank == "owner") { e.setFormat(Util.transMsg("&cOwner &4&l" + name + " &8>> &c" + msg)); }
else { e.setCancelled(true); p.sendMessage(Util.transMsg("&e<!> &6&lPepsiCore &8>> &eYour rank seems to be broken! Please let a member of staff know!")); }

}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {

Player p = e.getPlayer();
String uuid = p.getUniqueId().toString();

if (p.hasPlayedBefore() == false) {
getConfig().set("ranks." + uuid, "guest");
saveConfig();
return;
}

}

}

这是我的plugin.yml:

name: PepsiCore
author: P3pi
version: 1.0.3
main: com.pepsi.core.Main

commands:
pcore:
description: Core command
usage: /pcore
pepsicore:
description: Core command
usage: /pepsicore

感谢您的帮助

最佳答案

Bukkit 处理命令的方式是运行 JavaPlugin 类的 onCommand 方法。具体来说,onCommand 方法采用参数 CommandSender sender、Command command、String label、String[] args

正如 Vlexing 提到的,您的方法的参数顺序错误,因此在运行命令时它不会运行您的方法,而是从 JavaPlugin 运行 onCommand 方法> 仅包含return false;。由于您的plugin.yml,Bukkit 仍然可以识别您的命令,但是当它尝试运行您的方法时,它得到了false,因此它打印出了您的使用消息。

基本上,只需调整参数的顺序并遵循 Vlexing 的答案即可。

关于java - bukkit 命令已识别 n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43397568/

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