Spawning entity when there's nobody in the server

Discussion in 'Spigot Plugin Development' started by devcexx, Jul 8, 2015.

  1. Well, what I've done here is a test code for a plugin for Spigot 1.8.7 that spawns an entity when the plugin is enabled and despawns it when the server closes.

    Code (Text):
    public static Main ins;
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            for (Entity e : Bukkit.getWorld("world").getEntitiesByClass(ArmorStand.class)){
                sender.sendMessage(e.getUniqueId().toString());
            }
            sender.sendMessage("END");
            return true;
        }
        @Override
        public void onEnable(){
            ins = this;
            getServer().getPluginManager().registerEvents(this, this);

            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                @Override
                public void run() {
                    World w = Bukkit.getWorld("world");
                    System.out.println("Spawned UUID: " + w.spawn(new Location(w, 0, 0, 0), ArmorStand.class).getUniqueId());
               
                }
            },20L);
        }
        @Override
        public void onDisable(){
            for (Entity e : Bukkit.getWorld("world").getEntitiesByClass(ArmorStand.class)){
                e.remove();
            }
        }
        @EventHandler
        public void onEntitySpawn(EntitySpawnEvent e){
            if (e.getEntity() instanceof ArmorStand)
                System.out.println("Spawn detected: " + e.getEntity().getUniqueId().toString());
        }
    So, my problem is that there's anybody in the server and I make a /reload, appears the println of onEnable() and also the event onEntitySpawn is fired. But when I execute the command to test if there's any entity on the server, it just returns the "END" message. But when I do this process again while I'm the server, the command returns the correct UUID of the spawned entity. So my question is simple: can I spawn entities when there's nobody in the server or is locked by minecraft or I'm doing something wrong or...?
     
  2. The server unloads all chunks that are not near players (unless a plugin prevents the chunk from de-spawning). When there are no players online all chunks are unloaded. When a chunk is unloaded all the entities in that chunk are unloaded too. So when there are no players online, no chunks are loaded and no entities are loaded. The world.getEntities() only return LOADED entities. Loaded entities means entities that get ticked, so unloaded entities are unable to move or do anything basically. Normal mobs/animals also get removed when they get unloaded (unless they are name-tagged, or the de-spawn is cancelled by a plugin). This means that your armor stand is spawning correctly and is not despawned, it is only unloaded, and the world.getEntities() does therefor not include this entity.

    You can bypass this by force-loading and preventing the chunk from spawning if you really need the armor stand to stay loaded. But it will show up on the list as soon as a player gets close to it (or the chunks get loaded for some other reason).
     
    • Useful Useful x 1
  3. Thanks!!! :)
     
    • Funny Funny x 1