Skip to content

added support for incense activation #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 61 additions & 14 deletions src/main/java/com/pokegoapi/api/inventory/ItemBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.pokegoapi.api.inventory;

import POGOProtos.Inventory.Item.ItemDataOuterClass;
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage;
import POGOProtos.Networking.Requests.Messages.UseIncenseMessageOuterClass.UseIncenseMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass;
import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result;
import POGOProtos.Networking.Responses.UseIncenseResponseOuterClass.UseIncenseResponse;

import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import com.pokegoapi.util.Log;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -55,44 +57,48 @@ public void addItem(Item item) {
/**
* Remove item result.
*
* @param id the id
* @param quantity the quantity
* @param id
* the id
* @param quantity
* the quantity
* @return the result
* @throws RemoteServerException the remote server exception
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException
* the remote server exception
* @throws LoginFailedException
* the login failed exception
*/
public Result removeItem(ItemId id, int quantity) throws RemoteServerException, LoginFailedException {
Item item = getItem(id);
if (item.getCount() < quantity) {
throw new IllegalArgumentException("You cannont remove more quantity than you have");
}

RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder()
.setItemId(id)
.setCount(quantity)
RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity)
.build();

ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.RECYCLE_INVENTORY_ITEM, msg);
pgo.getRequestHandler().sendServerRequests(serverRequest);

RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse response;
try {
response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse
.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}

if (response.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
if (response
.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
item.setCount(response.getNewCount());
}
return response.getResult();
}


/**
* Gets item.
*
* @param type the type
* @param type
* the type
* @return the item
*/
public Item getItem(ItemId type) {
Expand All @@ -108,7 +114,6 @@ public Item getItem(ItemId type) {
return items.get(type);
}


public Collection<Item> getItems() {
return items.values();
}
Expand All @@ -125,4 +130,46 @@ public int getItemsCount() {
}
return ct;
}

public void useItem(ItemId type) throws RemoteServerException, LoginFailedException {
if (type == ItemId.UNRECOGNIZED) {
throw new IllegalArgumentException("You cannot use item for UNRECOGNIZED");
}

switch (type) {
case ITEM_INCENSE_ORDINARY:
case ITEM_INCENSE_SPICY:
case ITEM_INCENSE_COOL:
case ITEM_INCENSE_FLORAL:
useIncense(type);
break;
default:
break;
}
}

public void useIncense(ItemId type) throws RemoteServerException, LoginFailedException {
UseIncenseMessage useIncenseMessage =
UseIncenseMessage.newBuilder()
.setIncenseType(type)
.setIncenseTypeValue(type.getNumber())
.build();

ServerRequest useIncenseRequest = new ServerRequest(RequestTypeOuterClass.RequestType.USE_INCENSE,
useIncenseMessage);
pgo.getRequestHandler().sendServerRequests(useIncenseRequest);

UseIncenseResponse response = null;
try {
response = UseIncenseResponse.parseFrom(useIncenseRequest.getData());
Log.i("Main", "Use incense result: " + response.getResult());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
}

public void useIncense() throws RemoteServerException, LoginFailedException {
useIncense(ItemId.ITEM_INCENSE_ORDINARY);
}

}
65 changes: 65 additions & 0 deletions src/main/java/com/pokegoapi/examples/UseIncenseExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.pokegoapi.examples;



import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.auth.GoogleAutoCredentialProvider;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.util.Log;
import com.pokegoapi.util.SystemTimeImpl;

import okhttp3.OkHttpClient;

public class UseIncenseExample {

/**
* Catches a pokemon at an area.
*/
public static void main(String[] args) {
OkHttpClient http = new OkHttpClient();
try {
GoogleAutoCredentialProvider authProvider = new GoogleAutoCredentialProvider(http, ExampleLoginDetails.LOGIN, ExampleLoginDetails.PASSWORD);
//new PtcLogin(http).login(ExampleLoginDetails.LOGIN, ExampleLoginDetails.PASSWORD);
PokemonGo go = new PokemonGo(authProvider, http, new SystemTimeImpl());

go.setLocation(45.817521, 16.028199, 0);
go.getInventories().getItemBag().useIncense();

} catch (LoginFailedException | RemoteServerException e) {
// failed to login, invalid credentials, auth issue or server issue.
Log.e("Main", "Failed to login or server issue: ", e);

}
}
}