Skip to content

Commit

Permalink
Entity advanced matcher improvements for NPC handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 11, 2022
1 parent 54f6b1f commit 6f99205
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Expand Up @@ -123,6 +123,7 @@ public abstract class BukkitScriptEvent extends ScriptEvent {
// "entity_flagged:<flag>": a Flag Matchable for EntityTag flags.
// "player_flagged:<flag>": a Flag Matchable for PlayerTag flags (will never match non-players).
// "npc_flagged:<flag>": a Flag Matchable for NPCTag flags (will never match non-NPCs).
// "npc_<type>": matches if the NPC is the given entity type (like "npc_cow" or "npc_mob" or "npc_player").
// Any entity type name: matches if the entity is of the given type, using advanced matchers.
//
// InventoryTag matchers, sometimes identified as "<inventory>":
Expand Down
Expand Up @@ -423,32 +423,50 @@ public EntityTag(NPCTag npc) {

public static HashSet<String> specialEntityMatchables = new HashSet<>(Arrays.asList("entity", "npc", "player", "living", "vehicle", "fish", "projectile", "hanging", "monster", "mob", "animal"));

public final boolean trySpecialEntityMatcher(String text, boolean isNPC) {
if (isNPC) {
return text.equals("entity") || text.equals("npc");
}
switch (text) {
case "entity":
return true;
case "npc":
return isCitizensNPC();
case "player":
return isPlayer();
case "living":
return isLivingEntityType();
case "vehicle":
return getBukkitEntity() instanceof Vehicle;
case "fish":
return getBukkitEntity() instanceof Fish;
case "projectile":
return getBukkitEntity() instanceof Projectile;
case "hanging":
return getBukkitEntity() instanceof Hanging;
case "monster":
return isMonsterType();
case "mob":
return isMobType();
case "animal":
return isAnimalType();
}
return false;
}

public final boolean tryExactMatcher(String text) {
if (specialEntityMatchables.contains(text)) {
switch (text) {
case "entity":
return true;
case "npc":
return isCitizensNPC();
case "player":
return isPlayer();
case "living":
return isLivingEntityType();
case "vehicle":
return getBukkitEntity() instanceof Vehicle;
case "fish":
return getBukkitEntity() instanceof Fish;
case "projectile":
return getBukkitEntity() instanceof Projectile;
case "hanging":
return getBukkitEntity() instanceof Hanging;
case "monster":
return isMonsterType();
case "mob":
return isMobType();
case "animal":
return isAnimalType();
return trySpecialEntityMatcher(text, isCitizensNPC());
}
if (text.startsWith("npc_")) {
String check = text.substring("npc_".length());
if (specialEntityMatchables.contains(check)) {
if (check.equals("player")) { // Special case
return npc.getEntityType() == EntityType.PLAYER;
}
return trySpecialEntityMatcher(check, false);
}
return check.equals(CoreUtilities.toLowerCase(npc.getEntityType().name()));
}
if (text.contains(":")) {
if (text.startsWith("entity_flagged:")) {
Expand Down

0 comments on commit 6f99205

Please sign in to comment.