Skip to content

Commit

Permalink
More Connector compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplicitSaber committed Oct 23, 2024
1 parent 62719e7 commit 931ad1e
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.15.11

# Mod Properties
mod_version=1.20.1-1.1.0-fix1
mod_version=1.20.1-1.1.0-fix2
maven_group=io.github.implicitsaber
archives_base_name=forkcart

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import io.github.implicitsaber.forkcart.block.entity.TrackTiesBlockEntityRenderer;
import io.github.implicitsaber.forkcart.config.Config;
import io.github.implicitsaber.forkcart.config.ConfigOption;
import io.github.implicitsaber.forkcart.util.ForgeHelper;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.minecraft.client.render.entity.EmptyEntityRenderer;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.io.IOException;

Expand Down Expand Up @@ -47,5 +51,11 @@ public void onInitializeClient() {
.then(CONFIG.command(LiteralArgumentBuilder.literal("config"),
FabricClientCommandSource::sendFeedback))
));

ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
if(ForgeHelper.isConnector()) {
client.inGameHud.getChatHud().addMessage(Text.translatable("forkcart.msg.connector").formatted(Formatting.RED));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.joml.Matrix3d;
import org.joml.Vector3d;
import org.joml.Vector3f;

public class TrackTiesBlockEntityRenderer implements BlockEntityRenderer<TrackTiesBlockEntity> {

// Required for Connector on NeoForge to render this BE properly
public static final Box INFINITE_BOX = new Box(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

public static final int WHITE = 0xFFFFFFFF;
public static final Identifier CHAIN_TRACK_TEXTURE = Forkcart.id("textures/chain_track.png");
public static final Identifier STATION_TRACK_TEXTURE = Forkcart.id("textures/station_track.png");
Expand Down Expand Up @@ -159,6 +164,12 @@ public boolean rendersOutsideBoundingBox(TrackTiesBlockEntity blockEntity) {
return true;
}

// Required for Connector on NeoForge to render this BE properly
@SuppressWarnings("unused")
public Box getRenderBoundingBox(TrackTiesBlockEntity be) {
return INFINITE_BOX;
}

@Override
public int getRenderDistance() {
return ForkcartClient.CFG_TRACK_RENDER_DISTANCE.get() * 16;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.implicitsaber.forkcart.clientmixinconfig;

import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Set;

public class ForkcartClientMixinPlugin implements IMixinConfigPlugin {

@Override
public void onLoad(String mixinPackage) {

}

@Override
public String getRefMapperConfig() {
return null;
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return switch(mixinClassName) {
case "io.github.implicitsaber.forkcart.mixin.client.GameRendererMixin",
"io.github.implicitsaber.forkcart.mixin.client.CameraMixin" -> !FabricLoader.getInstance().isModLoaded("connectormod");
case "io.github.implicitsaber.forkcart.mixin.client.CameraMixinForge" -> FabricLoader.getInstance().isModLoaded("connectormod");
default -> true;
};
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {

}

@Override
public List<String> getMixins() {
return null;
}

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.implicitsaber.forkcart.mixin.client;

import com.llamalad7.mixinextras.sugar.Local;
import io.github.implicitsaber.forkcart.ForkcartClient;
import io.github.implicitsaber.forkcart.entity.TrackFollowerEntity;
import net.minecraft.client.render.Camera;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.BlockView;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Camera.class)
public abstract class CameraMixinForge {
@Shadow protected abstract void setPos(Vec3d pos);

@Shadow @Final private Quaternionf rotation;

@Inject(method = "update(Lnet/minecraft/world/BlockView;Lnet/minecraft/entity/Entity;ZZF)V",
at = @At(value = "INVOKE", shift = At.Shift.AFTER, ordinal = 0, target = "Lnet/minecraft/client/render/Camera;setPos(DDD)V"))
private void forkcart$updateCamPosWhileRiding(BlockView area, Entity self, boolean thirdPerson, boolean inverseView, float tickDelta, CallbackInfo info) {
var vehicle = self.getVehicle();
if (vehicle != null) {
var tf = vehicle.getVehicle();
if (tf instanceof TrackFollowerEntity trackFollower) {
var world = self.getWorld();
var diff = self.getPos().add(0, self.getStandingEyeHeight(), 0).subtract(trackFollower.getPos());
var camPos = new Vector3d(diff.getX(), diff.getY(), diff.getZ());
if (world.isClient()) {
var rot = new Quaternionf();
trackFollower.getClientOrientation(rot, tickDelta);
rot.transform(camPos);

this.setPos(new Vec3d(camPos.x(), camPos.y(), camPos.z()).add(trackFollower.getLerpedPos(tickDelta)));

// camera rotation is disabled due to glitches
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.implicitsaber.forkcart.mixin.client;

import com.llamalad7.mixinextras.sugar.Local;
import io.github.implicitsaber.forkcart.util.ForgeFixes;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -18,17 +17,17 @@ public class GameRendererMixin {

@Inject(method = "renderWorld", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/client/render/Camera;update(Lnet/minecraft/world/BlockView;Lnet/minecraft/entity/Entity;ZZF)V"))
public void forkcart$quaternionRotate(float tickDelta, long limitTime, MatrixStack matrices, CallbackInfo ci, @Local Camera cam) {
if(!ForgeFixes.isConnector()) matrices.peek().getPositionMatrix().set(new Matrix4f().rotate(cam.getRotation().conjugate(new Quaternionf())));
matrices.peek().getPositionMatrix().set(new Matrix4f().rotate(cam.getRotation().conjugate(new Quaternionf())));
}

@Redirect(method = "renderWorld", at = @At(value = "INVOKE", ordinal = 2, target = "Lnet/minecraft/client/util/math/MatrixStack;multiply(Lorg/joml/Quaternionf;)V"))
public void forkcart$noPitchYaw1(MatrixStack instance, Quaternionf quaternion) {
if(ForgeFixes.isConnector()) instance.multiply(quaternion);
// no-op
}

@Redirect(method = "renderWorld", at = @At(value = "INVOKE", ordinal = 3, target = "Lnet/minecraft/client/util/math/MatrixStack;multiply(Lorg/joml/Quaternionf;)V"))
public void forkcart$noPitchYaw2(MatrixStack instance, Quaternionf quaternion) {
if(ForgeFixes.isConnector()) instance.multiply(quaternion);
// no-op
}

}
8 changes: 4 additions & 4 deletions src/client/resources/forkcart.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"compatibilityLevel": "JAVA_17",
"client": [
"CameraMixin",
"CameraMixinForge",
"ClientPlayNetworkHandlerMixin",
"EntityRenderDispatcherMixin",
"GameRendererMixin",
"InGameOverlayRendererMixin",
"MinecartInsideSoundInstanceMixin",
"MovingMinecartSoundInstanceMixin",
"WorldRendererMixin"
],
"plugin": "io.github.implicitsaber.forkcart.clientmixinconfig.ForkcartClientMixinPlugin",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"GameRendererMixin"
]
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/implicitsaber/forkcart/Forkcart.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.github.implicitsaber.forkcart.entity.TrackFollowerEntity;
import io.github.implicitsaber.forkcart.item.LoreBlockItem;
import io.github.implicitsaber.forkcart.item.TrackItem;
import io.github.implicitsaber.forkcart.util.ForgeFixes;
import io.github.implicitsaber.forkcart.util.ForgeHelper;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
Expand Down Expand Up @@ -81,7 +81,7 @@ public BlockRenderType getRenderType(BlockState state) {

@Override
public void onInitialize() {
if(ForgeFixes.isConnector()) LOGGER.warn("Forkcart is running on Sinytra Connector. Be prepared for unexpected behavior!");
if(ForgeHelper.isConnector()) LOGGER.warn("Forkcart is running on Sinytra Connector. Be prepared for unexpected behavior!");

BlockItem tieItem = Registry.register(Registries.ITEM, id("track_ties"),
new LoreBlockItem(TRACK_TIES, new Item.Settings(), List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import net.fabricmc.loader.api.FabricLoader;

public class ForgeFixes {
public class ForgeHelper {

private static boolean connector = false;
private static boolean checked = false;

public static boolean isConnector() {
if(checked) return connector;
connector = FabricLoader.getInstance().isModLoaded("connector");
connector = FabricLoader.getInstance().isModLoaded("connectormod");
checked = true;
return connector;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/forkcart/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@

"tag.item.forkcart.track": "Track",

"itemGroup.forkcart": "Forkcart"
"itemGroup.forkcart": "Forkcart",

"forkcart.msg.connector": "Forkcart has detected that it is running in a Sinytra Connector instance pre-1.21. Camera rotation on tracks is disabled."
}

0 comments on commit 931ad1e

Please sign in to comment.