Skip to content

Commit 4726d04

Browse files
Karlo NovakGrover-c13
authored andcommitted
added support for incense activation (#314)
1 parent 21c60d3 commit 4726d04

File tree

2 files changed

+126
-14
lines changed

2 files changed

+126
-14
lines changed

src/main/java/com/pokegoapi/api/inventory/ItemBag.java

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1414
*/
1515

16-
1716
package com.pokegoapi.api.inventory;
1817

19-
import POGOProtos.Inventory.Item.ItemDataOuterClass;
2018
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
2119
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
2220
import POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage;
21+
import POGOProtos.Networking.Requests.Messages.UseIncenseMessageOuterClass.UseIncenseMessage;
2322
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
2423
import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass;
2524
import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result;
25+
import POGOProtos.Networking.Responses.UseIncenseResponseOuterClass.UseIncenseResponse;
26+
2627
import com.google.protobuf.InvalidProtocolBufferException;
2728
import com.pokegoapi.api.PokemonGo;
2829
import com.pokegoapi.exceptions.LoginFailedException;
2930
import com.pokegoapi.exceptions.RemoteServerException;
3031
import com.pokegoapi.main.ServerRequest;
32+
import com.pokegoapi.util.Log;
3133

3234
import java.util.Collection;
3335
import java.util.HashMap;
@@ -55,44 +57,48 @@ public void addItem(Item item) {
5557
/**
5658
* Remove item result.
5759
*
58-
* @param id the id
59-
* @param quantity the quantity
60+
* @param id
61+
* the id
62+
* @param quantity
63+
* the quantity
6064
* @return the result
61-
* @throws RemoteServerException the remote server exception
62-
* @throws LoginFailedException the login failed exception
65+
* @throws RemoteServerException
66+
* the remote server exception
67+
* @throws LoginFailedException
68+
* the login failed exception
6369
*/
6470
public Result removeItem(ItemId id, int quantity) throws RemoteServerException, LoginFailedException {
6571
Item item = getItem(id);
6672
if (item.getCount() < quantity) {
6773
throw new IllegalArgumentException("You cannont remove more quantity than you have");
6874
}
6975

70-
RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder()
71-
.setItemId(id)
72-
.setCount(quantity)
76+
RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity)
7377
.build();
7478

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

7882
RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse response;
7983
try {
80-
response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
84+
response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse
85+
.parseFrom(serverRequest.getData());
8186
} catch (InvalidProtocolBufferException e) {
8287
throw new RemoteServerException(e);
8388
}
8489

85-
if (response.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
90+
if (response
91+
.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
8692
item.setCount(response.getNewCount());
8793
}
8894
return response.getResult();
8995
}
9096

91-
9297
/**
9398
* Gets item.
9499
*
95-
* @param type the type
100+
* @param type
101+
* the type
96102
* @return the item
97103
*/
98104
public Item getItem(ItemId type) {
@@ -108,7 +114,6 @@ public Item getItem(ItemId type) {
108114
return items.get(type);
109115
}
110116

111-
112117
public Collection<Item> getItems() {
113118
return items.values();
114119
}
@@ -125,4 +130,46 @@ public int getItemsCount() {
125130
}
126131
return ct;
127132
}
133+
134+
public void useItem(ItemId type) throws RemoteServerException, LoginFailedException {
135+
if (type == ItemId.UNRECOGNIZED) {
136+
throw new IllegalArgumentException("You cannot use item for UNRECOGNIZED");
137+
}
138+
139+
switch (type) {
140+
case ITEM_INCENSE_ORDINARY:
141+
case ITEM_INCENSE_SPICY:
142+
case ITEM_INCENSE_COOL:
143+
case ITEM_INCENSE_FLORAL:
144+
useIncense(type);
145+
break;
146+
default:
147+
break;
148+
}
149+
}
150+
151+
public void useIncense(ItemId type) throws RemoteServerException, LoginFailedException {
152+
UseIncenseMessage useIncenseMessage =
153+
UseIncenseMessage.newBuilder()
154+
.setIncenseType(type)
155+
.setIncenseTypeValue(type.getNumber())
156+
.build();
157+
158+
ServerRequest useIncenseRequest = new ServerRequest(RequestTypeOuterClass.RequestType.USE_INCENSE,
159+
useIncenseMessage);
160+
pgo.getRequestHandler().sendServerRequests(useIncenseRequest);
161+
162+
UseIncenseResponse response = null;
163+
try {
164+
response = UseIncenseResponse.parseFrom(useIncenseRequest.getData());
165+
Log.i("Main", "Use incense result: " + response.getResult());
166+
} catch (InvalidProtocolBufferException e) {
167+
throw new RemoteServerException(e);
168+
}
169+
}
170+
171+
public void useIncense() throws RemoteServerException, LoginFailedException {
172+
useIncense(ItemId.ITEM_INCENSE_ORDINARY);
173+
}
174+
128175
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
/*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
29+
*/
30+
31+
package com.pokegoapi.examples;
32+
33+
34+
35+
import com.pokegoapi.api.PokemonGo;
36+
import com.pokegoapi.auth.GoogleAutoCredentialProvider;
37+
import com.pokegoapi.exceptions.LoginFailedException;
38+
import com.pokegoapi.exceptions.RemoteServerException;
39+
import com.pokegoapi.util.Log;
40+
import com.pokegoapi.util.SystemTimeImpl;
41+
42+
import okhttp3.OkHttpClient;
43+
44+
public class UseIncenseExample {
45+
46+
/**
47+
* Catches a pokemon at an area.
48+
*/
49+
public static void main(String[] args) {
50+
OkHttpClient http = new OkHttpClient();
51+
try {
52+
GoogleAutoCredentialProvider authProvider = new GoogleAutoCredentialProvider(http, ExampleLoginDetails.LOGIN, ExampleLoginDetails.PASSWORD);
53+
//new PtcLogin(http).login(ExampleLoginDetails.LOGIN, ExampleLoginDetails.PASSWORD);
54+
PokemonGo go = new PokemonGo(authProvider, http, new SystemTimeImpl());
55+
56+
go.setLocation(45.817521, 16.028199, 0);
57+
go.getInventories().getItemBag().useIncense();
58+
59+
} catch (LoginFailedException | RemoteServerException e) {
60+
// failed to login, invalid credentials, auth issue or server issue.
61+
Log.e("Main", "Failed to login or server issue: ", e);
62+
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)