package online.astradev.content.mixin; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; 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 { @Unique private Vec3 astra$beforePlayerPush; @Inject(method = "addAdditionalSaveData", at = @At("RETURN")) private void astra$save(ValueOutput output, CallbackInfo ci) { CouplingGeometry.save((AbstractMinecart) (Object) this, output); } @Inject(method = "readAdditionalSaveData", at = @At("RETURN")) private void astra$read(ValueInput input, CallbackInfo ci) { CouplingGeometry.load((AbstractMinecart) (Object) this, input); } @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 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) return; Vec3 impulse = self.getDeltaMovement().subtract(astra$beforePlayerPush); astra$beforePlayerPush = null; if (impulse.horizontalDistanceSqr() > 1e-12) Trains.playerPush(self); } }