Skip to content

Commit

Permalink
Proper EntityTag matching
Browse files Browse the repository at this point in the history
  • Loading branch information
tal5 committed Dec 20, 2023
1 parent b8d2798 commit 293904b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
@@ -1,11 +1,45 @@
package com.denizenscript.clientizen.events;

import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.events.ScriptEventCouldMatcher;
import net.minecraft.entity.EntityType;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ClientizenScriptEventRegistry {

public static void registerEvents() {
ScriptEvent.registerScriptEvent(KeyPressReleaseScriptEvent.class);
ScriptEvent.registerScriptEvent(ScreenOpenCloseEvent.class);

ScriptEventCouldMatcher.knownValidatorTypes.put("entity", ClientizenScriptEventRegistry::couldMatchEntity);
}

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

public static boolean couldMatchEntity(String matcher) {
if (ENTITY_PLAINTEXT_MATCHERS.contains(matcher)) {
return true;
}
if (ScriptEvent.isAdvancedMatchable(matcher)) {
ScriptEvent.MatchHelper matchHelper = ScriptEvent.createMatcher(matcher);
for (EntityType<?> entityType : Registries.ENTITY_TYPE) {
if (matchHelper.doesMatch(entityType.getUntranslatedName())) {
return true;
}
}
ScriptEvent.addPossibleCouldMatchFailReason("Matcher doesn't match any entity type", matcher);
return false;
}
if (Registries.ENTITY_TYPE.containsId(Identifier.tryParse(matcher))) {
return true;
}
ScriptEvent.addPossibleCouldMatchFailReason("Invalid entity type", matcher);
return false;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/denizenscript/clientizen/objects/EntityTag.java
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.clientizen.mixin.ClientWorldAccessor;
import com.denizenscript.clientizen.util.Utilities;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
Expand All @@ -13,6 +14,13 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.decoration.AbstractDecorationEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.passive.FishEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.entity.vehicle.VehicleEntity;
import net.minecraft.text.Text;

import java.util.UUID;
Expand All @@ -37,6 +45,19 @@ public class EntityTag implements ObjectTag, Adjustable {
//
// Note that a spawned entity can be a living entity (a player, animal, monster, etc.) or a non-living entity (a painting, item frame, etc).
//
// @Matchable
// EntityTag matchers, sometimes identified as <entity>:
// "entity" plaintext, always matches.
// "vehicle" plaintext: matches for any vehicle type (minecarts, boats, horses, etc).
// "fish" plaintext: matches for any fish type (cod, pufferfish, etc).
// "projectile" plaintext: matches for any projectile type (arrow, trident, fish hook, snowball, etc).
// "hanging" plaintext: matches for any hanging type (painting, item_frame, etc).
// "monster" plaintext: matches for any monster type (creepers, zombies, etc).
// "animal" plaintext: matches for any animal type (pigs, cows, etc).
// "mob" plaintext: matches for any mob type (creepers, pigs, etc).
// "living" plaintext: matches for any living type (players, pigs, creepers, etc).
// Any entity type name: matches if the entity is of the given type, using advanced matchers.
//
// -->

public final UUID uuid;
Expand Down Expand Up @@ -244,6 +265,24 @@ public boolean isUnique() {
return uuid != null;
}

@Override
public boolean advancedMatches(String matcher) {
return ScriptEvent.createMatcher(matcher).doesMatch(getTypeName(), text ->
switch (text) {
case "entity" -> true;
case "vehicle" -> getEntity() instanceof VehicleEntity;
case "fish" -> getEntity() instanceof FishEntity;
case "projectile" -> getEntity() instanceof ProjectileEntity;
case "hanging" -> getEntity() instanceof AbstractDecorationEntity;
case "monster" -> getEntity() instanceof Monster;
case "animal" -> getEntity() instanceof AnimalEntity;
case "mob" -> getEntity() instanceof MobEntity;
case "living" -> getEntity() instanceof LivingEntity;
default -> false;
}
);
}

private String prefix = "Entity";

@Override
Expand Down

0 comments on commit 293904b

Please sign in to comment.