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

Commit

Permalink
Represent Spawn Types/Causes
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Jun 15, 2018
1 parent fa87fc4 commit 98a1577
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Expand Up @@ -14,18 +14,20 @@
import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMap;
import org.spongepowered.api.CatalogType;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.data.type.HandType;
import org.spongepowered.api.entity.EntityType;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.event.cause.entity.spawn.SpawnType;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.weather.Weather;

import java.util.List;
import java.util.Optional;

import static org.spongepowered.api.Sponge.getRegistry;

public class D2SpongeEventHelper {

public static boolean checkBlockType(BlockType btype, ScriptEvent.ScriptEventData data, Action<String> error) {
Expand Down Expand Up @@ -149,7 +151,7 @@ public static boolean checkCatalogType(Class clazz, String type, ScriptEvent.Scr
return true;
}
for (AbstractTagObject ato : ListTag.getFor(error, data.switches.get(tname)).getInternal()) {
Optional<CatalogType> opt = Sponge.getRegistry().getType(clazz, ato.toString());
Optional<CatalogType> opt = getRegistry().getType(clazz, ato.toString());
if (!opt.isPresent()) {
error.run("Invalid " + clazz.getSimpleName() + " type: '" + ato.debug() + "'!");
return false;
Expand All @@ -161,6 +163,27 @@ else if (Utilities.getIdWithoutDefaultPrefix(opt.get().getId()).equals(type)) {
return false;
}

public static boolean checkSpawnType(String type, ScriptEvent.ScriptEventData data, Action<String> error) {
return checkSpawnType(type, data, error, "spawn_type");
}

public static boolean checkSpawnType(String type, ScriptEvent.ScriptEventData data, Action<String> error, String tname) {
if (!data.switches.containsKey(tname)) {
return true;
}
for (AbstractTagObject ato : ListTag.getFor(error, data.switches.get(tname)).getInternal()) {
Optional<SpawnType> opt = getRegistry().getType(SpawnType.class, ato.toString());
if (!opt.isPresent()) {
error.run("Invalid spawn type: '" + ato.debug() + "'!");
return false;
}
else if (Utilities.getIdWithoutDefaultPrefix(opt.get().getId()).equals(type)) {
return true;
}
}
return false;
}

// <--[explanation]
// @Since 0.3.0
// @Name With Item Switch For Events
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.TextTag;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
Expand All @@ -10,6 +11,7 @@
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
Expand All @@ -33,16 +35,29 @@ 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.
//
// @Triggers when an entity spawns in the world (non players).
// Possible spawn types can be found at <@link explanation Spawn Types>spawn types<@/link>.
//
// @Context
// entity (EntityTag) returns the entity that is attempting to spawn.
// spawn_type (TextTag) returns the type of spawn that fired this event.
//
// @Determinations
// None.
// -->

// <--[explanation]
// @Since 0.4.0
// @Name Spawn Types
// @Group Useful Lists
// @Description
// A list of all default spawn types 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.
// -->

@Override
public String getName() {
return "EntitySpawns";
Expand All @@ -62,17 +77,21 @@ public boolean matches(ScriptEvent.ScriptEventData data) {
&& D2SpongeEventHelper.checkWorld(world, data, this::error)
&& D2SpongeEventHelper.checkCuboid((new LocationTag(loc)).getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
world.getWeather().getId()), data, this::error);
world.getWeather().getId()), data, this::error)
&& D2SpongeEventHelper.checkSpawnType(spawnType.toString(), data, this::error);
}

public EntityTag entity;

public TextTag spawnType;

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);
return defs;
}

Expand All @@ -92,6 +111,8 @@ public void onEntiySpawns(SpawnEntityEvent evt) {
EntitySpawnsScriptEvent event = (EntitySpawnsScriptEvent) clone();
event.internal = evt;
event.entity = new EntityTag(ent);
event.spawnType = new TextTag(Utilities.getIdWithoutDefaultPrefix(
evt.getContext().get(EventContextKeys.SPAWN_TYPE).get().getId()));
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
Expand Down
Expand Up @@ -29,7 +29,7 @@ public class EntityTypeTag extends AbstractTagObject {
// @Group Useful Lists
// @Description
// A list of all default entity types can be found here:
// <@link url https://jd.spongepowered.org/7.0.0-SNAPSHOT/org/spongepowered/api/entity/EntityTypes.html>entity types list<@/link>
// <@link url https://jd.spongepowered.org/7.1.0-SNAPSHOT/org/spongepowered/api/entity/EntityTypes.html>entity types list<@/link>
// These can be used with the spawn command as well as with some event switches.
// -->

Expand Down

0 comments on commit 98a1577

Please sign in to comment.