Search in sources :

Example 1 with Inventory

use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.

the class RemoveEquippedItemHandler method handle.

@Override
public void handle(Player player, ItemActionMessage message) {
    if (message.getOption() == 1 && message.getInterfaceId() == SynchronizationInventoryListener.EQUIPMENT_ID) {
        Inventory inventory = player.getInventory();
        Inventory equipment = player.getEquipment();
        int slot = message.getSlot();
        Item item = equipment.get(slot);
        int id = item.getId();
        if (inventory.freeSlots() == 0 && !item.getDefinition().isStackable()) {
            inventory.forceCapacityExceeded();
            message.terminate();
            return;
        }
        boolean removed = true;
        inventory.stopFiringEvents();
        equipment.stopFiringEvents();
        try {
            int remaining = inventory.add(id, item.getAmount());
            removed = remaining == 0;
            equipment.set(slot, removed ? null : new Item(id, remaining));
        } finally {
            inventory.startFiringEvents();
            equipment.startFiringEvents();
        }
        if (removed) {
            inventory.forceRefresh();
            equipment.forceRefresh(slot);
        } else {
            inventory.forceCapacityExceeded();
        }
    }
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 2 with Inventory

use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.

the class EquipItemHandler method handle.

@Override
public void handle(Player player, ItemOptionMessage message) {
    if (message.getInterfaceId() != INVENTORY_ID || message.getOption() != EQUIP_OPTION) {
        return;
    }
    int inventorySlot = message.getSlot();
    Inventory inventory = player.getInventory();
    Item equipping = inventory.get(inventorySlot);
    int equippingId = equipping.getId();
    EquipmentDefinition definition = EquipmentDefinition.lookup(equippingId);
    if (definition == null) {
        return;
    } else if (!hasRequirements(player, definition)) {
        message.terminate();
        return;
    }
    Inventory equipment = player.getEquipment();
    Item weapon = equipment.get(WEAPON);
    Item shield = equipment.get(SHIELD);
    if (definition.isTwoHanded()) {
        handleTwoHanded(inventory, equipment, inventorySlot, weapon, shield, player, message);
        return;
    }
    int equipmentSlot = definition.getSlot();
    if (equipmentSlot == SHIELD && weapon != null && EquipmentDefinition.lookup(weapon.getId()).isTwoHanded()) {
        equipment.set(SHIELD, inventory.reset(inventorySlot));
        inventory.add(equipment.reset(WEAPON));
        return;
    }
    Item current = equipment.get(equipmentSlot);
    if (current != null && current.getId() == equippingId && current.getDefinition().isStackable()) {
        long total = (long) current.getAmount() + equipping.getAmount();
        if (total <= Integer.MAX_VALUE) {
            equipment.set(equipmentSlot, new Item(equippingId, (int) total));
            inventory.set(inventorySlot, null);
        } else {
            equipment.set(equipmentSlot, new Item(equippingId, Integer.MAX_VALUE));
            inventory.set(inventorySlot, new Item(equippingId, (int) (total - Integer.MAX_VALUE)));
        }
    } else {
        inventory.set(inventorySlot, null);
        equipment.set(equipmentSlot, equipping);
        if (current != null) {
            inventory.set(inventorySlot, current);
        }
    }
}
Also used : Item(org.apollo.game.model.Item) EquipmentDefinition(org.apollo.cache.def.EquipmentDefinition) Inventory(org.apollo.game.model.inv.Inventory)

Example 3 with Inventory

use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.

the class ItemOnItemVerificationHandler method handle.

@Override
public void handle(Player player, ItemOnItemMessage message) {
    Inventory inventory;
    switch(message.getInterfaceId()) {
        case SynchronizationInventoryListener.INVENTORY_ID:
        case BankConstants.SIDEBAR_INVENTORY_ID:
            inventory = player.getInventory();
            break;
        case SynchronizationInventoryListener.EQUIPMENT_ID:
            inventory = player.getEquipment();
            break;
        case BankConstants.BANK_INVENTORY_ID:
            inventory = player.getBank();
            break;
        default:
            message.terminate();
            return;
    }
    int slot = message.getTargetSlot();
    if (slot < 0 || slot >= inventory.capacity()) {
        message.terminate();
        return;
    }
    Item item = inventory.get(slot);
    if (item == null || item.getId() != message.getTargetId()) {
        message.terminate();
    }
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 4 with Inventory

use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.

the class PlayerSynchronizationMessageEncoder method putAppearanceBlock.

/**
 * Puts an appearance block into the specified builder.
 *
 * @param block The block.
 * @param builder The builder.
 */
private static void putAppearanceBlock(AppearanceBlock block, GamePacketBuilder builder) {
    Appearance appearance = block.getAppearance();
    GamePacketBuilder playerProperties = new GamePacketBuilder();
    playerProperties.put(DataType.BYTE, appearance.getGender().toInteger());
    playerProperties.put(DataType.BYTE, 0);
    if (block.appearingAsNpc()) {
        playerProperties.put(DataType.BYTE, 255);
        playerProperties.put(DataType.BYTE, 255);
        playerProperties.put(DataType.SHORT, block.getNpcId());
    } else {
        Inventory equipment = block.getEquipment();
        int[] style = appearance.getStyle();
        Item item, chest, helm;
        for (int slot = 0; slot < 4; slot++) {
            if ((item = equipment.get(slot)) != null) {
                playerProperties.put(DataType.SHORT, 0x200 + item.getId());
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        }
        if ((chest = equipment.get(EquipmentConstants.CHEST)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + chest.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[2]);
        }
        if ((item = equipment.get(EquipmentConstants.SHIELD)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.BYTE, 0);
        }
        if (chest != null) {
            EquipmentDefinition def = EquipmentDefinition.lookup(chest.getId());
            if (def != null && !def.isFullBody()) {
                playerProperties.put(DataType.SHORT, 0x100 + style[3]);
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[3]);
        }
        if ((item = equipment.get(EquipmentConstants.LEGS)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[5]);
        }
        if ((helm = equipment.get(EquipmentConstants.HAT)) != null) {
            EquipmentDefinition def = EquipmentDefinition.lookup(helm.getId());
            if (def != null && !def.isFullHat() && !def.isFullMask()) {
                playerProperties.put(DataType.SHORT, 0x100 + style[0]);
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[0]);
        }
        if ((item = equipment.get(EquipmentConstants.HANDS)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[4]);
        }
        if ((item = equipment.get(EquipmentConstants.FEET)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[6]);
        }
        EquipmentDefinition def = null;
        if (helm != null) {
            def = EquipmentDefinition.lookup(helm.getId());
        }
        if (def != null && (def.isFullMask()) || appearance.getGender() == Gender.FEMALE) {
            playerProperties.put(DataType.BYTE, 0);
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[1]);
        }
    }
    int[] colors = appearance.getColors();
    for (int color : colors) {
        playerProperties.put(DataType.BYTE, color);
    }
    // stand
    playerProperties.put(DataType.SHORT, 0x328);
    // stand turn
    playerProperties.put(DataType.SHORT, 0x337);
    // walk
    playerProperties.put(DataType.SHORT, 0x333);
    // turn 180
    playerProperties.put(DataType.SHORT, 0x334);
    // turn 90 cw
    playerProperties.put(DataType.SHORT, 0x335);
    // turn 90 ccw
    playerProperties.put(DataType.SHORT, 0x336);
    // run
    playerProperties.put(DataType.SHORT, 0x338);
    playerProperties.put(DataType.LONG, block.getName());
    playerProperties.put(DataType.BYTE, block.getCombatLevel());
    playerProperties.put(DataType.SHORT, block.getSkillLevel());
    builder.put(DataType.BYTE, DataTransformation.NEGATE, playerProperties.getLength());
    builder.putRawBuilder(playerProperties);
}
Also used : Item(org.apollo.game.model.Item) EquipmentDefinition(org.apollo.cache.def.EquipmentDefinition) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) Appearance(org.apollo.game.model.Appearance) Inventory(org.apollo.game.model.inv.Inventory)

Example 5 with Inventory

use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.

the class PlayerSynchronizationMessageEncoder method putAppearanceBlock.

/**
 * Puts an Appearance block into the specified builder.
 *
 * @param block The block.
 * @param builder The builder.
 */
private static void putAppearanceBlock(AppearanceBlock block, GamePacketBuilder builder) {
    Appearance appearance = block.getAppearance();
    GamePacketBuilder playerProperties = new GamePacketBuilder();
    playerProperties.put(DataType.BYTE, appearance.getGender().toInteger());
    playerProperties.put(DataType.BYTE, block.isSkulled() ? 1 : -1);
    playerProperties.put(DataType.BYTE, block.getHeadIcon());
    if (block.appearingAsNpc()) {
        playerProperties.put(DataType.BYTE, 255);
        playerProperties.put(DataType.BYTE, 255);
        playerProperties.put(DataType.SHORT, block.getNpcId());
    } else {
        Inventory equipment = block.getEquipment();
        int[] style = appearance.getStyle();
        Item item, chest, helm;
        for (int slot = 0; slot < 4; slot++) {
            if ((item = equipment.get(slot)) != null) {
                playerProperties.put(DataType.SHORT, 0x200 + item.getId());
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        }
        if ((chest = equipment.get(EquipmentConstants.CHEST)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + chest.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[2]);
        }
        if ((item = equipment.get(EquipmentConstants.SHIELD)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.BYTE, 0);
        }
        if (chest != null) {
            EquipmentDefinition def = EquipmentDefinition.lookup(chest.getId());
            if (def != null && !def.isFullBody()) {
                playerProperties.put(DataType.SHORT, 0x100 + style[3]);
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[3]);
        }
        if ((item = equipment.get(EquipmentConstants.LEGS)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[5]);
        }
        if ((helm = equipment.get(EquipmentConstants.HAT)) != null) {
            EquipmentDefinition def = EquipmentDefinition.lookup(helm.getId());
            if (def != null && !def.isFullHat() && !def.isFullMask()) {
                playerProperties.put(DataType.SHORT, 0x100 + style[0]);
            } else {
                playerProperties.put(DataType.BYTE, 0);
            }
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[0]);
        }
        if ((item = equipment.get(EquipmentConstants.HANDS)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[4]);
        }
        if ((item = equipment.get(EquipmentConstants.FEET)) != null) {
            playerProperties.put(DataType.SHORT, 0x200 + item.getId());
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[6]);
        }
        EquipmentDefinition def = null;
        if (helm != null) {
            def = EquipmentDefinition.lookup(helm.getId());
        }
        if (def != null && (def.isFullMask()) || appearance.getGender() == Gender.FEMALE) {
            playerProperties.put(DataType.BYTE, 0);
        } else {
            playerProperties.put(DataType.SHORT, 0x100 + style[1]);
        }
    }
    int[] colors = appearance.getColors();
    for (int color : colors) {
        playerProperties.put(DataType.BYTE, color);
    }
    // stand
    playerProperties.put(DataType.SHORT, 0x328);
    // stand turn
    playerProperties.put(DataType.SHORT, 0x337);
    // walk
    playerProperties.put(DataType.SHORT, 0x333);
    // turn 180
    playerProperties.put(DataType.SHORT, 0x334);
    // turn 90 cw
    playerProperties.put(DataType.SHORT, 0x335);
    // turn 90 ccw
    playerProperties.put(DataType.SHORT, 0x336);
    // run
    playerProperties.put(DataType.SHORT, 0x338);
    playerProperties.put(DataType.LONG, block.getName());
    playerProperties.put(DataType.BYTE, block.getCombatLevel());
    playerProperties.put(DataType.SHORT, block.getSkillLevel());
    builder.put(DataType.BYTE, playerProperties.getLength());
    builder.putRawBuilderReverse(playerProperties);
}
Also used : Item(org.apollo.game.model.Item) EquipmentDefinition(org.apollo.cache.def.EquipmentDefinition) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) Appearance(org.apollo.game.model.Appearance) Inventory(org.apollo.game.model.inv.Inventory)

Aggregations

Inventory (org.apollo.game.model.inv.Inventory)14 Item (org.apollo.game.model.Item)11 Position (org.apollo.game.model.Position)4 Region (org.apollo.game.model.area.Region)4 Player (org.apollo.game.model.entity.Player)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HashSet (java.util.HashSet)3 EquipmentDefinition (org.apollo.cache.def.EquipmentDefinition)3 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 World (org.apollo.game.model.World)3 RegionRepository (org.apollo.game.model.area.RegionRepository)3 Entity (org.apollo.game.model.entity.Entity)3 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)3 Appearance (org.apollo.game.model.Appearance)2 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)2 ItemOnItemMessage (org.apollo.game.message.impl.ItemOnItemMessage)1 GameObject (org.apollo.game.model.entity.obj.GameObject)1