|
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; |
@@ -151,6 +152,11 @@ public boolean containsKey(@Nullable Object key) { |
151 | 152 | return ourModelLocations.contains(key) || super.containsKey(key); |
152 | 153 | } |
153 | 154 |
|
| 155 | + @Override |
| 156 | + public Set<Entry<ModelResourceLocation, BakedModel>> entrySet() { |
| 157 | + return new DynamicModelEntrySet(this, ourModelLocations); |
| 158 | + } |
| 159 | + |
154 | 160 | @Override |
155 | 161 | public void replaceAll(BiFunction<? super ModelResourceLocation, ? super BakedModel, ? extends BakedModel> function) { |
156 | 162 | 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); |
@@ -178,4 +184,61 @@ public void replaceAll(BiFunction<? super ModelResourceLocation, ? super BakedMo |
178 | 184 | } |
179 | 185 | }; |
180 | 186 | } |
| 187 | + |
| 188 | + private static class DynamicModelEntrySet extends AbstractSet<Map.Entry<ModelResourceLocation, BakedModel>> { |
| 189 | + private final Map<ModelResourceLocation, BakedModel> modelRegistry; |
| 190 | + private final Set<ModelResourceLocation> modelLocations; |
| 191 | + |
| 192 | + private DynamicModelEntrySet(Map<ModelResourceLocation, BakedModel> modelRegistry, Set<ModelResourceLocation> modelLocations) { |
| 193 | + this.modelRegistry = modelRegistry; |
| 194 | + this.modelLocations = modelLocations; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public Iterator<Map.Entry<ModelResourceLocation, BakedModel>> iterator() { |
| 199 | + return Iterators.transform(Iterators.unmodifiableIterator(this.modelLocations.iterator()), DynamicModelEntry::new); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public boolean contains(Object o) { |
| 204 | + if(o instanceof Map.Entry entry) { |
| 205 | + return modelRegistry.containsKey(entry.getKey()); |
| 206 | + } else { |
| 207 | + return false; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public int size() { |
| 213 | + return modelRegistry.size(); |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public boolean removeAll(Collection<?> c) { |
| 218 | + throw new UnsupportedOperationException(); |
| 219 | + } |
| 220 | + |
| 221 | + private class DynamicModelEntry implements Map.Entry<ModelResourceLocation, BakedModel> { |
| 222 | + private final ModelResourceLocation location; |
| 223 | + |
| 224 | + private DynamicModelEntry(ModelResourceLocation location) { |
| 225 | + this.location = location; |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public ModelResourceLocation getKey() { |
| 230 | + return this.location; |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public BakedModel getValue() { |
| 235 | + return modelRegistry.get(this.location); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public BakedModel setValue(BakedModel value) { |
| 240 | + return modelRegistry.put(this.location, value); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
181 | 244 | } |
0 commit comments