Skip to content

Commit

Permalink
Rewrote "projectile hit" in ScriptEvent format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jul 8, 2015
1 parent ed32519 commit 06c69d5
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 106 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -677,6 +677,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new PlayerWalkScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerWalksOverScriptEvent());
ScriptEvent.registerScriptEvent(new PortalCreateScriptEvent());
ScriptEvent.registerScriptEvent(new ProjectileHitsScriptEvent());
ScriptEvent.registerScriptEvent(new ProjectileLaunchedScriptEvent());
ScriptEvent.registerScriptEvent(new PotionSplashScriptEvent());
ScriptEvent.registerScriptEvent(new RedstoneScriptEvent());
Expand Down
@@ -0,0 +1,174 @@
package net.aufdemrand.denizen.events.entity;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.util.BlockIterator;

import java.util.HashMap;

public class ProjectileHitsScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// projectile hits block (in <area>)
// projectile hits <material> (in <area>)
// <projectile> hits block (in <area>)
// <projectile> hits <material> (in <area>)
//
// @Cancellable false
//
// @Triggers when a projectile hits a block.
//
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
// <--[event]
// @Events
// entity shoots block (in <area>)
// entity shoots <material> (with <projectile>) (in <area>)
// <entity> shoots block (in <area>)
// <entity> shoots <material> (with <projectile>) (in <area>)
//
// @Triggers when a projectile shot by an entity hits a block.
//
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
public ProjectileHitsScriptEvent() {
instance = this;
}

public static ProjectileHitsScriptEvent instance;
public dEntity projectile;
public dEntity shooter;
public dLocation location;
private dMaterial material;
public ProjectileHitEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
return cmd.equals("hits") || cmd.equals("shoots");
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
String pTest = cmd.equals("hits") ? CoreUtilities.getXthArg(0, lower): CoreUtilities.getXthArg(4, lower);

if (pTest.length() > 0 && !projectile.matchesEntity(pTest)) {
return false;
}

String mat = CoreUtilities.getXthArg(2, lower);
if (!tryMaterial(material, mat)) {
return false;
}

String sTest = cmd.equals("shoots") ? CoreUtilities.getXthArg(0, lower) : "";
if (shooter != null && sTest.length() > 0 && !shooter.matchesEntity(sTest)) {
return false;
}

if (!runInCheck(scriptContainer, s, lower, location)) {
return false;
}

return true;
}

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

@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

@Override
public void destroy() {
ProjectileHitEvent.getHandlerList().unregister(this);
}

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(shooter != null && shooter.isPlayer() ? dEntity.getPlayerFrom(event.getEntity()) : null,
shooter != null && shooter.isCitizensNPC() ? dEntity.getNPCFrom(event.getEntity()) : null);
}

@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("projectile", projectile);
context.put("location", location);
if (shooter != null ) {
context.put("shooter", shooter);
}
return context;
}

@EventHandler
public void onProjectileHits(ProjectileHitEvent event) {
projectile = new dEntity(event.getEntity());
if (!projectile.getShooter().isPlayer()) return;
if (projectile.getLocation() == null)
return; // No, I can't explain how or why this would ever happen... nonetheless, it appears it does happen sometimes.

if (Double.isNaN(projectile.getLocation().getDirection().normalize().getX()))
return; // I can't explain this one either. It also chooses to happen whenever it pleases.

Block block = null;
try {
BlockIterator bi = new BlockIterator(projectile.getLocation().getWorld(),
projectile.getLocation().toVector(), projectile.getLocation().getDirection().normalize(), 0, 4);
while (bi.hasNext()) {
block = bi.next();
if (block.getTypeId() != 0) {
break;
}
}
}
catch (IllegalStateException ex) {
// This happens because it can. Also not explainable whatsoever.
// As this error happens on no fault of the user, display no error message... just cancel the event.
return;
}

if (block == null) {
return;
}
material = dMaterial.getMaterialFrom(block.getType(), block.getData());
shooter = projectile.getShooter();
location = new dLocation(event.getEntity().getLocation());
this.event = event;
fire();
}
}
Expand Up @@ -129,112 +129,6 @@ public void timeEvent() {
}
}

/////////////////////
// Additional EVENTS
/////////////////

// <--[event]
// @Events
// projectile hits block
// projectile hits <material>
// <projectile> hits block
// <projectile> hits <material>
//
// @Triggers when a projectile hits a block.
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
@EventHandler
public void projectileHit(ProjectileHitEvent event) {

dPlayer player = null;
dNPC npc = null;

if (event.getEntity() == null)
return;

dEntity projectile = new dEntity(event.getEntity());

if (projectile.getLocation() == null)
return; // No, I can't explain how or why this would ever happen... nonetheless, it appears it does happen sometimes.

if (Double.isNaN(projectile.getLocation().getDirection().normalize().getX()))
return; // I can't explain this one either. It also chooses to happen whenever it pleases.

Block block = null;
try {
BlockIterator bi = new BlockIterator(projectile.getLocation().getWorld(),
projectile.getLocation().toVector(), projectile.getLocation().getDirection().normalize(), 0, 4);
while (bi.hasNext()) {
block = bi.next();
if (block.getTypeId() != 0) {
break;
}
}
}
catch (IllegalStateException ex) {
// This happens because it can. Also not explainable whatsoever.
// As this error happens on no fault of the user, display no error message... just cancel the event.
return;
}

if (block == null)
return;

dEntity shooter = projectile.getShooter();
dMaterial material = dMaterial.getMaterialFrom(block.getType(), block.getData());

Map<String, dObject> context = new HashMap<String, dObject>();
context.put("projectile", projectile);
context.put("location", new dLocation(block.getLocation()));

List<String> events = new ArrayList<String>();
events.add("projectile hits block");
events.add("projectile hits " + material.identifySimple());
events.add(projectile.identifyType() + " hits block");
events.add(projectile.identifyType() + " hits " + material.identifySimple());

if (shooter != null) {
context.put("shooter", shooter.getDenizenObject());

// <--[event]
// @Events
// entity shoots block
// entity shoots <material> (with <projectile>)
// <entity> shoots block
// <entity> shoots <material> (with <projectile>)
//
// @Triggers when a projectile shot by an entity hits a block.
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->

if (shooter.isCitizensNPC()) {
npc = shooter.getDenizenNPC();
}
else if (shooter.isPlayer()) {
player = shooter.getDenizenPlayer();
}

events.add("entity shoots block");
events.add("entity shoots block with " + projectile.identifyType());
events.add("entity shoots " + material.identifySimple() + " with " + projectile.identifyType());
events.add("entity shoots " + material.identifySimple());
events.add(shooter.identifyType() + " shoots block");
events.add(shooter.identifyType() + " shoots block with " + projectile.identifyType());
events.add(shooter.identifyType() + " shoots " + material.identifySimple() + " with " + projectile.identifyType());
events.add(shooter.identifyType() + " shoots " + material.identifySimple());
}

doEvents(events, npc, player, context, true);
}

/////////////////////
// INVENTORY EVENTS
/////////////////
Expand Down

0 comments on commit 06c69d5

Please sign in to comment.