有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java与bukkit事件/命令有关的一些问题

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

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

我的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) 个答案

  1. # 1 楼答案

    尝试以以下方式应用效果:

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

    在使用药剂时,你们不必重复你们自己,创建一个方法来做到这一点,然后只在需要时调用那个方法