[solved]SpawnCause

Hello So I’m trying to find which Part of the API has the SpawnCause in 7.0.0…however I’m not finding it…am I missing something?

1 Like

I believe this has been become an EventContext key.

https://github.com/SpongePowered/SpongeAPI/blob/bleeding/src/main/java/org/spongepowered/api/event/cause/EventContextKeys.java#L97

You have a list of spawn types here.

https://github.com/SpongePowered/SpongeAPI/blob/bleeding/src/main/java/org/spongepowered/api/event/cause/entity/spawn/SpawnTypes.java

I haven’t tried this myself so let me know if this works. :slight_smile:

1 Like

The JD probably need updating.

1 Like

Yeah, they seem to lack lack clarity in to what it is (the SpawnTypes don’t have a JD for the class). The spawn type class is linking to the SpawnCause in its class JD which doesn’t exist anymore.

https://github.com/SpongePowered/SpongeAPI/blob/bleeding/src/main/java/org/spongepowered/api/event/cause/entity/spawn/SpawnType.java#L33

So I can understand the confusion.

ok…so i have the following

			 			Extent extent = spawnLocation.getExtent();
			 		    Entity entity = extent.createEntity(EntityTypes.ZOMBIE, spawnLocation.getPosition());
			 		        //Entity creeper = entity;
			 		        //extent.spawnEntity(creeper,
			 		        //    Cause.source(EntitySpawnCause.builder()
			 		        //    .entity(creeper).type(SpawnTypes.PLUGIN).build()).build());
			 				//Sponge.getServer().getBroadcastChannel().send(Text.of("ZOMBIE"));
		  			  }

the stuff insdie the commented out section is what’s not working…obviously this is from API 5…however I have added in the new version and Cannot find a way of using the SpawnCause…suggestions?

As long as your code creating/spawning entities is performed in a listener, scheduled task, or command, you should be relatively ok as the refactor affecting SpawnCauses (it whole heartedly removed them) made it an implementation thing that automatically assigns the SpawnType for said entity when created by a plugin. You yourself will no longer have to perform the spawning, except calling extent.spawnEntity(entity).

2 Likes

So what your sayins is replace the

		 		    Entity entity = extent.createEntity(EntityTypes.ZOMBIE, spawnLocation.getPosition());
		 		        //Entity creeper = entity;
		 		        //extent.spawnEntity(creeper,
		 		        //    Cause.source(EntitySpawnCause.builder()
		 		        //    .entity(creeper).type(SpawnTypes.PLUGIN).build()).build());
		 				//Sponge.getServer().getBroadcastChannel().send(Text.of("ZOMBIE"));
	  			  }

With a

			 			extent.spawnEntity((Entity) EntityTypes.SILVERFISH);
		  			  }

Right?

However…i’m hitting as I cannot cast Entity unto EntityTypes…so I need a way of naming that the entity is a Silverfish…ideas?

…actually hold off on the responses everyone…I may be seeing what you’re saying.

No. That doesn’t even compile, as you’ve found.

With the cause refactor, many of the methods that accepted a Cause object in previous API versions have had it removed. Instead, these methods have (for lack of a better term) ‘implied’ causes. For example, any code that runs through a listener, command, or task registered to your plugin will automatically include your plugin in the cause stack.

As an example, plugin commands are always registered with a plugin instance (CommandManager#register). When a player executes the command, the plugin that registered the command is pushed to the cause stack for the duration of its execution as shown here. A similar process is done for event listeners and tasks.

All you need to do is follow the same steps as before, just without the need to define the cause.

Optional<Entity> slime = extent.createEntity(EntityTypes.SLIME, location.getPosition());
if (slime.isPresent()) {
    boolean success = extent.spawnEntity(slime.get());
} else {
    //the entity could not be created.
}

More information regarding the cause refactor can be found here.

so… here’s what i I found actually works for this situation as these are all being done on schedule events.

 			if(listResult == "BLAZE"){
		  		  for (int i = 0; i< (ConfigurationManager.getInstance().getConfig(DayCounter.getWeeklyConfig()).getNode("Natural Spawning!", "Blaze", "#").getInt()); i++){
		  			  Random roll = new Random();
		  			  int answer = roll.nextInt(100) + 1;
		  			  if (answer <= (ConfigurationManager.getInstance().getConfig(DayCounter.getWeeklyConfig()).getNode("Natural Spawning!", "Blaze", "%").getInt())){
				 			Extent extent = spawnLocation.getExtent();
				 		    Entity entity = extent.createEntity(EntityTypes.BLAZE, spawnLocation.getPosition());
				 		        Entity creeper = entity;
				 		        extent.spawnEntity(creeper);
				 				Sponge.getServer().getBroadcastChannel().send(Text.of("BLAZE"));
			  			  }

		  		  }

 			}

Pretty much what i’m doing is attempting to rewrite what use to be Zombie Apocolypse but for sponge. So I’ve now managed the Hellish spawning of creatures onto the overworld. And i’ve seen that we now have the ability to target things. So yea.
@Simon_Flash I’ll be using this information later on in this Plugin Creation! Thank you for the very thorough explanation of why there is no more Cause :smiley:Everyone! THANK YOU! :smiley: