Having difficulty getting Mobs to spawn Help?

Hi sor here’s the code that’s erroring out…

Optional<Entity> optional = extent
        .createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());

and here’s the compiliation error

MobSpawner.java:19: error: incompatible types: Entity cannot be converted to Optional<Entity>
        .createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());

So my question…how would one go about fixing this?

If you’re curious I’m going to be tryinag this without EntitySpawnEvent, so that way i can spawn Mobs in the overworld without a EntitySpawn event…so any help would be nice.

Do you have your Sponge API up to date? It’s been changed to just return an entity.

…well here’s the thing lol here’s the older code i use to use

  public void onEntitySpawn(SpawnEntityEvent event){
  //this logs the Spawn event to show that this is working.
  //Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("Don't be an Idiot"));

  for (EntitySnapshot entitySnapshot : event.getEntitySnapshots()) {
      if (entitySnapshot.getType().equals(EntityTypes.BLAZE)) {
          java.util.Optional<Location<World>> location = entitySnapshot.getLocation();
          if (location.isPresent()) {
              World world = location.get().getExtent();
              for (int i = 0; i < (ConfigurationManager.getInstance().getConfig().getNode("Spawning!", "Blaze", "Blaze#").getInt()); i++) { // Do this 5 times
                  Random roll = new Random();
                  int answer = roll.nextInt(100)+1;
                  if (answer <= (ConfigurationManager.getInstance().getConfig().getNode("Spawning!", "Blaze", "Blaze%").getInt())){
                      Optional<Entity> newCreeper = (Optional<Entity>) world.createEntity(EntityTypes.BLAZE, location.get().getPosition());
                      if (newCreeper.isPresent()) {
                          event.getEntities().add(newCreeper.get());
                      }
                  }
              }
          }

      }

and when i do this here’s the error log that happens…

Log

I’m trying to grab the entity in this case as it spawns so that i can spawn more based upon a random figure that’s configurable with a percentile chance.

That’s not an Optional anymore

Entity entity = extent.createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());

see but that brings up a new error then.

this time when compiling

C:\Users\chris\Desktop\Mod Workstation\workshop\eclipse\HellOnSpongee\src\main\java\me\Cleardragonf\HOS\MobSpawning.java:31: error: no suitable method found for add(Location<World>)
                              event.getEntities().add(newCreeper.get());
                                                  ^
method Collection.add(Entity) is not applicable

It’s telling you that you’re trying to add a Location to the entity list, but the collection is of type Entity. Once you have an Entity, you don’t need to call .get() on it anymore.

You cannot spawn an entity by adding the new one in the event’s entities list. You must call world.spawnEntity()

Ok…i see that now. however when i change that and use the SpawnEntity() I’m now getting one error on the if(entity.isPresent())

here’s the compilation

C:\Users\chris\Desktop\Mod    Workstation\workshop\eclipse\HellOnSpongee\src\main\java\me\Cleardragonf\HOS\HOS.java:114: error: cannot find        symbol
					if (entity.isPresent()) {
					          ^

symbol: method isPresent()
location: variable entity of type Entity
1 error

Alright so I played around with it for a bit…and this seems to be getting the correct specifications…however the adding of another creeper doesn’t seem to be working…can someone point out the obvious for me?

		  	if(entity.getType().equals(EntityTypes.CREEPER)){
              Location<World> location = entity.getLocation();
	  			Vector3d world = event.getEntities().iterator().next().getLocation().getPosition();
				((EntityUniverse) world).spawnEntity(entity, Cause.source(EntitySpawnCause.builder().entity(entity).type(SpawnTypes.PLUGIN).build()).build());
	  		
	  	}

Can you post the current full code of onEntitySpawn ?

I can see a few problems with this code:

  1. You need to call createEntity() and then pass that to spawnEntity().
  2. You’re trying to spawn the entity from the event. Don’t do that; if it appears in the event, it will be spawned automatically after the listeners finish running.
  3. A Vector3d is not an EntityUniverse. A Vector3d is just an X/Y/Z position (although it’s used for rotation in some cases). The World is available from Location.getExtent(); it implements EntityUniverse.

Here’s what I have…However at the entity creeper = (entity) it’s erroring out

@Listener
public void onEntitySpawn(SpawnEntityEvent event){
  //This is the testing code v
  Entity spawnedEntity = null;
  for(Entity entity : event.getEntities()){
  if(entity != null){
	  spawnedEntity = entity;
	  break;
  }
  }
if(spawnedEntity == null){
  return;
}
String a = event.getEntities().iterator().next().getType().getName();
//testing this
Location<World> location = spawnedEntity.getLocation();
World world = location.getExtent();

if(spawnedEntity.getType().equals(EntityTypes.SKELETON)){
	
	//testing this
    Entity newCreeper = world.createEntity(EntityTypes.SKELETON, location.getPosition());
    
        newCreeper(location);
    
	
	  Sponge.getGame().getServer().getBroadcastChannel().send(Text.of("A " + a + " Just Spawned"));

	}
else{
  Sponge.getServer().getBroadcastChannel().send(Text.of("A " + spawnedEntity.getType().getName() + " has just been spawned"));

}
}
public void newCreeper(Location<World> spawnLocation) {
    Extent extent = spawnLocation.getExtent();
    Entity entity = extent.createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());
    if (optional.isPresent()) {
        Entity creeper = optional.get();
        extent.spawnEntity(creeper,
            Cause.source(EntitySpawnCause.builder()
            .entity(creeper).type(SpawnTypes.PLUGIN).build()).build());
    }
}

You mean its erroring at
if (optional.isPresent())

since you don’t have an optional variable-object in scope, because When you made this plugin before, the createEntity() method returned an optional vairable, and as everyone has been saying repeatedly, the newer API versions have changed that to NOT be an Optional return, but rather a straight-up Entity.

your Entity entity = The Creeper Object returned

and the Entity creeper = optional.get() is acting on nothing at all, again, because you dont’ have an “optional” variable object. And the creeper is already the entity, not Optional to need to .get()