|
2 | 2 |
|
3 | 3 | import com.google.common.collect.ForwardingMap; |
4 | 4 | import com.google.common.collect.ImmutableSet; |
| 5 | +import com.google.common.collect.Iterators; |
5 | 6 | import com.google.common.collect.Sets; |
6 | 7 | import com.google.common.graph.GraphBuilder; |
7 | 8 | import com.google.common.graph.MutableGraph; |
|
19 | 20 | import org.embeddedt.modernfix.util.ForwardingInclDefaultsMap; |
20 | 21 | import org.jetbrains.annotations.Nullable; |
21 | 22 |
|
| 23 | +import java.util.AbstractSet; |
22 | 24 | import java.util.ArrayList; |
23 | 25 | import java.util.Collection; |
24 | 26 | import java.util.HashSet; |
| 27 | +import java.util.Iterator; |
25 | 28 | import java.util.List; |
26 | 29 | import java.util.Map; |
27 | 30 | import java.util.Objects; |
@@ -158,6 +161,11 @@ public boolean containsKey(@Nullable Object key) { |
158 | 161 | return ourModelLocations.contains(key) || super.containsKey(key); |
159 | 162 | } |
160 | 163 |
|
| 164 | + @Override |
| 165 | + public Set<Entry<ResourceLocation, BakedModel>> entrySet() { |
| 166 | + return new DynamicModelEntrySet(this, ourModelLocations); |
| 167 | + } |
| 168 | + |
161 | 169 | @Override |
162 | 170 | public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> function) { |
163 | 171 | 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, |
185 | 193 | } |
186 | 194 | }; |
187 | 195 | } |
| 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 | + } |
188 | 253 | } |
0 commit comments