mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-22 05:12:16 +00:00
Add NBT matching to list of valid block/item entries (#1771)
Co-authored-by: Jason Mitchell <[email protected]>
This commit is contained in:
1 parent
c9633fd745
commit
dd1d3aa518
15 files changed
+1132
-252
No files matched your search
@@ -23,26 +23,33 @@ import java.util.Set;
|
||||
|
||||
public class BlockMaterialMapping {
|
||||
|
||||
public record BlockIdMaps(Reference2ObjectMap<Block, Int2IntMap> blockMetaMap, NbtConditionalIdMap<Block> tileEntityMap) {}
|
||||
|
||||
/** Meta-key bit OR'd in at runtime when a snowy-tagged block has snow above it. */
|
||||
public static final int SNOWY_META_BIT = 0x10;
|
||||
|
||||
/**
|
||||
* Creates a two-level map structure for block material IDs.
|
||||
* Based on Iris's BlockState mapping approach adapted for 1.7.10's metadata system.
|
||||
* Creates the standard block meta ID map, the TileEntity NBT-conditional map, and registers
|
||||
* snowy blocks on {@link BlockRenderingSettings}.
|
||||
*/
|
||||
public static Reference2ObjectMap<Block, Int2IntMap> createBlockMetaIdMap(Int2ObjectMap<List<BlockEntry>> blockPropertiesMap, boolean skipFlattening) {
|
||||
public static BlockIdMaps createBlockIdMaps(Int2ObjectMap<List<BlockEntry>> blockPropertiesMap, boolean skipFlattening) {
|
||||
final Reference2ObjectMap<Block, Int2IntMap> blockMatches = new Reference2ObjectOpenHashMap<>();
|
||||
final NbtConditionalIdMap<Block> tileEntityMap = new NbtConditionalIdMap<>();
|
||||
final ReferenceSet<Block> snowyBlocks = new ReferenceOpenHashSet<>();
|
||||
|
||||
blockPropertiesMap.forEach((intId, entries) -> {
|
||||
for (BlockEntry entry : entries) {
|
||||
addBlockMetas(entry, blockMatches, intId, skipFlattening, snowyBlocks);
|
||||
if (entry.hasNbtProperties()) {
|
||||
addTileEntityEntry(entry, tileEntityMap, intId);
|
||||
} else {
|
||||
addBlockMetas(entry, blockMatches, tileEntityMap, intId, skipFlattening, snowyBlocks);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
BlockRenderingSettings.INSTANCE.setHasSnowyEntries(!snowyBlocks.isEmpty());
|
||||
BlockRenderingSettings.INSTANCE.setSnowyBlocks(snowyBlocks);
|
||||
return blockMatches;
|
||||
return new BlockIdMaps(blockMatches, tileEntityMap);
|
||||
}
|
||||
|
||||
public static Map<Block, BlockRenderLayer> createBlockTypeMap(Map<NamespacedId, BlockRenderType> blockPropertiesMap) {
|
||||
@@ -55,7 +62,8 @@ public class BlockMaterialMapping {
|
||||
if (block == null && "minecraft".equals(id.getNamespace())) {
|
||||
final List<BlockEntry> legacyEntries = FlatteningMap.toLegacy(id.getName(), Map.of());
|
||||
if (legacyEntries != null) {
|
||||
block = resolveBlockOrNull(legacyEntries.getFirst().getId());
|
||||
// Use the first entry for render type (render type applies per-block)
|
||||
block = resolveBlockOrNull(legacyEntries.getFirst().id());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,28 +88,64 @@ public class BlockMaterialMapping {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an NBT-conditional ID map keyed by NamespacedId, for items or entities.
|
||||
*/
|
||||
public static NbtConditionalIdMap<NamespacedId> createNamespacedNbtMap(Int2ObjectMap<List<BlockEntry>> nbtEntries) {
|
||||
NbtConditionalIdMap<NamespacedId> map = new NbtConditionalIdMap<>();
|
||||
|
||||
nbtEntries.forEach((intId, entries) -> {
|
||||
for (BlockEntry entry : entries) {
|
||||
if (entry.hasNbtProperties()) {
|
||||
map.addCondition(entry.id(), entry.nbtProperties(), intId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void addTileEntityEntry(BlockEntry entry, NbtConditionalIdMap<Block> teMap, int intId) {
|
||||
final NamespacedId id = entry.id();
|
||||
final ResourceLocation resourceLocation = new ResourceLocation(id.getNamespace(), id.getName());
|
||||
|
||||
final Block block = (Block) Block.blockRegistry.getObject(resourceLocation.toString());
|
||||
|
||||
if (block == null || block == Blocks.air) {
|
||||
return;
|
||||
}
|
||||
|
||||
teMap.addCondition(block, entry.nbtProperties(), intId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds block+metadata combinations to the material ID map.
|
||||
* Based on Iris's addBlockStates method, adapted for 1.7.10 metadata system.
|
||||
*/
|
||||
private static void addBlockMetas(BlockEntry entry, Reference2ObjectMap<Block, Int2IntMap> idMap, int intId, boolean skipFlattening, ReferenceSet<Block> snowyBlocks) {
|
||||
final NamespacedId id = entry.getId();
|
||||
final Map<String, String> stateProps = entry.getStateProperties();
|
||||
private static void addBlockMetas(BlockEntry entry, Reference2ObjectMap<Block, Int2IntMap> idMap,
|
||||
NbtConditionalIdMap<Block> tileEntityMap, int intId,
|
||||
boolean skipFlattening, ReferenceSet<Block> snowyBlocks) {
|
||||
final NamespacedId id = entry.id();
|
||||
final Map<String, String> stateProps = entry.stateProperties();
|
||||
final String snowy = stateProps.get("snowy");
|
||||
final int snowyBit = "true".equals(snowy) ? SNOWY_META_BIT : 0;
|
||||
|
||||
// Vanilla modern names go through FlatteningMap; legacy-section packs and modded blocks
|
||||
// resolve directly from the registry.
|
||||
List<BlockEntry> targets = null;
|
||||
if (!skipFlattening && "minecraft".equals(id.getNamespace()) && (!stateProps.isEmpty() || entry.getMetas().isEmpty())) {
|
||||
if (!skipFlattening && "minecraft".equals(id.getNamespace()) && (!stateProps.isEmpty() || entry.metas().isEmpty())) {
|
||||
targets = FlatteningMap.toLegacy(id.getName(), stateProps);
|
||||
}
|
||||
if (targets == null) targets = List.of(entry);
|
||||
|
||||
for (BlockEntry target : targets) {
|
||||
final Block block = resolveBlockOrNull(target.getId());
|
||||
if (target.hasNbtProperties()) {
|
||||
addTileEntityEntry(target, tileEntityMap, intId);
|
||||
continue;
|
||||
}
|
||||
final Block block = resolveBlockOrNull(target.id());
|
||||
if (block == null) continue;
|
||||
applyMetas(block, target.getMetas(), idMap, intId, snowyBit);
|
||||
applyMetas(block, target.metas(), idMap, intId, snowyBit);
|
||||
if (snowy != null) snowyBlocks.add(block);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.coderbot.iris.block_rendering;
|
||||
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.BlockRenderLayer;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2LongLinkedOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntFunction;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.objects.ReferenceSet;
|
||||
@@ -18,16 +19,76 @@ import java.util.Map;
|
||||
public class BlockRenderingSettings {
|
||||
public static final BlockRenderingSettings INSTANCE = new BlockRenderingSettings();
|
||||
|
||||
public static final int CACHE_MISS = Integer.MIN_VALUE;
|
||||
|
||||
private static final int NBT_CACHE_INTERVAL_TICKS = 20;
|
||||
private static final int TE_NBT_CACHE_MAX = 8192;
|
||||
|
||||
private static final Long2LongLinkedOpenHashMap teNbtIdCache = new Long2LongLinkedOpenHashMap(256);
|
||||
private static final Object teNbtIdCacheLock = new Object();
|
||||
|
||||
static {
|
||||
teNbtIdCache.defaultReturnValue(-1L);
|
||||
}
|
||||
|
||||
public static long packBlockPos(int x, int y, int z) {
|
||||
return ((long) x & 0x3FFFFFFL) << 38 | ((long) y & 0xFFFL) << 26 | ((long) z & 0x3FFFFFFL);
|
||||
}
|
||||
|
||||
public static void invalidateTeNbtCache(int x, int y, int z) {
|
||||
final long key = packBlockPos(x, y, z);
|
||||
synchronized (teNbtIdCacheLock) {
|
||||
teNbtIdCache.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getCachedTeNbtId(long packedPos, long currentTick) {
|
||||
final long entry;
|
||||
synchronized (teNbtIdCacheLock) {
|
||||
entry = teNbtIdCache.get(packedPos);
|
||||
}
|
||||
if (entry == -1L) {
|
||||
return CACHE_MISS;
|
||||
}
|
||||
final long entryTick = entry >>> 32;
|
||||
if (currentTick - entryTick >= NBT_CACHE_INTERVAL_TICKS) {
|
||||
return CACHE_MISS;
|
||||
}
|
||||
return (int) entry;
|
||||
}
|
||||
|
||||
public static void cacheTeNbtId(long packedPos, int shaderId, long currentTick) {
|
||||
// Mask tick into low 31 bits so packed value never collides with the -1L sentinel.
|
||||
final long packed = ((currentTick & 0x7FFFFFFFL) << 32) | (shaderId & 0xFFFFFFFFL);
|
||||
synchronized (teNbtIdCacheLock) {
|
||||
teNbtIdCache.put(packedPos, packed);
|
||||
while (teNbtIdCache.size() > TE_NBT_CACHE_MAX) {
|
||||
teNbtIdCache.removeFirstLong();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearTeNbtCache() {
|
||||
synchronized (teNbtIdCacheLock) {
|
||||
teNbtIdCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
private boolean reloadRequired;
|
||||
private Reference2ObjectMap<Block, Int2IntMap> blockMetaMatches;
|
||||
private NbtConditionalIdMap<Block> blockNbtMap;
|
||||
private Map<Block, BlockRenderLayer> blockTypeIds;
|
||||
// note: no reload needed, entities are rebuilt every frame.
|
||||
@Setter
|
||||
private Object2IntFunction<NamespacedId> entityIds;
|
||||
@Setter
|
||||
private NbtConditionalIdMap<NamespacedId> entityNbtMap;
|
||||
// note: no reload needed, items are rendered every frame.
|
||||
@Setter
|
||||
private Object2IntFunction<NamespacedId> itemIds;
|
||||
@Setter
|
||||
private NbtConditionalIdMap<NamespacedId> itemNbtMap;
|
||||
@Getter
|
||||
private float ambientOcclusionLevel;
|
||||
private boolean disableDirectionalShading;
|
||||
@@ -41,6 +102,7 @@ public class BlockRenderingSettings {
|
||||
public BlockRenderingSettings() {
|
||||
reloadRequired = false;
|
||||
blockMetaMatches = null;
|
||||
blockNbtMap = null;
|
||||
blockTypeIds = null;
|
||||
ambientOcclusionLevel = 1.0F;
|
||||
disableDirectionalShading = false;
|
||||
@@ -82,11 +144,32 @@ public class BlockRenderingSettings {
|
||||
return itemIds;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NbtConditionalIdMap<Block> getBlockNbtMap() {
|
||||
return blockNbtMap;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NbtConditionalIdMap<NamespacedId> getItemNbtMap() {
|
||||
return itemNbtMap;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NbtConditionalIdMap<NamespacedId> getEntityNbtMap() {
|
||||
return entityNbtMap;
|
||||
}
|
||||
|
||||
public void setBlockMetaMatches(Reference2ObjectMap<Block, Int2IntMap> blockMetaIds) {
|
||||
this.reloadRequired = true;
|
||||
this.blockMetaMatches = blockMetaIds;
|
||||
}
|
||||
|
||||
public void setBlockNbtMap(NbtConditionalIdMap<Block> blockNbtMap) {
|
||||
this.reloadRequired = true;
|
||||
this.blockNbtMap = blockNbtMap;
|
||||
clearTeNbtCache();
|
||||
}
|
||||
|
||||
public boolean hasSnowyEntries() {
|
||||
return hasSnowyEntries;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
package net.coderbot.iris.block_rendering;
|
||||
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.shaderpack.materialmap.PropertiesTokenizer;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Maps objects with specific NBT properties to shader IDs. Generic over the
|
||||
* key type so it works for blocks, items, and entities.
|
||||
*
|
||||
* Example:
|
||||
* block.37=flower_pot[Item=minecraft:red_flower,Data=0]
|
||||
* item.10=diamond_sword[display.Name='Pointy Stick']
|
||||
*/
|
||||
public class NbtConditionalIdMap<K> {
|
||||
|
||||
/**
|
||||
* A single property matcher within a condition.
|
||||
*/
|
||||
record PropertyMatcher(String key, String[] path, String expectedValue,
|
||||
long expectedLong, boolean isExpectedLong,
|
||||
double expectedDouble, boolean isExpectedDouble) {
|
||||
boolean isPathMatch() { return path != null; }
|
||||
boolean isExistenceOnly() { return expectedValue == null; }
|
||||
}
|
||||
|
||||
record NbtCondition(PropertyMatcher[] matchers, int shaderId) {}
|
||||
|
||||
private final Map<K, List<NbtCondition>> conditionsByKey;
|
||||
|
||||
public NbtConditionalIdMap() {
|
||||
this.conditionsByKey = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an NBT condition. Unquoted values are resolved via item/block
|
||||
* registries at registration time. Single-quoted values are kept as literals.
|
||||
* Dot notation paths are pre-split here so we prevent allocations.
|
||||
*/
|
||||
public void addCondition(K key, Map<String, PropertiesTokenizer.NbtValue> nbtProperties, int shaderId) {
|
||||
final PropertyMatcher[] matchers = new PropertyMatcher[nbtProperties.size()];
|
||||
int i = 0;
|
||||
|
||||
for (Map.Entry<String, PropertiesTokenizer.NbtValue> entry : nbtProperties.entrySet()) {
|
||||
final String nbtKey = entry.getKey();
|
||||
final PropertiesTokenizer.NbtValue nbtValue = entry.getValue();
|
||||
|
||||
final String resolvedValue;
|
||||
if (nbtValue == null) {
|
||||
resolvedValue = null;
|
||||
|
||||
} else if (nbtValue.literal()) {
|
||||
resolvedValue = nbtValue.value();
|
||||
|
||||
} else {
|
||||
resolvedValue = resolveValue(nbtValue.value());
|
||||
}
|
||||
|
||||
final String[] path = nbtKey.contains(".") ? nbtKey.split("\\.") : null;
|
||||
|
||||
long expectedLong = 0;
|
||||
boolean isExpectedLong = false;
|
||||
double expectedDouble = 0;
|
||||
boolean isExpectedDouble = false;
|
||||
if (resolvedValue != null) {
|
||||
try {
|
||||
expectedLong = Long.parseLong(resolvedValue);
|
||||
isExpectedLong = true;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
try {
|
||||
expectedDouble = Double.parseDouble(resolvedValue);
|
||||
isExpectedDouble = true;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
|
||||
matchers[i++] = new PropertyMatcher(nbtKey, path, resolvedValue,
|
||||
expectedLong, isExpectedLong, expectedDouble, isExpectedDouble);
|
||||
}
|
||||
|
||||
conditionsByKey.computeIfAbsent(key, k -> new ArrayList<>())
|
||||
.add(new NbtCondition(matchers, shaderId));
|
||||
}
|
||||
|
||||
public boolean hasConditions(K key) {
|
||||
return conditionsByKey.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return conditionsByKey.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the shader ID by matching the provided NBT data.
|
||||
*/
|
||||
public int resolve(K key, NBTTagCompound nbt) {
|
||||
final List<NbtCondition> conditions = conditionsByKey.get(key);
|
||||
if (conditions == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (NbtCondition condition : conditions) {
|
||||
if (matchesNbt(nbt, condition.matchers())) {
|
||||
return condition.shaderId;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static boolean matchesNbt(NBTTagCompound nbt, PropertyMatcher[] matchers) {
|
||||
for (PropertyMatcher matcher : matchers) {
|
||||
if (matcher.isPathMatch()) {
|
||||
if (!matchesPath(nbt, matcher.path(), 0, matcher)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!nbt.hasKey(matcher.key())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!matcher.isExistenceOnly()) {
|
||||
if (!matchesNbtValue(nbt.getTag(matcher.key()), matcher)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search through dot-separated path in NBT data.
|
||||
*/
|
||||
private static boolean matchesPath(NBTBase tag, String[] path, int depth, PropertyMatcher matcher) {
|
||||
if (depth == path.length) {
|
||||
if (matcher.isExistenceOnly()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return matchesNbtValue(tag, matcher);
|
||||
}
|
||||
|
||||
final String segment = path[depth];
|
||||
|
||||
if (tag instanceof NBTTagCompound compound) {
|
||||
if (!compound.hasKey(segment)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return matchesPath(compound.getTag(segment), path, depth + 1, matcher);
|
||||
}
|
||||
|
||||
if (tag instanceof NBTTagList list) {
|
||||
for (int i = 0; i < list.tagCount(); i++) {
|
||||
if (matchesPath(list.getCompoundTagAt(i), path, depth, matcher)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares an NBT tag's value against an expected string using type-specific
|
||||
* accessors to avoid toString() allocations where possible.
|
||||
* Makes it so you don't need to specify the type either in the .properties files.
|
||||
*/
|
||||
private static boolean matchesNbtValue(NBTBase tag, PropertyMatcher matcher) {
|
||||
return switch (tag.getId()) {
|
||||
case 1 -> // TAG_Byte
|
||||
matcher.isExpectedLong() && ((net.minecraft.nbt.NBTTagByte) tag).func_150290_f() == matcher.expectedLong();
|
||||
case 2 -> // TAG_Short
|
||||
matcher.isExpectedLong() && ((net.minecraft.nbt.NBTTagShort) tag).func_150289_e() == matcher.expectedLong();
|
||||
case 3 -> // TAG_Int
|
||||
matcher.isExpectedLong() && ((net.minecraft.nbt.NBTTagInt) tag).func_150287_d() == matcher.expectedLong();
|
||||
case 4 -> // TAG_Long
|
||||
matcher.isExpectedLong() && ((net.minecraft.nbt.NBTTagLong) tag).func_150291_c() == matcher.expectedLong();
|
||||
case 5 -> // TAG_Float
|
||||
matcher.isExpectedDouble() && ((net.minecraft.nbt.NBTTagFloat) tag).func_150288_h() == (float) matcher.expectedDouble();
|
||||
case 6 -> // TAG_Double
|
||||
matcher.isExpectedDouble() && ((net.minecraft.nbt.NBTTagDouble) tag).func_150286_g() == matcher.expectedDouble();
|
||||
case 8 -> // TAG_String
|
||||
matcher.expectedValue().equals(((net.minecraft.nbt.NBTTagString) tag).func_150285_a_());
|
||||
default -> // Compounds, lists, arrays — fall back to toString
|
||||
matcher.expectedValue().equals(PropertiesTokenizer.stripQuotes(tag.toString()));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to resolve a value as a registry name (item or block) to its numeric ID.
|
||||
* If the value is already numeric or can't be resolved, return it unchanged.
|
||||
*/
|
||||
private static String resolveValue(String value) {
|
||||
try {
|
||||
Integer.parseInt(value);
|
||||
return value;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
if (!value.contains(":")) {
|
||||
Iris.logger.warn("NBT value '{}' is missing a namespace, please use the full form e.g. 'minecraft:{}'", value, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
final ResourceLocation loc = new ResourceLocation(value);
|
||||
|
||||
final Item item = (Item) Item.itemRegistry.getObject(loc.toString());
|
||||
if (item != null) {
|
||||
return String.valueOf(Item.getIdFromItem(item));
|
||||
}
|
||||
|
||||
final Block block = (Block) Block.blockRegistry.getObject(loc.toString());
|
||||
if (block != null) {
|
||||
return String.valueOf(Block.getIdFromBlock(block));
|
||||
}
|
||||
|
||||
Iris.logger.warn("Could not resolve '{}' to an item or block ID", value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user