Search in sources :

Example 1 with PEInventorySlot

use of org.dragonet.inventory.PEInventorySlot in project Dragonet-Legacy by DragonetMC.

the class WindowItemsPacket method encode.

@Override
public void encode() {
    try {
        setChannel(NetworkChannel.CHANNEL_PRIORITY);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        writer.writeByte(this.windowID);
        writer.writeShort((short) (this.slots.length & 0xFFFF));
        for (PEInventorySlot slot : this.slots) {
            PEInventorySlot.writeSlot(writer, slot);
        }
        if (windowID == PEWindowConstantID.PLAYER_INVENTORY && this.hotbar.length > 0) {
            writer.writeShort((short) (this.hotbar.length & 0xFFFF));
            for (int slot : this.hotbar) {
                writer.writeInt(slot);
            }
        } else {
            writer.writeShort((short) 0);
        }
        this.setData(bos.toByteArray());
    } catch (IOException e) {
    }
}
Also used : PEBinaryWriter(org.dragonet.utilities.io.PEBinaryWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PEInventorySlot(org.dragonet.inventory.PEInventorySlot)

Example 2 with PEInventorySlot

use of org.dragonet.inventory.PEInventorySlot in project Dragonet-Legacy by DragonetMC.

the class EntityEquipmentMessageTranslator method handleSpecific.

@Override
public PEPacket[] handleSpecific(EntityEquipmentMessage packet) {
    if (this.getSession().getPlayer() == null) {
        return null;
    }
    if (!(this.getSession().getPlayer().getWorld().getEntityManager().getEntity(packet.id) instanceof Player)) {
        // Only process player's equipments for now.
        return null;
    }
    PlayerEquipmentPacket pkEquipment = new PlayerEquipmentPacket();
    pkEquipment.eid = packet.id;
    pkEquipment.slot = -1;
    pkEquipment.selectedSlot = packet.slot;
    if (packet.stack != null) {
        pkEquipment.item = new PEInventorySlot((short) (packet.stack.getTypeId() & 0xFFFF), (byte) (packet.stack.getAmount() & 0xFF), (short) (packet.stack.getDurability() & 0xFFFF));
    } else {
        pkEquipment.item = new PEInventorySlot();
    }
    return new PEPacket[] { pkEquipment };
}
Also used : Player(org.bukkit.entity.Player) PEPacket(org.dragonet.net.packet.minecraft.PEPacket) PlayerEquipmentPacket(org.dragonet.net.packet.minecraft.PlayerEquipmentPacket) PEInventorySlot(org.dragonet.inventory.PEInventorySlot)

Example 3 with PEInventorySlot

use of org.dragonet.inventory.PEInventorySlot in project Dragonet-Legacy by DragonetMC.

the class SetWindowContentsMessageTranslator method handleSpecific.

@Override
public PEPacket[] handleSpecific(SetWindowContentsMessage packet) {
    if (packet.id == 0) {
        // Inventory Items(Included hotbar)
        WindowItemsPacket pkInventory = new WindowItemsPacket();
        pkInventory.windowID = PEWindowConstantID.PLAYER_INVENTORY;
        pkInventory.slots = new PEInventorySlot[InventoryType.SlotSize.PLAYER];
        for (int i = 9; i <= 44; i++) {
            if (packet.items[i] != null) {
                pkInventory.slots[i - 9] = new PEInventorySlot((short) (packet.items[i].getTypeId() & 0xFFFF), (byte) (packet.items[i].getAmount() & 0xFF), packet.items[i].getDurability());
            } else {
                pkInventory.slots[i - 9] = new PEInventorySlot();
            }
        }
        pkInventory.hotbar = new int[9];
        for (int i = 36; i <= 44; i++) {
            pkInventory.hotbar[i - 36] = i - 9;
        }
        // Armor
        WindowItemsPacket pkArmorInv = new WindowItemsPacket();
        pkArmorInv.windowID = PEWindowConstantID.PLAYER_ARMOR;
        pkArmorInv.slots = new PEInventorySlot[4];
        for (int i = 5; i <= 8; i++) {
            if (packet.items[i] != null) {
                pkArmorInv.slots[i - 5] = new PEInventorySlot((short) (packet.items[i].getTypeId() & 0xFFFF), (byte) (packet.items[i].getAmount() & 0xFF), packet.items[i].getDurability());
            } else {
                pkArmorInv.slots[i - 5] = new PEInventorySlot();
            }
        }
        return new PEPacket[] { pkInventory, pkArmorInv };
    }
    // TODO: Implement other types of inventory
    // switch(this.getSession().getPlayer().)
    System.out.println("Updating window content for " + packet.id + ", which has " + packet.items.length + " slots. ");
    return null;
}
Also used : WindowItemsPacket(org.dragonet.net.packet.minecraft.WindowItemsPacket) PEPacket(org.dragonet.net.packet.minecraft.PEPacket) PEInventorySlot(org.dragonet.inventory.PEInventorySlot)

Example 4 with PEInventorySlot

use of org.dragonet.inventory.PEInventorySlot in project Dragonet-Legacy by DragonetMC.

the class EntityMetadataMessageTranslator method handleSpecific.

@Override
public PEPacket[] handleSpecific(EntityMetadataMessage packet) {
    if (!this.getTranslator().cachedEntityIDs.contains(packet.id)) {
        // Not spawned yet, let's create them
        if (this.getTranslator().cachedSpawnObjects.containsKey(packet.id)) {
            // Spawn it :)
            SpawnObjectMessage msgObj = this.getTranslator().cachedSpawnObjects.get(packet.id);
            switch(msgObj.type) {
                case // Dropped Item
                2:
                    AddItemEntityPacket pkAddItemEntity = new AddItemEntityPacket();
                    pkAddItemEntity.eid = msgObj.id;
                    pkAddItemEntity.item = new PEInventorySlot((short) (((ItemStack) packet.entries.get(0).value).getTypeId() & 0xFFFF), (byte) (((ItemStack) packet.entries.get(0).value).getAmount() & 0xFF), (short) (((ItemStack) packet.entries.get(0).value).getDurability() & 0xFFFF));
                    pkAddItemEntity.x = (float) msgObj.velX / 32;
                    pkAddItemEntity.y = (float) msgObj.velY / 32;
                    pkAddItemEntity.z = (float) msgObj.velZ / 32;
                    return new PEPacket[] { pkAddItemEntity };
            }
            // Remove it
            this.getTranslator().cachedSpawnObjects.remove(packet.id);
        } else {
            return null;
        }
    }
    return null;
}
Also used : SpawnObjectMessage(net.glowstone.net.message.play.entity.SpawnObjectMessage) AddItemEntityPacket(org.dragonet.net.packet.minecraft.AddItemEntityPacket) PEPacket(org.dragonet.net.packet.minecraft.PEPacket) PEInventorySlot(org.dragonet.inventory.PEInventorySlot)

Example 5 with PEInventorySlot

use of org.dragonet.inventory.PEInventorySlot in project Dragonet-Legacy by DragonetMC.

the class UseItemPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(UseItemPacket packet) {
    UseItemPacket pkUseItem = (UseItemPacket) packet;
    System.out.println("FACE=" + (pkUseItem.face & 0xFF) + ", ITEM=" + packet.item);
    if (pkUseItem.face == 0xFF) {
        // Air touch
        // Left click air
        PlayerSwingArmMessage msg = new PlayerSwingArmMessage();
        return new Message[] { msg };
    }
    if (!(pkUseItem.face >= 0 && pkUseItem.face < 6)) {
        return null;
    }
    // Check the slot
    ItemStack test_holding = this.getSession().getPlayer().getInventory().getItemInHand();
    if (packet.item.id != this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) || packet.item.meta != test_holding.getDurability()) {
        // Not same, resend slot
        PlayerEquipmentPacket pkRet = new PlayerEquipmentPacket();
        pkRet.eid = this.getSession().getPlayer().getEntityId();
        pkRet.item = new PEInventorySlot((short) 0, (byte) 0, (short) 0);
        pkRet.item.id = (short) (this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) & 0xFFFF);
        pkRet.item.count = (byte) (test_holding.getAmount() & 0xFF);
        pkRet.item.meta = test_holding.getDurability();
        pkRet.selectedSlot = this.getSession().getPlayer().getInventory().getHeldItemSlot();
        // Resend block
        UpdateBlockPacket pkUpdateBlock = new UpdateBlockPacket();
        UpdateBlockPacket.UpdateBlockRecord blockRec = new UpdateBlockPacket.UpdateBlockRecord();
        blockRec.x = packet.x;
        blockRec.z = packet.z;
        blockRec.y = (byte) (packet.y & 0xFF);
        blockRec.block = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getTypeId() & 0xFF);
        blockRec.meta = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getData() & 0xFF);
        pkUpdateBlock.records = new UpdateBlockPacket.UpdateBlockRecord[] { blockRec };
        getSession().send(pkRet);
        getSession().send(pkUpdateBlock);
        return null;
    }
    // Copied from Glowstone class BlockPlacementHandler
    new BlockPlacementHandler().handle(getSession(), new BlockPlacementMessage(packet.x, packet.y, packet.z, packet.face, test_holding, 0, 0, 0));
    return null;
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UseItemPacket(org.dragonet.net.packet.minecraft.UseItemPacket) Message(com.flowpowered.networking.Message) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) ItemStack(org.bukkit.inventory.ItemStack) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) PlayerEquipmentPacket(org.dragonet.net.packet.minecraft.PlayerEquipmentPacket) BlockPlacementHandler(net.glowstone.net.handler.play.player.BlockPlacementHandler) PEInventorySlot(org.dragonet.inventory.PEInventorySlot) UpdateBlockPacket(org.dragonet.net.packet.minecraft.UpdateBlockPacket)

Aggregations

PEInventorySlot (org.dragonet.inventory.PEInventorySlot)6 PEPacket (org.dragonet.net.packet.minecraft.PEPacket)3 PlayerEquipmentPacket (org.dragonet.net.packet.minecraft.PlayerEquipmentPacket)2 WindowItemsPacket (org.dragonet.net.packet.minecraft.WindowItemsPacket)2 Message (com.flowpowered.networking.Message)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 BlockPlacementHandler (net.glowstone.net.handler.play.player.BlockPlacementHandler)1 SpawnObjectMessage (net.glowstone.net.message.play.entity.SpawnObjectMessage)1 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)1 PlayerSwingArmMessage (net.glowstone.net.message.play.player.PlayerSwingArmMessage)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1 AddItemEntityPacket (org.dragonet.net.packet.minecraft.AddItemEntityPacket)1 UpdateBlockPacket (org.dragonet.net.packet.minecraft.UpdateBlockPacket)1 UseItemPacket (org.dragonet.net.packet.minecraft.UseItemPacket)1 PEBinaryWriter (org.dragonet.utilities.io.PEBinaryWriter)1