Skip to content

Commit

Permalink
add projectile collides event, fixes #2075
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 24, 2019
1 parent abd7670 commit 4f9d4a0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Expand Up @@ -12,6 +12,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(new PlayerJumpsPaperScriptEventImpl());
ScriptEvent.registerScriptEvent(new PlayerSpectatesEntityScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerStopsSpectatingScriptEvent());
ScriptEvent.registerScriptEvent(new ProjectileCollideScriptEvent());
ScriptEvent.registerScriptEvent(new TNTPrimesScriptEvent());
}
}
@@ -0,0 +1,84 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class ProjectileCollideScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// projectile collides with entity
// <projectile> collides with <entity>
//
// @Regex ^on [^\s]+ collides with [^\s]+$
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Plugin Paper
//
// @Cancellable true
//
// @Triggers when a projectile entity collides with an entity (before any damage calculations are done).
//
// @Context
// <context.projectile> returns the projectile that is colliding.
// <context.entity> returns the entity that was collided with.
//
// @Player When the entity collided with is a player.
// @NPC When the entity collided with is a NPC.
//
// -->

public ProjectileCollideScriptEvent() {
instance = this;
}

public static ProjectileCollideScriptEvent instance;
public ProjectileCollideEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("collides") && path.eventArgLowerAt(2).equals("with");
}

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, event.getEntity().getLocation())) {
return false;
}
return super.matches(path);
}

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

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getCollidedWith());
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return new EntityTag(event.getCollidedWith());
}
else if (name.equals("projectile")) {
return new EntityTag(event.getEntity());
}
return super.getContext(name);
}

@EventHandler
public void projectileCollideEvent(ProjectileCollideEvent event) {
this.event = event;
fire(event);
}
}
@@ -1,10 +1,12 @@
package com.denizenscript.denizen.utilities.implementation;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.tags.TagContext;
import org.bukkit.entity.Entity;

public class BukkitScriptEntryData extends ScriptEntryData {
private PlayerTag player;
Expand All @@ -15,6 +17,22 @@ public BukkitScriptEntryData(PlayerTag player, NPCTag npc) {
this.npc = npc;
}

public BukkitScriptEntryData(EntityTag entity) {
if (entity == null) {
return;
}
if (entity.isCitizensNPC()) {
this.npc = entity.getDenizenNPC();
}
if (entity.isPlayer()) {
this.player = entity.getDenizenPlayer();
}
}

public BukkitScriptEntryData(Entity entity) {
this(entity == null ? null : new EntityTag(entity));
}

public PlayerTag getPlayer() {
return player;
}
Expand Down

0 comments on commit 4f9d4a0

Please sign in to comment.