use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class ItemOnItemVerificationHandlerTests method terminateWithNoTargetItem.
@Test
public void terminateWithNoTargetItem() throws Exception {
Player player = mock(Player.class);
Inventory inventory = new Inventory(28);
inventory.set(1, new Item(4151, 1));
when(player.getInventory()).thenReturn(inventory);
ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2);
itemOnItemVerificationHandler.handle(player, itemOnItemMessage);
assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid target item", itemOnItemMessage.terminated());
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class BankUtils method deposit.
/**
* Deposits an item into the player's bank.
*
* @param player The player.
* @param slot The slot.
* @param id The id.
* @param amount The amount.
* @return {@code false} if the chain should be broken.
*/
public static boolean deposit(Player player, int slot, int id, int amount) {
if (amount == 0) {
return true;
}
Inventory inventory = player.getInventory();
Inventory bank = player.getBank();
Item item = inventory.get(slot);
int newId = ItemDefinition.noteToItem(item.getId());
if (bank.freeSlots() == 0 && !bank.contains(item.getId())) {
bank.forceCapacityExceeded();
return true;
}
int removed;
if (amount > 1) {
inventory.stopFiringEvents();
}
try {
removed = inventory.remove(item.getId(), amount);
} finally {
if (amount > 1) {
inventory.startFiringEvents();
}
}
if (amount > 1) {
inventory.forceRefresh();
}
bank.add(newId, removed);
return true;
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class UpdateItemsMessageEncoder method encode.
@Override
public GamePacket encode(UpdateItemsMessage message) {
GamePacketBuilder builder = new GamePacketBuilder(53, PacketType.VARIABLE_SHORT);
Item[] items = message.getItems();
builder.put(DataType.SHORT, message.getInterfaceId());
builder.put(DataType.SHORT, items.length);
for (Item item : items) {
int id = item == null ? -1 : item.getId();
int amount = item == null ? 0 : item.getAmount();
if (amount > 254) {
builder.put(DataType.BYTE, 255);
builder.put(DataType.INT, DataOrder.INVERSED_MIDDLE, amount);
} else {
builder.put(DataType.BYTE, amount);
}
builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, id + 1);
}
return builder.toGamePacket();
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class ItemOnObjectVerificationHandlerTests method terminateIfObjectOutOfRange.
@Test
public void terminateIfObjectOutOfRange() throws Exception {
Position playerPosition = new Position(3200, 3200);
Position objectPosition = new Position(3200, 3200);
World world = mock(World.class);
Region region = mock(Region.class);
RegionRepository regionRepository = mock(RegionRepository.class);
Player player = mock(Player.class);
Set<Entity> entitySet = new HashSet<>();
entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0));
Inventory inventory = new Inventory(28);
inventory.set(1, new Item(4151, 1));
when(player.getInventory()).thenReturn(inventory);
when(world.getRegionRepository()).thenReturn(regionRepository);
when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
when(player.getPosition()).thenReturn(playerPosition);
when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, 1, objectPosition.getX(), objectPosition.getY());
ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world);
itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage);
assertTrue("ObjectVerificationHandler: message not terminated when object out of range!", itemOnObjectMessage.terminated());
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class Inventory method swap.
/**
* Swaps the two items at the specified slots.
*
* @param insert If the swap should be done in insertion mode.
* @param oldSlot The old slot.
* @param newSlot The new slot.
*/
public void swap(boolean insert, int oldSlot, int newSlot) {
checkBounds(oldSlot, newSlot);
if (insert) {
if (newSlot > oldSlot) {
for (int slot = oldSlot; slot < newSlot; slot++) {
swap(slot, slot + 1);
}
} else if (oldSlot > newSlot) {
for (int slot = oldSlot; slot > newSlot; slot--) {
swap(slot, slot - 1);
}
}
forceRefresh();
} else {
Item item = items[oldSlot];
items[oldSlot] = items[newSlot];
items[newSlot] = item;
notifyItemUpdated(oldSlot);
notifyItemUpdated(newSlot);
}
}
Aggregations