Emulate entrySet on dynamic resources model registry

Related: #429
This commit is contained in:
embeddedt 2024-07-07 11:26:06 -04:00
parent fd42c5b129
commit 7348737878
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -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<Entry<ResourceLocation, BakedModel>> entrySet() {
return new DynamicModelEntrySet(this, ourModelLocations);
}
@Override
public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> 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<Map.Entry<ResourceLocation, BakedModel>> {
private final Map<ResourceLocation, BakedModel> modelRegistry;
private final Set<ResourceLocation> modelLocations;
private DynamicModelEntrySet(Map<ResourceLocation, BakedModel> modelRegistry, Set<ResourceLocation> modelLocations) {
this.modelRegistry = modelRegistry;
this.modelLocations = modelLocations;
}
@Override
public Iterator<Map.Entry<ResourceLocation, BakedModel>> 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<ResourceLocation, BakedModel> {
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);
}
}
}
}