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

Commit

Permalink
Collision Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Nov 3, 2017
1 parent b52c661 commit f68fe7a
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 3 deletions.
Expand Up @@ -171,6 +171,8 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new UnloadWorldCommand());
Denizen2Core.register(new WeatherCommand());
// Events: Entity
Denizen2Core.register(new EntityCollidesWithBlockScriptEvent());
Denizen2Core.register(new EntityCollidesWithEntityScriptEvent());
Denizen2Core.register(new EntityDamagedScriptEvent());
Denizen2Core.register(new EntityDiesScriptEvent());
Denizen2Core.register(new EntityEntersAreaScriptEvent());
Expand Down
@@ -0,0 +1,114 @@
package com.denizenscript.denizen2sponge.events.entity;

import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.BlockTypeTag;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.CollideBlockEvent;

import java.util.HashMap;

public class EntityCollidesWithBlockScriptEvent extends ScriptEvent {

// <--[event]
// @Since 0.3.2
// @Events
// entity collides with block
//
// @Updated 2017/10/27
//
// @Group Entity
//
// @Cancellable true
//
// @Triggers when an entity collides with a block. Note: this event may fire very rapidly.
//
// @Switch entity_type (EntityTypeTag) checks the entity type.
// @Switch block_type (BlockTypeTag) checks the block type.
// @Switch world (WorldTag) checks the world.
// @Switch cuboid (CuboidTag) checks the cuboid area.
// @Switch weather (TextTag) checks the weather.
//
// @Context
// entity (EntityTag) returns the entity that collided with the block.
// location (LocationTag) returns the location of the colliding block.
// impact_normal (LocationTag) returns the impact normal of the collision.
//
// @Determinations
// None.
// -->

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

@Override
public boolean couldMatch(ScriptEventData data) {
return data.eventPath.startsWith("entity collides with block");
}

@Override
public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkEntityType(entity.getInternal().getType(), data, this::error, "entity_type")
&& D2SpongeEventHelper.checkBlockType(material.getInternal(), data, this::error, "block_type")
&& D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public EntityTag entity;

public BlockTypeTag material;

public LocationTag location;

public LocationTag impact_normal;

public CollideBlockEvent internal;

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

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

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

@Listener
public void onEntityCollidesWithBlock(CollideBlockEvent evt) {
EntityCollidesWithBlockScriptEvent event = (EntityCollidesWithBlockScriptEvent) clone();
event.internal = evt;
event.entity = new EntityTag((Entity) evt.getSource());
event.material = new BlockTypeTag(evt.getTargetBlock().getType());
event.location = new LocationTag(evt.getTargetLocation());
event.impact_normal = new LocationTag(evt.getTargetSide().asOffset());
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
}

@Override
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) {
super.applyDetermination(errors, determination, value);
}
}
@@ -0,0 +1,103 @@
package com.denizenscript.denizen2sponge.events.entity;

import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.ListTag;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.CollideEntityEvent;

import java.util.HashMap;

public class EntityCollidesWithEntityScriptEvent extends ScriptEvent {

// <--[event]
// @Since 0.3.2
// @Events
// entity collides with entity
//
// @Updated 2017/11/03
//
// @Group Entity
//
// @Cancellable true
//
// @Triggers when an entity collides with another entity. Note: this event may fire very rapidly.
//
// @Switch type (EntityTypeTag) checks the first entity type.
// @Switch world (WorldTag) checks the world.
// @Switch cuboid (CuboidTag) checks the cuboid area.
// @Switch weather (TextTag) checks the weather.
//
// @Context
// entities (EntityTag) returns the entities that collided.
//
// @Determinations
// None.
// -->

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

@Override
public boolean couldMatch(ScriptEventData data) {
return data.eventPath.startsWith("entity collides with entity");
}

@Override
public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkEntityType(((EntityTag) entities.getInternal().get(0)).getInternal().getType(), data, this::error, "type")
&& D2SpongeEventHelper.checkWorld(((EntityTag) entities.getInternal().get(0)).getInternal().getLocation().getExtent(), data, this::error)
&& D2SpongeEventHelper.checkCuboid(new LocationTag(((EntityTag) entities.getInternal().get(0)).getInternal().getLocation()).getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
((EntityTag) entities.getInternal().get(0)).getInternal().getLocation().getExtent().getWeather().getId()), data, this::error);
}

public ListTag entities;

public CollideEntityEvent internal;

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

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

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

@Listener
public void onEntityCollidesWithEntity(CollideEntityEvent evt) {
EntityCollidesWithEntityScriptEvent event = (EntityCollidesWithEntityScriptEvent) clone();
event.internal = evt;
ListTag list = new ListTag();
for (Entity ent : evt.getEntities()) {
list.getInternal().add(new EntityTag(ent));
}
event.entities = list;
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
}

@Override
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) {
super.applyDetermination(errors, determination, value);
}
}
Expand Up @@ -18,7 +18,7 @@
public class ProjectileImpactsBlockScriptEvent extends ScriptEvent {

// <--[event]
// @Since 0.3.0
// @Since 0.3.2
// @Events
// projectile impacts block
//
Expand Down
Expand Up @@ -4,7 +4,6 @@
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.BlockTypeTag;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
Expand All @@ -18,7 +17,7 @@
public class ProjectileImpactsEntityScriptEvent extends ScriptEvent {

// <--[event]
// @Since 0.3.0
// @Since 0.3.2
// @Events
// projectile impacts entity
//
Expand Down

0 comments on commit f68fe7a

Please sign in to comment.