Search in sources :

Example 1 with PlayerEquipmentPacket

use of org.dragonet.net.packet.minecraft.PlayerEquipmentPacket 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 2 with PlayerEquipmentPacket

use of org.dragonet.net.packet.minecraft.PlayerEquipmentPacket in project Dragonet-Legacy by DragonetMC.

the class PlayerEquipmentPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(PlayerEquipmentPacket packet) {
    PlayerEquipmentPacket pkEquipment = (PlayerEquipmentPacket) packet;
    if (pkEquipment.slot == 0x28 || pkEquipment.slot == 0 || pkEquipment.slot == 255) {
        if (this.getSession().getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
            this.getSession().getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR));
            return null;
        } else {
            if (this.getSession().getPlayer().getInventory().firstEmpty() == -1) {
                this.getSession().sendInventory();
                this.getSession().getPlayer().sendMessage("Your inventory is full, you can't hold nothing. ");
            } else {
                this.getSession().getPlayer().getInventory().setHeldItemSlot(this.getSession().getPlayer().getInventory().firstEmpty());
            }
        }
        return null;
    }
    if (this.getSession().getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
        this.getSession().getPlayer().getInventory().setItemInHand(new ItemStack(pkEquipment.item.id, 1, pkEquipment.item.meta, (byte) 0));
        return null;
    }
    int slot = pkEquipment.slot - 9;
    if (slot < 27) {
        //Normal Inventory
        //Swap
        ItemStack item = this.getSession().getPlayer().getInventory().getItem(slot + 9);
        this.getSession().getPlayer().getInventory().setItem(slot + 9, this.getSession().getPlayer().getInventory().getItem(0));
        this.getSession().getPlayer().getInventory().setItem(0, item);
        if (item.getAmount() <= 0) {
            this.getSession().getPlayer().getInventory().setItem(slot + 9, null);
            this.getSession().sendInventory();
            return null;
        }
        if (item.getTypeId() == pkEquipment.item.id) {
            this.getSession().getPlayer().getInventory().setHeldItemSlot(slot + 9);
        }
        this.getSession().sendInventory();
    } else if (slot >= 27) {
        //Hotbar
        ItemStack item = this.getSession().getPlayer().getInventory().getItem(slot - 27);
        if (item.getAmount() <= 0) {
            this.getSession().getPlayer().getInventory().setItem(slot - 27, null);
            this.getSession().sendInventory();
            return null;
        }
        if (item.getTypeId() == pkEquipment.item.id) {
            this.getSession().getPlayer().getInventory().setHeldItemSlot(slot - 27);
        } else {
            this.getSession().sendInventory();
        }
    }
    return null;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) PlayerEquipmentPacket(org.dragonet.net.packet.minecraft.PlayerEquipmentPacket)

Example 3 with PlayerEquipmentPacket

use of org.dragonet.net.packet.minecraft.PlayerEquipmentPacket 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

PlayerEquipmentPacket (org.dragonet.net.packet.minecraft.PlayerEquipmentPacket)3 ItemStack (org.bukkit.inventory.ItemStack)2 PEInventorySlot (org.dragonet.inventory.PEInventorySlot)2 Message (com.flowpowered.networking.Message)1 BlockPlacementHandler (net.glowstone.net.handler.play.player.BlockPlacementHandler)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 PEPacket (org.dragonet.net.packet.minecraft.PEPacket)1 UpdateBlockPacket (org.dragonet.net.packet.minecraft.UpdateBlockPacket)1 UseItemPacket (org.dragonet.net.packet.minecraft.UseItemPacket)1