Files
AstraContent/src/main/java/online/astradev/content/mixin/TrainCartMixin.java
T

66 lines
2.4 KiB
Java
Raw Normal View History

package online.astradev.content.mixin;
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$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;
}
@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;
}
}