diff --git a/forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java b/forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java index a206bcfa..ca38593b 100644 --- a/forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java +++ b/forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java @@ -2,6 +2,7 @@ package org.embeddedt.modernfix.forge.dynresources; import com.google.common.collect.ForwardingMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterators; import com.google.common.collect.Sets; import com.google.common.graph.GraphBuilder; import com.google.common.graph.MutableGraph; @@ -19,9 +20,11 @@ import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.util.ForwardingInclDefaultsMap; import org.jetbrains.annotations.Nullable; +import java.util.AbstractSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -158,6 +161,11 @@ public class ModelBakeEventHelper { return ourModelLocations.contains(key) || super.containsKey(key); } + @Override + public Set> entrySet() { + return new DynamicModelEntrySet(this, ourModelLocations); + } + @Override public void replaceAll(BiFunction function) { ModernFix.LOGGER.warn("Mod '{}' is calling replaceAll on the model registry. Some hacks will be used to keep this fast, but they may not be 100% compatible.", modId); @@ -185,4 +193,61 @@ public class ModelBakeEventHelper { } }; } + + private static class DynamicModelEntrySet extends AbstractSet> { + private final Map modelRegistry; + private final Set modelLocations; + + private DynamicModelEntrySet(Map modelRegistry, Set modelLocations) { + this.modelRegistry = modelRegistry; + this.modelLocations = modelLocations; + } + + @Override + public Iterator> iterator() { + return Iterators.transform(Iterators.unmodifiableIterator(this.modelLocations.iterator()), DynamicModelEntry::new); + } + + @Override + public boolean contains(Object o) { + if(o instanceof Map.Entry entry) { + return modelRegistry.containsKey(entry.getKey()); + } else { + return false; + } + } + + @Override + public int size() { + return modelRegistry.size(); + } + + @Override + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException(); + } + + private class DynamicModelEntry implements Map.Entry { + private final ResourceLocation location; + + private DynamicModelEntry(ResourceLocation location) { + this.location = location; + } + + @Override + public ResourceLocation getKey() { + return this.location; + } + + @Override + public BakedModel getValue() { + return modelRegistry.get(this.location); + } + + @Override + public BakedModel setValue(BakedModel value) { + return modelRegistry.put(this.location, value); + } + } + } }