Skip to content

Commit

Permalink
Proper MaterialTag matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
tal5 committed Dec 20, 2023
1 parent 293904b commit 932031d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
@@ -1,5 +1,6 @@
package com.denizenscript.clientizen.events;

import com.denizenscript.clientizen.util.Utilities;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.events.ScriptEventCouldMatcher;
import net.minecraft.entity.EntityType;
Expand All @@ -13,10 +14,11 @@
public class ClientizenScriptEventRegistry {

public static void registerEvents() {
ScriptEventCouldMatcher.knownValidatorTypes.put("entity", ClientizenScriptEventRegistry::couldMatchEntity);
ScriptEventCouldMatcher.knownValidatorTypes.put("material", ClientizenScriptEventRegistry::couldMatchMaterial);

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(
Expand All @@ -42,4 +44,33 @@ public static boolean couldMatchEntity(String matcher) {
ScriptEvent.addPossibleCouldMatchFailReason("Invalid entity type", matcher);
return false;
}

public static final Set<String> MATERIAL_PLAINTEXT_MATCHERS = new HashSet<>(Arrays.asList("material", "block", "item"));

public static boolean couldMatchMaterial(String matcher) {
if (MATERIAL_PLAINTEXT_MATCHERS.contains(matcher)) {
return true;
}
if (ScriptEvent.isAdvancedMatchable(matcher)) {
ScriptEvent.MatchHelper matchHelper = ScriptEvent.createMatcher(matcher);
for (Identifier item : Registries.ITEM.getIds()) {
if (matchHelper.doesMatch(Utilities.idToString(item))) {
return true;
}
}
for (Identifier block : Registries.BLOCK.getIds()) {
if (matchHelper.doesMatch(Utilities.idToString(block))) {
return true;
}
}
ScriptEvent.addPossibleCouldMatchFailReason("Matcher doesn't match any block/item", matcher);
return false;
}
Identifier id = Identifier.tryParse(matcher);
if (Registries.ITEM.containsId(id) || Registries.BLOCK.containsId(id)) {
return true;
}
ScriptEvent.addPossibleCouldMatchFailReason("Invalid block/item name", matcher);
return false;
}
}
@@ -1,6 +1,7 @@
package com.denizenscript.clientizen.objects;

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 Down Expand Up @@ -38,6 +39,13 @@ public class MaterialTag implements ObjectTag, Adjustable {
//
// Material types: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html>.
//
// @Matchable
// MaterialTag matchers, sometimes identified as <material>:
// "material" plaintext: always matches.
// "block" plaintext: matches if the material is a block-type material.
// "item" plaintext: matches if the material is an item-type material.
// Any block/item name: matches if the material is of the given type, using advanced matchers.
//
// -->

// Needs to match the server-side impl, which uses bukkit's all-in-one Material enum
Expand Down Expand Up @@ -96,6 +104,14 @@ public String getName() {
return Utilities.idToString(state != null ? Registries.BLOCK.getId(state.getBlock()) : Registries.ITEM.getId(item));
}

public boolean isBlock() {
return state != null || Registries.BLOCK.containsId(Registries.ITEM.getId(item));
}

public boolean isItem() {
return item != null || Registries.ITEM.containsId(Registries.BLOCK.getId(state.getBlock()));
}

public static void register() {
PropertyParser.registerPropertyTagHandlers(MaterialTag.class, tagProcessor);

Expand Down Expand Up @@ -151,6 +167,18 @@ public ObjectTag duplicate() {
return state != null ? new MaterialTag(state) : new MaterialTag(item);
}

@Override
public boolean advancedMatches(String matcher) {
return ScriptEvent.createMatcher(matcher).doesMatch(getName(), text ->
switch (text) {
case "material" -> true;
case "block" -> isBlock();
case "item" -> isItem();
default -> false;
}
);
}

private String prefix = "Material";

@Override
Expand Down

0 comments on commit 932031d

Please sign in to comment.