Skip to content

Commit

Permalink
impl tryObjectSwitch and improve damaged events
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 7, 2022
1 parent f6b02a7 commit 59ceb4c
Show file tree
Hide file tree
Showing 19 changed files with 22 additions and 24 deletions.
Expand Up @@ -63,8 +63,7 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, event.getLoc(), "to")) {
return false;
}
String at = path.switches.get("at");
if (at != null && (target == null || !target.tryAdvancedMatcher(at))) {
if (!path.tryObjectSwitch("at", target)) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -63,8 +63,7 @@ public boolean matches(ScriptPath path) {
if (!runWithCheck(path, firework)) {
return false;
}
if (path.switches.containsKey("elytra")
&& !new ItemTag(player.getPlayerEntity().getEquipment().getChestplate()).tryAdvancedMatcher(path.switches.get("elytra"))) {
if (!path.tryObjectSwitch("elytra", new ItemTag(player.getPlayerEntity().getEquipment().getChestplate()))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -56,7 +56,7 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, event.getPlayer().getLocation())) {
return false;
}
if (path.switches.containsKey("result") && !new ItemTag(event.getTrade().getResult()).tryAdvancedMatcher(path.switches.get("result"))) {
if (!path.tryObjectSwitch("result", new ItemTag(event.getTrade().getResult()))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -66,7 +66,7 @@ public boolean matches(ScriptPath path) {
if (!material.tryAdvancedMatcher(path.eventArgLowerAt(0))) {
return false;
}
if (path.switches.containsKey("type") && !material.tryAdvancedMatcher(path.switches.get("type"))) {
if (!path.tryObjectSwitch("type", material)) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -71,7 +71,7 @@ public boolean matches(ScriptPath path) {
return false;
}
}
if (path.switches.containsKey("type") && !material.tryAdvancedMatcher(path.switches.get("type"))) {
if (!path.tryObjectSwitch("type", material)) {
return false;
}
if (!runInCheck(path, location) && !runInCheck(path, destination)) {
Expand Down
Expand Up @@ -41,6 +41,7 @@ public class EntityDamagedScriptEvent extends BukkitScriptEvent implements Liste
// @Location true
//
// @Switch with:<item> to only process the event when the item used to cause damage (in the damager's hand) is a specified item.
// @Switch type:<entity> to only run if the entity damaged matches the entity input.
//
// @Cancellable true
//
Expand Down Expand Up @@ -71,7 +72,7 @@ public EntityDamagedScriptEvent() {
registerCouldMatcher("<entity> damaged (by <'cause'>)");
registerCouldMatcher("<entity> damaged by <entity>");
registerCouldMatcher("<entity> damages <entity>");
registerSwitches("with");
registerSwitches("with", "type");
}

public static EntityDamagedScriptEvent instance;
Expand Down Expand Up @@ -123,7 +124,7 @@ public boolean matches(ScriptPath path) {
}
}
}
if (!entity.tryAdvancedMatcher(target)) {
if (!entity.tryAdvancedMatcher(target) || !path.tryObjectSwitch("type", entity)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
Expand Down
Expand Up @@ -86,7 +86,7 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (path.switches.containsKey("by") && (damager == null || !damager.tryAdvancedMatcher(path.switches.get("by")))) {
if (!path.tryObjectSwitch("by", damager)) {
return false;
}
if (!runGenericSwitchCheck(path, "cause", cause == null ? null : cause.asString())) {
Expand Down
Expand Up @@ -60,7 +60,7 @@ public boolean matches(ScriptPath path) {
if (!entity.tryAdvancedMatcher(target)) {
return false;
}
if (path.switches.containsKey("item") && !item.tryAdvancedMatcher(path.switches.get("item"))) {
if (!path.tryObjectSwitch("item", item)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
Expand Down
Expand Up @@ -119,7 +119,7 @@ else if (cmd.equals("shoots")) {
if (!pTest.isEmpty() && !pTest.equals("projectile") && !projectile.tryAdvancedMatcher(pTest)) {
return false;
}
if (path.switches.containsKey("with") && !projectile.tryAdvancedMatcher(path.switches.get("with"))) {
if (!path.tryObjectSwitch("with", projectile)) {
return false;
}
if (!material.tryAdvancedMatcher(path.eventArgLowerAt(2))) {
Expand Down
Expand Up @@ -78,7 +78,7 @@ public boolean matches(ScriptPath path) {
if (!hitEntity.tryAdvancedMatcher(path.eventArgLowerAt(2))) {
return false;
}
if (path.switches.containsKey("shooter") && (shooter == null || !shooter.tryAdvancedMatcher(path.switches.get("shooter")))) {
if (!path.tryObjectSwitch("shooter", shooter)) {
return false;
}
if (!runInCheck(path, location)) {
Expand Down
Expand Up @@ -58,7 +58,7 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, event.getPlayer().getLocation())) {
return false;
}
if (path.switches.containsKey("item") && !new ItemTag(event.getPlayer().getInventory().getItem(event.getNewSlot())).tryAdvancedMatcher(path.switches.get("item"))) {
if (!path.tryObjectSwitch("item", new ItemTag(event.getPlayer().getInventory().getItem(event.getNewSlot())))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -143,7 +143,7 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, location != null ? location : event.getPlayer().getLocation())) {
return false;
}
if (path.switches.containsKey("type") && !blockMaterial.tryAdvancedMatcher(path.switches.get("type"))) {
if (!path.tryObjectSwitch("type", blockMaterial)) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -68,7 +68,7 @@ public boolean matches(ScriptPath path) {
if (path.eventArgLowerAt(isAt ? 5 : 4).equals("with") && !item.tryAdvancedMatcher(path.eventArgLowerAt(isAt ? 6 : 5))) {
return false;
}
if (path.switches.containsKey("type") && !entity.tryAdvancedMatcher(path.switches.get("type"))) {
if (!path.tryObjectSwitch("type", entity)) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -56,10 +56,10 @@ public boolean couldMatch(ScriptPath path) {

@Override
public boolean matches(ScriptPath path) {
if (path.switches.containsKey("main") && !new ItemTag(event.getMainHandItem()).tryAdvancedMatcher(path.switches.get("main"))) {
if (!path.tryObjectSwitch("main", new ItemTag(event.getMainHandItem()))) {
return false;
}
if (path.switches.containsKey("offhand") && !new ItemTag(event.getOffHandItem()).tryAdvancedMatcher(path.switches.get("offhand"))) {
if (!path.tryObjectSwitch("offhand", new ItemTag(event.getOffHandItem()))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -63,10 +63,10 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, to) && !runInCheck(path, from)) {
return false;
}
if (path.switches.containsKey("from") && !from.tryAdvancedMatcher(path.switches.get("from"))) {
if (!path.tryObjectSwitch("from", from)) {
return false;
}
if (path.switches.containsKey("to") && (to == null || !to.tryAdvancedMatcher(path.switches.get("to")))) {
if (!path.tryObjectSwitch("to", to)) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -60,7 +60,7 @@ public boolean couldMatch(ScriptPath path) {
if (!cmd.equals("damaged") && !cmd.equals("damages")) {
return false;
}
if (path.eventArgLowerAt(3).equals("by")) {
if (path.eventArgLowerAt(2).equals("by")) {
return false;
}
if (!exactMatchesVehicle(path.eventArgLowerAt(0)) && !exactMatchesVehicle(path.eventArgLowerAt(2))) {
Expand Down
Expand Up @@ -38,7 +38,7 @@ public SpawnChangeScriptEvent() {

@Override
public boolean matches(ScriptPath path) {
if (path.switches.containsKey("world") && !new WorldTag(event.getWorld()).tryAdvancedMatcher(path.switches.get("world"))) {
if (!path.tryObjectSwitch("world", new WorldTag(event.getWorld()))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -53,7 +53,7 @@ else if (changeType.equals("begins")) {
else if (!changeType.equals("changes")) {
return false;
}
if (path.switches.containsKey("in") && !new WorldTag(event.getWorld()).tryAdvancedMatcher(path.switches.get("in"))) {
if (!path.tryObjectSwitch("in", new WorldTag(event.getWorld()))) {
return false;
}
return super.matches(path);
Expand Down
Expand Up @@ -8,7 +8,6 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

Expand Down

0 comments on commit 59ceb4c

Please sign in to comment.