Add NBT matching to list of valid block/item entries (#1771)

Co-authored-by: Jason Mitchell <[email protected]>
This commit is contained in:
Eclipse-SolandJason Mitchell authored and GitHub committed 2026-05-23 23:08:48 -07:00
1 parent c9633fd745
commit dd1d3aa518
15 files changed
+1132 -252

No files matched your search

@@ -1,124 +1,45 @@
package net.coderbot.iris.shaderpack.materialmap;
import lombok.Getter;
import net.coderbot.iris.Iris;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@Getter
public class BlockEntry {
private final NamespacedId id;
private final Set<Integer> metas;
private final Map<String, String> stateProperties;
public record BlockEntry(
NamespacedId id,
Set<Integer> metas,
Map<String, String> stateProperties,
Map<String, PropertiesTokenizer.NbtValue> nbtProperties
) {
public BlockEntry(NamespacedId id, Set<Integer> metas) {
this(id, metas, Collections.emptyMap(), Collections.emptyMap());
}
public BlockEntry(NamespacedId id, Set<Integer> metas) {
this(id, metas, Collections.emptyMap());
}
public BlockEntry(NamespacedId id, Set<Integer> metas, Map<String, String> stateProperties) {
this(id, metas, stateProperties, Collections.emptyMap());
}
public BlockEntry(NamespacedId id, Set<Integer> metas, Map<String, String> stateProperties) {
this.id = id;
this.metas = metas;
this.stateProperties = stateProperties;
}
public boolean hasNbtProperties() {
return !nbtProperties.isEmpty();
}
/**
* Parses a block ID entry.
*
* @param entry The string representation of the entry. Must not be empty.
*/
@NotNull
public static BlockEntry parse(@NotNull String entry) {
if (entry.isEmpty()) {
throw new IllegalArgumentException("Called BlockEntry::parse with an empty string");
}
public boolean hasStateProperties() {
return !stateProperties.isEmpty();
}
// We can assume that this array is of at least array length because the input string is non-empty.
final String[] splitStates = entry.split(":");
// Trivial case: no states, no namespace
if (splitStates.length == 1) {
return new BlockEntry(new NamespacedId("minecraft", entry), Collections.emptySet());
}
// Examples of what we accept:
// stone
// stone:0
// minecraft:stone:0
// minecraft:stone:0,1,2
// minecraft:furnace:lit=true (blockstate property)
// minecraft:oak_log:axis=y (blockstate property)
// Less trivial case: no metas or state properties, just a namespace
if (splitStates.length == 2 && !StringUtils.isNumeric(splitStates[1].substring(0, 1))
&& !splitStates[1].contains("=")) {
return new BlockEntry(new NamespacedId(splitStates[0], splitStates[1]), Collections.emptySet());
}
// Complex case: metas and/or state properties
final int statesStart;
final NamespacedId id;
if (splitStates.length == 2) {
// "stone:0" or "stone:lit=true"
statesStart = 1;
id = new NamespacedId("minecraft", splitStates[0]);
} else if (StringUtils.isNumeric(splitStates[1].substring(0, 1)) || splitStates[1].contains("=")) {
// "stone:0:something" or "stone:lit=true:something" — unlikely but handle it
statesStart = 1;
id = new NamespacedId("minecraft", splitStates[0]);
} else {
// "minecraft:stone:0" or "minecraft:furnace:lit=true"
statesStart = 2;
id = new NamespacedId(splitStates[0], splitStates[1]);
}
final Set<Integer> metas = new HashSet<>();
final Map<String, String> properties = new HashMap<>();
for (int index = statesStart; index < splitStates.length; index++) {
String segment = splitStates[index];
if (segment.contains("=")) {
// Blockstate property: key=value
for (String prop : segment.split(",")) {
int eq = prop.indexOf('=');
if (eq > 0 && eq < prop.length() - 1) {
properties.put(prop.substring(0, eq), prop.substring(eq + 1));
}
}
} else {
// Metadata IDs: comma-separated integers
for (String metaPart : segment.split(",")) {
try {
metas.add(Integer.parseInt(metaPart));
} catch (NumberFormatException e) {
Iris.logger.warn("Warning: the block ID map entry \"" + entry + "\" could not be fully parsed:");
Iris.logger.warn("- Metadata ids must be a comma separated list of one or more integers, but " + segment + " is not of that form!");
}
}
}
/**
* Parses a block ID entry. All tokenization is delegated to {@link PropertiesTokenizer}.
*
* @param entry The string representation of the entry. Must not be empty.
*/
@NotNull
public static BlockEntry parse(@NotNull String entry) {
if (entry.isEmpty()) {
throw new IllegalArgumentException("Called BlockEntry::parse with an empty string");
}
return new BlockEntry(id, metas, properties);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final BlockEntry that = (BlockEntry) o;
return Objects.equals(id, that.id) && Objects.equals(metas, that.metas) && Objects.equals(stateProperties, that.stateProperties);
}
@Override
public int hashCode() {
return Objects.hash(id, metas, stateProperties);
}
final PropertiesTokenizer.ParsedBlockIdentifier parsed = PropertiesTokenizer.parseBlockIdentifier(entry);
return new BlockEntry(parsed.id(), parsed.metas(), parsed.stateProperties(), parsed.nbtProperties());
}
}
@@ -1,5 +1,6 @@
package net.coderbot.iris.shaderpack.materialmap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -10,14 +11,15 @@ import java.util.Map;
* In 1.7.10, entities are registered with names like "CaveSpider".
* Some entities were also renamed or split into multiple types.
*
* This map is used at lookup time: when a modern shader pack references an entity by its
* modern name, we resolve it to the 1.7.10 name that EntityList uses.
*/
public class EntityFlatteningMap {
/** Maps modern entity name -> 1.7.10 entity name. */
private static final Map<String, String> MODERN_TO_LEGACY = new HashMap<>();
/** Maps modern entity name -> legacy entity name + NBT. */
private static final Map<String, BlockEntry> MODERN_TO_LEGACY_NBT = new HashMap<>();
static {
// Simple case changes
simple("Arrow", "arrow");
@@ -35,7 +37,6 @@ public class EntityFlatteningMap {
simple("Pig", "pig");
simple("Sheep", "sheep");
simple("Silverfish", "silverfish");
simple("Skeleton", "skeleton");
simple("Slime", "slime");
simple("Snowball", "snowball");
simple("Spider", "spider");
@@ -49,7 +50,6 @@ public class EntityFlatteningMap {
simple("CaveSpider", "cave_spider");
simple("EnderCrystal", "end_crystal");
simple("EnderDragon", "ender_dragon");
simple("EntityHorse", "horse");
simple("EyeOfEnderSignal", "eye_of_ender");
simple("FallingSand", "falling_block");
simple("Fireball", "fireball");
@@ -79,6 +79,36 @@ public class EntityFlatteningMap {
simple("MinecartHopper", "hopper_minecart");
simple("MinecartSpawner", "spawner_minecart");
simple("MinecartTNT", "tnt_minecart");
MODERN_TO_LEGACY_NBT.put("skeleton", new BlockEntry(
new NamespacedId("Skeleton"), Collections.emptySet(), Collections.emptyMap(),
Map.of("SkeletonType", new PropertiesTokenizer.NbtValue("0", false))));
MODERN_TO_LEGACY_NBT.put("wither_skeleton", new BlockEntry(
new NamespacedId("Skeleton"), Collections.emptySet(), Collections.emptyMap(),
Map.of("SkeletonType", new PropertiesTokenizer.NbtValue("1", false))));
// 1.7.10 EntityHorse with Type byte: 0=horse, 1=donkey, 2=mule, 3=zombie_horse,
// 4=skeleton_horse. 1.11+ split each into its own entity type.
MODERN_TO_LEGACY_NBT.put("horse", new BlockEntry(
new NamespacedId("EntityHorse"), Collections.emptySet(), Collections.emptyMap(),
Map.of("Type", new PropertiesTokenizer.NbtValue("0", false))));
MODERN_TO_LEGACY_NBT.put("donkey", new BlockEntry(
new NamespacedId("EntityHorse"), Collections.emptySet(), Collections.emptyMap(),
Map.of("Type", new PropertiesTokenizer.NbtValue("1", false))));
MODERN_TO_LEGACY_NBT.put("mule", new BlockEntry(
new NamespacedId("EntityHorse"), Collections.emptySet(), Collections.emptyMap(),
Map.of("Type", new PropertiesTokenizer.NbtValue("2", false))));
MODERN_TO_LEGACY_NBT.put("zombie_horse", new BlockEntry(
new NamespacedId("EntityHorse"), Collections.emptySet(), Collections.emptyMap(),
Map.of("Type", new PropertiesTokenizer.NbtValue("3", false))));
MODERN_TO_LEGACY_NBT.put("skeleton_horse", new BlockEntry(
new NamespacedId("EntityHorse"), Collections.emptySet(), Collections.emptyMap(),
Map.of("Type", new PropertiesTokenizer.NbtValue("4", false))));
// 1.7.10 Zombie with IsVillager.
MODERN_TO_LEGACY_NBT.put("zombie_villager", new BlockEntry(
new NamespacedId("Zombie"), Collections.emptySet(), Collections.emptyMap(),
Map.of("IsVillager", new PropertiesTokenizer.NbtValue("1", false))));
}
/**
@@ -89,6 +119,10 @@ public class EntityFlatteningMap {
return MODERN_TO_LEGACY.get(modernName);
}
public static BlockEntry toLegacyWithNbt(String modernName) {
return MODERN_TO_LEGACY_NBT.get(modernName);
}
private static void simple(String legacy, String modern) {
MODERN_TO_LEGACY.put(modern, legacy);
}
@@ -158,18 +158,18 @@ public class FlatteningMap {
rename("dark_oak_wall_sign", "wall_sign");
// === Skull / Head blocks (ID 144) ===
// In 1.7.10, skull type is stored in tile entity, not metadata.
// All modern skull blocks map to the same 1.7.10 block.
rename("skeleton_skull", "skull");
rename("skeleton_wall_skull", "skull");
rename("wither_skeleton_skull", "skull");
rename("wither_skeleton_wall_skull", "skull");
rename("zombie_head", "skull");
rename("zombie_wall_head", "skull");
rename("player_head", "skull");
rename("player_wall_head", "skull");
rename("creeper_head", "skull");
rename("creeper_wall_head", "skull");
// 1.7.10 stores skull type in TileEntitySkull's SkullType byte (0=skeleton,
// 1=wither, 2=zombie, 3=player, 4=creeper).
MODERN_TO_LEGACY.put("skeleton_skull", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("skeleton_wall_skull", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("wither_skeleton_skull", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("1", false)))));
MODERN_TO_LEGACY.put("wither_skeleton_wall_skull", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("1", false)))));
MODERN_TO_LEGACY.put("zombie_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("2", false)))));
MODERN_TO_LEGACY.put("zombie_wall_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("2", false)))));
MODERN_TO_LEGACY.put("player_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("3", false)))));
MODERN_TO_LEGACY.put("player_wall_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("3", false)))));
MODERN_TO_LEGACY.put("creeper_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("4", false)))));
MODERN_TO_LEGACY.put("creeper_wall_head", List.of(new BlockEntry(new NamespacedId("minecraft", "skull"), Collections.emptySet(), Collections.emptyMap(), Map.of("SkullType", new PropertiesTokenizer.NbtValue("4", false)))));
// ==========================================
// Metadata variants (block split by flattening)
@@ -321,29 +321,29 @@ public class FlatteningMap {
meta("mossy_cobblestone_wall", "cobblestone_wall", 1);
// === Flower pot (ID 140) ===
// In 1.7.10, flower pot contents are in the tile entity.
// In 1.13+, each potted plant is a separate block.
rename("potted_oak_sapling", "flower_pot");
rename("potted_spruce_sapling", "flower_pot");
rename("potted_birch_sapling", "flower_pot");
rename("potted_jungle_sapling", "flower_pot");
rename("potted_acacia_sapling", "flower_pot");
rename("potted_dark_oak_sapling", "flower_pot");
rename("potted_fern", "flower_pot");
rename("potted_dandelion", "flower_pot");
rename("potted_poppy", "flower_pot");
rename("potted_blue_orchid", "flower_pot");
rename("potted_allium", "flower_pot");
rename("potted_azure_bluet", "flower_pot");
rename("potted_red_tulip", "flower_pot");
rename("potted_orange_tulip", "flower_pot");
rename("potted_white_tulip", "flower_pot");
rename("potted_pink_tulip", "flower_pot");
rename("potted_oxeye_daisy", "flower_pot");
rename("potted_red_mushroom", "flower_pot");
rename("potted_brown_mushroom", "flower_pot");
rename("potted_dead_bush", "flower_pot");
rename("potted_cactus", "flower_pot");
// 1.7.10 stores potted contents in TileEntityFlowerPot's NBT. 1.13+ has a separate block
// per potted plant.
MODERN_TO_LEGACY.put("potted_oak_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_spruce_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("1", false)))));
MODERN_TO_LEGACY.put("potted_birch_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("2", false)))));
MODERN_TO_LEGACY.put("potted_jungle_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("3", false)))));
MODERN_TO_LEGACY.put("potted_acacia_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("4", false)))));
MODERN_TO_LEGACY.put("potted_dark_oak_sapling", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:sapling", false), "Data", new PropertiesTokenizer.NbtValue("5", false)))));
MODERN_TO_LEGACY.put("potted_fern", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:tallgrass", false), "Data", new PropertiesTokenizer.NbtValue("2", false)))));
MODERN_TO_LEGACY.put("potted_dandelion", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:yellow_flower", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_poppy", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_blue_orchid", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("1", false)))));
MODERN_TO_LEGACY.put("potted_allium", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("2", false)))));
MODERN_TO_LEGACY.put("potted_azure_bluet", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("3", false)))));
MODERN_TO_LEGACY.put("potted_red_tulip", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("4", false)))));
MODERN_TO_LEGACY.put("potted_orange_tulip", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("5", false)))));
MODERN_TO_LEGACY.put("potted_white_tulip", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("6", false)))));
MODERN_TO_LEGACY.put("potted_pink_tulip", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("7", false)))));
MODERN_TO_LEGACY.put("potted_oxeye_daisy", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_flower", false), "Data", new PropertiesTokenizer.NbtValue("8", false)))));
MODERN_TO_LEGACY.put("potted_red_mushroom", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:red_mushroom", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_brown_mushroom", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:brown_mushroom", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_dead_bush", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:deadbush", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
MODERN_TO_LEGACY.put("potted_cactus", List.of(new BlockEntry(new NamespacedId("minecraft", "flower_pot"), Collections.emptySet(), Collections.emptyMap(), Map.of("Item", new PropertiesTokenizer.NbtValue("minecraft:cactus", false), "Data", new PropertiesTokenizer.NbtValue("0", false)))));
// === Anvil (ID 145) ===
// Bits 0-1 = facing, bits 2-3 = damage level
@@ -928,26 +928,26 @@ public class FlatteningMap {
List<BlockEntry> result = new ArrayList<>(Math.min(a.size(), b.size()));
for (BlockEntry entryA : a) {
for (BlockEntry entryB : b) {
if (!entryA.getId().equals(entryB.getId())) continue;
if (!entryA.id().equals(entryB.id())) continue;
Set<Integer> metasA = entryA.getMetas();
Set<Integer> metasB = entryB.getMetas();
Set<Integer> metasA = entryA.metas();
Set<Integer> metasB = entryB.metas();
if (metasA.isEmpty() && metasB.isEmpty()) {
// Both wildcard -> wildcard
result.add(new BlockEntry(entryA.getId(), Collections.emptySet()));
result.add(new BlockEntry(entryA.id(), Collections.emptySet()));
} else if (metasA.isEmpty()) {
// A is wildcard -> use B's constraint
result.add(new BlockEntry(entryA.getId(), metasB));
result.add(new BlockEntry(entryA.id(), metasB));
} else if (metasB.isEmpty()) {
// B is wildcard -> use A's constraint
result.add(new BlockEntry(entryA.getId(), metasA));
result.add(new BlockEntry(entryA.id(), metasA));
} else {
// Both have specific metas -> intersect
Set<Integer> intersection = new IntOpenHashSet(metasA);
intersection.retainAll(metasB);
if (!intersection.isEmpty()) {
result.add(new BlockEntry(entryA.getId(), intersection));
result.add(new BlockEntry(entryA.id(), intersection));
}
}
}
@@ -0,0 +1,388 @@
package net.coderbot.iris.shaderpack.materialmap;
import net.coderbot.iris.Iris;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
// This got complicated fast
/**
* This class handles all processing of individual block entries per material ID line.
*/
public final class PropertiesTokenizer {
private PropertiesTokenizer() {
}
/**
* Splits a property value into individual block identifier tokens.
*/
public static List<String> tokenizeValues(String input, char delimiter) {
if (input.indexOf('"') == -1 && input.indexOf('\\') == -1 && input.indexOf('[') == -1) {
return tokenizeSimple(input, delimiter);
}
return tokenizeFull(input, delimiter);
}
// Most blocks should take this path
private static List<String> tokenizeSimple(String input, char delimiter) {
final List<String> result = new ArrayList<>();
final boolean isWhitespaceDelim = Character.isWhitespace(delimiter);
if (isWhitespaceDelim) {
for (String part : input.split("\\s+")) {
if (!part.isEmpty()) {
result.add(part);
}
}
} else {
for (String part : input.split(String.valueOf(delimiter))) {
final String trimmed = part.trim();
if (!trimmed.isEmpty()) {
result.add(trimmed);
}
}
}
return result;
}
// Found special characters
private static List<String> tokenizeFull(String input, char delimiter) {
final boolean isWhitespaceDelim = Character.isWhitespace(delimiter);
final List<String> result = new ArrayList<>();
final StringBuilder current = new StringBuilder();
boolean inQuotes = false;
boolean escaped = false;
int bracketDepth = 0;
for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);
if (escaped) {
if (bracketDepth > 0) {
// Preserve escapes inside brackets for parseBlockIdentifier
current.append('\\');
}
current.append(c);
escaped = false;
} else if (c == '\\') {
escaped = true;
} else if (bracketDepth > 0) {
current.append(c);
if (c == '[') {
bracketDepth++;
}
else if (c == ']') {
bracketDepth--;
}
} else if (c == '[' && !inQuotes) {
current.append(c);
bracketDepth = 1;
} else if (c == '"') {
inQuotes = !inQuotes;
} else if (!inQuotes && (isWhitespaceDelim ? Character.isWhitespace(c) : c == delimiter)) {
if (!current.isEmpty()) {
result.add(current.toString());
current.setLength(0);
}
} else {
current.append(c);
}
}
if (inQuotes) {
Iris.logger.warn("Unclosed quote in properties token: {}", input);
}
if (!current.isEmpty()) {
result.add(current.toString());
}
return result;
}
// Begin reading the special case block
public static ParsedBlockIdentifier parseBlockIdentifier(String entry) {
final StringBuilder blockIdBuilder = new StringBuilder();
final Map<String, NbtValue> nbt = new LinkedHashMap<>();
boolean escaped = false;
boolean inDoubleQuotes = false;
boolean inSingleQuotes = false;
boolean inBracket = false;
// NBT pair
StringBuilder nbtKey = new StringBuilder();
StringBuilder nbtValue = new StringBuilder();
boolean readingValue = false;
boolean valueLiteral = false;
for (int i = 0; i < entry.length(); i++) {
final char c = entry.charAt(i);
// Ignore special characters
if (escaped) {
appendCurrent(inBracket, readingValue, blockIdBuilder, nbtKey, nbtValue, c);
escaped = false;
continue;
}
if (c == '\\') {
escaped = true;
continue;
}
// Detect special characters
if (inSingleQuotes) {
if (c == '\'') {
inSingleQuotes = false;
} else {
nbtValue.append(c);
}
continue;
}
if (inDoubleQuotes) {
if (c == '"') {
inDoubleQuotes = false;
} else {
appendCurrent(inBracket, readingValue, blockIdBuilder, nbtKey, nbtValue, c);
}
continue;
}
if (c == '"') {
inDoubleQuotes = true;
continue;
}
if (!inBracket) {
if (c == '[') {
inBracket = true;
nbtKey.setLength(0);
nbtValue.setLength(0);
readingValue = false;
valueLiteral = false;
} else {
blockIdBuilder.append(c);
}
} else {
if (c == ']') {
flushNbtPair(nbtKey, nbtValue, readingValue, valueLiteral, nbt);
inBracket = false;
} else if (c == '=' && !readingValue) {
readingValue = true;
valueLiteral = false;
} else if (c == '\'' && readingValue && nbtValue.isEmpty()) {
inSingleQuotes = true;
valueLiteral = true;
} else if (c == ',') {
flushNbtPair(nbtKey, nbtValue, readingValue, valueLiteral, nbt);
nbtKey.setLength(0);
nbtValue.setLength(0);
readingValue = false;
valueLiteral = false;
} else {
if (readingValue) {
nbtValue.append(c);
} else {
nbtKey.append(c);
}
}
}
}
if (inDoubleQuotes) {
Iris.logger.warn("Unclosed double quote in block entry: {}", entry);
}
if (inSingleQuotes) {
Iris.logger.warn("Unclosed single quote in block entry: {}", entry);
}
if (escaped) {
Iris.logger.warn("Trailing backslash in block entry: {}", entry);
}
if (inBracket) {
Iris.logger.warn("Unclosed bracket in block entry: {}", entry);
flushNbtPair(nbtKey, nbtValue, readingValue, valueLiteral, nbt);
}
final String baseEntry = blockIdBuilder.toString().trim();
final Map<String, NbtValue> nbtProperties = nbt.isEmpty() ? Collections.emptyMap() : nbt;
return splitBaseEntry(entry, baseEntry, nbtProperties);
}
/**
* Splits the bracket-stripped portion of a block identifier (e.g. {@code minecraft:furnace:lit=true})
* into its components.
*
* Accepted forms:
* stone (vanilla block)
* stone:0 (vanilla block with meta)
* minecraft:stone (block with namespaced id specified)
* minecraft:stone:0 (single meta)
* minecraft:stone:0,1,2 (multiple metas)
* minecraft:furnace:lit=true (blockstate property)
* minecraft:oak_log:axis=y,x:variant=oak (multiple blockstate properties)
* flower_pot:1[Item=minecraft:red_flower,Data=0] (used together with NBT brackets)
*/
private static ParsedBlockIdentifier splitBaseEntry(String entry, String baseEntry, Map<String, NbtValue> nbtProperties) {
final Set<Integer> metas = new HashSet<>();
final Map<String, String> stateProperties = new LinkedHashMap<>();
if (baseEntry.isEmpty()) {
return new ParsedBlockIdentifier(
new NamespacedId("minecraft", baseEntry),
Collections.emptySet(),
Collections.emptyMap(),
nbtProperties
);
}
final String[] splitStates = baseEntry.split(":");
// Vanilla block (minecraft namespaced)
if (splitStates.length == 1) {
return new ParsedBlockIdentifier(
new NamespacedId("minecraft", baseEntry),
Collections.emptySet(),
Collections.emptyMap(),
nbtProperties
);
}
// Two-segment with no metas/state properties: "minecraft:stone"
if (splitStates.length == 2
&& !StringUtils.isNumeric(splitStates[1].substring(0, 1))
&& !splitStates[1].contains("=")) {
return new ParsedBlockIdentifier(
new NamespacedId(splitStates[0], splitStates[1]),
Collections.emptySet(),
Collections.emptyMap(),
nbtProperties
);
}
// Metas and/or state properties
final NamespacedId id;
final int statesStart;
if (splitStates.length == 2) {
// "stone:0" or "stone:lit=true"
id = new NamespacedId("minecraft", splitStates[0]);
statesStart = 1;
} else if (StringUtils.isNumeric(splitStates[1].substring(0, 1)) || splitStates[1].contains("=")) {
// "stone:0:something" or "stone:lit=true:something", likely will need this for snowy blocks in the future.
id = new NamespacedId("minecraft", splitStates[0]);
statesStart = 1;
} else {
// "minecraft:stone:0" or "minecraft:furnace:lit=true"
id = new NamespacedId(splitStates[0], splitStates[1]);
statesStart = 2;
}
for (int index = statesStart; index < splitStates.length; index++) {
final String segment = splitStates[index];
if (segment.contains("=")) {
// Blockstate property segment: key=value[,key=value...]
for (String prop : segment.split(",")) {
final int eq = prop.indexOf('=');
if (eq > 0 && eq < prop.length() - 1) {
stateProperties.put(prop.substring(0, eq), prop.substring(eq + 1));
}
}
} else {
// Metadata segment (stone:[1,2,3,4,5])
for (String metaPart : segment.split(",")) {
try {
metas.add(Integer.parseInt(metaPart));
} catch (NumberFormatException e) {
Iris.logger.warn("Warning: the block ID map entry \"{}\" could not be fully parsed:", entry);
Iris.logger.warn("- Metadata ids must be a comma separated list of one or more integers, but {} is not of that form!", segment);
}
}
}
}
return new ParsedBlockIdentifier(
id,
metas.isEmpty() ? Collections.emptySet() : metas,
stateProperties.isEmpty() ? Collections.emptyMap() : stateProperties,
nbtProperties
);
}
private static void appendCurrent(boolean inBracket, boolean readingValue,
StringBuilder blockIdBuilder, StringBuilder nbtKey,
StringBuilder nbtValue, char c) {
if (!inBracket) {
blockIdBuilder.append(c);
} else if (readingValue) {
nbtValue.append(c);
} else {
nbtKey.append(c);
}
}
private static void flushNbtPair(StringBuilder key, StringBuilder value,
boolean readingValue, boolean valueLiteral,
Map<String, NbtValue> nbt) {
final String k = key.toString().trim();
if (k.isEmpty()) return;
if (!readingValue) {
// No = was found, existence is good enough
nbt.put(k, null);
return;
}
nbt.put(k, new NbtValue(value.toString().trim(), valueLiteral));
}
public static String stripQuotes(String value) {
if (value.length() >= 2 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
return value.substring(1, value.length() - 1);
}
return value;
}
public record NbtValue(String value, boolean literal) {
}
public record ParsedBlockIdentifier(
NamespacedId id,
Set<Integer> metas,
Map<String, String> stateProperties,
Map<String, NbtValue> nbtProperties
) {
}
}