Spawn Mobs in an Area

Discussion in 'Plugin Development' started by EnderTroll68, Mar 18, 2014.

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

    EnderTroll68

    Hey Bukkit Forums!

    I am making a plugin where I spawn 50 mobs within an area, but I can not get it to work. I am doing this:
    Code:
        public static void startGame() {
     
            File file = new File(Bukkit.getServer().getPluginManager().getPlugin("SquireGames").getDataFolder().getAbsolutePath(), "main.yml");
            YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
     
            Location loc = new Location(Bukkit.getWorld(yml.getString("spawn.world")), yml.getDouble("spawn.x"), yml.getDouble("spawn.y"), yml.getDouble("spawn.z"));
            World world = loc.getWorld();
     
            for (int i = 0; i < 50; i++) {
                Location spawnSpot = loc;
                spawnSpot.setX(spawnSpot.getX() - 50);
                spawnSpot.setZ(spawnSpot.getZ() - 50);
     
                spawnSpot.setX(spawnSpot.getX() + Math.random() * 100);
                spawnSpot.setZ(spawnSpot.getZ() + Math.random() * 100);
     
                world.spawnEntity(spawnSpot, EntityType.ZOMBIE);
                Bukkit.broadcastMessage(spawnSpot.toString());
            }
     
        }
    And it is spawning mobs randomly all over the place, both inside and outside where I want them. I have a box that is 100x100 and I have the center point defined in a config. How do I get this to work? What am I doing wrong?
     
  2. Offline

    xTrollxDudex

  3. Offline

    xXSniperzzXx_SD

    xTrollxDudex Just saying it will not work doesn't really help him fix it...

    EnderTroll68
    I believe something like this should work
    Code:java
    1.  
    2. Location spawnSpot = ...;
    3. for (int i = 0; i < 50; i++) {
    4.  
    5. Random r = new Random();
    6. int i = r.nextInt(200)-100;
    7. spawnSpot.setX(spawnSpot.getX() + i);
    8. spawnSpot.setZ(spawnSpot.getZ() + i);
    9.  
    10. world.spawnEntity(spawnSpot, EntityType.ZOMBIE);
    11. Bukkit.broadcastMessage(spawnSpot.toString());
    12. }
    13.  
     
  4. Offline

    xTrollxDudex

    xXSniperzzXx_SD
    Two things went wrong:
    First, yes, it will help. It's basic math, I expect him to look at the algorithm closely again.
    Second, don't spoon feed. That's counterintuitive and doesn't help anymore than my post.
     
  5. Offline

    EnderTroll68

    xXSniperzzXx_SD
    The only thing I have to call from the files is the very center of the area, and then I am going from there.

    xTrollxDudex
    I was not asking to be spoon fed code, and also I am aware that it does not work, what I am asking is for the math aspect of this, not the code.
     
  6. Offline

    xTrollxDudex

    EnderTroll68
    I was referring to Sniperzz about the spoonfeeding.

    I recommend you to this SO post about bounded randoms.
     
  7. Offline

    EnderTroll68

    xTrollxDudex
    Code:java
    1. Location loc = new Location(Bukkit.getWorld(yml.getString("spawn.world")), yml.getDouble("spawn.x"), yml.getDouble("spawn.y"), yml.getDouble("spawn.z"));
    2. World world = loc.getWorld();
    3. for (int i = 0; i < 50; i++) {
    4. Location spawnSpot = loc;
    5. spawnSpot.setX(spawnSpot.getX() + (-50 + (Math.random() * 101)));
    6. spawnSpot.setY(spawnSpot.getY() + (-50 + (Math.random() * 101)));
    7. world.spawnEntity(spawnSpot, EntityType.ZOMBIE);
    8. }


    So like that then?
     
Thread Status:
Not open for further replies.

Share This Page