Declare copper rails through AstraPlus and simplify train state

This commit is contained in:
2026-09-13 23:17:48 +03:00
parent a43f56e25b
commit f590256777
146 changed files with 575 additions and 657 deletions
@@ -4,6 +4,7 @@ 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.CartSpeed;
import online.astradev.content.rail.RailSpeed;
import org.spongepowered.asm.mixin.*;
@@ -11,7 +12,7 @@ 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 {
public abstract class MinecartMixin implements CartSpeed {
@Unique private double astra$speed;
@Inject(method = "getMaxSpeed", at = @At("RETURN"), cancellable = true)
@@ -7,15 +7,11 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.*;
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) {
@@ -29,31 +25,4 @@ public abstract class RailBlockMixin extends BaseRailBlock {
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();
}
@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);
}
return super.getCloneItemStack(level, pos, state, includeData);
}
}
@@ -1,22 +0,0 @@
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);
}
}
@@ -1,6 +1,7 @@
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;
@@ -12,33 +13,17 @@ 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;
public abstract class TrainCartMixin {
@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);
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 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;
private void astra$read(ValueInput input, CallbackInfo ci) {
CouplingGeometry.load((AbstractMinecart) (Object) this, input);
}
@Inject(
@@ -47,19 +32,16 @@ public abstract class TrainCartMixin implements CartFrame {
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;
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
&& self.getDeltaMovement().subtract(astra$beforePlayerPush).horizontalDistanceSqr()
> 1e-12) Trains.playerPush(self);
if (astra$beforePlayerPush == null) return;
Vec3 impulse = self.getDeltaMovement().subtract(astra$beforePlayerPush);
astra$beforePlayerPush = null;
if (impulse.horizontalDistanceSqr() > 1e-12) Trains.playerPush(self);
}
}