'Mutating' mobs

Discussion in 'Plugin Development' started by epicfacecreeper, May 7, 2013.

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

    epicfacecreeper

    A plugin I am developing has a feature where mobs randomly 'mutate' to a more powerful mob when they spawn, such as zombies to giants, skeletons to wither skeletons.

    I am currently using CreatureSpawnEvent and a Random to determine whether they mutate, but this EXTREMELY lags the server, 20x so at night. Can anyone suggest a better way to do this?

    Here's my code if you need it. There's some extra stuff about zombies in there, but that isn't it, I commented that out before testing it.

    Code:
    public class MonsterSpawn implements Listener {
        private final Apocalyptic a;
        @EventHandler
        public void onMonsterSpawn(CreatureSpawnEvent e) {
            Random r = new Random();
            if (e.getEntityType() == EntityType.ZOMBIE) {
                if (a.getConfig().getBoolean("worlds." + e.getLocation().getWorld().getName() + ".mobs.mutants.zombie")) {
                    Location l = e.getLocation();
                    if (r.nextInt(300) == 0) {
                        e.setCancelled(true);
                        l.getWorld().spawnEntity(l, EntityType.GIANT);
                        return;
                    }
                    if (a.getConfig().getDouble("worlds." + e.getLocation().getWorld().getName() + ".mobs.zombies.speedMultiplier") != 1.0)
                        ((Zombie) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, (int) a.getConfig().getDouble("worlds." + e.getLocation().getWorld().getName() + ".mobs.zombies.speedMultiplier"), 999999999));
                    return;
                    if (e.getSpawnReason() != SpawnReason.CUSTOM) {
                        int hordeSize = r.nextInt(
                                a.getConfig().getWorld(e.getEntity().getWorld()).getInt("mobs.zombies.hordeSize.max") -
                                a.getConfig().getWorld(e.getEntity().getWorld()).getInt("mobs.zombies.hordeSize.min")) +
                                a.getConfig().getWorld(e.getEntity().getWorld()).getInt("mobs.zombies.hordeSize.min");
                        for (int i=0;i<hordeSize;i++) {
                            int spotX = 7-r.nextInt(14);
                            int spotZ = 7-r.nextInt(14);
                            Location spawnPoint = l.add(spotX, 0, spotZ);
                            spawnPoint.setY(l.getWorld().getHighestBlockYAt(spotX, spotZ));
                         
                            l.getWorld().spawnEntity(spawnPoint, EntityType.ZOMBIE);
                         
                        }
                    }
                }
            }
            if (e.getEntityType() == EntityType.CREEPER) {
                if (a.getConfig().getBoolean("worlds." + e.getLocation().getWorld().getName() + ".mobs.mutants.creeper")) {
                    Location l = e.getLocation();
                    if (r.nextInt(100) == 0) {
                        ((Creeper) e.getEntity()).setPowered(true);
                        return;
                    }
                }
            }
            if (e.getEntityType() == EntityType.SKELETON) {
                if (a.getConfig().getBoolean("worlds." + e.getLocation().getWorld().getName() + ".mobs.mutants.skeleton")) {
                    if (r.nextInt(100) == 0) {
                        ((Skeleton) e.getEntity()).setSkeletonType(Skeleton.SkeletonType.WITHER);
                        ((Skeleton) e.getEntity()).getEquipment().setItemInHand(new ItemStack(Material.IRON_SWORD, 0));
                    }
                }
            }
        }
        public MonsterSpawn(Apocalyptic a) {
            this.a = a;
        }
    }
    
    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  2. Offline

    ferrybig

    you can use a integer for this, add 1 by it every time the event is run, if its 300 set its to 0 and spawn a big zombie
     
  3. Offline

    epicfacecreeper

    Will that be more efficient though? I think my problem might be the creation of a new Random every spawn. That was kind of stupid.
     
  4. Offline

    boolean

    That should fix it, Running a random number every time is stupid.. You can also easily make the number (300) changeable in the config.yml
     
  5. Offline

    DSH105 Retired Staff

Thread Status:
Not open for further replies.

Share This Page