bukkit 的一些问题 events/commands

A few problems with bukkit events/commands

我正在开发一个插件,当我 运行 遇到一些错误时:

  1. 在我的重生事件中,它不会给用户带来效果,即使事件本身有效(所有控制台日志都有效)。
  2. 当我创建一个给用户效果的命令时,它并没有给出效果,除非我 运行 命令两次,而且它并不总是清除效果。
  3. 我不明白如何使用 PlayerItemConsumeEvent 检查用户是否有 d运行k 牛奶。

我的玩家重生事件:

@EventHandler
public static void onRespawn(PlayerRespawnEvent e) {
    System.out.println("Player has re-spawned!");
    Player p = e.getPlayer();
    String un = p.getDisplayName();
    System.out.println(p);

    if (p.isOp() || p.hasPermission("lpe.override")) {
        System.out.println(un + "Has OP or the permission lpe.override adding permission overrides. ");
    } else {
        if (isPlayerInGroup(p, "human")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
        } else if (isPlayerInGroup(p, "thief")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 999999999, 1));
        } else if (isPlayerInGroup(p, "viking")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
        } else if (isPlayerInGroup(p, "barbarian ")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
            p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
        } else if (isPlayerInGroup(p, "half_giant")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
        } else if (isPlayerInGroup(p, "fire-giant")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
        } else if (isPlayerInGroup(p, "frost-giant")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
            p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
        } else if (isPlayerInGroup(p, "orc")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
        } else if (isPlayerInGroup(p, "elf")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 2));
        } else if (isPlayerInGroup(p, "dark-elf")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.CONDUIT_POWER, 999999999, 1));
        } else if (isPlayerInGroup(p, "black-elf")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
        } else if (isPlayerInGroup(p, "high-elf")) {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 999999999, 1));
            p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
        }
    }
}

我的命令:

   @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (sender instanceof Player) {
        Player player = (Player) sender;
        String uid = String.valueOf(player.getUniqueId());

        String url = "jdbc:mysql://localhost:3306/ckc?autoRecconect=true&useSSL=false";
        String user = "root";
        String passowrd = "password";
        Connection connection = null;
        String groupName = null;
        String username = player.getDisplayName();
        try {
            for (PotionEffect effect : player.getActivePotionEffects())
                player.removePotionEffect(effect.getType());
            String targetGroup = args[1];
            connection = DriverManager.getConnection(url, user, passowrd);
            System.out.println("connected to MySQL (Operaton 1)!");
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM ckcperms where uuid = ?");
            preparedStatement.setString(1, uid);

            ResultSet resultSet = preparedStatement.executeQuery();
            System.out.println("executing query (Operation 2)");

            if(resultSet.next()) {
                System.out.println("User found! Editing!");
                for (PotionEffect effect : player.getActivePotionEffects())
                    player.removePotionEffect(effect.getType());
                groupName = resultSet.getString(4);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + username + " parent remove " + groupName);
                PreparedStatement statement = connection.prepareStatement("UPDATE ckcperms SET groupId = ? WHERE uuid = ?");
                statement.setString(1, targetGroup);
                statement.setString(2, uid);
                statement.executeUpdate();
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + username + " parent switchprimarygroup " + targetGroup.toLowerCase());
                if(player.isOp() || player.hasPermission("lpe.override")) {
                    System.out.println(username + "Has OP or the permission lpe.override adding permission overrides. ");
                } else {
                    if(isPlayerInGroup(player, "human")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
                    }  else if(isPlayerInGroup(player, "thief")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 999999999, 1));
                    } else if(isPlayerInGroup(player, "viking")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                    } else if(isPlayerInGroup(player, "barbarian ")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
                    } else if(isPlayerInGroup(player, "half_giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
                    } else if(isPlayerInGroup(player, "fire-giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
                    } else if(isPlayerInGroup(player, "frost-giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
                    } else if(isPlayerInGroup(player, "orc")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
                    } else if(isPlayerInGroup(player, "elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 2));
                    } else if(isPlayerInGroup(player, "dark-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONDUIT_POWER, 999999999, 1));
                    } else if(isPlayerInGroup(player, "black-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
                    } else if(isPlayerInGroup(player, "high-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
                    }
                }
                } else {
                System.out.println("User's group not found! Creating user!");
                for (PotionEffect effect : player.getActivePotionEffects())
                    player.removePotionEffect(effect.getType());
                System.out.println("Creating MySQL query (Operation 3)");
                PreparedStatement ps = connection.prepareStatement("INSERT INTO ckcperms ( groupId, uuid) VALUES (?, ?)");
                ps.setString(1, targetGroup);
                ps.setString(2, uid);
                ps.executeUpdate();
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + username + " parent switchprimarygroup " + targetGroup.toLowerCase());
                System.out.println("Executing MySQL query (Check 2)");
                PreparedStatement pt = connection.prepareStatement("SELECT * FROM `ckcperms` WHERE `uuid`= ? AND `groupId`= ?");
                pt.setString(1, uid);
                pt.setString(2, targetGroup);
                ResultSet rs = pt.executeQuery();
                if(player.isOp() || player.hasPermission("lpe.override")) {
                    System.out.println(username + "Has OP or the permission lpe.override adding permission overrides. ");
                } else {
                    if(isPlayerInGroup(player, "human")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
                    }  else if(isPlayerInGroup(player, "thief")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 999999999, 1));
                    } else if(isPlayerInGroup(player, "viking")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                    } else if(isPlayerInGroup(player, "barbarian ")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
                    } else if(isPlayerInGroup(player, "half_giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
                    } else if(isPlayerInGroup(player, "fire-giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
                    } else if(isPlayerInGroup(player, "frost-giant")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 4));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 999999999, 4));
                    } else if(isPlayerInGroup(player, "orc")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
                    } else if(isPlayerInGroup(player, "elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 2));
                    } else if(isPlayerInGroup(player, "dark-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONDUIT_POWER, 999999999, 1));
                    } else if(isPlayerInGroup(player, "black-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999999, 1));
                    } else if(isPlayerInGroup(player, "high-elf")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 999999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999999, 1));
                    }
                }
                if(rs.next()) {
                    System.out.println("User updated!");
                }
                 preparedStatement.executeQuery();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    return true;
}

尝试以这种方式应用效果:

PotionEffect eff = 
    new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE, 1);
eff.apply(player);

而且你不必在应用药水时重复自己,创建方法来执行此操作,然后仅在需要时调用该方法。