diff --git a/src/main/java/mendedminecarts/RailPlacementHelper.java b/src/main/java/mendedminecarts/RailPlacementHelper.java new file mode 100644 index 0000000..7000ab5 --- /dev/null +++ b/src/main/java/mendedminecarts/RailPlacementHelper.java @@ -0,0 +1,8 @@ +package mendedminecarts; + +import net.minecraft.util.math.BlockPos; + +public class RailPlacementHelper { + public static final ThreadLocal NO_CONNECT_POS = new ThreadLocal<>(); + +} diff --git a/src/main/java/mendedminecarts/mixin/placement/AbstractRailBlockMixin.java b/src/main/java/mendedminecarts/mixin/placement/AbstractRailBlockMixin.java index 2990872..eaa7c24 100644 --- a/src/main/java/mendedminecarts/mixin/placement/AbstractRailBlockMixin.java +++ b/src/main/java/mendedminecarts/mixin/placement/AbstractRailBlockMixin.java @@ -19,11 +19,13 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import static mendedminecarts.RailPlacementHelper.NO_CONNECT_POS; + @Mixin(AbstractRailBlock.class) public abstract class AbstractRailBlockMixin extends Block implements Waterloggable { - @Shadow @Final public static BooleanProperty WATERLOGGED; @Shadow @Final private boolean forbidCurves; @@ -55,6 +57,8 @@ private void getSmartPlacementState(ItemPlacementContext ctx, CallbackInfoReturn boolean shouldCurve = (clickSide == Direction.UP || clickSide == Direction.DOWN); boolean isEastWest = railDirection == Direction.EAST || railDirection == Direction.WEST; + + boolean modified = false; RailShape railShape = isEastWest ? RailShape.EAST_WEST : RailShape.NORTH_SOUTH; if (shouldAscend) { if (railDirection == Direction.DOWN || railDirection == Direction.UP) { @@ -75,6 +79,7 @@ private void getSmartPlacementState(ItemPlacementContext ctx, CallbackInfoReturn } if (railShape1 != null && !shouldDropRail(blockPos, ctx.getWorld(), railShape1)) { railShape = railShape1; + modified = true; } } } else if (shouldCurve && !this.forbidCurves) { @@ -85,16 +90,37 @@ private void getSmartPlacementState(ItemPlacementContext ctx, CallbackInfoReturn //SOUTH AND EAST are positive. SOUTH is Z axis, EAST is X if (positiveX && positiveZ) { railShape = RailShape.SOUTH_EAST; + modified = true; } else if (positiveX && negativeZ) { railShape = RailShape.NORTH_EAST; + modified = true; } else if (negativeX && positiveZ) { railShape = RailShape.SOUTH_WEST; + modified = true; } else if (negativeX && negativeZ) { railShape = RailShape.NORTH_WEST; + modified = true; } } - retBlockState = retBlockState.with(this.getShapeProperty(), railShape); - cir.setReturnValue(retBlockState); + if (modified) { + retBlockState = retBlockState.with(this.getShapeProperty(), railShape); + cir.setReturnValue(retBlockState); + } + NO_CONNECT_POS.set(blockPos); + } + } + + @Inject( + method = "onBlockAdded", at = @At("HEAD"), cancellable = true + ) + private void cancelUpdates(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify, CallbackInfo ci) { + BlockPos noUpdatePos = NO_CONNECT_POS.get(); + if (pos.equals(noUpdatePos)) { + ci.cancel(); + } + + if (noUpdatePos != null) { + NO_CONNECT_POS.set(null); } } } diff --git a/src/main/java/mendedminecarts/mixin/placement/DetectorRailBlockMixin.java b/src/main/java/mendedminecarts/mixin/placement/DetectorRailBlockMixin.java new file mode 100644 index 0000000..fd67da0 --- /dev/null +++ b/src/main/java/mendedminecarts/mixin/placement/DetectorRailBlockMixin.java @@ -0,0 +1,34 @@ +package mendedminecarts.mixin.placement; + +import net.minecraft.block.BlockState; +import net.minecraft.block.DetectorRailBlock; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import static mendedminecarts.RailPlacementHelper.NO_CONNECT_POS; + +@Mixin(DetectorRailBlock.class) +public abstract class DetectorRailBlockMixin { + + @Shadow protected abstract void updatePoweredStatus(World world, BlockPos pos, BlockState state); + + @Inject( + method = "onBlockAdded", at = @At("HEAD"), cancellable = true + ) + private void cancelUpdates(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify, CallbackInfo ci) { + BlockPos noUpdatePos = NO_CONNECT_POS.get(); + if (pos.equals(noUpdatePos)) { + ci.cancel(); + this.updatePoweredStatus(world, pos, state); + } + + if (noUpdatePos != null) { + NO_CONNECT_POS.set(null); + } + } +} diff --git a/src/main/resources/mendedminecarts.mixins.json b/src/main/resources/mendedminecarts.mixins.json index 9522edf..b50a74d 100644 --- a/src/main/resources/mendedminecarts.mixins.json +++ b/src/main/resources/mendedminecarts.mixins.json @@ -21,6 +21,7 @@ "TntMinecartEntityMixin", "boat.BoatEntityMixin", "placement.AbstractRailBlockMixin", + "placement.DetectorRailBlockMixin", "settings.ClientPlayNetworkHandlerMixin", "settings.PlayerManagerMixin" ],