Search in sources :

Example 1 with UseItemData

use of org.dragonet.protocol.type.transaction.data.UseItemData in project DragonProxy by DragonetMC.

the class InventoryTransactionPacket method encodePayload.

@Override
public void encodePayload() {
    this.reset();
    this.putUnsignedVarInt(this.transactionType);
    this.putUnsignedVarInt(this.actions.length);
    for (InventoryTransactionAction action : this.actions) {
        action.write(this);
    }
    switch(this.transactionType) {
        case TYPE_NORMAL:
        case TYPE_MISMATCH:
            break;
        case TYPE_USE_ITEM:
            UseItemData useItemData = (UseItemData) this.transactionData;
            this.putUnsignedVarInt(useItemData.actionType);
            this.putBlockPosition(useItemData.blockPos);
            this.putVarInt(useItemData.face);
            this.putVarInt(useItemData.hotbarSlot);
            this.putSlot(useItemData.itemInHand);
            // TODO check
            this.putVector3F(useItemData.playerPos);
            this.putVector3F(useItemData.clickPos);
            break;
        case TYPE_USE_ITEM_ON_ENTITY:
            UseItemOnEntityData useItemOnEntityData = (UseItemOnEntityData) this.transactionData;
            this.putEntityRuntimeId(useItemOnEntityData.entityRuntimeId);
            this.putUnsignedVarInt(useItemOnEntityData.actionType);
            this.putVarInt(useItemOnEntityData.hotbarSlot);
            this.putSlot(useItemOnEntityData.itemInHand);
            this.putVector3F(useItemOnEntityData.playerPos);
            this.putVector3F(useItemOnEntityData.clickPos);
            break;
        case TYPE_RELEASE_ITEM:
            ReleaseItemData releaseItemData = (ReleaseItemData) this.transactionData;
            this.putUnsignedVarInt(releaseItemData.actionType);
            this.putVarInt(releaseItemData.hotbarSlot);
            this.putSlot(releaseItemData.itemInHand);
            this.putVector3F(releaseItemData.headRot);
            break;
        default:
            throw new RuntimeException("Unknown transaction type " + this.transactionType);
    }
}
Also used : ReleaseItemData(org.dragonet.protocol.type.transaction.data.ReleaseItemData) InventoryTransactionAction(org.dragonet.protocol.type.transaction.InventoryTransactionAction) UseItemData(org.dragonet.protocol.type.transaction.data.UseItemData) UseItemOnEntityData(org.dragonet.protocol.type.transaction.data.UseItemOnEntityData)

Example 2 with UseItemData

use of org.dragonet.protocol.type.transaction.data.UseItemData 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 3 with UseItemData

use of org.dragonet.protocol.type.transaction.data.UseItemData in project DragonProxy by DragonetMC.

the class InventoryTransactionPacket method decodePayload.

@Override
public void decodePayload() {
    this.transactionType = (int) this.getUnsignedVarInt();
    this.actions = new InventoryTransactionAction[(int) this.getUnsignedVarInt()];
    for (int i = 0; i < this.actions.length; i++) {
        this.actions[i] = new InventoryTransactionAction().read(this);
    }
    switch(this.transactionType) {
        case TYPE_NORMAL:
        case TYPE_MISMATCH:
            // Regular ComplexInventoryTransaction doesn't read any extra data
            break;
        case TYPE_USE_ITEM:
            UseItemData itemData = new UseItemData();
            itemData.actionType = (int) this.getUnsignedVarInt();
            itemData.blockPos = this.getBlockPosition();
            itemData.face = this.getVarInt();
            itemData.hotbarSlot = this.getVarInt();
            itemData.itemInHand = this.getSlot();
            itemData.playerPos = this.getVector3F();
            itemData.clickPos = this.getVector3F();
            this.transactionData = itemData;
            break;
        case TYPE_USE_ITEM_ON_ENTITY:
            UseItemOnEntityData useItemOnEntityData = new UseItemOnEntityData();
            useItemOnEntityData.entityRuntimeId = this.getEntityRuntimeId();
            useItemOnEntityData.actionType = (int) this.getUnsignedVarInt();
            useItemOnEntityData.hotbarSlot = this.getVarInt();
            useItemOnEntityData.itemInHand = this.getSlot();
            useItemOnEntityData.playerPos = this.getVector3F();
            useItemOnEntityData.clickPos = this.getVector3F();
            this.transactionData = useItemOnEntityData;
            break;
        case TYPE_RELEASE_ITEM:
            ReleaseItemData releaseItemData = new ReleaseItemData();
            releaseItemData.actionType = (int) getUnsignedVarInt();
            releaseItemData.hotbarSlot = getVarInt();
            releaseItemData.itemInHand = getSlot();
            releaseItemData.headRot = this.getVector3F();
            this.transactionData = releaseItemData;
            break;
        default:
            throw new RuntimeException("Unknown transaction type " + this.transactionType);
    }
}
Also used : ReleaseItemData(org.dragonet.protocol.type.transaction.data.ReleaseItemData) InventoryTransactionAction(org.dragonet.protocol.type.transaction.InventoryTransactionAction) UseItemData(org.dragonet.protocol.type.transaction.data.UseItemData) UseItemOnEntityData(org.dragonet.protocol.type.transaction.data.UseItemOnEntityData)

Aggregations

ReleaseItemData (org.dragonet.protocol.type.transaction.data.ReleaseItemData)3 UseItemData (org.dragonet.protocol.type.transaction.data.UseItemData)3 UseItemOnEntityData (org.dragonet.protocol.type.transaction.data.UseItemOnEntityData)3 InventoryTransactionAction (org.dragonet.protocol.type.transaction.InventoryTransactionAction)2 Position (com.github.steveice10.mc.protocol.data.game.entity.metadata.Position)1 InteractAction (com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction)1 ClientPlayerActionPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket)1 ClientPlayerInteractEntityPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket)1 ClientPlayerPlaceBlockPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPlaceBlockPacket)1 ClientPlayerSwingArmPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerSwingArmPacket)1 ClientPlayerUseItemPacket (com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket)1 ClientWindowActionPacket (com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket)1 Packet (com.github.steveice10.packetlib.packet.Packet)1 Slot (org.dragonet.common.data.inventory.Slot)1 BlockPosition (org.dragonet.common.maths.BlockPosition)1 AddEntityPacket (org.dragonet.protocol.packets.AddEntityPacket)1 InventoryTransactionPacket (org.dragonet.protocol.packets.InventoryTransactionPacket)1 CachedEntity (org.dragonet.proxy.network.cache.CachedEntity)1