34 lines
1.7 KiB
Java
34 lines
1.7 KiB
Java
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.*;
|
||
|
|
|
||
|
|
@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);
|
||
|
|
}
|
||
|
|
@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);
|
||
|
|
}
|
||
|
|
@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;
|
||
|
|
}
|
||
|
|
}
|