Skip to content

Commit fa69a54

Browse files
committed
Clean Up DestinyItem and introduce searchForItems()
1 parent a639228 commit fa69a54

File tree

2 files changed

+83
-27
lines changed

2 files changed

+83
-27
lines changed

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

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,114 @@
88

99
package material.inventory;
1010

11+
import com.google.gson.JsonElement;
1112
import com.google.gson.JsonObject;
1213
import material.manifest.ManifestEntityTypes;
1314
import utils.HttpUtils;
15+
import utils.framework.ContentInterface;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
1419

1520
/**
1621
* A Destiny Inventory Item such as any weapon or armor piece
1722
*/
18-
public class DestinyItem {
23+
public class DestinyItem implements ContentInterface {
1924

2025
HttpUtils hu = new HttpUtils();
2126

22-
private String hashID;
23-
private String name;
24-
private String icon;
25-
private String description;
27+
private String hashID, name, icon, description;
2628
private boolean hasIcon;
27-
private String collectibleHash;
28-
private String screenshot;
29-
private ItemTier itemType;
29+
private String collectibleHash, screenshot;
30+
private ItemTier itemTier;
3031

3132
private ItemTier tier;
3233

33-
private JsonObject jo;
34+
private JsonObject jo, dp;
3435

3536
public DestinyItem(String hashID) {
3637
this.hashID = hashID;
37-
jo = hu.manifestGET(ManifestEntityTypes.INVENTORYITEM, hashID).getAsJsonObject("Response");
38-
assignValues();
3938
}
4039

41-
private void assignValues() {
42-
JsonObject dp = jo.getAsJsonObject("displayProperties");
43-
name = dp.get("name").getAsString();
44-
description = dp.get("description").getAsString();
45-
icon = dp.get("icon").getAsString();
46-
hasIcon = dp.get("hasIcon").getAsBoolean();
47-
if(jo.has("collectibleHash")) {
48-
collectibleHash = jo.get("collectibleHash").getAsString();
49-
}
50-
51-
if(jo.has("screenshot")) {
52-
screenshot = jo.get("screenshot").getAsString();
53-
}
54-
itemType = assessItemTier();
40+
public DestinyItem(String hashID, String name, String icon, boolean hasIcon) {
41+
this.hashID = hashID;
42+
this.name = name;
43+
this.icon = icon;
44+
this.hasIcon = hasIcon;
5545
}
5646

5747
public String getHashID() {
48+
if(hashID == null) {
49+
checkJO();
50+
hashID = jo.get("hash").getAsString();
51+
}
5852
return hashID;
5953
}
6054

6155
/**
6256
* Gets the name of the item
6357
*/
6458
public String getName() {
59+
if(name == null) {
60+
checkDP();
61+
name = dp.get("name").getAsString();
62+
}
6563
return name;
6664
}
6765

6866
/**
6967
* Plug this after https://www.bungie.net/ in a browser
7068
*/
7169
public String getIcon() {
70+
if(icon == null) {
71+
checkDP();
72+
icon = dp.get("icon").getAsString();
73+
}
7274
return icon;
7375
}
7476

7577
/**
7678
* Gets the lore descriptions associated with this item
7779
*/
7880
public String getDescription() {
81+
if(description == null) {
82+
checkDP();
83+
description = dp.get("description").getAsString();
84+
}
7985
return description;
8086
}
8187

8288
public boolean hasIcon() {
89+
checkDP();
90+
hasIcon = dp.get("hasIcon").getAsBoolean();
8391
return hasIcon;
8492
}
8593

8694
public String getCollectibleHash() {
95+
if(jo.has("collectibleHash")) {
96+
collectibleHash = jo.get("collectibleHash").getAsString();
97+
}
98+
8799
return collectibleHash;
88100
}
89101

102+
public String getScreenshot() {
103+
if(jo.has("screenshot") && screenshot == null) {
104+
screenshot = jo.get("screenshot").getAsString();
105+
}
106+
107+
return screenshot;
108+
}
109+
110+
public ItemTier getItemTier() {
111+
if(itemTier == null) {
112+
itemTier = assessItemTier();
113+
}
114+
return itemTier;
115+
}
116+
90117
public ItemTier assessItemTier() {
118+
checkJO();
91119
switch(jo.getAsJsonObject("inventory").get("tierTypeName").getAsString()) {
92120
case "Common":
93121
return ItemTier.COMMON;
@@ -103,11 +131,41 @@ public ItemTier assessItemTier() {
103131
return null;
104132
}
105133

134+
@Override
135+
public void checkJO() {
136+
if(jo == null) {
137+
jo = hu.manifestGET(ManifestEntityTypes.INVENTORYITEM, hashID).getAsJsonObject("Response");
138+
}
139+
}
140+
141+
public void checkDP() {
142+
if(dp == null) {
143+
checkJO();
144+
dp = jo.getAsJsonObject("displayProperties");
145+
}
146+
}
147+
106148
public enum ItemTier {
107149
COMMON,
108150
UNCOMMON,
109151
RARE,
110152
LEGENDARY,
111153
EXOTIC;
112154
}
155+
156+
/**
157+
* Return a list of all items that contain or match the name provided
158+
*/
159+
public static List<DestinyItem> searchForItems(String itemName) {
160+
HttpUtils httpUtils = new HttpUtils();
161+
List<DestinyItem> destinyItemList = new ArrayList<>();
162+
163+
for(JsonElement jsonElement : httpUtils.urlRequestGET("https://www.bungie.net/Platform/Destiny2/Armory/Search/DestinyInventoryItemDefinition/" + itemName + "/").getAsJsonObject("Response").getAsJsonObject("results").getAsJsonArray("results")) {
164+
JsonObject jsonObject = jsonElement.getAsJsonObject();
165+
JsonObject displayProperties = jsonObject.getAsJsonObject("displayProperties");
166+
destinyItemList.add(new DestinyItem(jsonObject.get("hash").getAsString(), displayProperties.get("name").getAsString(),
167+
displayProperties.get("icon").getAsString(), displayProperties.get("hasIcon").getAsBoolean()));
168+
}
169+
return destinyItemList;
170+
}
113171
}

src/main/java/utils/framework/ContentInterface.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
package utils.framework;
1010

11-
import com.google.gson.JsonObject;
12-
1311
public interface ContentInterface {
1412

1513
/**

0 commit comments

Comments
 (0)