From 566d2a7bcab72c9e7bdcfc45aba459816a3b9724 Mon Sep 17 00:00:00 2001 From: ramidzkh Date: Tue, 26 Jan 2021 16:19:44 +1100 Subject: [PATCH] Use WorldRenderEvents if available (#680) * Use WorldRenderEvents if available Fixes an issue when Canvas is installed * Improve code * Depend only on fabric-rendering-v1 * Use fabricApi.module --- build.gradle | 2 +- gradle.properties | 5 +-- src/main/java/carpet/CarpetServer.java | 2 + .../WorldRenderer_scarpetRenderMixin.java | 3 +- .../java/carpet/utils/FabricAPIHooks.java | 44 +++++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/main/java/carpet/utils/FabricAPIHooks.java diff --git a/build.gradle b/build.gradle index 5795603e..f1027e81 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ dependencies { modCompile "net.fabricmc:fabric-loader:${project.loader_version}" // Fabric API. This is technically optional, but you probably want it anyway. - // modCompile "net.fabricmc:fabric:${project.fabric_version}" + modCompileOnly fabricApi.module("fabric-rendering-v1", project.fabric_version) // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. diff --git a/gradle.properties b/gradle.properties index 6818b005..ea3f2c49 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,7 @@ org.gradle.jvmargs=-Xmx1G minecraft_version=1.16.5 yarn_mappings=1.16.5+build.1 loader_version=0.11.1 + fabric_version=0.29.4+1.16 # Mod Properties mod_version = 1.4.24 @@ -26,7 +27,3 @@ org.gradle.jvmargs=-Xmx1G # This is needed because CF uses too vague names for snapshots # Can also be the version ID directly coming from https://minecraft.curseforge.com/api/game/versions?token=[API_TOKEN] release-extra-curse-version = 1.17 - -# Dependencies - # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api - #fabric_version=0.3.0-pre+build.166 diff --git a/src/main/java/carpet/CarpetServer.java b/src/main/java/carpet/CarpetServer.java index c8cae167..a08e3e66 100644 --- a/src/main/java/carpet/CarpetServer.java +++ b/src/main/java/carpet/CarpetServer.java @@ -25,6 +25,7 @@ import carpet.logging.LoggerRegistry; import carpet.script.CarpetScriptServer; import carpet.settings.SettingsManager; import carpet.logging.HUDController; +import carpet.utils.FabricAPIHooks; import carpet.utils.MobAI; import carpet.utils.SpawnReporter; import com.mojang.brigadier.CommandDispatcher; @@ -80,6 +81,7 @@ public class CarpetServer implements ClientModInitializer,DedicatedServerModInit settingsManager = new SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod"); settingsManager.parseSettingsClass(CarpetSettings.class); extensions.forEach(CarpetExtension::onGameStarted); + FabricAPIHooks.initialize(); } public static void onServerLoaded(MinecraftServer server) diff --git a/src/main/java/carpet/mixins/WorldRenderer_scarpetRenderMixin.java b/src/main/java/carpet/mixins/WorldRenderer_scarpetRenderMixin.java index 61b342b2..4eb54527 100644 --- a/src/main/java/carpet/mixins/WorldRenderer_scarpetRenderMixin.java +++ b/src/main/java/carpet/mixins/WorldRenderer_scarpetRenderMixin.java @@ -2,6 +2,7 @@ package carpet.mixins; import carpet.network.CarpetClient; import carpet.script.utils.ShapesRenderer; +import carpet.utils.FabricAPIHooks; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.BufferBuilderStorage; @@ -28,7 +29,7 @@ public class WorldRenderer_scarpetRenderMixin @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/debug/DebugRenderer;render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider$Immediate;DDD)V")) private void renderScarpetThings(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f matrix4f, CallbackInfo ci) { - if (CarpetClient.shapes != null) + if (!FabricAPIHooks.WORLD_RENDER_EVENTS && CarpetClient.shapes != null) { RenderSystem.pushMatrix(); CarpetClient.shapes.render(camera, tickDelta); diff --git a/src/main/java/carpet/utils/FabricAPIHooks.java b/src/main/java/carpet/utils/FabricAPIHooks.java new file mode 100644 index 00000000..b9c49bd5 --- /dev/null +++ b/src/main/java/carpet/utils/FabricAPIHooks.java @@ -0,0 +1,44 @@ +package carpet.utils; + +import carpet.network.CarpetClient; +import com.mojang.blaze3d.systems.RenderSystem; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.SemanticVersion; +import net.fabricmc.loader.api.Version; +import net.fabricmc.loader.api.VersionParsingException; + +public class FabricAPIHooks { + + public static final boolean WORLD_RENDER_EVENTS = hasMod("fabric-rendering-v1", "1.5.0"); + + private FabricAPIHooks() { + } + + public static void initialize() { + if (WORLD_RENDER_EVENTS) { + WorldRenderEvents.BEFORE_DEBUG_RENDER.register(context -> { + if (CarpetClient.shapes != null) { + RenderSystem.pushMatrix(); + CarpetClient.shapes.render(context.camera(), context.tickDelta()); + RenderSystem.popMatrix(); + } + }); + } + } + + private static boolean hasMod(String id, String minimumVersion) { + return FabricLoader.getInstance().getModContainer(id).map(m -> { + Version version = m.getMetadata().getVersion(); + + if (version instanceof SemanticVersion) { + try { + return ((SemanticVersion) version).compareTo(SemanticVersion.parse(minimumVersion)) >= 0; + } catch (VersionParsingException ignored) { + } + } + + return false; + }).orElse(false); + } +}