Skip to content

Commit 2a30735

Browse files
committed
DestinyManifest and CollectionsManager
1 parent 17b6687 commit 2a30735

8 files changed

Lines changed: 218 additions & 5 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) dec4234 2021. Access is granted, without any express warranties or guarantees of
3+
* any kind, to all wishing to use this software for their benefit. No one may specifically claim credit, or
4+
* ownership of this software without the explicit permission of the author.
5+
*
6+
* GitHub -> https://github.com/dec4234/JavaDestinyAPI
7+
*/
8+
9+
package material.inventory;
10+
11+
import material.user.BungieUser;
12+
import utils.framework.ContentFramework;
13+
14+
public class CollectionsManager extends ContentFramework {
15+
16+
private BungieUser bungieUser;
17+
18+
public CollectionsManager(BungieUser bungieUser) {
19+
super("https://www.bungie.net/Platform/Destiny2/" + bungieUser.getMembershipType() + "/Profile/" + bungieUser.getBungieMembershipID() + "/?components=800", source -> {
20+
return source.getAsJsonObject("Response");
21+
});
22+
this.bungieUser = bungieUser;
23+
}
24+
25+
public boolean hasCollectedItem(String collectibleHash) {
26+
try {
27+
return getJO().getAsJsonObject("profileCollectibles").getAsJsonObject("data").getAsJsonObject("collectibles").getAsJsonObject(collectibleHash).get("state").getAsInt() == 0;
28+
} catch (NullPointerException e) {
29+
return false;
30+
}
31+
}
32+
33+
public boolean hasCollectedItem(DestinyItem destinyItem) {
34+
try {
35+
return getJO().getAsJsonObject("profileCollectibles").getAsJsonObject("data").getAsJsonObject("collectibles").getAsJsonObject(destinyItem.getCollectibleHash()).get("state").getAsInt() == 0;
36+
} catch (NullPointerException e) {
37+
return false;
38+
}
39+
}
40+
}

src/main/java/material/inventory/DestinyItem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public boolean hasIcon() {
9292
}
9393

9494
public String getCollectibleHash() {
95+
checkJO();
9596
if(jo.has("collectibleHash")) {
9697
collectibleHash = jo.get("collectibleHash").getAsString();
9798
}
@@ -100,6 +101,7 @@ public String getCollectibleHash() {
100101
}
101102

102103
public String getScreenshot() {
104+
checkJO();
103105
if(jo.has("screenshot") && screenshot == null) {
104106
screenshot = jo.get("screenshot").getAsString();
105107
}
@@ -159,6 +161,7 @@ public enum ItemTier {
159161
public static List<DestinyItem> searchForItems(String itemName) {
160162
HttpUtils httpUtils = new HttpUtils();
161163
List<DestinyItem> destinyItemList = new ArrayList<>();
164+
itemName = itemName.replace(" ", "%20");
162165

163166
for(JsonElement jsonElement : httpUtils.urlRequestGET("https://www.bungie.net/Platform/Destiny2/Armory/Search/DestinyInventoryItemDefinition/" + itemName + "/").getAsJsonObject("Response").getAsJsonObject("results").getAsJsonArray("results")) {
164167
JsonObject jsonObject = jsonElement.getAsJsonObject();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) dec4234 2021. Access is granted, without any express warranties or guarantees of
3+
* any kind, to all wishing to use this software for their benefit. No one may specifically claim credit, or
4+
* ownership of this software without the explicit permission of the author.
5+
*
6+
* GitHub -> https://github.com/dec4234/JavaDestinyAPI
7+
*/
8+
9+
package material.inventory;
10+
11+
import material.user.BungieUser;
12+
13+
public class InventoryManager {
14+
15+
private BungieUser bungieUser;
16+
17+
public InventoryManager(BungieUser bungieUser) {
18+
this.bungieUser = bungieUser;
19+
}
20+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) dec4234 2021. Access is granted, without any express warranties or guarantees of
3+
* any kind, to all wishing to use this software for their benefit. No one may specifically claim credit, or
4+
* ownership of this software without the explicit permission of the author.
5+
*
6+
* GitHub -> https://github.com/dec4234/JavaDestinyAPI
7+
*/
8+
9+
package material.manifest;
10+
11+
import com.google.gson.JsonObject;
12+
import utils.HttpUtils;
13+
import utils.framework.ContentFramework;
14+
15+
import java.util.HashMap;
16+
17+
public class DestinyManifest extends ContentFramework {
18+
19+
private static HashMap<String, JsonObject> worldComponents = new HashMap<>();
20+
21+
public DestinyManifest() {
22+
super("https://www.bungie.net/Platform/Destiny2/Manifest/", source -> {
23+
return source.getAsJsonObject("Response");
24+
});
25+
}
26+
27+
/**
28+
* A standard Manifest GET
29+
* Unlike the manifestGET in HttpUtils(), this will not make a request every single time
30+
*
31+
* It'll download the entire definition library the first time and it'll cache it
32+
*/
33+
public JsonObject manifestGET(ManifestEntityTypes manifestEntityTypes, String hash) {
34+
JsonObject jsonObject = getDefinitionLibrary(manifestEntityTypes);
35+
36+
if(jsonObject.has(hash)) {
37+
return jsonObject.getAsJsonObject(hash);
38+
}
39+
40+
return null;
41+
}
42+
43+
/**
44+
* Get the current version of the manifest
45+
* Useful for checking for updates
46+
*/
47+
public String getVersion() {
48+
return getJO().get("version").getAsString();
49+
}
50+
51+
public String getMobileAssetContentPath() {
52+
return getJO().get("mobileAssetContentPath").getAsString();
53+
}
54+
55+
public String getMobileWorldContentPath(Language language) {
56+
return getJO().getAsJsonObject("mobileWorldContentPaths").get(language.getCode()).getAsString();
57+
}
58+
59+
public String getJsonWorldContentPath(Language language) {
60+
return getJO().getAsJsonObject("jsonWorldContentPaths").get(language.getCode()).getAsString();
61+
}
62+
63+
public JsonObject getWorldContent(Language language) {
64+
return new HttpUtils().urlRequestGET("https://www.bungie.net" + getJsonWorldContentPath(language));
65+
}
66+
67+
/**
68+
* Get the entirety of the specified definition library
69+
*/
70+
public JsonObject getDefinitionLibrary(ManifestEntityTypes manifestEntityTypes) {
71+
Language language = Language.ENGLISH;
72+
73+
if(!worldComponents.containsKey(manifestEntityTypes.getBungieEntityValue())) {
74+
worldComponents.put(manifestEntityTypes.getBungieEntityValue(), new HttpUtils().urlRequestGET("https://www.bungie.net" + getJO().getAsJsonObject("jsonWorldComponentContentPaths").getAsJsonObject(language.getCode()).get(manifestEntityTypes.getBungieEntityValue()).getAsString()));
75+
}
76+
77+
return worldComponents.get(manifestEntityTypes.getBungieEntityValue());
78+
}
79+
80+
public JsonObject getDefinitionLibrary(Language language, ManifestEntityTypes manifestEntityTypes) {
81+
if(!worldComponents.containsKey(manifestEntityTypes.getBungieEntityValue())) {
82+
worldComponents.put(manifestEntityTypes.getBungieEntityValue(), new HttpUtils().urlRequestGET("https://www.bungie.net" + getJO().getAsJsonObject("jsonWorldComponentContentPaths").getAsJsonObject(language.getCode()).get(manifestEntityTypes.getBungieEntityValue()).getAsString()));
83+
}
84+
85+
return worldComponents.get(manifestEntityTypes.getBungieEntityValue());
86+
}
87+
88+
public String getMobileClanBannerDatabasePath() {
89+
return getJO().get("mobileClanBannerDatabasePath").getAsString();
90+
}
91+
92+
public enum Language {
93+
94+
ENGLISH("en"),
95+
FRENCH("fr"),
96+
SPANISH("es"),
97+
SPANISH_MEXICO("es-mx"),
98+
GERMAN("de"),
99+
ITALIAN("it"),
100+
JAPANESE("ja"),
101+
PORTUGEUSE_BRAZIL("pt-br"),
102+
RUSSIAN("ru"),
103+
KOREAN("ko"),
104+
ZH_CHT("zh-cht"),
105+
ZH_CHS("zh-chs");
106+
107+
String code;
108+
109+
private Language(String code) {
110+
this.code = code;
111+
}
112+
113+
public String getCode() {
114+
return code;
115+
}
116+
}
117+
}

src/main/java/material/stats/activities/ActivityHistoryReview.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import utils.HttpUtils;
1919
import utils.StringUtils;
2020

21-
import java.net.ConnectException;
2221
import java.util.*;
23-
import java.util.concurrent.ExecutionException;
2422

2523
public class ActivityHistoryReview {
2624

@@ -33,7 +31,7 @@ public LinkedHashMap<String, Activity> getMostUnrecentAttempts(Clan clan, Activi
3331

3432
for(BungieUser bungieUser : clan.getMembers()) {
3533
for(DestinyCharacter destinyCharacter : bungieUser.getCharacters()) {
36-
for (int i = 0; i < 25; i++) {
34+
for (int i = 0; i < Integer.MAX_VALUE; i++) {
3735
JsonObject jo = httpUtils.urlRequestGET("https://www.bungie.net/Platform/Destiny2/" + bungieUser.getMembershipType() + "/Account/" + bungieUser.getBungieMembershipID() + "/Character/" + destinyCharacter.getCharacterID() + "/Stats/Activities/?page=" + i + "&count=250&mode=" + activityIdentifier.getMode().getBungieValue());
3836

3937
if (jo == null || !jo.has("Response") || !jo.getAsJsonObject("Response").has("activities")) {

src/main/java/material/user/BungieUser.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.google.gson.JsonElement;
1313
import com.google.gson.JsonObject;
1414
import material.clan.Clan;
15+
import material.inventory.CollectionsManager;
16+
import material.inventory.InventoryManager;
1517
import material.stats.activities.ActivityInfo;
1618
import utils.HttpUtils;
1719
import utils.StringUtils;
@@ -43,6 +45,8 @@ public class BungieUser extends ContentFramework {
4345
private int playTime = -1, crossSaveOverride = -1, membershipType = -1;
4446
private boolean isPublic, isCrossSavePrimary, isOverridden = false;
4547
private int intendedPlatform = -2;
48+
private InventoryManager inventoryManager;
49+
private CollectionsManager collectionsManager;
4650

4751
private Clan clan;
4852

@@ -272,7 +276,7 @@ public void requestToJoinClan(Clan clan) {
272276
}
273277

274278
/**
275-
* Get the profile object to be used to pull most data
279+
* Get the Destiny profile object to be used to pull most data
276280
* Uses the preferred platform profile if it has been declared or
277281
* it will select the first profile in the profiles array
278282
*/
@@ -319,4 +323,20 @@ public void setIntendedPlatform(DestinyPlatform destinyPlatform) {
319323
intendedPlatform = destinyPlatform.getPlatformCode();
320324
je = null;
321325
}
326+
327+
public InventoryManager getInventoryManager() {
328+
if(inventoryManager == null) {
329+
inventoryManager = new InventoryManager(this);
330+
}
331+
332+
return inventoryManager;
333+
}
334+
335+
public CollectionsManager getCollectionsManager() {
336+
if(collectionsManager == null) {
337+
collectionsManager = new CollectionsManager(this);
338+
}
339+
340+
return collectionsManager;
341+
}
322342
}

src/main/java/utils/HttpUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ public String urlRequestPOSTOauth(String url, String body) {
7878
}
7979

8080
/**
81-
* Make a request to the Bungie manifest to reveal information about a hash-identified item
81+
* Make a request to the Bungie manifest to reveal information about a hash-identified item.
82+
*
83+
* Deprecated in favor of DestinyManifest#manifestGET()
8284
*/
85+
@Deprecated
8386
public JsonObject manifestGET(ManifestEntityTypes entityType, String hashIdentifier) {
8487
return urlRequestGET("https://www.bungie.net/Platform/Destiny2/Manifest/" + entityType.getBungieEntityValue() + "/" + hashIdentifier + "/");
8588
}
@@ -169,6 +172,7 @@ private String getStringResponse(HttpRequest httpRequest) {
169172
String responseString = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApplyAsync(HttpResponse::body).get();
170173

171174
if (DestinyAPI.isDebugEnabled()) {
175+
System.out.println(httpRequest.method() + " " + httpRequest.uri().toString());
172176
System.out.println(responseString);
173177
}
174178
return responseString;

src/main/java/utils/framework/ContentFramework.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public void checkJO() {
4444
}
4545
}
4646

47+
/**
48+
* Refresh the jsonobject to potentially account for new changes
49+
*/
50+
public void refreshJO() {
51+
if(manifestType == null) {
52+
jo = new HttpUtils().urlRequestGET(url);
53+
} else {
54+
jo = new HttpUtils().manifestGET(manifestType, url);
55+
}
56+
}
57+
4758
public JsonObject getJO() {
4859
checkJO();
4960
return jsonObjectModifier.modify(jo);

0 commit comments

Comments
 (0)