Bukkit op 命令不再有效

Bukkit op command no longer works

我一直在创建一个插件,我注意到 op 命令不起作用。

当我在控制台中输入 op 时,它显示 "please specify a player"。当我 运行 带有参数的命令时,这不应该发生,也不会发生。在游戏中,它说 "You are now op!" 我应该收到这条消息,但我也应该获得 op 权限,我 没有 得到。

我不知道是什么问题。这是我的代码:

public class System extends JavaPlugin implements Listener{

    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");
    }

    public void onDisable() {
        Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent joinevent){
        Player getplayer = joinevent.getPlayer();
        getplayer.sendMessage(ChatColor.AQUA + "Hey " + getplayer.getName() + "! Welcome to the Ultimate Prison server!");
        if(getplayer.getName().equals("Herobrine112211")){
            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.GOLD + "Server Creator Herobrine112211 has joined the game!!!!!!!!!!");
        }
    }

    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){

            if (cmd.getName().equalsIgnoreCase("contactowner")) {
                if (!sender.hasPermission("contactowner")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
                    return true;
                }
                if(sender instanceof Player){
                    Player player = (Player) sender;
                player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
                return true;
                }
            }

            if (cmd.getName().equalsIgnoreCase("reloadconfig")){
                if (!sender.hasPermission("config.reload")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                reloadConfig();
                sender.sendMessage(ChatColor.RED + "Server config reloaded successfully.");
                return true;
            }

            if (cmd.getName().equalsIgnoreCase("saveconfig")){
                if (!sender.hasPermission("config.save")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                reloadConfig();
                sender.sendMessage(ChatColor.RED + "Server config saved successfully.");
                return true;
            }

            if (cmd.getName().equalsIgnoreCase("fakeop")) {
                if (!sender.hasPermission("fake.op")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Player must be online!");
                    return true;
                }
                target.sendMessage(ChatColor.YELLOW + "You are now op!");
            }

            if (cmd.getName().equalsIgnoreCase("fakejoin")) {
                if (!sender.hasPermission("fake.join")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a name!");
                    return true;
                }
                Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " joined the game.");
            }

            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                if (!sender.hasPermission("broadcast")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please give a message to broadcast!");
                    return true;
                }
                StringBuilder strbl = new StringBuilder();
                for(int i = 0; i < args.length; i++){
                    strbl.append(args[i] + " ");
                }
                String broadcastas = strbl.toString();
                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.RED + broadcastas);
            }

            if (cmd.getName().equalsIgnoreCase("heal")) {
                if (!sender.hasPermission("heal")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Please specify a player!");
                        return true;
                }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                            return true;
                    }
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + "CONSOLE" + " healed you!");
                    sender.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
                    return true;
                }
                    if(sender instanceof Player){
                    if (args.length == 0) {
                        Player player = (Player) sender;
                            player.setHealth(20);
                            player.setFireTicks(0);
                            player.sendMessage(ChatColor.GREEN + "Your health is now full!");
                            return true;
                    }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                            return true;
                    }
                    Player player = (Player) sender;
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
                    player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
                    }
            }

            if (cmd.getName().equalsIgnoreCase("feed")) {
                if (!sender.hasPermission("feed")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Please specify a player!");
                        return true;
                }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be fed!");
                            return true;
                    }
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + "CONSOLE" + " fed you!");
                    sender.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
                    return true;
                }
                if(sender instanceof Player){
                if (args.length == 0) {
                    Player player = (Player) sender;
                        player.setFoodLevel(20);
                        player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
                        return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.RED + "Player must be online to be fed!");
                        return true;
                }
                Player player = (Player) sender;
                target.setFoodLevel(20);
                target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
                player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
                }
        }

            if(cmd.getName().equalsIgnoreCase("tpall")){
                if (!sender.hasPermission("teleport")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Cannot teleport everyone to console!");
                    return true;
                }
                if(sender instanceof Player){
                if(args.length == 0){
                    for(Player allplayers : Bukkit.getOnlinePlayers()) {
                        Player player = (Player) sender;
                        World w = player.getLocation().getWorld();
                        double x = player.getLocation().getX();
                        double y = player.getLocation().getY();
                        double z = player.getLocation().getZ();
                        allplayers.teleport(new Location(w, x, y, z));
                        player.sendMessage(ChatColor.GREEN + "All players teleported to you!");
                        return true;
                    }
                }
                for(Player allplayers : Bukkit.getOnlinePlayers()) {
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if(target == null){
                        Player player = (Player) sender;
                        player.sendMessage(ChatColor.RED + "Player must be online!");
                        return true;
                    }
                    Player player = (Player) sender;
                    World w = target.getLocation().getWorld();
                    double x = target.getLocation().getX();
                    double y = target.getLocation().getY();
                    double z = target.getLocation().getZ();
                    allplayers.teleport(new Location(w, x, y, z));
                    player.sendMessage(ChatColor.GREEN + "All players teleported to player!");
                    return true;
                }
              }
            }

            if(cmd.getName().equalsIgnoreCase("setspawn")){
                if (!sender.hasPermission("setspawn")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "CONSOLE Cannot set spawn!");
                    return true;
                }
                if(sender instanceof Player){
                Player player = (Player) sender;
                getConfig().set("spawn.world", player.getLocation().getWorld());
                getConfig().set("spawn.x", player.getLocation().getX());
                getConfig().set("spawn.y", player.getLocation().getY());
                getConfig().set("spawn.z", player.getLocation().getZ());
                saveConfig();
                player.sendMessage(ChatColor.GREEN + "Spawn has been set!");
                return true;
                }
            }

            if(cmd.getName().equalsIgnoreCase("spawn")){
                if (!sender.hasPermission("spawn")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "CONSOLE Cannot teleport to spawn!");
                    return true;
                }
                if(getConfig().getConfigurationSection("spawn") == null){
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.RED + "Spawn is not set!");
                    return true;
                }
                if(sender instanceof Player){
                Player player = (Player) sender;
                World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                double x = getConfig().getDouble("spawn.x");
                double y = getConfig().getDouble("spawn.y");
                double z = getConfig().getDouble("spawn.z");
                player.teleport(new Location(w, x, y, z));
                return true;
                }
            }
        return false;
   }
}

这是我的 plugin.yml 文件

name: UltimateCommandPack
version: 1.0.6
main: me.herobrine112211.ulticmdpack.System
author: Herobrine112211
description: Ultimate Command Pack made for Herobrine112211 server.

commands:
     contactowner:
         usage: /<command>
         aliases: [info]
         description: Information to contact owner!
     heal:
         usage: /<command>
         aliases: [health]
         description: Healing command.
     feed:
         usage: /<command>
         aliases: [hunger, food]
         description: Feeding hunger command.
     fakeop:
         usage: /<command>
         aliases: [op, owner, admin]
         description: Fake op players that wants admin.
     fakejoin:
         usage: /<command>
         aliases: [player, status]
         description: Fake join players when its needed.
     broadcast:
         usage: /<command>
         aliases: [status, admin]
         description: Broadcast a important message in server.
     tpall:
         usage: /<command>
         aliases: [tp, admin]
         description: Teleports all players to specific player.
     setspawn:
         usage: /<command>
         aliases: [location, admin]
         description: Sets player respawn or spawn.
     spawn:
         usage: /<command>
         aliases: [location, player]
         description: Teleports player to respawn.


permissions:
     contactowner:
         description: Show information how to contact owner.
         default: true
     config.reload:
         description: Reload plugin config file.
         default: op
     config.save:
         description: Save plugin config file.
         default: op
     fake.op:
         description: Give fake op to players.
         default: op
     fake.join:
         description: Fake join any player using any name.
         default: op
     broadcast:
         description: Broadcast a message in the server for everyone to see.
         default: op
     heal:
         description: Heal any player or yourself.
         default: op
     feed:
         description: Feed any player or yourself.
         default: op
     setspawn:
         description: Set spawn location for players.
         default: op
     spawn:
         description: Teleport your self or others to someone long version.
         default: true
     teleport:
         description: Teleports all players to specific player.
         default: op

使用op命令时,控制台没有报错。我认为可能是 fake.op 权限或 /fakeop 命令导致了问题。

您收到错误是因为您将 op 命令作为 fakeop 的别名。您的 plugin.yml,有:

fakeop:
     usage: /<command>
     aliases: [op, owner, admin] # << /op is an alias of /fakeop
     description: Fake op players that wants admin.

如果你想解决这个问题,你应该删除 /op 作为 /fakeop 的别名:

fakeop:
     usage: /<command>
     aliases: [owner, admin]
     description: Fake op players that wants admin.

如果您想将 /op 保留为 /fakeop 的别名,并使 /op 只有在来自控制台的 运行 时才能正常工作,您可以使用:

if(cmd.getName().equalsIgnoreCase("fakeop")){
  if(!sender.hasPermission("fake.op")){
    sender.sendMessage(ChatColor.RED + "Access denied.");
    return true;
  }
  if(args.length == 0){
    sender.sendMessage(ChatColor.RED + "Please specify a player!");
    return true;
  }

  Player target = Bukkit.getServer().getPlayer(args[0]);

  if(target == null){
    sender.sendMessage(ChatColor.RED + "Player must be online!");
  }
  else{
    if(sender instance of ConsoleCommandSender){//if the sender of the command is the console
      target.setOp(true); //op the player
    }
    target.sendMessage(ChatColor.YELLOW + "You are now op!");
  }
  return true;
}