Skip to content

Commit

Permalink
Fix player placed rails trying to connect to partially connected stra…
Browse files Browse the repository at this point in the history
…ight rails
  • Loading branch information
2No2Name committed Jan 30, 2025
1 parent ca2e84c commit 3ac29c6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ minecraft_version=1.17
loader_version=0.16.9

# Mod Properties
mod_version=1.0.0
mod_version=1.1.0
maven_group=no2.railplacementfix
archives_base_name=rail-placement-fix
4 changes: 3 additions & 1 deletion src/main/java/no2/railplacementfix/RailPlacementFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class RailPlacementFix implements ModInitializer {
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

@Override
public static boolean EXPERIMENTAL_FEATURES = true;

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

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

public static final ThreadLocal<BlockPos> PLAYER_PLACED_POS = new ThreadLocal<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -25,6 +24,7 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import static no2.railplacementfix.common.RailPlacementHelper.NO_CONNECT_POS;
import static no2.railplacementfix.common.RailPlacementHelper.PLAYER_PLACED_POS;

@Mixin(BaseRailBlock.class)
public abstract class BaseRailBlockMixin extends Block implements SimpleWaterloggedBlock {
Expand All @@ -48,8 +48,9 @@ public BaseRailBlockMixin(Properties properties) {
), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD
)
private void getSmartPlacementState(BlockPlaceContext blockPlaceContext, CallbackInfoReturnable<BlockState> cir, boolean shouldWaterlog, BlockState defaultRailState, Direction placementDirection) {
BlockPos blockPos = blockPlaceContext.getClickedPos();
PLAYER_PLACED_POS.set(blockPos);
if (blockPlaceContext.getPlayer() != null && blockPlaceContext.getPlayer().isShiftKeyDown()) {
BlockPos blockPos = blockPlaceContext.getClickedPos();
Direction clickSide = blockPlaceContext.getClickedFace();

Vec3 clickLocation = blockPlaceContext.getClickLocation();
Expand Down Expand Up @@ -127,12 +128,14 @@ private BlockState cancelUpdates(BaseRailBlock instance, Level level, BlockPos b
BlockPos noUpdatePos = NO_CONNECT_POS.get();

if (noUpdatePos != null) {
NO_CONNECT_POS.set(null);
NO_CONNECT_POS.remove();
}

if (!blockPos.equals(noUpdatePos)) {
return this.updateDir(level, blockPos, blockState, bl);
}
PLAYER_PLACED_POS.remove();

return blockState;
}
}
49 changes: 49 additions & 0 deletions src/main/java/no2/railplacementfix/mixin/RailStateMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package no2.railplacementfix.mixin;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.RailState;
import no2.railplacementfix.RailPlacementFix;
import no2.railplacementfix.common.RailPlacementHelper;
import org.spongepowered.asm.mixin.Final;
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.CallbackInfoReturnable;

import java.util.List;

@Mixin(RailState.class)
public abstract class RailStateMixin {

@Shadow protected abstract boolean connectsTo(RailState railState);

@Shadow @Final private List<BlockPos> connections;

@Shadow @Final private boolean isStraight;

@Shadow public abstract List<BlockPos> getConnections();

@Shadow @Final private BlockPos pos;


@Inject(
method = "canConnectTo",
at = @At(
value = "INVOKE",
target = "Ljava/util/List;size()I"
), cancellable = true
)
private void canConnectTo(RailState railState, CallbackInfoReturnable<Boolean> cir) {
BlockPos neighborPos = ((RailStateMixin) (Object) railState).pos;
if (neighborPos.equals(RailPlacementHelper.PLAYER_PLACED_POS.get()) && this.isStraight) {
if (this.connections.size() == 1) {
BlockPos possible_connection = this.pos.offset(this.pos.subtract(this.connections.get(0)));
if (neighborPos.getX() != (possible_connection.getX()) ||
neighborPos.getZ() != (possible_connection.getZ())) {
cir.setReturnValue(false);
}
}
}
}
}
15 changes: 8 additions & 7 deletions src/main/resources/rail-placement-fix.mixins.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"required": true,
"package": "no2.railplacementfix.mixin",
"mixins": [
"BaseRailBlockMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"package": "no2.railplacementfix.mixin",
"mixins": [
"BaseRailBlockMixin",
"RailStateMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 3ac29c6

Please sign in to comment.