Search in sources :

Example 6 with Item

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

the class Inventory method addStackable.

/**
 * This method adds as much of the specified stackable {@code item} to this inventory as possible
 *
 * @param item The item to add to this inventory.
 * @param id The item's id
 * @return The item that may remain, if nothing remains, {@link Optional#empty an empty Optional} is returned.
 */
private Optional<Item> addStackable(Item item, int id) {
    int slot = slotOf(id);
    if (slot != -1) {
        Item other = items[slot];
        long total = (long) item.getAmount() + other.getAmount();
        int amount, remaining;
        if (total > Integer.MAX_VALUE) {
            amount = (int) (total - Integer.MAX_VALUE);
            remaining = (int) (total - amount);
            notifyCapacityExceeded();
        } else {
            amount = (int) total;
            remaining = 0;
        }
        set(slot, new Item(id, amount));
        return remaining > 0 ? Optional.of(new Item(id, remaining)) : Optional.empty();
    }
    for (slot = 0; slot < capacity; slot++) {
        if (items[slot] == null) {
            set(slot, item);
            return Optional.empty();
        }
    }
    notifyCapacityExceeded();
    return Optional.of(item);
}
Also used : Item(org.apollo.game.model.Item)

Example 7 with Item

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

the class Inventory method add.

/**
 * Attempts to add as much of the specified {@code item} to this inventory as possible. If any of the item remains,
 * an {@link Item item with the remainder} will be returned (in the case of stack-able items, any quantity that
 * remains in the stack is returned). If nothing remains, the method will return {@link Optional#empty an empty
 * Optional}.
 *
 * <p>
 * If anything remains at all, the listener will be notified which could be used for notifying a player that their
 * inventory is full, for example.
 *
 * @param item The item to add to this inventory.
 * @return The item that may remain, if nothing remains, {@link Optional#empty an empty Optional} is returned.
 */
public Optional<Item> add(Item item) {
    int id = item.getId();
    boolean stackable = isStackable(item.getDefinition());
    if (stackable) {
        return addStackable(item, id);
    }
    int remaining = item.getAmount();
    if (remaining == 0) {
        return Optional.empty();
    }
    stopFiringEvents();
    try {
        Item single = new Item(item.getId(), 1);
        for (int slot = 0; slot < capacity; slot++) {
            if (items[slot] == null) {
                // share the instances
                set(slot, single);
                if (--remaining <= 0) {
                    return Optional.empty();
                }
            }
        }
    } finally {
        startFiringEvents();
        if (remaining != item.getAmount()) {
            notifyItemsUpdated();
        }
    }
    notifyCapacityExceeded();
    return Optional.of(new Item(item.getId(), remaining));
}
Also used : Item(org.apollo.game.model.Item)

Example 8 with Item

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

the class Inventory method reset.

/**
 * Removes the item (if any) that is in the specified slot.
 *
 * @param slot The slot to reset.
 * @return The item that was in the slot.
 */
public Item reset(int slot) {
    checkBounds(slot);
    Item old = items[slot];
    if (old != null) {
        size--;
    }
    items[slot] = null;
    notifyItemUpdated(slot);
    return old;
}
Also used : Item(org.apollo.game.model.Item)

Example 9 with Item

use of org.apollo.game.model.Item 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)

Example 10 with Item

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

the class UpdateSlottedItemsMessageEncoder method encode.

@Override
public GamePacket encode(UpdateSlottedItemsMessage message) {
    GamePacketBuilder builder = new GamePacketBuilder(134, PacketType.VARIABLE_SHORT);
    SlottedItem[] items = message.getSlottedItems();
    builder.put(DataType.SHORT, message.getInterfaceId());
    for (SlottedItem slottedItem : items) {
        builder.putSmart(slottedItem.getSlot());
        Item item = slottedItem.getItem();
        int id = item == null ? -1 : item.getId();
        int amount = item == null ? 0 : item.getAmount();
        builder.put(DataType.SHORT, id + 1);
        if (amount > 254) {
            builder.put(DataType.BYTE, 255);
            builder.put(DataType.INT, amount);
        } else {
            builder.put(DataType.BYTE, amount);
        }
    }
    return builder.toGamePacket();
}
Also used : SlottedItem(org.apollo.game.model.inv.SlottedItem) Item(org.apollo.game.model.Item) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder) SlottedItem(org.apollo.game.model.inv.SlottedItem)

Aggregations

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