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);
}
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));
}
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;
}
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);
}
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();
}
Aggregations