Simplify train and rail logic and consolidate integration tests

This commit is contained in:
2026-09-13 21:55:44 +03:00
parent c39e91d836
commit 2c651a12fc
46 changed files with 2660 additions and 1233 deletions
@@ -4,35 +4,62 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
import net.minecraft.world.level.storage.*;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.train.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(AbstractMinecart.class)
public abstract class TrainCartMixin implements CartFrame {
@Unique private Vec3 astra$front=Vec3.ZERO;
@Unique private Vec3 astra$front = Vec3.ZERO;
@Unique private Vec3 astra$beforePlayerPush;
@Override public Vec3 astracontent$front(){return astra$front;}
@Override public void astracontent$front(Vec3 axis){astra$front=axis;}
@Inject(method="addAdditionalSaveData",at=@At("RETURN"))
private void astra$save(ValueOutput out,CallbackInfo ci){out.putDouble("AstraTrainFrontX",astra$front.x);out.putDouble("AstraTrainFrontZ",astra$front.z);}
@Inject(method="readAdditionalSaveData",at=@At("RETURN"))
private void astra$read(ValueInput in,CallbackInfo ci){
double x=in.getDoubleOr("AstraTrainFrontX",0),z=in.getDoubleOr("AstraTrainFrontZ",0);
astra$front=Double.isFinite(x)&&Double.isFinite(z)?new Vec3(x,0,z).normalize():Vec3.ZERO;
@Override
public Vec3 astracontent$front() {
return astra$front;
}
@Inject(method="push(Lnet/minecraft/world/entity/Entity;)V",at=@At("HEAD"),cancellable=true)
private void astra$noInternalCollision(Entity other,CallbackInfo ci){
var self=(AbstractMinecart)(Object)this;
astra$beforePlayerPush=other instanceof net.minecraft.world.entity.player.Player?self.getDeltaMovement():null;
if(other instanceof AbstractMinecart cart && Trains.connected(self,cart))ci.cancel();
@Override
public void astracontent$front(Vec3 axis) {
astra$front = axis;
}
@Inject(method="push(Lnet/minecraft/world/entity/Entity;)V",at=@At("RETURN"))
private void astra$playerPush(Entity other,CallbackInfo ci){
var self=(AbstractMinecart)(Object)this;
if(astra$beforePlayerPush!=null && self.getDeltaMovement().subtract(astra$beforePlayerPush).horizontalDistanceSqr()>1e-12)
Trains.playerPush(self);
astra$beforePlayerPush=null;
@Inject(method = "addAdditionalSaveData", at = @At("RETURN"))
private void astra$save(ValueOutput out, CallbackInfo ci) {
out.putDouble("AstraTrainFrontX", astra$front.x);
out.putDouble("AstraTrainFrontZ", astra$front.z);
}
@Inject(method = "readAdditionalSaveData", at = @At("RETURN"))
private void astra$read(ValueInput in, CallbackInfo ci) {
double x = in.getDoubleOr("AstraTrainFrontX", 0), z = in.getDoubleOr("AstraTrainFrontZ", 0);
astra$front =
Double.isFinite(x) && Double.isFinite(z)
? new Vec3(x, 0, z).normalize()
: Vec3.ZERO;
}
@Inject(
method = "push(Lnet/minecraft/world/entity/Entity;)V",
at = @At("HEAD"),
cancellable = true)
private void astra$noInternalCollision(Entity other, CallbackInfo ci) {
var self = (AbstractMinecart) (Object) this;
astra$beforePlayerPush =
other instanceof net.minecraft.world.entity.player.Player
? self.getDeltaMovement()
: null;
if (other instanceof AbstractMinecart cart && Trains.connected(self, cart)) ci.cancel();
}
@Inject(method = "push(Lnet/minecraft/world/entity/Entity;)V", at = @At("RETURN"))
private void astra$playerPush(Entity other, CallbackInfo ci) {
var self = (AbstractMinecart) (Object) this;
if (astra$beforePlayerPush != null
&& self.getDeltaMovement().subtract(astra$beforePlayerPush).horizontalDistanceSqr()
> 1e-12) Trains.playerPush(self);
astra$beforePlayerPush = null;
}
}