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
@@ -1,23 +1,43 @@
package online.astradev.content.mixin;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.network.protocol.game.ServerboundMoveVehiclePacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.transport.SlimeBounce;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerGamePacketListenerImpl.class)
public abstract class BoatPacketMixin {
@Shadow public ServerPlayer player;
@Unique private Entity astra$boat;
@Unique private Vec3 astra$boatBefore;
// Run after vanilla's thread check, so packet-thread rescheduling never mutates state.
@Inject(method="handleMoveVehicle",at=@At(value="INVOKE",target="Lnet/minecraft/server/level/ServerPlayer;getRootVehicle()Lnet/minecraft/world/entity/Entity;",shift=At.Shift.BEFORE))
private void astra$before(ServerboundMoveVehiclePacket packet,CallbackInfo ci){astra$boat=player.getRootVehicle();astra$boatBefore=astra$boat.position();}
@Inject(method="handleMoveVehicle",at=@At("RETURN"))
private void astra$after(ServerboundMoveVehiclePacket packet,CallbackInfo ci){
if(astra$boat!=null&&astra$boatBefore!=null){SlimeBounce.boatPacket(astra$boat,astra$boatBefore);astra$boat=null;astra$boatBefore=null;}
@Inject(
method = "handleMoveVehicle",
at =
@At(
value = "INVOKE",
target =
"Lnet/minecraft/server/level/ServerPlayer;getRootVehicle()Lnet/minecraft/world/entity/Entity;",
shift = At.Shift.BEFORE))
private void astra$before(ServerboundMoveVehiclePacket packet, CallbackInfo ci) {
astra$boat = player.getRootVehicle();
astra$boatBefore = astra$boat.position();
}
@Inject(method = "handleMoveVehicle", at = @At("RETURN"))
private void astra$after(ServerboundMoveVehiclePacket packet, CallbackInfo ci) {
if (astra$boat != null && astra$boatBefore != null) {
SlimeBounce.boatPacket(astra$boat, astra$boatBefore);
astra$boat = null;
astra$boatBefore = null;
}
}
}
@@ -1,21 +1,39 @@
package online.astradev.content.mixin;
import java.util.function.Predicate;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import online.astradev.content.weapon.Ammunition;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.function.Predicate;
@Mixin(CrossbowItem.class)
public abstract class CrossbowMixin {
@Inject(method = {"getSupportedHeldProjectiles()Ljava/util/function/Predicate;", "getAllSupportedProjectiles()Ljava/util/function/Predicate;"}, at = @At("RETURN"), cancellable = true)
private void astra$ammo(CallbackInfoReturnable<Predicate<ItemStack>> cir) { cir.setReturnValue(cir.getReturnValue().or(Ammunition::accepts)); }
@Inject(
method = {
"getSupportedHeldProjectiles()Ljava/util/function/Predicate;",
"getAllSupportedProjectiles()Ljava/util/function/Predicate;"
},
at = @At("RETURN"),
cancellable = true)
private void astra$ammo(CallbackInfoReturnable<Predicate<ItemStack>> cir) {
cir.setReturnValue(cir.getReturnValue().or(Ammunition::accepts));
}
@Inject(method = "createProjectile", at = @At("HEAD"), cancellable = true)
private void astra$create(Level level, LivingEntity owner, ItemStack weapon, ItemStack ammo, boolean critical, CallbackInfoReturnable<Projectile> cir) {
private void astra$create(
Level level,
LivingEntity owner,
ItemStack weapon,
ItemStack ammo,
boolean critical,
CallbackInfoReturnable<Projectile> cir) {
if (Ammunition.accepts(ammo)) cir.setReturnValue(Ammunition.create(level, owner, ammo));
}
}
@@ -3,19 +3,25 @@ package online.astradev.content.mixin;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.vehicle.minecart.*;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.rail.CartSpeed;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(NewMinecartBehavior.class)
public abstract class ExperimentalCartMixin extends MinecartBehavior {
protected ExperimentalCartMixin(AbstractMinecart cart){super(cart);}
@Inject(method="calculateTrackSpeed",at=@At("RETURN"),cancellable=true)
protected ExperimentalCartMixin(AbstractMinecart cart) {
super(cart);
}
@Inject(method = "calculateTrackSpeed", at = @At("RETURN"), cancellable = true)
private void astra$exactSpeed(CallbackInfoReturnable<Vec3> cir) {
if(!(level() instanceof ServerLevel level))return;
double limit=((CartSpeed)minecart).astracontent$limit(level);
var movement=cir.getReturnValue();
if(limit>0 && movement.horizontalDistance()>limit)cir.setReturnValue(movement.scale(limit/movement.horizontalDistance()));
if (!(level() instanceof ServerLevel level)) return;
double limit = ((CartSpeed) minecart).astracontent$limit(level);
var movement = cir.getReturnValue();
if (limit > 0 && movement.horizontalDistance() > limit)
cir.setReturnValue(movement.scale(limit / movement.horizontalDistance()));
}
}
@@ -1,14 +1,19 @@
package online.astradev.content.mixin;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.vehicle.minecart.MinecartFurnace;
import online.astradev.content.rail.CartSpeed;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(MinecartFurnace.class)
public abstract class FurnaceCartMixin {
@Inject(method="getMaxSpeed",at=@At("RETURN"),cancellable=true)
private void astra$furnace(ServerLevel level,CallbackInfoReturnable<Double> cir) {
double limit=((CartSpeed)this).astracontent$limit(level);if(limit>0)cir.setReturnValue(limit);
@Inject(method = "getMaxSpeed", at = @At("RETURN"), cancellable = true)
private void astra$furnace(ServerLevel level, CallbackInfoReturnable<Double> cir) {
double limit = ((CartSpeed) this).astracontent$limit(level);
if (limit > 0) cir.setReturnValue(limit);
}
}
@@ -3,7 +3,9 @@ package online.astradev.content.mixin;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
import net.minecraft.world.level.storage.*;
import online.astradev.content.rail.RailSpeed;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.*;
@@ -11,23 +13,31 @@ import org.spongepowered.asm.mixin.injection.callback.*;
@Mixin(AbstractMinecart.class)
public abstract class MinecartMixin implements online.astradev.content.rail.CartSpeed {
@Unique private double astra$speed;
@Inject(method="getMaxSpeed",at=@At("RETURN"),cancellable=true)
private void astra$limit(ServerLevel level,CallbackInfoReturnable<Double> cir) {
double limit=astracontent$limit(level);
if(limit>0)cir.setReturnValue(limit);
@Inject(method = "getMaxSpeed", at = @At("RETURN"), cancellable = true)
private void astra$limit(ServerLevel level, CallbackInfoReturnable<Double> cir) {
double limit = astracontent$limit(level);
if (limit > 0) cir.setReturnValue(limit);
}
@Override public double astracontent$limit(ServerLevel level) {
var cart=(AbstractMinecart)(Object)this;
astra$speed=RailSpeed.target(level,cart.getCurrentBlockPosOrRailBelow(),astra$speed);
@Override
public double astracontent$limit(ServerLevel level) {
var cart = (AbstractMinecart) (Object) this;
astra$speed = RailSpeed.target(level, cart.getCurrentBlockPosOrRailBelow(), astra$speed);
// Preserve existing experimental gamerules until this cart reaches a regulator.
if(astra$speed==0) return 0;
double limit=RailSpeed.safeLimit(cart,level,astra$speed/20);
return limit*(cart.isInWater()?.5:1);
if (astra$speed == 0) return 0;
double limit = RailSpeed.safeLimit(cart, level, astra$speed / 20);
return limit * (cart.isInWater() ? .5 : 1);
}
@Inject(method="addAdditionalSaveData",at=@At("RETURN"))
private void astra$save(ValueOutput out,CallbackInfo ci) { if(astra$speed>0)out.putDouble("AstraContentSpeed",astra$speed); }
@Inject(method="readAdditionalSaveData",at=@At("RETURN"))
private void astra$read(ValueInput in,CallbackInfo ci) {
double value=in.getDoubleOr("AstraContentSpeed",0); astra$speed=Double.isFinite(value) && value>0?Math.clamp(value,2,30):0;
@Inject(method = "addAdditionalSaveData", at = @At("RETURN"))
private void astra$save(ValueOutput out, CallbackInfo ci) {
if (astra$speed > 0) out.putDouble("AstraContentSpeed", astra$speed);
}
@Inject(method = "readAdditionalSaveData", at = @At("RETURN"))
private void astra$read(ValueInput in, CallbackInfo ci) {
double value = in.getDoubleOr("AstraContentSpeed", 0);
astra$speed = Double.isFinite(value) && value > 0 ? Math.clamp(value, 2, 30) : 0;
}
}
@@ -2,7 +2,9 @@ package online.astradev.content.mixin;
import net.minecraft.world.entity.projectile.EyeOfEnder;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.weapon.PayloadProjectile;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -10,8 +12,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(EyeOfEnder.class)
public abstract class PayloadMixin {
@Shadow private boolean surviveAfterDeath;
@Inject(method = "signalTo", at = @At("RETURN"))
private void astra$noDuplicate(Vec3 target, CallbackInfo ci) {
if (((EyeOfEnder)(Object)this).entityTags().contains(PayloadProjectile.NO_EYE_DROP)) surviveAfterDeath = false;
if (((EyeOfEnder) (Object) this).entityTags().contains(PayloadProjectile.NO_EYE_DROP))
surviveAfterDeath = false;
}
}
@@ -1,6 +1,5 @@
package online.astradev.content.mixin;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.LivingEntity;
@@ -10,26 +9,51 @@ import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import online.astradev.content.rail.Rails;
import org.spongepowered.asm.mixin.Mixin;
import java.util.List;
@Mixin(PoweredRailBlock.class)
public abstract class RailBlockMixin extends BaseRailBlock {
protected RailBlockMixin(boolean straight, Properties properties) { super(straight,properties); }
@Override public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level,pos,state,placer,stack);
if (state.is(Blocks.POWERED_RAIL) && level instanceof ServerLevel server) Rails.placed(server,pos,stack);
protected RailBlockMixin(boolean straight, Properties properties) {
super(straight, properties);
}
@Override protected List<ItemStack> getDrops(BlockState state, LootParams.Builder params) {
var drops = super.getDrops(state,params); var origin = params.getOptionalParameter(LootContextParams.ORIGIN);
@Override
public void setPlacedBy(
Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (state.is(Blocks.POWERED_RAIL) && level instanceof ServerLevel server)
Rails.placed(server, pos, stack);
}
@Override
protected List<ItemStack> getDrops(BlockState state, LootParams.Builder params) {
var drops = super.getDrops(state, params);
var origin = params.getOptionalParameter(LootContextParams.ORIGIN);
if (!state.is(Blocks.POWERED_RAIL) || origin == null) return drops;
var kind = Rails.loot(params.getLevel(),BlockPos.containing(origin));
return kind == null ? drops : drops.stream().map(stack -> stack.is(asItem()) ? Rails.stack(kind,stack.getCount()) : stack).toList();
var kind = Rails.loot(params.getLevel(), BlockPos.containing(origin));
return kind == null
? drops
: drops.stream()
.map(
stack ->
stack.is(asItem())
? Rails.stack(kind, stack.getCount())
: stack)
.toList();
}
@Override protected ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData) {
@Override
protected ItemStack getCloneItemStack(
LevelReader level, BlockPos pos, BlockState state, boolean includeData) {
if (level instanceof ServerLevel server && state.is(Blocks.POWERED_RAIL)) {
var kind=Rails.find(server,pos); if(kind!=null) return Rails.stack(kind,1);
var kind = Rails.find(server, pos);
if (kind != null) return Rails.stack(kind, 1);
}
return super.getCloneItemStack(level,pos,state,includeData);
return super.getCloneItemStack(level, pos, state, includeData);
}
}
@@ -1,16 +1,22 @@
package online.astradev.content.mixin;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import online.astradev.content.rail.Rails;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(LevelChunk.class)
public abstract class RailChunkMixin {
@Inject(method="setBlockState",at=@At("RETURN"))
private void astra$changed(BlockPos pos,BlockState state,int flags,CallbackInfoReturnable<BlockState> cir) {
if (((LevelChunk)(Object)this).getLevel() instanceof ServerLevel level) Rails.changed(level,pos,cir.getReturnValue(),state);
@Inject(method = "setBlockState", at = @At("RETURN"))
private void astra$changed(
BlockPos pos, BlockState state, int flags, CallbackInfoReturnable<BlockState> cir) {
if (((LevelChunk) (Object) this).getLevel() instanceof ServerLevel level)
Rails.changed(level, pos, cir.getReturnValue(), state);
}
}
@@ -1,29 +1,43 @@
package online.astradev.content.mixin;
import net.minecraft.world.entity.*;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.transport.SlimeBounce;
import online.astradev.content.train.Trains;
import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
import net.minecraft.world.level.block.BaseRailBlock;
import net.minecraft.world.phys.Vec3;
import online.astradev.content.train.Trains;
import online.astradev.content.transport.SlimeBounce;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(Entity.class)
public abstract class SlimeCollisionMixin {
@Unique private Vec3 astra$beforeMove;
@Unique private Vec3 astra$incoming;
@Inject(method="move",at=@At("HEAD"),cancellable=true)
private void astra$before(MoverType type,Vec3 delta,CallbackInfo ci){
var entity=(Entity)(Object)this;astra$beforeMove=type==MoverType.SELF&&SlimeBounce.supports(entity)?entity.position():null;
astra$incoming=entity.getDeltaMovement();
@Inject(method = "move", at = @At("HEAD"), cancellable = true)
private void astra$before(MoverType type, Vec3 delta, CallbackInfo ci) {
var entity = (Entity) (Object) this;
astra$beforeMove =
type == MoverType.SELF && SlimeBounce.supports(entity) ? entity.position() : null;
astra$incoming = entity.getDeltaMovement();
// Native powered rails can accelerate from zero within this same tick.
if(type==MoverType.SELF && entity instanceof AbstractMinecart cart && Trains.paused(cart)
&& cart.level().getBlockState(cart.getCurrentBlockPosOrRailBelow()).getBlock() instanceof BaseRailBlock){
astra$beforeMove=null;cart.setDeltaMovement(Vec3.ZERO);ci.cancel();
if (type == MoverType.SELF
&& entity instanceof AbstractMinecart cart
&& Trains.paused(cart)
&& cart.level().getBlockState(cart.getCurrentBlockPosOrRailBelow()).getBlock()
instanceof BaseRailBlock) {
astra$beforeMove = null;
cart.setDeltaMovement(Vec3.ZERO);
ci.cancel();
}
}
@Inject(method="move",at=@At("RETURN"))
private void astra$after(MoverType type,Vec3 delta,CallbackInfo ci){
if(astra$beforeMove!=null)SlimeBounce.afterMove((Entity)(Object)this,astra$beforeMove,delta,astra$incoming);
@Inject(method = "move", at = @At("RETURN"))
private void astra$after(MoverType type, Vec3 delta, CallbackInfo ci) {
if (astra$beforeMove != null)
SlimeBounce.afterMove((Entity) (Object) this, astra$beforeMove, delta, astra$incoming);
}
}
@@ -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;
}
}