bukkit事件/命令的一些问题

时间:2020-11-11 15:16:28

标签: java minecraft bukkit

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

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

我的PlayerRespawnEvent:

@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;
}

1 个答案:

答案 0 :(得分:1)

尝试以这种方式应用效果

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

并且在使用药水时不必重复自己,创建方法来这样做,然后仅在需要时调用该方法。