Search in sources :

Example 1 with PistonValueType

use of com.github.steveice10.mc.protocol.data.game.level.block.value.PistonValueType in project Geyser by GeyserMC.

the class GeyserPistonListener method onPistonAction.

private void onPistonAction(BlockPistonEvent event) {
    if (event.isCancelled()) {
        return;
    }
    World world = event.getBlock().getWorld();
    boolean isExtend = event instanceof BlockPistonExtendEvent;
    Location location = event.getBlock().getLocation();
    Vector3i position = getVector(location);
    PistonValueType type = isExtend ? PistonValueType.PUSHING : PistonValueType.PULLING;
    boolean sticky = event.isSticky();
    Object2IntMap<Vector3i> attachedBlocks = new Object2IntOpenHashMap<>();
    boolean blocksFilled = false;
    for (Map.Entry<UUID, GeyserSession> entry : geyser.getSessionManager().getSessions().entrySet()) {
        Player player = Bukkit.getPlayer(entry.getKey());
        if (player == null || !player.getWorld().equals(world)) {
            continue;
        }
        GeyserSession session = entry.getValue();
        int dX = Math.abs(location.getBlockX() - player.getLocation().getBlockX()) >> 4;
        int dZ = Math.abs(location.getBlockZ() - player.getLocation().getBlockZ()) >> 4;
        if ((dX * dX + dZ * dZ) > session.getServerRenderDistance() * session.getServerRenderDistance()) {
            // Ignore pistons outside the player's render distance
            continue;
        }
        // being returned instead.
        if (!blocksFilled) {
            // Blocks currently require a player for 1.12, so let's just leech off one player to get all blocks
            // and call it a day for the rest of the sessions (mostly to save on execution time)
            List<Block> blocks = isExtend ? ((BlockPistonExtendEvent) event).getBlocks() : ((BlockPistonRetractEvent) event).getBlocks();
            for (Block block : blocks) {
                Location attachedLocation = block.getLocation();
                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;
        }
        int pistonBlockId = worldManager.getBlockNetworkId(player, event.getBlock(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
        // event.getDirection() is unreliable
        Direction orientation = BlockStateValues.getPistonOrientation(pistonBlockId);
        session.executeInEventLoop(() -> {
            PistonCache pistonCache = session.getPistonCache();
            PistonBlockEntity blockEntity = pistonCache.getPistons().computeIfAbsent(position, pos -> new PistonBlockEntity(session, position, orientation, sticky, !isExtend));
            blockEntity.setAction(type, attachedBlocks);
        });
    }
}
Also used : Player(org.bukkit.entity.Player) GeyserSession(org.geysermc.geyser.session.GeyserSession) PistonCache(org.geysermc.geyser.session.cache.PistonCache) World(org.bukkit.World) Direction(org.geysermc.geyser.level.physics.Direction) BlockPistonExtendEvent(org.bukkit.event.block.BlockPistonExtendEvent) Vector3i(com.nukkitx.math.vector.Vector3i) PistonValueType(com.github.steveice10.mc.protocol.data.game.level.block.value.PistonValueType) Block(org.bukkit.block.Block) PistonBlockEntity(org.geysermc.geyser.translator.level.block.entity.PistonBlockEntity) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) UUID(java.util.UUID) Map(java.util.Map) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) Location(org.bukkit.Location)

Aggregations

PistonValueType (com.github.steveice10.mc.protocol.data.game.level.block.value.PistonValueType)1 Vector3i (com.nukkitx.math.vector.Vector3i)1 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 BlockPistonExtendEvent (org.bukkit.event.block.BlockPistonExtendEvent)1 Direction (org.geysermc.geyser.level.physics.Direction)1 GeyserSession (org.geysermc.geyser.session.GeyserSession)1 PistonCache (org.geysermc.geyser.session.cache.PistonCache)1 PistonBlockEntity (org.geysermc.geyser.translator.level.block.entity.PistonBlockEntity)1