From 2d5152420e0807e9af672d790799fd8399725a1c Mon Sep 17 00:00:00 2001 From: Daniel Orr Date: Sat, 16 Nov 2024 03:26:21 +0000 Subject: [PATCH] feat: RailroadPlayer, refactors --- .../mcbrawls/railroad/mixin/ExampleMixin.java | 15 -------------- .../mixin/ServerPlayerEntityMixin.java | 16 +++++++++++++++ .../railroad/player/RailroadPlayer.java | 4 ++++ .../net/mcbrawls/railroad/CameraTrack.kt | 11 ++++++++-- .../kotlin/net/mcbrawls/railroad/Keyframe.kt | 5 +++-- .../kotlin/net/mcbrawls/railroad/Vectors.kt | 11 ---------- .../mcbrawls/railroad/codec/ExtraCodecs.kt | 20 +++++++++++++++++++ .../railroad/player/HandledCameraTrack.kt | 7 +++++++ src/main/resources/fabric.mod.json | 2 +- src/main/resources/railroad.mixins.json | 4 ++-- 10 files changed, 62 insertions(+), 33 deletions(-) delete mode 100644 src/main/java/net/mcbrawls/railroad/mixin/ExampleMixin.java create mode 100644 src/main/java/net/mcbrawls/railroad/mixin/ServerPlayerEntityMixin.java create mode 100644 src/main/java/net/mcbrawls/railroad/player/RailroadPlayer.java create mode 100644 src/main/kotlin/net/mcbrawls/railroad/codec/ExtraCodecs.kt create mode 100644 src/main/kotlin/net/mcbrawls/railroad/player/HandledCameraTrack.kt diff --git a/src/main/java/net/mcbrawls/railroad/mixin/ExampleMixin.java b/src/main/java/net/mcbrawls/railroad/mixin/ExampleMixin.java deleted file mode 100644 index f0dea87..0000000 --- a/src/main/java/net/mcbrawls/railroad/mixin/ExampleMixin.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.mcbrawls.railroad.mixin; - -import net.minecraft.server.MinecraftServer; -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(MinecraftServer.class) -public class ExampleMixin { - @Inject(at = @At("HEAD"), method = "loadWorld") - private void init(CallbackInfo info) { - // This code is injected into the start of MinecraftServer.loadWorld()V - } -} \ No newline at end of file diff --git a/src/main/java/net/mcbrawls/railroad/mixin/ServerPlayerEntityMixin.java b/src/main/java/net/mcbrawls/railroad/mixin/ServerPlayerEntityMixin.java new file mode 100644 index 0000000..8e24c23 --- /dev/null +++ b/src/main/java/net/mcbrawls/railroad/mixin/ServerPlayerEntityMixin.java @@ -0,0 +1,16 @@ +package net.mcbrawls.railroad.mixin; + +import com.mojang.authlib.GameProfile; +import net.mcbrawls.railroad.player.RailroadPlayer; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(ServerPlayerEntity.class) +public abstract class ServerPlayerEntityMixin extends PlayerEntity implements RailroadPlayer { + private ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) { + super(world, pos, yaw, gameProfile); + } +} diff --git a/src/main/java/net/mcbrawls/railroad/player/RailroadPlayer.java b/src/main/java/net/mcbrawls/railroad/player/RailroadPlayer.java new file mode 100644 index 0000000..f534392 --- /dev/null +++ b/src/main/java/net/mcbrawls/railroad/player/RailroadPlayer.java @@ -0,0 +1,4 @@ +package net.mcbrawls.railroad.player; + +public interface RailroadPlayer { +} diff --git a/src/main/kotlin/net/mcbrawls/railroad/CameraTrack.kt b/src/main/kotlin/net/mcbrawls/railroad/CameraTrack.kt index a2589c4..aa0f32b 100644 --- a/src/main/kotlin/net/mcbrawls/railroad/CameraTrack.kt +++ b/src/main/kotlin/net/mcbrawls/railroad/CameraTrack.kt @@ -2,6 +2,8 @@ package net.mcbrawls.railroad import com.mojang.serialization.Codec import com.mojang.serialization.codecs.RecordCodecBuilder +import net.mcbrawls.railroad.codec.ExtraCodecs +import kotlin.time.Duration /** * A camera track which can be played to a player. @@ -9,11 +11,16 @@ import com.mojang.serialization.codecs.RecordCodecBuilder data class CameraTrack( val keyframes: List, ) { + /** + * The total duration of the track. + */ + val duration: Duration = keyframes.lastOrNull()?.time ?: Duration.ZERO + data class TrackKeyframe( /** * The time at which the keyframe is played on the track. */ - val seconds: Float, + val time: Duration, /** * The keyframe to be played. @@ -26,7 +33,7 @@ data class CameraTrack( */ val CODEC: Codec = RecordCodecBuilder.create { instance -> instance.group( - Codec.FLOAT.fieldOf("seconds").forGetter(TrackKeyframe::seconds), + ExtraCodecs.DURATION.fieldOf("time").forGetter(TrackKeyframe::time), Keyframe.CODEC.fieldOf("keyframe").forGetter(TrackKeyframe::keyframe), ).apply(instance, ::TrackKeyframe) } diff --git a/src/main/kotlin/net/mcbrawls/railroad/Keyframe.kt b/src/main/kotlin/net/mcbrawls/railroad/Keyframe.kt index 530177b..375a672 100644 --- a/src/main/kotlin/net/mcbrawls/railroad/Keyframe.kt +++ b/src/main/kotlin/net/mcbrawls/railroad/Keyframe.kt @@ -2,6 +2,7 @@ package net.mcbrawls.railroad import com.mojang.serialization.Codec import com.mojang.serialization.codecs.RecordCodecBuilder +import net.mcbrawls.railroad.codec.ExtraCodecs import org.joml.Vector2f import org.joml.Vector3d @@ -18,8 +19,8 @@ data class Keyframe( */ val CODEC: Codec = RecordCodecBuilder.create { instance -> instance.group( - Vectors.VECTOR_3D.fieldOf("position").forGetter(Keyframe::pos), - Vectors.VECTOR_2F.fieldOf("rotation").forGetter(Keyframe::rotation), + ExtraCodecs.VECTOR_3D.fieldOf("position").forGetter(Keyframe::pos), + ExtraCodecs.VECTOR_2F.fieldOf("rotation").forGetter(Keyframe::rotation), ).apply(instance, ::Keyframe) } } diff --git a/src/main/kotlin/net/mcbrawls/railroad/Vectors.kt b/src/main/kotlin/net/mcbrawls/railroad/Vectors.kt index 69167ad..3e19a78 100644 --- a/src/main/kotlin/net/mcbrawls/railroad/Vectors.kt +++ b/src/main/kotlin/net/mcbrawls/railroad/Vectors.kt @@ -1,20 +1,9 @@ package net.mcbrawls.railroad -import com.mojang.serialization.Codec import org.joml.Vector2f import org.joml.Vector3d object Vectors { - val VECTOR_3D: Codec = Codec.DOUBLE.listOf().xmap( - { list -> Vector3d(list[0], list[1], list[2]) }, - { vect -> listOf(vect.x, vect.y, vect.z) } - ) - - val VECTOR_2F: Codec = Codec.FLOAT.listOf().xmap( - { list -> Vector2f(list[0], list[1]) }, - { vect -> listOf(vect.x, vect.y) } - ) - fun Vector3d.cubicInterpolate(end: Vector3d, delta: Float): Vector3d { val start = this diff --git a/src/main/kotlin/net/mcbrawls/railroad/codec/ExtraCodecs.kt b/src/main/kotlin/net/mcbrawls/railroad/codec/ExtraCodecs.kt new file mode 100644 index 0000000..1609d9a --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/railroad/codec/ExtraCodecs.kt @@ -0,0 +1,20 @@ +package net.mcbrawls.railroad.codec + +import com.mojang.serialization.Codec +import org.joml.Vector2f +import org.joml.Vector3d +import kotlin.time.Duration + +object ExtraCodecs { + val VECTOR_3D: Codec = Codec.DOUBLE.listOf().xmap( + { list -> Vector3d(list[0], list[1], list[2]) }, + { vect -> listOf(vect.x, vect.y, vect.z) } + ) + + val VECTOR_2F: Codec = Codec.FLOAT.listOf().xmap( + { list -> Vector2f(list[0], list[1]) }, + { vect -> listOf(vect.x, vect.y) } + ) + + val DURATION: Codec = Codec.STRING.xmap(Duration::parse, Duration::toString) +} diff --git a/src/main/kotlin/net/mcbrawls/railroad/player/HandledCameraTrack.kt b/src/main/kotlin/net/mcbrawls/railroad/player/HandledCameraTrack.kt new file mode 100644 index 0000000..d74ae5d --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/railroad/player/HandledCameraTrack.kt @@ -0,0 +1,7 @@ +package net.mcbrawls.railroad.player + +import net.mcbrawls.railroad.CameraTrack + +class HandledCameraTrack(val cameraTrack: CameraTrack) { + // +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index eec5022..10f9c39 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -20,7 +20,7 @@ "custom": { "loom:injected_interfaces": { "net/minecraft/class_3222": [ - "net/mcbrawls/railroad/RailroadPlayer" + "net/mcbrawls/railroad/player/RailroadPlayer" ] } }, diff --git a/src/main/resources/railroad.mixins.json b/src/main/resources/railroad.mixins.json index 9288ae1..21f9c7b 100644 --- a/src/main/resources/railroad.mixins.json +++ b/src/main/resources/railroad.mixins.json @@ -3,9 +3,9 @@ "package": "net.mcbrawls.railroad.mixin", "compatibilityLevel": "JAVA_21", "mixins": [ - "ExampleMixin" + "ServerPlayerEntityMixin" ], "injectors": { "defaultRequire": 1 } -} \ No newline at end of file +}