diff --git a/build.gradle b/build.gradle index 5377afe..e6eb383 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ neoForge { } // This line is optional. Access Transformers are automatically detected - // accessTransformers = project.files('src/main/resources/META-INF/accesstransformer.cfg') + accessTransformers.from 'src/main/resources/META-INF/accesstransformer.cfg' // Default run configurations. // These can be tweaked, removed, or duplicated as needed. diff --git a/src/main/java/gay/sylv/legacy_landscape/client/HackedRenderSystem.java b/src/main/java/gay/sylv/legacy_landscape/client/HackedRenderSystem.java new file mode 100644 index 0000000..1586dc9 --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/client/HackedRenderSystem.java @@ -0,0 +1,29 @@ +package gay.sylv.legacy_landscape.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import gay.sylv.legacy_landscape.mixin.client.Accessor_RenderSystem; +import net.minecraft.client.renderer.texture.AbstractTexture; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.resources.ResourceLocation; + +public final class HackedRenderSystem { + private HackedRenderSystem() {} + + public static void setShaderTexture(int shaderTexture, ResourceLocation textureId, boolean blur, boolean mipmap) { + if (!RenderSystem.isOnRenderThread()) { + RenderSystem.recordRenderCall(() -> _setShaderTexture(shaderTexture, textureId, blur, mipmap)); + } else { + _setShaderTexture(shaderTexture, textureId, blur, mipmap); + } + } + + private static void _setShaderTexture(int shaderTexture, ResourceLocation textureId, boolean blur, boolean mipmap) { + int[] shaderTextures = Accessor_RenderSystem.getShaderTextures(); + if (shaderTexture >= 0 && shaderTexture < shaderTextures.length) { + TextureManager textureManager = LegacyResourceFaker.getTextureManager(); + AbstractTexture abstractTexture = textureManager.getTexture(textureId); + abstractTexture.setFilter(blur, mipmap); + shaderTextures[shaderTexture] = abstractTexture.getId(); + } + } +} diff --git a/src/main/java/gay/sylv/legacy_landscape/client/ClientLegacyLandscape.java b/src/main/java/gay/sylv/legacy_landscape/client/LegacyResourceFaker.java similarity index 65% rename from src/main/java/gay/sylv/legacy_landscape/client/ClientLegacyLandscape.java rename to src/main/java/gay/sylv/legacy_landscape/client/LegacyResourceFaker.java index 9bd060e..6d5dafd 100644 --- a/src/main/java/gay/sylv/legacy_landscape/client/ClientLegacyLandscape.java +++ b/src/main/java/gay/sylv/legacy_landscape/client/LegacyResourceFaker.java @@ -34,9 +34,10 @@ modid = LegacyLandscape.MOD_ID, bus = EventBusSubscriber.Bus.MOD ) -public final class ClientLegacyLandscape { +public final class LegacyResourceFaker { private static final Logger LOGGER = LogUtils.getLogger(); + private static boolean initialized; private static PackResources programmerArt; private static ReloadableResourceManager resourceManager; private static TextureManager textureManager; @@ -49,25 +50,35 @@ public final class ClientLegacyLandscape { private static void registerReloadListeners(RegisterClientReloadListenersEvent event) { event.registerReloadListener((ResourceManagerReloadListener) resourceManager1 -> { Minecraft client = Minecraft.getInstance(); - try (PackResources programmerArtResources = Objects.requireNonNull(client.getResourcePackRepository().getPack("programmer_art")).open()) { - programmerArt = programmerArtResources; - resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); - textureManager = new TextureManager(resourceManager); - resourceManager.registerReloadListener(textureManager); - splashManager = new SplashManager(Minecraft.getInstance().getUser()); - resourceManager.registerReloadListener(splashManager); - resourceManager.registerReloadListener(new GrassColorReloadListener()); - resourceManager.registerReloadListener(new FoliageColorReloadListener()); - blockColors = BlockColors.createDefault(); - modelManager = new ModelManager(textureManager, blockColors, 0); - resourceManager.registerReloadListener(modelManager); - entityModels = new EntityModelSet(); - resourceManager.registerReloadListener(entityModels); + if (!initialized) { + try (PackResources programmerArtResources = Objects.requireNonNull(client.getResourcePackRepository().getPack("programmer_art")).open()) { + programmerArt = programmerArtResources; + resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES); + textureManager = new TextureManager(resourceManager); + resourceManager.registerReloadListener(textureManager); + splashManager = new SplashManager(Minecraft.getInstance().getUser()); + resourceManager.registerReloadListener(splashManager); + resourceManager.registerReloadListener(new GrassColorReloadListener()); + resourceManager.registerReloadListener(new FoliageColorReloadListener()); + blockColors = BlockColors.createDefault(); + modelManager = new ModelManager(textureManager, blockColors, client.options.mipmapLevels().get()); + resourceManager.registerReloadListener(modelManager); + entityModels = new EntityModelSet(); + resourceManager.registerReloadListener(entityModels); + } + + initialized = true; } + modelManager.updateMaxMipLevel(client.options.mipmapLevels().get()); + List fullResourcesList = client.getResourceManager().listPacks().collect(Collectors.toCollection(ArrayList::new)); fullResourcesList.add(programmerArt); resourceManager.createReload(Util.backgroundExecutor(), client, CompletableFuture.completedFuture(Unit.INSTANCE), fullResourcesList).done().thenRun(() -> LOGGER.info("Loaded programmer art resources")); }); } + + public static TextureManager getTextureManager() { + return textureManager; + } } diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/ClientLevelAccessor.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_ClientLevel.java similarity index 82% rename from src/main/java/gay/sylv/legacy_landscape/mixin/ClientLevelAccessor.java rename to src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_ClientLevel.java index 757b796..16156ea 100644 --- a/src/main/java/gay/sylv/legacy_landscape/mixin/ClientLevelAccessor.java +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_ClientLevel.java @@ -1,4 +1,4 @@ -package gay.sylv.legacy_landscape.mixin; +package gay.sylv.legacy_landscape.mixin.client; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import net.minecraft.client.color.block.BlockTintCache; @@ -8,7 +8,7 @@ import org.spongepowered.asm.mixin.gen.Accessor; @Mixin(ClientLevel.class) -public interface ClientLevelAccessor { +public interface Accessor_ClientLevel { @Accessor Object2ObjectArrayMap getTintCaches(); } diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeRenderType.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeRenderType.java new file mode 100644 index 0000000..aa511d3 --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeRenderType.java @@ -0,0 +1,11 @@ +package gay.sylv.legacy_landscape.mixin.client; + +import net.minecraft.client.renderer.RenderType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(RenderType.CompositeRenderType.class) +public interface Accessor_CompositeRenderType { + @Accessor + RenderType.CompositeState getState(); +} diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeState.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeState.java new file mode 100644 index 0000000..4f6f295 --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_CompositeState.java @@ -0,0 +1,12 @@ +package gay.sylv.legacy_landscape.mixin.client; + +import net.minecraft.client.renderer.RenderStateShard; +import net.minecraft.client.renderer.RenderType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(RenderType.CompositeState.class) +public interface Accessor_CompositeState { + @Accessor + RenderStateShard.EmptyTextureStateShard getTextureState(); +} diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_RenderSystem.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_RenderSystem.java new file mode 100644 index 0000000..b7b819f --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_RenderSystem.java @@ -0,0 +1,13 @@ +package gay.sylv.legacy_landscape.mixin.client; + +import com.mojang.blaze3d.systems.RenderSystem; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(RenderSystem.class) +public interface Accessor_RenderSystem { + @Accessor + static int[] getShaderTextures() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_TextureStateShard.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_TextureStateShard.java new file mode 100644 index 0000000..407d923 --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Accessor_TextureStateShard.java @@ -0,0 +1,20 @@ +package gay.sylv.legacy_landscape.mixin.client; + +import net.minecraft.client.renderer.RenderStateShard; +import net.minecraft.resources.ResourceLocation; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import java.util.Optional; + +@Mixin(RenderStateShard.TextureStateShard.class) +public interface Accessor_TextureStateShard { + @Accessor + Optional getTexture(); + + @Accessor + boolean isBlur(); + + @Accessor + boolean isMipmap(); +} diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/client/Mixin_LevelRenderer.java b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Mixin_LevelRenderer.java new file mode 100644 index 0000000..accb522 --- /dev/null +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/client/Mixin_LevelRenderer.java @@ -0,0 +1,43 @@ +package gay.sylv.legacy_landscape.mixin.client; + +import com.llamalad7.mixinextras.sugar.Local; +import gay.sylv.legacy_landscape.client.HackedRenderSystem; +import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.LevelRenderer; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.ShaderInstance; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.chunk.LevelChunk; +import org.joml.Matrix4f; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(LevelRenderer.class) +public final class Mixin_LevelRenderer { + private Mixin_LevelRenderer() {} + + @Inject( + method = "renderSectionLayer", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/renderer/ShaderInstance;setDefaultUniforms(Lcom/mojang/blaze3d/vertex/VertexFormat$Mode;Lorg/joml/Matrix4f;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/platform/Window;)V" + ) + ) + private void setLegacyTextures(RenderType renderType, double x, double y, double z, Matrix4f frustrumMatrix, Matrix4f projectionMatrix, CallbackInfo ci, @Local ShaderInstance shaderInstance) { + Minecraft client = Minecraft.getInstance(); + assert client.level != null; + LevelChunk chunk = client.level.getChunkAt(BlockPos.containing(x, y, z)); + if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) { + Accessor_CompositeRenderType compositeRenderType = (Accessor_CompositeRenderType) renderType; + Accessor_CompositeState compositeState = (Accessor_CompositeState) (Object) compositeRenderType.getState(); + assert compositeState != null; + Accessor_TextureStateShard textureState = (Accessor_TextureStateShard) (compositeState.getTextureState()); + if (textureState.getTexture().isPresent()) { + HackedRenderSystem.setShaderTexture(0, textureState.getTexture().get(), textureState.isBlur(), textureState.isMipmap()); + } + } + } +} diff --git a/src/main/java/gay/sylv/legacy_landscape/networking/ClientPayloadHandler.java b/src/main/java/gay/sylv/legacy_landscape/networking/ClientPayloadHandler.java index 228a94d..85feec8 100644 --- a/src/main/java/gay/sylv/legacy_landscape/networking/ClientPayloadHandler.java +++ b/src/main/java/gay/sylv/legacy_landscape/networking/ClientPayloadHandler.java @@ -1,7 +1,7 @@ package gay.sylv.legacy_landscape.networking; import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments; -import gay.sylv.legacy_landscape.mixin.ClientLevelAccessor; +import gay.sylv.legacy_landscape.mixin.client.Accessor_ClientLevel; import gay.sylv.legacy_landscape.networking.client_bound.LegacyChunkPayload; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; @@ -22,7 +22,7 @@ public static void handleLegacyChunkPayload(final LegacyChunkPayload payload, fi } else { chunk.removeData(LegacyAttachments.LEGACY_CHUNK); } - ((ClientLevelAccessor) level).getTintCaches().forEach((colorResolver, blockTintCache) -> blockTintCache.invalidateForChunk(chunk.getPos().x, chunk.getPos().z)); + ((Accessor_ClientLevel) level).getTintCaches().forEach((colorResolver, blockTintCache) -> blockTintCache.invalidateForChunk(chunk.getPos().x, chunk.getPos().z)); for (int y = level.getMinSection(); y < level.getMaxSection(); y++) { Minecraft.getInstance().levelRenderer.setSectionDirty(chunk.getPos().x, y, chunk.getPos().z); } diff --git a/src/main/java/gay/sylv/legacy_landscape/networking/LegacyNetworking.java b/src/main/java/gay/sylv/legacy_landscape/networking/LegacyNetworking.java index 433da47..f7ecabb 100644 --- a/src/main/java/gay/sylv/legacy_landscape/networking/LegacyNetworking.java +++ b/src/main/java/gay/sylv/legacy_landscape/networking/LegacyNetworking.java @@ -23,8 +23,8 @@ private LegacyNetworking() {} @SubscribeEvent public static void register(final RegisterPayloadHandlersEvent event) { final PayloadRegistrar registrar = event.registrar("1"); - registrar.executesOn(HandlerThread.MAIN); - registrar.playToClient( + final PayloadRegistrar mainThreadRegistrar = registrar.executesOn(HandlerThread.MAIN); + mainThreadRegistrar.playToClient( LegacyChunkPayload.TYPE, LegacyChunkPayload.STREAM_CODEC, ClientPayloadHandler::handleLegacyChunkPayload diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..dfb84d6 --- /dev/null +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.renderer.RenderType$CompositeRenderType diff --git a/src/main/resources/legacy_landscape.mixins.json b/src/main/resources/legacy_landscape.mixins.json index 17c8a65..7b83193 100644 --- a/src/main/resources/legacy_landscape.mixins.json +++ b/src/main/resources/legacy_landscape.mixins.json @@ -4,10 +4,15 @@ "package": "gay.sylv.legacy_landscape.mixin", "compatibilityLevel": "JAVA_21", "mixins": [ - "ClientLevelAccessor" ], "client": [ - "client.Mixin_ClientLevel" + "client.Accessor_ClientLevel", + "client.Accessor_CompositeRenderType", + "client.Accessor_CompositeState", + "client.Accessor_RenderSystem", + "client.Accessor_TextureStateShard", + "client.Mixin_ClientLevel", + "client.Mixin_LevelRenderer" ], "server": [], "injectors": {