Prevent blocks destroyed by pistons from moving on Geyser-Spigot (#2627)

This commit is contained in:
David Choo 2021-11-12 20:36:01 -05:00 committed by GitHub
parent 09e3793fb2
commit adbadbbba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 26 deletions

View File

@ -110,8 +110,12 @@ public class GeyserPistonListener implements Listener {
List<Block> blocks = isExtend ? ((BlockPistonExtendEvent) event).getBlocks() : ((BlockPistonRetractEvent) event).getBlocks();
for (Block block : blocks) {
Location attachedLocation = block.getLocation();
attachedBlocks.put(getVector(attachedLocation), worldManager.getBlockNetworkId(player, block,
attachedLocation.getBlockX(), attachedLocation.getBlockY(), attachedLocation.getBlockZ()));
int blockId = worldManager.getBlockNetworkId(player, block,
attachedLocation.getBlockX(), attachedLocation.getBlockY(), attachedLocation.getBlockZ());
// Ignore blocks that will be destroyed
if (BlockStateValues.canPistonMoveBlock(blockId, isExtend)) {
attachedBlocks.put(getVector(attachedLocation), blockId);
}
}
blocksFilled = true;
}

View File

@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import it.unimi.dsi.fastutil.ints.*;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.geysermc.connector.network.translators.world.block.entity.PistonBlockEntityTranslator;
import org.geysermc.connector.registry.BlockRegistries;
import org.geysermc.connector.registry.type.BlockMapping;
import org.geysermc.connector.utils.Direction;
@ -335,6 +336,26 @@ public class BlockStateValues {
return BlockRegistries.JAVA_BLOCKS.getOrDefault(state, BlockMapping.AIR).getPistonBehavior() == PistonBehavior.DESTROY;
}
public static boolean canPistonMoveBlock(int javaId, boolean isPushing) {
if (javaId == JAVA_AIR_ID) {
return true;
}
// Pistons can only be moved if they aren't extended
if (PistonBlockEntityTranslator.isBlock(javaId)) {
return !PISTON_VALUES.get(javaId);
}
BlockMapping block = BlockRegistries.JAVA_BLOCKS.getOrDefault(javaId, BlockMapping.AIR);
// Bedrock, End portal frames, etc. can't be moved
if (block.getHardness() == -1.0d) {
return false;
}
return switch (block.getPistonBehavior()) {
case BLOCK, DESTROY -> false;
case PUSH_ONLY -> isPushing; // Glazed terracotta can only be pushed
default -> !block.isBlockEntity(); // Pistons can't move block entities
};
}
/**
* Skull variations are part of the namespaced ID in Java Edition, but part of the block entity tag in Bedrock.
* This gives a byte variant ID that Bedrock can use.

View File

@ -43,9 +43,7 @@ import org.geysermc.connector.network.translators.collision.BoundingBox;
import org.geysermc.connector.network.translators.collision.CollisionManager;
import org.geysermc.connector.network.translators.collision.translators.BlockCollision;
import org.geysermc.connector.network.translators.world.block.BlockStateValues;
import org.geysermc.connector.registry.BlockRegistries;
import org.geysermc.connector.registry.Registries;
import org.geysermc.connector.registry.type.BlockMapping;
import org.geysermc.connector.utils.*;
import java.util.LinkedList;
@ -256,7 +254,7 @@ public class PistonBlockEntity {
if (blockId == BlockStateValues.JAVA_AIR_ID) {
continue;
}
if (canMoveBlock(blockId, action == PistonValueType.PUSHING)) {
if (BlockStateValues.canPistonMoveBlock(blockId, action == PistonValueType.PUSHING)) {
attachedBlocks.put(blockPos, blockId);
if (BlockStateValues.isBlockSticky(blockId)) {
// For honey blocks and slime blocks check the blocks adjacent to it
@ -276,7 +274,7 @@ public class PistonBlockEntity {
continue;
}
int adjacentBlockId = session.getConnector().getWorldManager().getBlockAt(session, adjacentPos);
if (adjacentBlockId != BlockStateValues.JAVA_AIR_ID && BlockStateValues.isBlockAttached(blockId, adjacentBlockId) && canMoveBlock(adjacentBlockId, false)) {
if (adjacentBlockId != BlockStateValues.JAVA_AIR_ID && BlockStateValues.isBlockAttached(blockId, adjacentBlockId) && BlockStateValues.canPistonMoveBlock(adjacentBlockId, false)) {
// If it is another slime/honey block we need to check its adjacent blocks
if (BlockStateValues.isBlockSticky(adjacentBlockId)) {
blocksToCheck.add(adjacentPos);
@ -303,26 +301,6 @@ public class PistonBlockEntity {
}
}
private boolean canMoveBlock(int javaId, boolean isPushing) {
if (javaId == BlockStateValues.JAVA_AIR_ID) {
return true;
}
// Pistons can only be moved if they aren't extended
if (PistonBlockEntityTranslator.isBlock(javaId)) {
return !BlockStateValues.getPistonValues().get(javaId);
}
BlockMapping block = BlockRegistries.JAVA_BLOCKS.getOrDefault(javaId, BlockMapping.AIR);
// Bedrock, End portal frames, etc. can't be moved
if (block.getHardness() == -1.0d) {
return false;
}
return switch (block.getPistonBehavior()) {
case BLOCK, DESTROY -> false;
case PUSH_ONLY -> isPushing; // Glazed terracotta can only be pushed
default -> !block.isBlockEntity(); // Pistons can't move block entities
};
}
/**
* Get the unit vector for the direction of movement
*