Skip to content

Commit

Permalink
Always block movement outside world bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Apr 15, 2024
1 parent d2b701f commit b4ba2cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2022 Hugo Dupanloup (Yeregorix)
* Copyright (c) 2018-2024 Hugo Dupanloup (Yeregorix)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -98,13 +98,17 @@ public PistonStructure calculateStructure() {

@SuppressWarnings("ForLoopReplaceableByForEach")
protected boolean calculate(Vector3i origin) {
BlockState state = this.world.block(origin);
if (isPositionBlocked(origin))
return false;

BlockState state = this.world.block(origin);
MovementReaction reaction = getReaction(state, origin);

if (reaction == MovementReaction.DESTROY) {
this.toDestroy.add(origin);
return true;
}

if (reaction == MovementReaction.BLOCK)
return false;

Expand All @@ -121,15 +125,15 @@ protected boolean calculate(Vector3i origin) {
}

protected boolean addBlockLine(Vector3i origin, Direction dir) {
BlockState state = this.world.block(origin);

if (BlockUtil.isAir(state))
if (this.toMoveSet.contains(origin))
return true;

if (this.toMoveSet.contains(origin))
if (this.piston.position().equals(origin) || isPositionBlocked(origin))
return true;

if (this.piston.position().equals(origin))
BlockState state = this.world.block(origin);

if (BlockUtil.isAir(state))
return true;

MovementReaction reaction = getReaction(state, origin);
Expand All @@ -148,12 +152,13 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) {
Vector3i prevPos = pos;

pos = pos.sub(offset);
state = this.world.block(pos);

if (BlockUtil.isAir(state))
if (this.piston.position().equals(pos) || isPositionBlocked(pos))
break;

if (this.piston.position().equals(pos))
state = this.world.block(pos);

if (BlockUtil.isAir(state))
break;

if (!canStickToEachOther(prevState, prevPos, state, pos))
Expand Down Expand Up @@ -198,7 +203,7 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) {
if (BlockUtil.isAir(state))
return true;

if (this.piston.position().equals(pos))
if (this.piston.position().equals(pos) || isPositionBlocked(pos))
return false;

reaction = getReaction(state, pos);
Expand Down Expand Up @@ -242,6 +247,10 @@ protected void reorderListAtCollision(int start, int pivot) {
this.toMove.addAll(list);
}

protected boolean isPositionBlocked(Vector3i pos) {
return ReactionUtil.isPositionBlocked(this.world, pos, this.movement);
}

public boolean isSticky(BlockState state, Vector3i pos) {
return STICKY_BLOCKS.contains(state.type());
}
Expand All @@ -255,10 +264,10 @@ public boolean canStickToEachOther(BlockState state1, Vector3i pos1, BlockState
}

public MovementReaction getReaction(BlockState state, Vector3i pos) {
return ReactionUtil.getDefaultReaction(this.world, state, pos, this.movement);
return ReactionUtil.getDefaultReaction(state);
}

public static enum MovementReaction {
public enum MovementReaction {
NORMAL, DESTROY, BLOCK, PUSH_ONLY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package net.smoofyuniverse.superpiston.impl;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;

public class BlockUtil {

Expand All @@ -32,6 +31,6 @@ public static boolean hasBlockEntity(org.spongepowered.api.block.BlockState stat
}

public static boolean isAir(org.spongepowered.api.block.BlockState state) {
return ((BlockState) state).getMaterial() == Material.AIR;
return ((BlockState) state).isAir();
}
}
40 changes: 16 additions & 24 deletions src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.piston.PistonBaseBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.PushReaction;
import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator.MovementReaction;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.server.ServerWorld;
Expand All @@ -37,20 +36,24 @@

public class ReactionUtil {

public static MovementReaction getDefaultReaction(ServerWorld world, org.spongepowered.api.block.BlockState state, Vector3i pos, Direction movement) {
public static boolean isPositionBlocked(ServerWorld world, Vector3i pos, Direction movement) {
BlockPos blockPos = VecHelper.toBlockPos(pos);
ServerLevel level = (ServerLevel) world;

if (!level.getWorldBorder().isWithinBounds(blockPos))
return MovementReaction.BLOCK;
return true;

if (pos.y() < 0 || (movement == Direction.DOWN && pos.y() == 0))
return MovementReaction.BLOCK;
return true;

int h = level.getMaxBuildHeight() - 1;
if (pos.y() > h || (movement == Direction.UP && pos.y() == h))
return MovementReaction.BLOCK;
return true;

return false;
}

public static MovementReaction getDefaultReaction(org.spongepowered.api.block.BlockState state) {
if (BlockUtil.hasBlockEntity(state))
return MovementReaction.BLOCK;

Expand All @@ -61,26 +64,15 @@ public static MovementReaction getDefaultReaction(ServerWorld world, org.spongep

if (block == Blocks.OBSIDIAN || block == Blocks.CRYING_OBSIDIAN
|| block == Blocks.RESPAWN_ANCHOR || block == Blocks.REINFORCED_DEEPSLATE
|| nmsState.getDestroySpeed(level, blockPos) == -1.0f)
|| block.defaultDestroyTime() == -1.0f)
return MovementReaction.BLOCK;

return fromNMS(nmsState.getPistonPushReaction());
}

public static MovementReaction fromNMS(PushReaction reaction) {
switch (reaction) {
case NORMAL:
return MovementReaction.NORMAL;
case DESTROY:
return MovementReaction.DESTROY;
case BLOCK:
return MovementReaction.BLOCK;
case PUSH_ONLY:
return MovementReaction.PUSH_ONLY;
case IGNORE:
throw new IllegalArgumentException("IGNORE");
default:
throw new IllegalArgumentException();
}
return switch (nmsState.getPistonPushReaction()) {
case NORMAL -> MovementReaction.NORMAL;
case DESTROY -> MovementReaction.DESTROY;
case BLOCK -> MovementReaction.BLOCK;
case PUSH_ONLY -> MovementReaction.PUSH_ONLY;
case IGNORE -> throw new IllegalArgumentException("IGNORE");
};
}
}

0 comments on commit b4ba2cf

Please sign in to comment.