Random Giant spawn help

Discussion in 'Plugin Development' started by pkt, Dec 2, 2012.

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

    pkt

    I am having trouble getting giants to spawn randomly
    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onCreatureSpawn(CreatureSpawnEvent event) {
            Entity entity = event.getEntity();
            EntityType type = event.getEntityType();
     
            if (!API.getGiantSpawnWorlds().contains(entity.getWorld().getName())) {
                return;
            }
     
            if ((type == EntityType.ZOMBIE) || (type == EntityType.SKELETON) || (type == EntityType.SPIDER) || (type == EntityType.CREEPER) || (type == EntityType.ENDERMAN)) {
                String string = API.getConfig().getProperty(config, Config.spawnChance);
                int sRate;
                try {
                    sRate = Integer.parseInt(string);
                } catch (NumberFormatException e) {
                    sRate = 0;
                }
                int chance = 100 - sRate;
     
                Random rand = new Random();
                int choice = rand.nextInt(100) < chance ? 1 : 0;
                if (choice == 0) {
                    Location location = event.getEntity().getLocation();
                    double x = location.getX();
                    double y = location.getY();
                    double z = location.getZ();
     
                    int x2 = (int) x;
                    int y2 = (int) y;
                    int z2 = (int) z;
     
                    int spawngiant = 1;
                    int checkcount = 0;
                    while (checkcount < 10) {
                        y2 += checkcount;
     
                        if (entity.getWorld().getBlockTypeIdAt(x2, y2, z2) != 0) {
                            spawngiant = 0;
                        }
                        checkcount++;
                    }
                    if (spawngiant == 1) {
                        SpawnEvent SE = new SpawnEvent(location);
                        Bukkit.getServer().getPluginManager().callEvent(SE);
                    }
                }
            }
        }
    My SpawnEvent class states:

    Code:
        public SpawnEvent(Location loc) {
            location = loc;
     
            if (!API.getGiantSpawnWorlds().contains(loc.getWorld().getName())) {
                setCancelled(true);
            }
     
            if (!isCancelled()) {
                entity = loc.getWorld().spawnEntity(location, EntityType.GIANT);
            }
        }
    Can anyone help me out?
     
  2. Offline

    microgeek

    Why not have a utility to get a random boolean, check if the entity is hostile, cancel the event, then spawn a giant there? Looks like you have a lot more code than needed. Also, are you getting any errors?
     
  3. Offline

    pkt

    Could you show me an example :3 I don't really understand...

    Nope, no errors...
     
  4. Offline

    microgeek

    Run the same check you had, with the EntityType, then use the boolean below(Modify it to your probability), and if it's true, spawn a giant at the desired spot.

    PHP:
    public boolean getRnd() {
      
    Random r = new Random();
      return 
    t.nextInt(1) == 0;
    }
     
     
Thread Status:
Not open for further replies.

Share This Page