Skip to content

Commit

Permalink
change: RailPlacement without updates
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Jul 20, 2023
1 parent 967209e commit 67dad8e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/main/java/mendedminecarts/RailPlacementHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mendedminecarts;

import net.minecraft.util.math.BlockPos;

public class RailPlacementHelper {
public static final ThreadLocal<BlockPos> NO_CONNECT_POS = new ThreadLocal<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/mendedminecarts.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"TntMinecartEntityMixin",
"boat.BoatEntityMixin",
"placement.AbstractRailBlockMixin",
"placement.DetectorRailBlockMixin",
"settings.ClientPlayNetworkHandlerMixin",
"settings.PlayerManagerMixin"
],
Expand Down

0 comments on commit 67dad8e

Please sign in to comment.