Spawning creatures every x block

Discussion in 'Plugin Development' started by linkrock4, Nov 28, 2012.

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

    linkrock4

    Hey, i was just wondering if any of you had any code example to show me how to spawn creatures every x blocks, for example, every 500 blocks.

    Thank you!

    no one?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Would kill the server (and maybe even corrupt the world while crashing), but here you go:
    Code:java
    1. Chunk c;
    2. boolean unload,
    3. int rx ry;
    4. Location loc;
    5. for(int x = Integer.MIN_VALUE; x < Integer.MAX_VALUE / 500; x++)
    6. for(int z = Integer.MIN_VALUE; z < Integer.MAX_VALUE / 500; z++)
    7. {
    8. loc = new Location(world, x + 500, 0, y + 500);
    9. c = loc.getChunk();
    10. if(!c.isLoaded())
    11. {
    12. c.load();
    13. unload = true;
    14. }
    15. else
    16. unload = false;
    17. loc.setY(world.getHighestBlockYat(loc.getX(), loc.getZ());
    18. world.spawnEntity(loc, EntityType.ZOMBIE);
    19. if(unload)
    20. c.unload(true);
    21. }

    (out of my mind, may contain errors)
     
  3. Offline

    linkrock4

    Thanks alot! want me to tell you the errors cuz first time i do this so i got no idea whats wrong with it.
     
  4. If I would know the errors I would have fixed them. As told b4: This is just from my mind...

    Also keep in mind that this will crash your server for sure you will load/unload hundreds, if not thousands or millions of chunks ans spawn entities there. Event if RAM should be a lesser problem cause it unloads the chunk after use this may take days to execute completely...
     
  5. Offline

    linkrock4

  6. linkrock4 That's doable, yes. Read and understand the codes to see how. But why not save where the entity should spawn and spawn it in onChunkLoad() then? Should be way less performance intensive...
    For the errors:
    - The world error you can/must fix for yourself.
    - You imported the wrong Location class.
    - The line "int rx ry;" is missing a character, but also it's not needed at all (again, read and understand codes to see why. If you can't understand codes: Don't use them!).
     
  7. Offline

    linkrock4

    V10lator
    I have fixed almost everything but the world error and the y that is not resolved to a variable are something im not understanding, ill be looking more into it.
     
  8. linkrock4 Hint: You want to spawn that entities in a pre-defined world... I seriously can't pre-define it for you.
     
  9. Offline

    linkrock4

    I fixed all the errors, now just to find out how to make a maximum of chunks, which i got no idea how, you got any ideas? :/

    EDIT: Nvm i did not fix the errors :confused: It randomly came back.. D:
     
  10. Change Integer.MIN_VALUE and Integer.MAX_VALUE. Again: Read and understand the codes. If you don't understand them don't use them! This is like driving a car without license...
     
  11. Offline

    linkrock4

    Im trying to understand them :D Im still a beginner coder :p
     
  12. linkrock4 Well, that's fine, we all started without knowledge. :)
    I would recommend to you to read the code line by line, then guess what each line does, try&error a bit with simpler codes and if you don't know what something does google it / use bukkits or javas documentation.
     
  13. Offline

    linkrock4

    V10lator
    Yup, one last question, should the code be placed in a onBlockPlace? Or should it look like this:

    Code:
    package net.skydevs.src;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.block.Block;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.inventory.ItemStack;
    import java.util.Random;
     
    public class Christmas extends JavaPlugin implements Listener {
        Random rand = new Random();
        public String ChristmasMessage;
     
        public void onEnable() { // Server Starts
            getLogger().info("Christmas Plugin has been enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
        public void onDisable() { // Server Stops
            getLogger().info("Christmas Plugin has been disabled!");
     
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String Label,
                String[] args) {
             
            if (cmd.getName().equalsIgnoreCase("Christmas")) {
                Bukkit.broadcastMessage(ChatColor.BLACK+ "["+ ChatColor.RED+ "Christmas"+ ChatColor.BLACK+ "] "+ ChatColor.GREEN+ "Happy "+ ChatColor.RED+ "Christmas! The plugin has been activated!");
     
            }
     
            return false;
        }
        // Presents
        @EventHandler
        @SuppressWarnings("deprecation")
        public void onBlockPlace(BlockPlaceEvent e) {
            Block b = e.getBlockPlaced();
            if (b.getTypeId() == 56) {
                int id = 0;
                id = rand.nextInt(400);
                if (Material.getMaterial(id) == null) {
                    e.getPlayer().getInventory().addItem(new ItemStack(56, 1));
                    e.getBlockPlaced().breakNaturally(null);
                }
                else {
                    e.getPlayer().sendMessage(
                            ChatColor.BLACK + "[" + ChatColor.RED + "Christmas"
                                    + ChatColor.BLACK + "] " + ChatColor.GREEN
                                    + "Ho! Ho! Ho! " + ChatColor.RED
                                    + "Merry Christmas! Enjoy your gift!");
                    e.getPlayer().getInventory().addItem(new ItemStack(id, 1));
                    e.getPlayer().updateInventory();
                    e.getPlayer().getInventory().removeItem(new ItemStack(56, 1));
                    e.getPlayer().updateInventory();
                    e.setCancelled(true);
     
                } 
            }
                //Weather
     
            }
            // Snowman Spawning
            Chunk c;
            boolean unload;
            int rx ;
            int ry ;
            Location loc; {
            for(int x = Integer.MIN_VALUE; x < Integer.MAX_VALUE / 500; x++)
                for(int z = Integer.MIN_VALUE; z < Integer.MAX_VALUE / 500; z++)
                {
                    loc = new Location(world, x + 500, 0, y + 500);
                    c = loc.getChunk();
                    if(!c.isLoaded())
                    {
                        c.load();
                        unload = true;
                    }
                    else
                        unload = false;
                    loc.setY(world.getHighestBlockYat(loc.getX(), loc.getZ()));
                    world.spawnEntity(loc, EntityType.SNOWMAN);
                    if(unload)
                        c.unload(true);
                }
        // Snow under blocks
    }
    }
     
  14. I don't understand your question. Either the code is in the event or your spacing is messed up (won't read full code, just following spacing).

    BTW: Just saw that you call updateInventory() more times. Normally it shouldn't be needed at all, but if it is one call (at the end) is enough and all other is wasting resources.

    Also make sure to backup your world(s) before executing this codes, as I warned you b4. ^^
     
  15. Offline

    linkrock4

    V10lator
    Okay well, instead of using the code you sent me because it will crash the world i made:
    Code:
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            if(e.getPlayer().getNearbyEntities(e.getPlayer().getLocation().getX() + 100, e.getPlayer().getLocation().getY() + 100, e.getPlayer().getLocation().getZ() + 100).contains(EntityType.SNOWMAN)) {
            } else {
                spawnSnowMan(e.getPlayer().getLocation());
            }
            }
        public void spawnSnowMan(Location loc) {
            loc.setX(loc.getX() + 100);
            loc.setZ(loc.getZ() + 100);
            loc.setY(loc.getY() + 5);
            loc.getWorld().spawnEntity(loc, EntityType.SNOWMAN);
        }
    }
    But it doesnt work, any idea why?

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page