Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions source/plugin/entities/spawning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,50 @@
Spawning an Entity
==================

.. warning::
These docs were written for SpongeAPI 7 and are likely out of date.
`If you feel like you can help update them, please submit a PR! <https://github.com/SpongePowered/SpongeDocs>`__

.. javadoc-import::
com.flowpowered.math.vector.Vector3d
org.spongepowered.api.entity.Entity
org.spongepowered.api.entity.EntityType
org.spongepowered.api.event.cause.entity.spawn.SpawnType
org.spongepowered.api.event.cause.entity.spawn.SpawnTypes
org.spongepowered.api.world.Location
org.spongepowered.api.world.extent.Extent
org.spongepowered.api.world.extent.EntityUniverse
org.spongepowered.api.world.server.ServerLocation
org.spongepowered.api.world.server.ServerWorld
org.spongepowered.api.world.volume.entity.EntityVolume.Modifiable

You will need three things for spawning an :javadoc:`Entity`: a :javadoc:`Location`, an :javadoc:`Extent`, and an
:javadoc:`EntityType`. The process for getting these is quite simple, you just need to grab a ``Location`` from
You will need three things for spawning an :javadoc:`Entity`: a **Position** as a ``Vector3d``, an :javadoc:`World`, and an
:javadoc:`EntityType`. The process for getting these is quite simple, you just need to grab a ``Position`` from
somewhere in your plugin code and choose the type of ``Entity`` you wish to spawn.

For example, let's try to spawn a Creeper:

.. code-block:: java

import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.monster.Creeper;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.event.CauseStackManager.StackFrame;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;

import java.util.Optional;

public void spawnEntity(Location<World> spawnLocation) {
World world = spawnLocation.getExtent();
public void spawnEntity(ServerLocation spawnLocation) {
ServerWorld world = spawnLocation.world();

Entity creeper = world.createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());
Creaper creeper = world.createEntity(EntityTypes.CREEPER, spawnLocation.getPosition());

// We need to push a new cause StackFrame to the stack so we can add our own causes
// In previous versions of the API you had to submit a Cause parameter
// that would often not contain the real root cause
// By default the current plugin's PluginContainer is already pushed to the stack.
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
try (StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
world.spawnEntity(creeper);
}
}

This will grab the world from our ``Location``, which we will need for the actual spawning. Next, it uses
:javadoc:`EntityUniverse#createEntity(EntityType, Vector3d)` to create the entity, but do note that this does not
This will grab the world from our ``ServerLocation``, which we will need for the actual spawning. Next, it uses
:javadoc:`EntityVolume.Modifiable#createEntity(EntityType, Vector3d)` to create the entity, but do note that this does not
spawn the entity into the world, it just will create it. We will need to specify the type of ``Entity`` to spawn, and
the co-ordinates from our ``Location``.

Expand Down
Loading