Search in sources :

Example 6 with Position

use of com.github.steveice10.mc.protocol.data.game.entity.metadata.Position in project DragonProxy by DragonetMC.

the class ChunkCache method getBlock.

public final ItemStack getBlock(Position position) {
    ChunkPos columnPos = new ChunkPos(position.getX() >> 4, position.getZ() >> 4);
    if (chunkCache.containsKey(columnPos))
        try {
            Column column = chunkCache.get(columnPos);
            BlockPosition blockPos = columnPos.getBlockInChunk(position.getX(), position.getY(), position.getZ());
            Chunk chunk = column.getChunks()[position.getY() >> 4];
            if (chunk != null) {
                BlockState block = chunk.getBlocks().get(blockPos.x, blockPos.y, blockPos.z);
                return new ItemStack(block.getId(), 1, block.getData());
            }
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
            this.session.getProxy().getLogger().severe("(getBlock(" + position.toString() + ")) fail to get chunk " + (position.getX() >> 4) + "/" + (position.getY() >> 4) + "/" + (position.getZ() >> 4));
            ex.printStackTrace();
        }
    return null;
}
Also used : BlockState(com.github.steveice10.mc.protocol.data.game.world.block.BlockState) Column(com.github.steveice10.mc.protocol.data.game.chunk.Column) BlockPosition(org.dragonet.common.maths.BlockPosition) ChunkPos(org.dragonet.common.maths.ChunkPos) Chunk(com.github.steveice10.mc.protocol.data.game.chunk.Chunk) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)

Example 7 with Position

use of com.github.steveice10.mc.protocol.data.game.entity.metadata.Position in project DragonProxy by DragonetMC.

the class PEInventoryTransactionPacketTranslator method translate.

@Override
public Packet[] translate(UpstreamSession session, InventoryTransactionPacket packet) {
    // /!\ THIS IS A SIMPLE HANDLING WITHOUT JAVA DENY TRANSACTION
    switch(packet.transactionType) {
        case // 0 INVENTORY & CHEST
        InventoryTransactionPacket.TYPE_NORMAL:
            // System.out.println("TYPE_NORMAL");
            Slot cursor = null;
            if (// main inventory
            packet.actions.length <= 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_WORLD && packet.actions[1].containerId == ContainerId.INVENTORY.getId()) {
                // drop item
                ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.DROP_ITEM, new Position(0, 0, 0), BlockFace.DOWN);
                return new Packet[] { act };
            }
            // System.out.println("packet.actions[1].containerId " + packet.actions[1].containerId);
            if (packet.actions.length == 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_CONTAINER && packet.actions[1].containerId == ContainerId.CURSOR.getId()) {
                // desktop version: click on an item (maybe pick/place/merge/swap)
                // System.out.println("packet.actions[0].oldItem " + packet.actions[0].oldItem);
                // System.out.println("packet.actions[0].newItem " + packet.actions[0].newItem);
                // System.out.println("packet.actions[1].oldItem " + packet.actions[1].oldItem);
                // System.out.println("packet.actions[1].newItem " + packet.actions[1].newItem);
                // set the cursor
                cursor = packet.actions[0].oldItem;
                int windowID = 0;
                if (session.getDataCache().containsKey(CacheKey.CURRENT_WINDOW_ID))
                    windowID = (int) session.getDataCache().get(CacheKey.CURRENT_WINDOW_ID);
                int size = 0;
                if (session.getDataCache().containsKey(CacheKey.CURRENT_WINDOW_SIZE))
                    size = (int) session.getDataCache().get(CacheKey.CURRENT_WINDOW_SIZE);
                int slot = packet.actions[0].slotId;
                int slotBE = packet.actions[0].slotId;
                if (windowID == 0) {
                    // Player inventory, no window
                    if (// Main Inventory
                    packet.actions[0].containerId == ContainerId.INVENTORY.getId())
                        if (// hotbar
                        slot < 9)
                            slot += size;
                    if (packet.actions[0].containerId == ContainerId.ARMOR.getId())
                        slot += 5;
                    if (// Crafting grid, not sure why 2 ids
                    packet.actions[0].containerId == -2 || packet.actions[0].containerId == -3)
                        slot += 1;
                } else {
                    if (packet.actions[0].containerId == ContainerId.INVENTORY.getId()) {
                        // if 0, source is player inv
                        if (// Top Inventory
                        slot >= 9)
                            slot -= 9;
                        else
                            // Hotbar offset
                            slot += size + 27;
                    }
                }
                // System.out.println("interact in chest (" + packet.actions[0].containerId + ") slot JE: " + slot + " BE:" + slotBE);
                // send action to server
                ClientWindowActionPacket windowActionPacket = new ClientWindowActionPacket(// window id
                windowID, // transaction id
                session.getWindowCache().currentTransactionId.incrementAndGet(), // slot
                slot, ItemBlockTranslator.translateToPC(cursor), WindowAction.CLICK_ITEM, ClickItemParam.LEFT_CLICK);
                // System.out.println("WINDOWACTIONPACKET \n" + DebugTools.getAllFields(windowActionPacket));
                session.getDownstream().send(windowActionPacket);
            // after the previous one, we can detect SHIFT click or move items on mobile devices
            // if (packet.actions.length == 2 && packet.actions[0].sourceType == InventoryTransactionAction.SOURCE_CONTAINER && packet.actions[1].sourceType == InventoryTransactionAction.SOURCE_CONTAINER)
            // mobile version: move item
            // desktop version: SHIFT-click item
            // System.out.println("Move item from " + packet.actions[0].containerId + " slot " + packet.actions[0].slotId
            // + " to " + packet.actions[1].containerId + " slot " + packet.actions[1].slotId);
            }
            // it's okay to return null
            return null;
        case // 1
        InventoryTransactionPacket.TYPE_MISMATCH:
            // System.out.println("TYPE_MISMATCH");
            break;
        case // 2
        InventoryTransactionPacket.TYPE_USE_ITEM:
            // System.out.println("TYPE_USE_ITEM");
            UseItemData useItemData = (UseItemData) packet.transactionData;
            if (useItemData.itemInHand.id == 346) {
                AddEntityPacket pk = new AddEntityPacket();
                return new Packet[] { new ClientPlayerUseItemPacket(Hand.MAIN_HAND) };
            }
            if (useItemData.blockPos.equals(new BlockPosition(0, 0, 0)))
                return new Packet[] { new ClientPlayerUseItemPacket(Hand.MAIN_HAND) };
            switch(useItemData.actionType) {
                case // 2
                InventoryTransactionPacket.USE_ITEM_ACTION_BREAK_BLOCK:
                    {
                        ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.START_DIGGING, new Position(useItemData.blockPos.x, useItemData.blockPos.y, useItemData.blockPos.z), MagicValues.key(BlockFace.class, useItemData.face));
                        session.getDataCache().put(CacheKey.BLOCK_BREAKING_POSITION, act.getPosition());
                        return new Packet[] { act };
                    }
                // 0
                case InventoryTransactionPacket.USE_ITEM_ACTION_CLICK_BLOCK:
                case // 1
                InventoryTransactionPacket.USE_ITEM_ACTION_CLICK_AIR:
                    {
                        ClientPlayerPlaceBlockPacket placePacket = new ClientPlayerPlaceBlockPacket(new Position(useItemData.blockPos.x, useItemData.blockPos.y, useItemData.blockPos.z), ItemBlockTranslator.translateToPC(useItemData.face), Hand.MAIN_HAND, useItemData.clickPos.x, useItemData.clickPos.y, useItemData.clickPos.z);
                        return new Packet[] { placePacket, new ClientPlayerSwingArmPacket(Hand.MAIN_HAND) };
                    }
            }
        case // 3
        InventoryTransactionPacket.TYPE_USE_ITEM_ON_ENTITY:
            // System.out.println("TYPE_USE_ITEM_ON_ENTITY");
            UseItemOnEntityData useItemOnEntityData = (UseItemOnEntityData) packet.transactionData;
            CachedEntity cachedEntity = session.getEntityCache().getByLocalEID(useItemOnEntityData.entityRuntimeId);
            if (cachedEntity == null)
                return null;
            InteractAction interractAction = InteractAction.INTERACT;
            if (useItemOnEntityData.actionType == InventoryTransactionPacket.USE_ITEM_ON_ENTITY_ACTION_ATTACK)
                interractAction = InteractAction.ATTACK;
            ClientPlayerInteractEntityPacket interractPacket = new ClientPlayerInteractEntityPacket((int) cachedEntity.eid, interractAction);
            return new Packet[] { interractPacket };
        case // 4
        InventoryTransactionPacket.TYPE_RELEASE_ITEM:
            // System.out.println("TYPE_RELEASE_ITEM");
            if (((ReleaseItemData) packet.transactionData).actionType == 0)
                return new Packet[] { new ClientPlayerUseItemPacket(Hand.MAIN_HAND), new ClientPlayerActionPacket(PlayerAction.RELEASE_USE_ITEM, new Position(0, 0, 0), BlockFace.DOWN) };
            ReleaseItemData releaseItemData = (ReleaseItemData) packet.transactionData;
    }
    return null;
}
Also used : ClientWindowActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket) ClientPlayerUseItemPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket) ClientPlayerInteractEntityPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket) AddEntityPacket(org.dragonet.protocol.packets.AddEntityPacket) ClientPlayerSwingArmPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerSwingArmPacket) InventoryTransactionPacket(org.dragonet.protocol.packets.InventoryTransactionPacket) ClientPlayerActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket) Packet(com.github.steveice10.packetlib.packet.Packet) ClientWindowActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket) ClientPlayerPlaceBlockPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPlaceBlockPacket) Position(com.github.steveice10.mc.protocol.data.game.entity.metadata.Position) BlockPosition(org.dragonet.common.maths.BlockPosition) ClientPlayerSwingArmPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerSwingArmPacket) AddEntityPacket(org.dragonet.protocol.packets.AddEntityPacket) BlockPosition(org.dragonet.common.maths.BlockPosition) CachedEntity(org.dragonet.proxy.network.cache.CachedEntity) ReleaseItemData(org.dragonet.protocol.type.transaction.data.ReleaseItemData) UseItemData(org.dragonet.protocol.type.transaction.data.UseItemData) ClientPlayerInteractEntityPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket) UseItemOnEntityData(org.dragonet.protocol.type.transaction.data.UseItemOnEntityData) InteractAction(com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction) ClientPlayerUseItemPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket) Slot(org.dragonet.common.data.inventory.Slot) ClientPlayerActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket) ClientPlayerPlaceBlockPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPlaceBlockPacket)

Example 8 with Position

use of com.github.steveice10.mc.protocol.data.game.entity.metadata.Position in project DragonProxy by DragonetMC.

the class PEPlayerActionPacketTranslator method translate.

public Packet[] translate(UpstreamSession session, PlayerActionPacket packet) {
    if (packet.action == PlayerActionPacket.ACTION_RESPAWN) {
        return new Packet[] { new ClientRequestPacket(ClientRequest.RESPAWN) };
    }
    if (packet.action == PlayerActionPacket.ACTION_START_SPRINT) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.START_SPRINTING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_STOP_SPRINT) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.STOP_SPRINTING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_START_GLIDE) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.START_ELYTRA_FLYING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_STOP_GLIDE) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.START_ELYTRA_FLYING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_START_SNEAK) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.START_SNEAKING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_STOP_SNEAK) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.STOP_SNEAKING);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_STOP_SLEEPING) {
        ClientPlayerStatePacket stat = new ClientPlayerStatePacket((int) session.getDataCache().get(CacheKey.PLAYER_EID), PlayerState.LEAVE_BED);
        return new Packet[] { stat };
    }
    if (packet.action == PlayerActionPacket.ACTION_DROP_ITEM) {
        ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.DROP_ITEM, new Position(0, 0, 0), BlockFace.UP);
        return new Packet[] { act };
    }
    if (packet.action == PlayerActionPacket.ACTION_START_BREAK) {
        ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.START_DIGGING, new Position(packet.position.x, packet.position.y, packet.position.z), MagicValues.key(BlockFace.class, packet.face));
        session.getDataCache().put(CacheKey.BLOCK_BREAKING_POSITION, act.getPosition());
        return new Packet[] { act };
    }
    if (session.getDataCache().containsKey(CacheKey.BLOCK_BREAKING_POSITION)) {
        if (packet.action == PlayerActionPacket.ACTION_STOP_BREAK) {
            ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.FINISH_DIGGING, (Position) session.getDataCache().remove(CacheKey.BLOCK_BREAKING_POSITION), MagicValues.key(BlockFace.class, packet.face));
            return new Packet[] { act };
        }
        if (packet.action == PlayerActionPacket.ACTION_ABORT_BREAK) {
            ClientPlayerActionPacket act = new ClientPlayerActionPacket(com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction.CANCEL_DIGGING, (Position) session.getDataCache().remove(CacheKey.BLOCK_BREAKING_POSITION), MagicValues.key(BlockFace.class, packet.face));
            return new Packet[] { act };
        }
    }
    return null;
}
Also used : ClientPlayerActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket) ClientRequestPacket(com.github.steveice10.mc.protocol.packet.ingame.client.ClientRequestPacket) ClientPlayerStatePacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerStatePacket) PlayerActionPacket(org.dragonet.protocol.packets.PlayerActionPacket) Packet(com.github.steveice10.packetlib.packet.Packet) Position(com.github.steveice10.mc.protocol.data.game.entity.metadata.Position) BlockFace(com.github.steveice10.mc.protocol.data.game.world.block.BlockFace) ClientRequestPacket(com.github.steveice10.mc.protocol.packet.ingame.client.ClientRequestPacket) ClientPlayerActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket) ClientPlayerStatePacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerStatePacket)

Example 9 with Position

use of com.github.steveice10.mc.protocol.data.game.entity.metadata.Position in project DragonProxy by DragonetMC.

the class PEBlockPickRequestPacketTranslator method translate.

@Override
public Packet[] translate(UpstreamSession session, BlockPickRequestPacket packet) {
    ItemStack item = session.getChunkCache().getBlock(new Position(packet.x, packet.y, packet.z));
    int selectedSlot = (int) session.getDataCache().getOrDefault(CacheKey.PLAYER_SELECTED_SLOT, 36);
    ClientCreativeInventoryActionPacket backPacket = new ClientCreativeInventoryActionPacket(selectedSlot + 36, item);
    // System.out.println("BlockPickRequestPacket " + DebugTools.getAllFields(packet));
    // System.out.println("ItemStack " + DebugTools.getAllFields(item));
    session.getChunkCache().getDebugGrid();
    return new Packet[] { backPacket };
}
Also used : Packet(com.github.steveice10.packetlib.packet.Packet) PlayerSkinPacket(org.dragonet.protocol.packets.PlayerSkinPacket) ClientCreativeInventoryActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket) BlockPickRequestPacket(org.dragonet.protocol.packets.BlockPickRequestPacket) Position(com.github.steveice10.mc.protocol.data.game.entity.metadata.Position) ClientCreativeInventoryActionPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)

Example 10 with Position

use of com.github.steveice10.mc.protocol.data.game.entity.metadata.Position in project DragonProxy by DragonetMC.

the class PEMovePlayerPacketTranslator method translate.

public Packet[] translate(UpstreamSession session, MovePlayerPacket packet) {
    CachedEntity entity = session.getEntityCache().getClientEntity();
    if (entity.riding != 0 && packet.ridingRuntimeId != 0) {
        // Riding case
        ClientVehicleMovePacket pk = new ClientVehicleMovePacket(packet.position.x, packet.position.y - EntityType.PLAYER.getOffset(), packet.position.z, packet.yaw, packet.pitch);
        session.getDownstream().send(pk);
    } else {
        // not riding
        ClientPlayerPositionRotationPacket pk = new ClientPlayerPositionRotationPacket(packet.onGround, packet.position.x, // To simplify the movements
        ceilToHalf(packet.position.y - EntityType.PLAYER.getOffset()), packet.position.z, packet.yaw, packet.pitch);
        // Special blocks handling
        boolean isColliding = false;
        BlockPosition blockPos = new BlockPosition(NukkitMath.floorDouble(packet.position.x), NukkitMath.floorDouble(packet.position.y), NukkitMath.floorDouble(packet.position.z));
        // System.out.println("block " + blockPos.toString());
        try {
            ItemEntry PEBlock = session.getChunkCache().translateBlock(blockPos.asPosition());
            if (PEBlock != null) {
                Block b = Block.get(PEBlock.getId(), PEBlock.getPEDamage(), blockPos);
                if (b != null && b.collidesWithBB(session.getEntityCache().getClientEntity().boundingBox.clone())) {
                    DragonProxy.getInstance().getLogger().info("Player position : " + entity.x + ", " + entity.y + ", " + entity.z + " collide with " + b.getClass().getSimpleName() + " on " + blockPos.toString());
                    isColliding = true;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        entity.absoluteMove(pk.getX(), pk.getY(), pk.getZ(), (float) pk.getYaw(), (float) pk.getPitch());
        if (!isColliding) {
            session.getDownstream().send(pk);
            session.getChunkCache().sendOrderedChunks();
        }
    }
    return null;
}
Also used : ClientVehicleMovePacket(com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientVehicleMovePacket) CachedEntity(org.dragonet.proxy.network.cache.CachedEntity) BlockPosition(org.dragonet.common.maths.BlockPosition) ClientPlayerPositionRotationPacket(com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPositionRotationPacket) Block(org.dragonet.common.data.blocks.Block) ItemEntry(org.dragonet.common.data.itemsblocks.ItemEntry)

Aggregations

BlockPosition (org.dragonet.common.maths.BlockPosition)7 Position (com.github.steveice10.mc.protocol.data.game.entity.metadata.Position)5 Packet (com.github.steveice10.packetlib.packet.Packet)4 ChunkPos (org.dragonet.common.maths.ChunkPos)4 Chunk (com.github.steveice10.mc.protocol.data.game.chunk.Chunk)3 Column (com.github.steveice10.mc.protocol.data.game.chunk.Column)3 BlockState (com.github.steveice10.mc.protocol.data.game.world.block.BlockState)3 ClientPlayerActionPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket)3 CachedEntity (org.dragonet.proxy.network.cache.CachedEntity)3 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)2 ClientRequestPacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientRequestPacket)2 ClientPlayerUseItemPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket)2 ClientTeleportConfirmPacket (com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientTeleportConfirmPacket)2 ServerJoinGamePacket (com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket)2 PlayerListEntry (com.github.steveice10.mc.protocol.data.game.PlayerListEntry)1 EntityMetadata (com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata)1 InteractAction (com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction)1 BlockFace (com.github.steveice10.mc.protocol.data.game.world.block.BlockFace)1 ClientPluginMessagePacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientPluginMessagePacket)1 ClientSettingsPacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientSettingsPacket)1