diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index f643650dd..290722f23 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -48,6 +48,7 @@ import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegisterEvent; @@ -104,6 +105,7 @@ public class Flywheel { modEventBus.addListener(Flywheel::registerClientReloadListeners); modEventBus.addListener(Flywheel::onClientSetup); + modEventBus.addListener(Flywheel::onLoadComplete); modEventBus.addListener(BackendManagerImpl::onEndClientResourceReload); @@ -133,7 +135,9 @@ public class Flywheel { ShaderIndices.init(); VanillaVisuals.init(); + } + private static void onLoadComplete(FMLLoadCompleteEvent event) { RegistryImpl.freezeAll(); IdRegistryImpl.freezeAll(); } diff --git a/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java b/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java index d9ee732dd..75bd4bd8f 100644 --- a/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.api.registry; import java.util.Collection; import java.util.Set; +import java.util.function.Consumer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; @@ -31,7 +32,7 @@ public interface IdRegistry extends Iterable { @Unmodifiable Collection getAll(); - void addFreezeCallback(Runnable callback); + void addFreezeCallback(Consumer> callback); boolean isFrozen(); } diff --git a/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java b/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java index 8683b684c..fdfc3dc61 100644 --- a/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java +++ b/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.api.registry; import java.util.Set; +import java.util.function.Consumer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Unmodifiable; @@ -14,7 +15,7 @@ public interface Registry extends Iterable { @Unmodifiable Set getAll(); - void addFreezeCallback(Runnable callback); + void addFreezeCallback(Consumer> callback); boolean isFrozen(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderIndices.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderIndices.java index 69fd6c7c8..f486979eb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderIndices.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderIndices.java @@ -2,11 +2,13 @@ package com.jozufozu.flywheel.backend; import java.util.List; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; import com.jozufozu.flywheel.api.material.CutoutShader; import com.jozufozu.flywheel.api.material.FogShader; import com.jozufozu.flywheel.api.material.MaterialShaders; +import com.jozufozu.flywheel.api.registry.Registry; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntMaps; @@ -17,9 +19,13 @@ import it.unimi.dsi.fastutil.objects.ObjectLists; import net.minecraft.resources.ResourceLocation; public final class ShaderIndices { + @Nullable private static Index vertexShaders; + @Nullable private static Index fragmentShaders; + @Nullable private static Index fogShaders; + @Nullable private static Index cutoutShaders; private ShaderIndices() { @@ -69,14 +75,14 @@ public final class ShaderIndices { return cutout().index(cutoutShader.source()); } - private static void initMaterialShaders() { - int amount = MaterialShaders.REGISTRY.getAll() + private static void initMaterialShaders(Registry registry) { + int amount = registry.getAll() .size(); var vertexShaders = new IndexBuilder(amount); var fragmentShaders = new IndexBuilder(amount); - for (MaterialShaders shaders : MaterialShaders.REGISTRY) { + for (MaterialShaders shaders : registry) { vertexShaders.add(shaders.vertexShader()); fragmentShaders.add(shaders.fragmentShader()); } @@ -85,26 +91,26 @@ public final class ShaderIndices { ShaderIndices.fragmentShaders = fragmentShaders.build(); } - private static void initFogShaders() { - int amount = FogShader.REGISTRY.getAll() + private static void initFogShaders(Registry registry) { + int amount = registry.getAll() .size(); var fog = new IndexBuilder(amount); - for (FogShader shaders : FogShader.REGISTRY) { + for (FogShader shaders : registry) { fog.add(shaders.source()); } ShaderIndices.fogShaders = fog.build(); } - private static void initCutoutShaders() { - int amount = CutoutShader.REGISTRY.getAll() + private static void initCutoutShaders(Registry registry) { + int amount = registry.getAll() .size(); var cutout = new IndexBuilder(amount); - for (CutoutShader shaders : CutoutShader.REGISTRY) { + for (CutoutShader shaders : registry) { cutout.add(shaders.source()); } diff --git a/src/main/java/com/jozufozu/flywheel/impl/registry/IdRegistryImpl.java b/src/main/java/com/jozufozu/flywheel/impl/registry/IdRegistryImpl.java index 58cca6c5d..d247c15ca 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/registry/IdRegistryImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/registry/IdRegistryImpl.java @@ -3,6 +3,7 @@ package com.jozufozu.flywheel.impl.registry; import java.util.Collection; import java.util.Iterator; import java.util.Set; +import java.util.function.Consumer; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; @@ -10,12 +11,15 @@ import org.jetbrains.annotations.Unmodifiable; import com.jozufozu.flywheel.api.registry.IdRegistry; import it.unimi.dsi.fastutil.objects.Object2ReferenceMap; +import it.unimi.dsi.fastutil.objects.Object2ReferenceMaps; import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectList; +import it.unimi.dsi.fastutil.objects.ObjectLists; import it.unimi.dsi.fastutil.objects.ObjectSet; import it.unimi.dsi.fastutil.objects.ObjectSets; import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; +import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ReferenceCollection; import it.unimi.dsi.fastutil.objects.ReferenceCollections; @@ -24,11 +28,11 @@ import net.minecraft.resources.ResourceLocation; public class IdRegistryImpl implements IdRegistry { private static final ObjectList> ALL = new ObjectArrayList<>(); - private final Object2ReferenceMap map = new Object2ReferenceOpenHashMap<>(); - private final Reference2ObjectMap reverseMap = new Reference2ObjectOpenHashMap<>(); + private final Object2ReferenceMap map = Object2ReferenceMaps.synchronize(new Object2ReferenceOpenHashMap<>()); + private final Reference2ObjectMap reverseMap = Reference2ObjectMaps.synchronize(new Reference2ObjectOpenHashMap<>()); private final ObjectSet keysView = ObjectSets.unmodifiable(map.keySet()); private final ReferenceCollection valuesView = ReferenceCollections.unmodifiable(map.values()); - private final ObjectList freezeCallbacks = new ObjectArrayList<>(); + private final ObjectList>> freezeCallbacks = ObjectLists.synchronize(new ObjectArrayList<>()); private boolean frozen; public IdRegistryImpl() { @@ -99,7 +103,7 @@ public class IdRegistryImpl implements IdRegistry { } @Override - public void addFreezeCallback(Runnable callback) { + public void addFreezeCallback(Consumer> callback) { if (frozen) { throw new IllegalStateException("Cannot add freeze callback to frozen registry!"); } @@ -118,8 +122,8 @@ public class IdRegistryImpl implements IdRegistry { public void freeze() { frozen = true; - for (Runnable runnable : freezeCallbacks) { - runnable.run(); + for (var callback : freezeCallbacks) { + callback.accept(this); } freezeCallbacks.clear(); } diff --git a/src/main/java/com/jozufozu/flywheel/impl/registry/RegistryImpl.java b/src/main/java/com/jozufozu/flywheel/impl/registry/RegistryImpl.java index 2c50b0bbc..75f8feaa3 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/registry/RegistryImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/registry/RegistryImpl.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.impl.registry; import java.util.Iterator; import java.util.Set; +import java.util.function.Consumer; import org.jetbrains.annotations.Unmodifiable; @@ -9,6 +10,7 @@ import com.jozufozu.flywheel.api.registry.Registry; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectList; +import it.unimi.dsi.fastutil.objects.ObjectLists; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectSet; import it.unimi.dsi.fastutil.objects.ObjectSets; @@ -16,9 +18,9 @@ import it.unimi.dsi.fastutil.objects.ObjectSets; public class RegistryImpl implements Registry { private static final ObjectList> ALL = new ObjectArrayList<>(); - private final ObjectSet set = new ObjectOpenHashSet<>(); + private final ObjectSet set = ObjectSets.synchronize(new ObjectOpenHashSet<>()); private final ObjectSet setView = ObjectSets.unmodifiable(set); - private final ObjectList freezeCallbacks = new ObjectArrayList<>(); + private final ObjectList>> freezeCallbacks = ObjectLists.synchronize(new ObjectArrayList<>()); private boolean frozen; public RegistryImpl() { @@ -49,7 +51,7 @@ public class RegistryImpl implements Registry { } @Override - public void addFreezeCallback(Runnable callback) { + public void addFreezeCallback(Consumer> callback) { if (frozen) { throw new IllegalStateException("Cannot add freeze callback to frozen registry!"); } @@ -68,8 +70,8 @@ public class RegistryImpl implements Registry { public void freeze() { frozen = true; - for (Runnable runnable : freezeCallbacks) { - runnable.run(); + for (var callback : freezeCallbacks) { + callback.accept(this); } freezeCallbacks.clear(); }