From 6edf7ec68e49e631ce987a787aac4c30c27a7244 Mon Sep 17 00:00:00 2001 From: TropheusJ Date: Sat, 15 Feb 2025 19:54:44 -0500 Subject: [PATCH] invalidate everything --- .../create/api/registry/SimpleRegistry.java | 11 ++++---- .../MountedStorageTypeRegistryImpl.java | 2 +- .../impl/registry/SimpleRegistryImpl.java | 27 +++---------------- .../create/impl/registry/TagProviderImpl.java | 2 +- 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/simibubi/create/api/registry/SimpleRegistry.java b/src/main/java/com/simibubi/create/api/registry/SimpleRegistry.java index 83090419b4..09db5d84b1 100644 --- a/src/main/java/com/simibubi/create/api/registry/SimpleRegistry.java +++ b/src/main/java/com/simibubi/create/api/registry/SimpleRegistry.java @@ -31,10 +31,11 @@ public interface SimpleRegistry { void registerProvider(Provider provider); /** - * Invalidate the cached values provided by the given provider, so they get re-computed on the next query. - * @throws IllegalArgumentException if the provider is not registered to this registry + * Invalidate the cached values provided by all providers, so they get re-computed on the next query. + * This should be called by providers when something changes that would affect their results, such as + * a resource reload in the case of providers based on tags. */ - void invalidateProvider(Provider provider); + void invalidate(); /** * Query the value associated with the given object. May be null if no association is present. @@ -48,10 +49,10 @@ public interface SimpleRegistry { /** * A provider can provide values to the registry in a lazy fashion. When a key does not have an - * associated value, all providers will be queried in reverse-registration order. + * associated value, all providers will be queried in reverse-registration order (newest first). *

* The values returned by providers are cached so that repeated queries always return the same value. - * To invalidate the cache of a provider, call {@link SimpleRegistry#invalidateProvider(Provider)}. + * To invalidate the cache of a registry, call {@link SimpleRegistry#invalidate()}. */ @FunctionalInterface interface Provider { diff --git a/src/main/java/com/simibubi/create/impl/contraption/storage/MountedStorageTypeRegistryImpl.java b/src/main/java/com/simibubi/create/impl/contraption/storage/MountedStorageTypeRegistryImpl.java index 63321fd0e8..f47dbdb6bb 100644 --- a/src/main/java/com/simibubi/create/impl/contraption/storage/MountedStorageTypeRegistryImpl.java +++ b/src/main/java/com/simibubi/create/impl/contraption/storage/MountedStorageTypeRegistryImpl.java @@ -73,7 +73,7 @@ public class MountedStorageTypeRegistryImpl { public void onRegister(SimpleRegistry> registry) { MinecraftForge.EVENT_BUS.addListener((TagsUpdatedEvent event) -> { if (event.shouldUpdateStaticData()) { - registry.invalidateProvider(this); + registry.invalidate(); } }); } diff --git a/src/main/java/com/simibubi/create/impl/registry/SimpleRegistryImpl.java b/src/main/java/com/simibubi/create/impl/registry/SimpleRegistryImpl.java index 7b3dad1a80..660857a1f8 100644 --- a/src/main/java/com/simibubi/create/impl/registry/SimpleRegistryImpl.java +++ b/src/main/java/com/simibubi/create/impl/registry/SimpleRegistryImpl.java @@ -1,12 +1,10 @@ package com.simibubi.create.impl.registry; import java.util.ArrayList; -import java.util.Collections; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; import org.jetbrains.annotations.Nullable; @@ -18,7 +16,6 @@ public class SimpleRegistryImpl implements SimpleRegistry { private final Map registrations = new IdentityHashMap<>(); private final List> providers = new ArrayList<>(); - private final Map, Set> providedKeys = new IdentityHashMap<>(); private final Map providedValues = new IdentityHashMap<>(); @Override @@ -48,20 +45,8 @@ public class SimpleRegistryImpl implements SimpleRegistry { } @Override - public synchronized void invalidateProvider(Provider provider) { - Objects.requireNonNull(provider); - if (!this.providers.contains(provider)) { - throw new IllegalArgumentException("Cannot invalidate non-registered provider: " + provider); - } - - // discard all the values the provider has provided - Set keys = providedKeys.remove(provider); - if (keys != null) { - keys.forEach(providedValues::remove); - } - - // when a provider is invalidated, we need to clear all cached values that evaluated to null, so they can be re-queried - this.providedValues.values().removeIf(value -> value == nullMarker); + public synchronized void invalidate() { + this.providedValues.clear(); } @Override @@ -75,13 +60,11 @@ public class SimpleRegistryImpl implements SimpleRegistry { } // no value known, check providers - // descendingSet: go in reverse-registration order + // new providers are added to the start, so normal iteration is reverse-registration order for (Provider provider : this.providers) { V value = provider.get(object); if (value != null) { this.providedValues.put(object, value); - // track which provider provided the value for invalidation - this.providedKeys.computeIfAbsent(provider, $ -> identityHashSet()).add(object); return value; } } @@ -95,8 +78,4 @@ public class SimpleRegistryImpl implements SimpleRegistry { private static T nullMarker() { return (T) nullMarker; } - - private static Set identityHashSet() { - return Collections.newSetFromMap(new IdentityHashMap<>()); - } } diff --git a/src/main/java/com/simibubi/create/impl/registry/TagProviderImpl.java b/src/main/java/com/simibubi/create/impl/registry/TagProviderImpl.java index c802552254..d0aa81eb48 100644 --- a/src/main/java/com/simibubi/create/impl/registry/TagProviderImpl.java +++ b/src/main/java/com/simibubi/create/impl/registry/TagProviderImpl.java @@ -34,7 +34,7 @@ public class TagProviderImpl implements SimpleRegistry.Provider { public void onRegister(SimpleRegistry registry) { MinecraftForge.EVENT_BUS.addListener((TagsUpdatedEvent event) -> { if (event.shouldUpdateStaticData()) { - registry.invalidateProvider(this); + registry.invalidate(); } }); }