Skip to content

Commit 7348737

Browse files
committed
Emulate entrySet on dynamic resources model registry
Related: #429
1 parent fd42c5b commit 7348737

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.collect.ForwardingMap;
44
import com.google.common.collect.ImmutableSet;
5+
import com.google.common.collect.Iterators;
56
import com.google.common.collect.Sets;
67
import com.google.common.graph.GraphBuilder;
78
import com.google.common.graph.MutableGraph;
@@ -19,9 +20,11 @@
1920
import org.embeddedt.modernfix.util.ForwardingInclDefaultsMap;
2021
import org.jetbrains.annotations.Nullable;
2122

23+
import java.util.AbstractSet;
2224
import java.util.ArrayList;
2325
import java.util.Collection;
2426
import java.util.HashSet;
27+
import java.util.Iterator;
2528
import java.util.List;
2629
import java.util.Map;
2730
import java.util.Objects;
@@ -158,6 +161,11 @@ public boolean containsKey(@Nullable Object key) {
158161
return ourModelLocations.contains(key) || super.containsKey(key);
159162
}
160163

164+
@Override
165+
public Set<Entry<ResourceLocation, BakedModel>> entrySet() {
166+
return new DynamicModelEntrySet(this, ourModelLocations);
167+
}
168+
161169
@Override
162170
public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> function) {
163171
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 void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel,
185193
}
186194
};
187195
}
196+
197+
private static class DynamicModelEntrySet extends AbstractSet<Map.Entry<ResourceLocation, BakedModel>> {
198+
private final Map<ResourceLocation, BakedModel> modelRegistry;
199+
private final Set<ResourceLocation> modelLocations;
200+
201+
private DynamicModelEntrySet(Map<ResourceLocation, BakedModel> modelRegistry, Set<ResourceLocation> modelLocations) {
202+
this.modelRegistry = modelRegistry;
203+
this.modelLocations = modelLocations;
204+
}
205+
206+
@Override
207+
public Iterator<Map.Entry<ResourceLocation, BakedModel>> iterator() {
208+
return Iterators.transform(Iterators.unmodifiableIterator(this.modelLocations.iterator()), DynamicModelEntry::new);
209+
}
210+
211+
@Override
212+
public boolean contains(Object o) {
213+
if(o instanceof Map.Entry entry) {
214+
return modelRegistry.containsKey(entry.getKey());
215+
} else {
216+
return false;
217+
}
218+
}
219+
220+
@Override
221+
public int size() {
222+
return modelRegistry.size();
223+
}
224+
225+
@Override
226+
public boolean removeAll(Collection<?> c) {
227+
throw new UnsupportedOperationException();
228+
}
229+
230+
private class DynamicModelEntry implements Map.Entry<ResourceLocation, BakedModel> {
231+
private final ResourceLocation location;
232+
233+
private DynamicModelEntry(ResourceLocation location) {
234+
this.location = location;
235+
}
236+
237+
@Override
238+
public ResourceLocation getKey() {
239+
return this.location;
240+
}
241+
242+
@Override
243+
public BakedModel getValue() {
244+
return modelRegistry.get(this.location);
245+
}
246+
247+
@Override
248+
public BakedModel setValue(BakedModel value) {
249+
return modelRegistry.put(this.location, value);
250+
}
251+
}
252+
}
188253
}

0 commit comments

Comments
 (0)