Search in sources :

Example 11 with Vector3i

use of com.nukkitx.math.vector.Vector3i in project Geyser by GeyserMC.

the class DoubleChestInventoryTranslator method prepareInventory.

@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
    // See BlockInventoryHolder - same concept there except we're also dealing with a specific block state
    if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) {
        int javaBlockId = session.getGeyser().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition());
        String[] javaBlockString = BlockRegistries.JAVA_IDENTIFIERS.get().getOrDefault(javaBlockId, "minecraft:air").split("\\[");
        if (javaBlockString.length > 1 && (javaBlockString[0].equals("minecraft:chest") || javaBlockString[0].equals("minecraft:trapped_chest")) && !javaBlockString[1].contains("type=single")) {
            inventory.setHolderPosition(session.getLastInteractionBlockPosition());
            ((Container) inventory).setUsingRealBlock(true, javaBlockString[0]);
            NbtMapBuilder tag = NbtMap.builder().putString("id", "Chest").putInt("x", session.getLastInteractionBlockPosition().getX()).putInt("y", session.getLastInteractionBlockPosition().getY()).putInt("z", session.getLastInteractionBlockPosition().getZ()).putString("CustomName", inventory.getTitle()).putString("id", "Chest");
            DoubleChestValue chestValue = BlockStateValues.getDoubleChestValues().get(javaBlockId);
            DoubleChestBlockEntityTranslator.translateChestValue(tag, chestValue, session.getLastInteractionBlockPosition().getX(), session.getLastInteractionBlockPosition().getZ());
            BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
            dataPacket.setData(tag.build());
            dataPacket.setBlockPosition(session.getLastInteractionBlockPosition());
            session.sendUpstreamPacket(dataPacket);
            return;
        }
    }
    Vector3i position = session.getPlayerEntity().getPosition().toInt().add(Vector3i.UP);
    Vector3i pairPosition = position.add(Vector3i.UNIT_X);
    int bedrockBlockId = session.getBlockMappings().getBedrockBlockId(defaultJavaBlockState);
    UpdateBlockPacket blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(position);
    blockPacket.setRuntimeId(bedrockBlockId);
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
    NbtMap tag = NbtMap.builder().putString("id", "Chest").putInt("x", position.getX()).putInt("y", position.getY()).putInt("z", position.getZ()).putInt("pairx", pairPosition.getX()).putInt("pairz", pairPosition.getZ()).putString("CustomName", inventory.getTitle()).build();
    BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
    dataPacket.setData(tag);
    dataPacket.setBlockPosition(position);
    session.sendUpstreamPacket(dataPacket);
    blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(pairPosition);
    blockPacket.setRuntimeId(bedrockBlockId);
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
    tag = NbtMap.builder().putString("id", "Chest").putInt("x", pairPosition.getX()).putInt("y", pairPosition.getY()).putInt("z", pairPosition.getZ()).putInt("pairx", position.getX()).putInt("pairz", position.getZ()).putString("CustomName", inventory.getTitle()).build();
    dataPacket = new BlockEntityDataPacket();
    dataPacket.setData(tag);
    dataPacket.setBlockPosition(pairPosition);
    session.sendUpstreamPacket(dataPacket);
    inventory.setHolderPosition(position);
}
Also used : Container(org.geysermc.geyser.inventory.Container) Vector3i(com.nukkitx.math.vector.Vector3i) NbtMap(com.nukkitx.nbt.NbtMap) DoubleChestValue(org.geysermc.geyser.level.block.DoubleChestValue) BlockEntityDataPacket(com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Example 12 with Vector3i

use of com.nukkitx.math.vector.Vector3i in project Geyser by GeyserMC.

the class ItemEntity method getDrag.

@Override
protected float getDrag() {
    if (onGround) {
        Vector3i groundBlockPos = position.toInt().down(1);
        int blockState = session.getGeyser().getWorldManager().getBlockAt(session, groundBlockPos);
        return BlockStateValues.getSlipperiness(blockState) * 0.98f;
    }
    return 0.98f;
}
Also used : Vector3i(com.nukkitx.math.vector.Vector3i)

Example 13 with Vector3i

use of com.nukkitx.math.vector.Vector3i 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)

Example 14 with Vector3i

use of com.nukkitx.math.vector.Vector3i in project Geyser by GeyserMC.

the class CollisionManager method checkPose.

/**
 * @param height check and see if this height is invalid in the current player position
 */
private boolean checkPose(float height) {
    Vector3i position = session.getPlayerEntity().getPosition().toInt();
    BlockCollision collision = BlockUtils.getCollisionAt(session, position);
    if (collision != null) {
        // Determine, if the player's bounding box *were* at full height, if it would intersect with the block
        // at the current location.
        double originalY = playerBoundingBox.getMiddleY();
        double originalHeight = playerBoundingBox.getSizeY();
        double standingY = originalY - (originalHeight / 2.0) + (height / 2.0);
        playerBoundingBox.setSizeY(EntityDefinitions.PLAYER.height());
        playerBoundingBox.setMiddleY(standingY);
        boolean result = collision.checkIntersection(position, playerBoundingBox);
        result |= session.getPistonCache().checkCollision(position, playerBoundingBox);
        playerBoundingBox.setSizeY(originalHeight);
        playerBoundingBox.setMiddleY(originalY);
        return result;
    }
    return false;
}
Also used : BlockCollision(org.geysermc.geyser.translator.collision.BlockCollision) Vector3i(com.nukkitx.math.vector.Vector3i)

Example 15 with Vector3i

use of com.nukkitx.math.vector.Vector3i in project Geyser by GeyserMC.

the class BlockInventoryHolder method closeInventory.

@Override
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    if (((Container) inventory).isUsingRealBlock()) {
        // No need to reset a block since we didn't change any blocks
        // But send a container close packet because we aren't destroying the original.
        ContainerClosePacket packet = new ContainerClosePacket();
        packet.setId((byte) inventory.getId());
        // TODO needs to be changed in Protocol to "server-side" or something
        packet.setUnknownBool0(true);
        session.sendUpstreamPacket(packet);
        return;
    }
    Vector3i holderPos = inventory.getHolderPosition();
    Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
    int realBlock = session.getGeyser().getWorldManager().getBlockAt(session, pos.getX(), pos.getY(), pos.getZ());
    UpdateBlockPacket blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(holderPos);
    blockPacket.setRuntimeId(session.getBlockMappings().getBedrockBlockId(realBlock));
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
}
Also used : Container(org.geysermc.geyser.inventory.Container) Position(com.github.steveice10.mc.protocol.data.game.entity.metadata.Position) ContainerClosePacket(com.nukkitx.protocol.bedrock.packet.ContainerClosePacket) Vector3i(com.nukkitx.math.vector.Vector3i) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Aggregations

Vector3i (com.nukkitx.math.vector.Vector3i)25 UpdateBlockPacket (com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)5 Vector3f (com.nukkitx.math.vector.Vector3f)4 Container (org.geysermc.geyser.inventory.Container)4 Position (com.github.steveice10.mc.protocol.data.game.entity.metadata.Position)3 NbtMap (com.nukkitx.nbt.NbtMap)3 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)3 StructureMirror (com.nukkitx.protocol.bedrock.data.structure.StructureMirror)3 StructureRotation (com.nukkitx.protocol.bedrock.data.structure.StructureRotation)3 StructureSettings (com.nukkitx.protocol.bedrock.data.structure.StructureSettings)3 ItemFrameEntity (org.geysermc.geyser.entity.type.ItemFrameEntity)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 BlockEntityDataPacket (com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket)2 ContainerClosePacket (com.nukkitx.protocol.bedrock.packet.ContainerClosePacket)2 LevelChunkPacket (com.nukkitx.protocol.bedrock.packet.LevelChunkPacket)2 LevelSoundEventPacket (com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket)2 BitStorage (com.github.steveice10.mc.protocol.data.game.chunk.BitStorage)1 ChunkSection (com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection)1 DataPalette (com.github.steveice10.mc.protocol.data.game.chunk.DataPalette)1 GlobalPalette (com.github.steveice10.mc.protocol.data.game.chunk.palette.GlobalPalette)1