Entities spawned in GameStartedServerEvent are invisible?

I have the following code that spawns an ArmorStand in the position 0,80,0 in the main world:

    public void onServerStarted(GameStartedServerEvent event) {
    	Optional<World> worldOptional = Sponge.getServer().getWorld("world");
    	if (worldOptional.isPresent()) {

    		World world = worldOptional.get();

    		    Entity entity = world.createEntity(EntityTypes.ARMOR_STAND, new Vector3d(0, 80, 0));

    		    if (world.spawnEntity(
    				    Cause.source(EntitySpawnCause.builder().entity(entity).type(SpawnTypes.PLUGIN)).build())) {
    			    Log.info("The entity was spawned.");

When starting the server, the text “The entity was spawned” successfully prints in the log. Calling entity.isLoaded() returns true and entity.isRemoved() returns false.

The problem is that the spawned entity is completely invisible. However, if I move the same exact code from GameStartedServerEvent to ClientConnectionEvent.Join then it works just fine. I am not using a different location or anything, it is the same exact code.

Am I missing something here or should this work? Does it work for you?

My guess is either sponge has not fully loaded so it wont allow the spawning of entities (dont think that one is true due to the fact even sponge says that it spawned the entity).
My other guess is its to do with minecraft efficiency, check on entity remove event for that same entity.

I think that the entity is not invisible but just despawned due to the lack of interaction in that area

I’ve been struggling with this for several hours now. Sometimes it randomly works and sometimes it doesn’t work, sometimes you have to disconnect and connect another time when having that code in ClientConnectionEvent.Join.

I am starting to get really annoyed with how buggy this is :confused:

What if you forcibly loaded the chunk before placing the entity in it?

1 Like

Ah, thank you, this seems to have fixed it.

I didn’t consider this as I thought the chunk should already be loaded. The docs say that the worlds are loaded at that state, so I assumed that included the spawn chunks for the world:

The GameStartedServerEvent event is triggered. The server instance exists, and worlds are loaded.

And as it’s a chunk near 0,0,0 it should always be loaded, so I didn’t even think to try this.

I still feel like there’s a problem here. Either world.spawnEntity() should return false or there is a bug that spawning entities in unloaded chunks makes them invisible or not spawn properly. Do you think I should submit an issue?

The spawn point isn’t always at 0,0,0. There should be a method in World to get the approximate spawn location. (There is some natural variation in where players spawn; they spawn within several blocks of the real spawn point.)

16, to be exact.