Search in sources :

Example 21 with Item

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

the class BinaryPlayerSerializer method writeInventory.

/**
 * Writes an inventory to the specified output stream.
 *
 * @param out The output stream.
 * @param inventory The inventory.
 * @throws IOException If an I/O error occurs.
 */
private void writeInventory(DataOutputStream out, Inventory inventory) throws IOException {
    int capacity = inventory.capacity();
    out.writeShort(capacity);
    for (int slot = 0; slot < capacity; slot++) {
        Item item = inventory.get(slot);
        if (item != null) {
            out.writeShort(item.getId() + 1);
            out.writeInt(item.getAmount());
        } else {
            out.writeShort(0);
            out.writeInt(0);
        }
    }
}
Also used : Item(org.apollo.game.model.Item)

Example 22 with Item

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

the class ItemVerificationHandler method handle.

@Override
public void handle(Player player, InventoryItemMessage message) {
    InventorySupplier supplier = inventories.get(message.getInterfaceId());
    if (supplier == null) {
        message.terminate();
        return;
    }
    Inventory inventory = supplier.getInventory(player);
    int slot = message.getSlot();
    if (inventory == null || slot < 0 || slot >= inventory.capacity()) {
        message.terminate();
        return;
    }
    Item item = inventory.get(slot);
    if (item == null || item.getId() != message.getId()) {
        message.terminate();
    }
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 23 with Item

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

the class ItemOnObjectVerificationHandler method handle.

@Override
public void handle(Player player, ItemOnObjectMessage message) {
    if (message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID && message.getInterfaceId() != BankConstants.SIDEBAR_INVENTORY_ID) {
        message.terminate();
        return;
    }
    Inventory inventory = player.getInventory();
    int slot = message.getSlot();
    if (slot < 0 || slot >= inventory.capacity()) {
        message.terminate();
        return;
    }
    Item item = inventory.get(slot);
    if (item == null || item.getId() != message.getId()) {
        message.terminate();
        return;
    }
    int objectId = message.getObjectId();
    if (objectId < 0 || objectId >= ObjectDefinition.count()) {
        message.terminate();
        return;
    }
    Position position = message.getPosition();
    Region region = world.getRegionRepository().fromPosition(position);
    Set<GameObject> objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT);
    if (!player.getPosition().isWithinDistance(position, 15) || !ObjectActionVerificationHandler.containsObject(objectId, objects)) {
        message.terminate();
        return;
    }
}
Also used : Item(org.apollo.game.model.Item) Position(org.apollo.game.model.Position) GameObject(org.apollo.game.model.entity.obj.GameObject) Region(org.apollo.game.model.area.Region) Inventory(org.apollo.game.model.inv.Inventory)

Example 24 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(34, 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