Search in sources :

Example 1 with RecycleInventoryItemMessage

use of POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage in project PokeGOAPI-Java by Grover-c13.

the class ItemBag method removeItem.

/**
	 * Discards the given item.
	 *
	 * @param id the id
	 * @param quantity the quantity
	 * @return the result
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public Result removeItem(ItemId id, int quantity) throws RequestFailedException {
    Item item = getItem(id);
    if (item.getCount() < quantity) {
        throw new IllegalArgumentException("You cannot remove more quantity than you have");
    }
    RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.RECYCLE_INVENTORY_ITEM, msg);
    api.getRequestHandler().sendServerRequests(serverRequest);
    RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse response;
    try {
        response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    if (response.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
        item.setCount(response.getNewCount());
        if (item.getCount() <= 0) {
            removeItem(item.getItemId());
        }
    }
    return response.getResult();
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) RecycleInventoryItemResponseOuterClass(POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) RecycleInventoryItemMessage(POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage)

Aggregations

RecycleInventoryItemMessage (POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage)1 RecycleInventoryItemResponseOuterClass (POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1