Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Spawn Causes
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Jun 15, 2018
1 parent dcfa9e6 commit 40faa00
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
Expand Up @@ -20,6 +20,7 @@
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityType;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnType;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;

import java.util.Map;
Expand All @@ -31,21 +32,25 @@ public class SpawnCommand extends AbstractCommand {
// @Name spawn
// @Arguments <entity type> <location> [map of properties]
// @Short spawns a new entity.
// @Updated 2017/10/18
// @Updated 2018/06/03
// @Group Entity
// @Minimum 2
// @Maximum 3
// @Named cause (TextTag) Sets what caused this entity to spawn.
// @Tag <[spawn_success]> (BooleanTag) returns whether the spawn passed.
// @Tag <[spawn_entity]> (EntityTag) returns the entity that was spawned (only if the spawn passed).
// @Description
// Spawns an entity at the specified location. Optionally, specify a MapTag
// of properties to spawn the entity with those values automatically set on
// it. The MapTag can also contain a "rotation" key with a LocationTag.
// Related information: <@link explanation Entity Types>entity types<@/link>.
// Spawns an entity of the specified entity type or from a script at a location.
// This entity Optionally, specify a MapTag of properties to spawn the entity with those values
// automatically set on it. The MapTag can also contain an "orientation" key with a LocationTag.
// Related information: <@link explanation Entity Types>entity types<@/link> and <@link explanation Spawn Causes>spawn causes<@/link>.
// Related commands: <@link command remove>remove<@/link>.
// @Example
// # Spawns a sheep that feels the burn.
// - spawn sheep <player.location> display_name:<texts.for_input[text:Bahhhb]>|max_health:300|health:300|fire_ticks:999999|is_sheared:true
// @Example
// # Spawns the custom mob "cool_zombie" with the "breeding" spawn cause.
// - spawn cool_zombie <player.location> --cause breeding
// -->

@Override
Expand Down Expand Up @@ -119,16 +124,28 @@ public void execute(CommandQueue queue, CommandEntry entry) {
}
}
}
SpawnType cause;
if (entry.namedArgs.containsKey("cause")) {
String causeStr = entry.getNamedArgumentObject(queue, "cause").toString();
cause = (SpawnType) Utilities.getTypeWithDefaultPrefix(SpawnType.class, causeStr);
if (cause == null) {
queue.handleError(entry, "Invalid spawn cause '" + causeStr + "' in Spawn command!");
return;
}
}
else {
cause = SpawnTypes.CUSTOM;
}
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, cause);
if (queue.shouldShowGood()) {
queue.outGood("Spawning an entity " + ColorSet.emphasis
queue.outGood("Spawning an entity " + ColorSet.emphasis
+ (fromScript ? "from script " + str : "of type " + entType.getId())
+ ColorSet.good + " with the following additional properties: "
+ ColorSet.emphasis + propertyMap.debug() + ColorSet.good + " at location "
+ ColorSet.emphasis + locationTag.debug() + ColorSet.good + "...");
+ ColorSet.emphasis + locationTag.debug() + ColorSet.good + " and with cause "
+ ColorSet.emphasis + Utilities.getIdWithoutDefaultPrefix(cause.getId()) + "...");
}
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
boolean passed = location.world.spawnEntity(entity);
// TODO: "Cause" argument!
if (queue.shouldShowGood()) {
queue.outGood("Spawning " + (passed ? "succeeded" : "was blocked") + "!");
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ public class EntitySpawnsScriptEvent extends ScriptEvent {
// @Events
// entity spawns
//
// @Updated 2017/10/14
// @Updated 2018/06/03
//
// @Group Entity
//
Expand All @@ -36,27 +36,28 @@ public class EntitySpawnsScriptEvent extends ScriptEvent {
// @Switch world (WorldTag) checks the world.
// @Switch cuboid (CuboidTag) checks the cuboid area.
// @Switch weather (TextTag) checks the weather.
// @Switch spawn_type (TextTag) checks the spawn type.
// @Switch cause (TextTag) checks the spawn cause.
//
// @Triggers when an entity spawns in the world (non players).
// Possible spawn types can be found at <@link explanation Spawn Types>spawn types<@/link>.
// Possible spawn causes can be found at <@link explanation Spawn Causes>spawn causes<@/link>.
//
// @Context
// entity (EntityTag) returns the entity that is attempting to spawn.
// spawn_type (TextTag) returns the type of spawn that fired this event.
// cause (TextTag) returns the type of spawn that fired this event.
//
// @Determinations
// None.
// -->

// <--[explanation]
// @Since 0.4.0
// @Name Spawn Types
// @Name Spawn Causes
// @Group Useful Lists
// @Description
// A list of all default spawn types can be found here:
// A list of all default spawn causes can be found here:
// <@link url https://jd.spongepowered.org/7.1.0-SNAPSHOT/org/spongepowered/api/event/cause/entity/spawn/SpawnTypes.html>spawn types list<@/link>
// This information can be useful to understand the spawn_type context in spawn events.
// This information can be useful to understand the cause context in spawn events,
// and cause named argument in spawn commands.
// -->

@Override
Expand All @@ -80,20 +81,20 @@ public boolean matches(ScriptEvent.ScriptEventData data) {
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
world.getWeather().getId()), data, this::error)
&& D2SpongeEventHelper.checkCatalogType(
SpawnType.class, spawnType.toString(), data, this::error, "spawn_type");
SpawnType.class, cause.toString(), data, this::error, "cause");
}

public EntityTag entity;

public TextTag spawnType;
public TextTag cause;

public SpawnEntityEvent internal;

@Override
public HashMap<String, AbstractTagObject> getDefinitions(ScriptEvent.ScriptEventData data) {
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data);
defs.put("entity", entity);
defs.put("spawn_type", spawnType);
defs.put("cause", cause);
return defs;
}

Expand All @@ -113,7 +114,7 @@ public void onEntiySpawns(SpawnEntityEvent evt) {
EntitySpawnsScriptEvent event = (EntitySpawnsScriptEvent) clone();
event.internal = evt;
event.entity = new EntityTag(ent);
event.spawnType = new TextTag(Utilities.getIdWithoutDefaultPrefix(
event.cause = new TextTag(Utilities.getIdWithoutDefaultPrefix(
evt.getContext().get(EventContextKeys.SPAWN_TYPE).get().getId()));
event.cancelled = evt.isCancelled();
event.run();
Expand Down

0 comments on commit 40faa00

Please sign in to comment.