Solved Every player gets plague rather than certain amount

Discussion in 'Plugin Development' started by voltywolty, Dec 4, 2021.

  1. Offline

    voltywolty

    Hey all. I'm working on a plague system that makes a certain amount of players affected by it. However, all players are still being affected by it regardless of the count of monsters. Not too sure where I'm going wrong, but if someone could help me out, that would be great.

    Code:
    package main.me.volt.dvz.events;
    
    import com.nisovin.magicspells.MagicSpells;
    import com.nisovin.magicspells.mana.ManaChangeReason;
    import main.me.volt.dvz.DvZ;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import java.util.*;
    
    public class PlagueEvent extends GameEvent {
        private Set<Player> plagued = new HashSet<>();
    
        public PlagueEvent(DvZ plugin) {
            super(plugin);
        }
    
        public String getName() {
            return "plague";
        }
    
        public void run() {
            List<UUID> players = new ArrayList<>();
            for (Player p : Bukkit.getOnlinePlayers()) {
                players.add(p.getUniqueId());
                p.sendMessage(ChatColor.RED + "You feel other around you beginning to feel a bit sick...");
            }
    
            int monsterCount = this.plugin.monsters.size();
            int monstersWanted = 4;
    
            if (Bukkit.getOnlinePlayers().size() <= 7) {
                monstersWanted = Bukkit.getOnlinePlayers().size() / 2;
            }
            System.out.println("Monster Count: " + monsterCount + " | Monsters Wanted: " + monstersWanted);
    
            if (monsterCount < monstersWanted) {
                for (int i = monsterCount; i < monstersWanted; i++) {
                    System.out.println("Monsters not full. Finding more victims.");
    
                    for (UUID uuid : players) {
                        Player player = Bukkit.getPlayer(uuid);
    
                        if (player == null)
                            continue;
    
                        if (!this.plugin.dwarves.contains(player.getPlayer())) {
                            System.out.println("Tried to infect " + player.getName() + ", but that player is not a dwarf!");
                            continue;
                        }
                        if (this.plagued.contains(player.getPlayer())) {
                            System.out.println("Tried to infect " + player.getName() + ", but that player already has the plague!");
                        }
                        if (monsterCount > monstersWanted) {
                            break;
                        }
    
                        System.out.println("Player " + player.getName() + " has been given the plague!");
                        plaguePlayer(player);
                        continue;
                    }
                }
            }
            this.plugin.releaseMonsters();
        }
    
        private void plaguePlayer(final Player player) {
            this.plagued.add(player.getPlayer());
    
            player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, Integer.MAX_VALUE, 3));
            player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, Integer.MAX_VALUE, 1));
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 2));
            player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, Integer.MAX_VALUE, 4));
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, Integer.MAX_VALUE, 2));
    
            if (player.getHealth() > 20.0D) {
                player.setHealth(20.0D);
            }
            MagicSpells.getManaHandler().addMana(player, -1000, ManaChangeReason.OTHER);
    
            player.sendMessage(ChatColor.DARK_RED + "You have contracted the " + ChatColor.GREEN + "Zombie Plague" + ChatColor.DARK_RED + "!");
            Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
                public void run() {
                    if (player.isValid() && PlagueEvent.this.plugin.dwarves.contains(player.getPlayer())) {
                        player.setHealth(0.0D);
                    }
                }
            }, 600L);
        }
    
        public void setDwarf(Player paramPlayer) {
            this.plagued.remove(paramPlayer);
        }
    
        public boolean deathCountsAsKillForMonsterTeam(Player paramPlayer) {
            return !this.plagued.contains(paramPlayer);
        }
    
        public DamageResult checkDamage(Player attacker, Player defender) {
            if (this.plugin.dwarves.contains(attacker) && this.plugin.dwarves.contains(defender) && this.plagued.contains(defender)) {
                return DamageResult.ALLOW;
            }
            return DamageResult.NORMAL;
        }
    }
    
     
  2. Offline

    Strahan

    Well, you open the monster processing section with this logic statement:
    Code:
    if (monsterCount < monstersWanted) {
    Then inside the loop, to determine if you should break you have:
    Code:
    if (monsterCount > monstersWanted) {
    Neither monsterCount nor monstersWanted are being modified within the block, so essentially that break can never be hit. What I'd do is make a randomized collection of online players, then loop that and check how many were plague'd and break when appropriate.

    The random thing depends on how bukkit returns the players collection. It may already be sufficiently random, I never tested. But like if it sorts by something like player name or whatever, I'd shuffle it up. Actually I just tested; logged in with the six MC accounts I have. The order is just by join time, so yea, I'd probably shuffle it.
     
    Last edited: Dec 5, 2021
  3. Offline

    voltywolty

    You are correct about that. I managed to fix it a while ago, so thank you.
     

Share This Page