Need help checking over code

Discussion in 'Plugin Development' started by plisov, Oct 12, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    plisov

    Hey,
    I'm trying to make a method that would allow players to spawn a random amount of mobs and have those mobs follow a random target. I have everything setup and working expect for the targeting part. Here is what I have currently.
    Code:
                    Player target = null;
    
                    Location playerLoc = player.getLocation();
    
                    Location center = loc;
                    double distance = radius;
                    distance = Math.pow(distance, 2);
                    List<Player> playersInZone = new ArrayList<Player>();
                    // NOTE: Storing players in a list is not the best idea,
                    // But the method Bukkit.getPlayer(String name) is deprecated.
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.getLocation().distanceSquared(center) <= distance) {
                            playersInZone.add(p);
                        }
                    }
                    int num = (int) Math.random() * playersInZone.size();
                    target = playersInZone.get(num);
                    for (int i = 0; i < circleAmount; i++) {
                        filledCircle(target, loc.add(0, 0, 0), 25, radius, entity);
    
                        for (int d = 0; d < zombieAmount; d++) {
    
                            int chance = getRandom(-5, 5);
    
                            Creature zombie = (Creature) player.getWorld().spawnEntity(loc.add(chance, 0, chance),
                                    EntityType.ZOMBIE);
    
                            zombie.setLastDamage(12);
                            zombie.setTarget(target);
    
                            player.getWorld().spawnParticle(Particle.FLAME, zombie.getLocation(), 100);
                            player.getWorld().spawnParticle(Particle.SMOKE_LARGE, playerLoc, 100);
    
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 1));
                        }
    
                        for (int d = 0; d < skeletonAmount; d++) {
    
                            // int chance = getRandom(-5, 5);
    
                            Creature skeleton = (Creature) player.getWorld().spawnEntity(playerLoc, EntityType.SKELETON);
    
                            skeleton.setTarget(player);
    
                        }
    
                        for (int d = 0; d < spiderAmount; d++) {
    
                            // int chance = getRandom(-5, 5);
    
                            Creature spider = (Creature) player.getWorld().spawnEntity(playerLoc, EntityType.SPIDER);
    
                            spider.setTarget(player);
    
                        }
                    }
    For some reason even though I have the target set to a random person that doesn't equal the person who ran the method, it still sometimes sets the target as the player who ran the method.

    Any help is much appreciated,
    plisov
     
  2. Offline

    Zombie_Striker

    @plisov
    The entities targetting system targets valid entities that are the closest to the entity. To counter act this, create a bukkit runnable for each entity. Every couple of ticks (10 ticks should be enough), reset the entity's target.
     
  3. Offline

    plisov

    Would I be able to do this with a cooldown that uses a HashMap instead of a Bukkit Runnable?
     
  4. Offline

    MightyOne

    @Zombie_Striker there is also the EntityTargetEvent. No need for a Runnable if you can prevent the targeting there.
    Code:
    int num = (int) Math.random() * playersInZone.size();
    This multplies the size of playersInZone with 0 = always 0. The cast (int) just casts Math.random() back to zero;
     
Thread Status:
Not open for further replies.

Share This Page