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
This commit is contained in:
ramidzkh authored and GitHub committed 2021-01-26 01:19:44 -04:00
1 parent a83548876a
commit 566d2a7bca
5 files changed
+50 -6

No files matched your search

+1 -1
View File
@@ -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.
+1 -4
View File
@@ -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
+2
View File
@@ -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)
@@ -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);
@@ -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);
}
}