Skip to content

Commit

Permalink
Fixed event so that it handles cause and cleaned up the couldMatch fo…
Browse files Browse the repository at this point in the history
…r more entities.
  • Loading branch information
Talamar1 committed Jun 14, 2015
1 parent 5eb5aed commit e9243a6
Showing 1 changed file with 33 additions and 11 deletions.
Expand Up @@ -15,16 +15,22 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class EntityKilledScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// entity killed
// entity killed by <cause>
// entity killed by entity
// entity killed by <entity>
// <entity> killed
// <entity> killed by <cause>
// <entity> killed by entity
// <entity> killed by <entity>
// entity kills entity
// entity kills <entity>
// <entity> kills entity
Expand Down Expand Up @@ -65,23 +71,39 @@ public EntityKilledScriptEvent() {
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.contains(" kills")
|| lower.contains(" killed");
String cmd = CoreUtilities.getXthArg(1, lower);
String entOne = CoreUtilities.getXthArg(0, lower);
String entTwo = lower.contains(" by ") ? CoreUtilities.getXthArg(3, lower): CoreUtilities.getXthArg(2, lower);
List<String> types = Arrays.asList("entity", "player", "npc");
return ((types.contains(entOne) || dEntity.matches(entOne))
&& (cmd.equals("killed") || cmd.equals("kills"))
&& ((entTwo.length() == 0) || (types.contains(entTwo) || dEntity.matches(entTwo))));
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);

boolean by = lower.contains(" by ");
dEntity entOne = by ? entity: damager;
if (!entOne.matchesEntity(CoreUtilities.getXthArg(0, s))) {
return false;
String cmd = CoreUtilities.getXthArg(1, lower);
String attacker = cmd.equals("kills") ? CoreUtilities.getXthArg(0, lower): CoreUtilities.getXthArg(3, lower);
String target = cmd.equals("kills") ? CoreUtilities.getXthArg(2, lower): CoreUtilities.getXthArg(0, lower);
if (attacker.length() > 0) {
if (dEntity.matches(attacker)) {
if (!damager.matchesEntity(attacker)) {
return false;
}
}
else {
if (!cause.asString().equals(attacker)) {
return false;
}
}
}

dEntity entTwo = by ? damager: entity;
if (!(!by && lower.contains("killed") || entTwo.matchesEntity(CoreUtilities.getXthArg(by ? 3: 2, s)))) {
return false;
if (target.length() > 0) {
if (dEntity.matches(target)) {
if (!entity.matchesEntity(target)) {
return false;
}
}
}

if (entity.isValid() && entity.isLivingEntity()) {
Expand Down

0 comments on commit e9243a6

Please sign in to comment.