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

Commit

Permalink
Explosion Detonates Event
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Oct 3, 2017
1 parent 241c6d6 commit 93f8e39
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
Expand Up @@ -21,10 +21,7 @@
import com.denizenscript.denizen2sponge.events.server.ClientPingsServerScriptEvent;
import com.denizenscript.denizen2sponge.events.server.InternalScriptEvent;
import com.denizenscript.denizen2sponge.events.server.ServerStopsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.BlockChangeScriptEvent;
import com.denizenscript.denizen2sponge.events.world.BlockFadesScriptEvent;
import com.denizenscript.denizen2sponge.events.world.WorldLoadsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.WorldUnloadsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.*;
import com.denizenscript.denizen2sponge.spongecommands.ExCommand;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadedEvent;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadingEvent;
Expand Down Expand Up @@ -198,6 +195,7 @@ public void onServerStart(GamePreInitializationEvent event) {
// Events: World
Denizen2Core.register(new BlockChangeScriptEvent());
Denizen2Core.register(new BlockFadesScriptEvent());
Denizen2Core.register(new ExplosionDetonatesScriptEvent());
Denizen2Core.register(new WorldLoadsScriptEvent());
Denizen2Core.register(new WorldUnloadsScriptEvent());
// Tag Handlers: Sponge Basics
Expand Down
@@ -0,0 +1,143 @@
package com.denizenscript.denizen2sponge.events.world;

import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.BooleanTag;
import com.denizenscript.denizen2core.tags.objects.ListTag;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.tags.objects.NumberTag;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.world.ExplosionEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.explosion.Explosion;

import java.util.ArrayList;
import java.util.HashMap;

public class ExplosionDetonatesScriptEvent extends ScriptEvent {

// <--[event]
// @Events
// explosion detonates
//
// @Updated 2017/10/03
//
// @Group World
//
// @Cancellable true
//
// @Triggers when an explosion detonates and is calculating the affected locations and entities.
//
// @Context
// locations (ListTag<LocationTag>) returns the locations affected by the explosion.
// entities (ListTag<EntityTag>) returns the entities affected by the explosion.
// explosion_data (MapTag) returns the data associated with the explosion, such as radius or whether it should cause fire.
//
// @Determinations
// locations (ListTag<LocationTag>) to set the locations affected by the explosion.
// entities (ListTag<EntityTag>) to set the entities affected by the explosion.
// -->

@Override
public String getName() {
return "ExplosionDetonates";
}

@Override
public boolean couldMatch(ScriptEventData data) {
return data.eventPath.startsWith("explosion detonates");
}

@Override
public boolean matches(ScriptEventData data) {
return true;
}

public ListTag locations;

public ListTag entities;

public MapTag explosion_data;

public ExplosionEvent.Detonate internal;

@Override
public HashMap<String, AbstractTagObject> getDefinitions(ScriptEventData data) {
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data);
defs.put("locations", locations);
defs.put("entities", entities);
defs.put("explosion_data", explosion_data);
return defs;
}

@Override
public void enable() {
Sponge.getEventManager().registerListeners(Denizen2Sponge.instance, this);
}

@Override
public void disable() {
Sponge.getEventManager().unregisterListeners(this);
}

@Listener
public void onExplosionDetonates(ExplosionEvent.Detonate evt) {
ExplosionDetonatesScriptEvent event = (ExplosionDetonatesScriptEvent) clone();
event.internal = evt;
ListTag locs = new ListTag();
for (Location<World> loc : evt.getAffectedLocations()) {
locs.getInternal().add(new LocationTag(loc));
}
event.locations = locs;
ListTag ents = new ListTag();
for (Entity ent : evt.getEntities()) {
locs.getInternal().add(new EntityTag(ent));
}
event.entities = ents;
Explosion exp = evt.getExplosion();
MapTag data = new MapTag();
data.getInternal().put("radius", new NumberTag(exp.getRadius()));
data.getInternal().put("fire", new BooleanTag(exp.canCauseFire()));
data.getInternal().put("break_blocks", new BooleanTag(exp.shouldBreakBlocks()));
data.getInternal().put("damage_entities", new BooleanTag(exp.shouldDamageEntities()));
data.getInternal().put("smoke", new BooleanTag(exp.shouldPlaySmoke()));
event.explosion_data = data;
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
}

@Override
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) {
if (determination.equals("locations")) {
ListTag lt = ListTag.getFor(this::error, value);
locations = lt;
ArrayList<Location<World>> locs = new ArrayList<>();
for (AbstractTagObject loc : lt.getInternal()) {
locs.add(((LocationTag) loc).getInternal().toLocation());
}
internal.getAffectedLocations().clear();
internal.getAffectedLocations().addAll(locs);
}
else if (determination.equals("entities")) {
ListTag lt = ListTag.getFor(this::error, value);
entities = lt;
ArrayList<Entity> ents = new ArrayList<>();
for (AbstractTagObject ent : lt.getInternal()) {
ents.add(((EntityTag) ent).getInternal());
}
internal.getEntities().clear();
internal.getEntities().addAll(ents);
}
else {
super.applyDetermination(errors, determination, value);
}
}
}

0 comments on commit 93f8e39

Please sign in to comment.