Pemijahan suatu Kesatuan

Peringatan

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!

Anda akan membutuhkan tiga hal untuk pemijahan an :javadoc: Entity: a :javadoc:` Location`, an :javadoc: Extent, dan an :javadoc:` EntityType`. Proses untuk mendapatkan ini cukup sederhana, Anda hanya perlu mengambil `` Lokasi`` dari suatu tempat di kode plugin Anda dan memilih jenis `` Entity`` yang ingin Anda timbulkan.

Sebagai contoh, mari mencoba menelurkan seekor Creeper:

import org.spongepowered.api.entity.Entity;
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 java.util.Optional;

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

    Entity 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()) {
        frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
        world.spawnEntity(creeper);
    }
}

Ini akan merebut dunia dari `` Lokasi``, yang akan kita perlukan untuk pemijahan yang sebenarnya. Selanjutnya, ia menggunakan :javadoc: EntityUniverse # createEntity (EntityType, Vector3d) untuk membuat entitas, tapi perhatikan bahwa ini tidak menimbulkan entitas ke dunia, itu hanya akan membuatnya. Kita perlu menentukan tipe `` Entity`` untuk membuat, dan koordinat dari Location` kita.

Once we have created our Entity we can then use the world for spawning the Entity. We should specify a Cause for the spawning so other plugins can handle it accordingly. For spawning Entitys, it is best to use a SpawnType as part of the context. In this example, we stated that our entity was spawned from a plugin, however we can make it any cause/context that best describes why we are spawning this in, such as a mob spawner (See SpawnTypes#MOB_SPAWNER), or spawn egg (See SpawnTypes#SPAWN_EGG).