Search code examples
javaminecraftbukkit

A few problems with bukkit events/commands


I was working on a plugin, when i ran into a few errors:

  1. On my respawn event, it doesn't give the user the effects, even though the event itself works (all out the console logs worked.)
  2. When I created a command to give the user effects, it doesn't give the effects, unless I run the command twice, and it doesn't always clear the effects.
  3. I don't understand how to use the PlayerItemConsumeEvent to check if the user has drunk milk.

My 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));
        }
    }
}

My Command:

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

Solution

  • Try applying effects this way:

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

    And you do not have to repeat yourself when applying potions, create method to do that, and then only call that metod when needed.