Skip to content

Commit e13144d

Browse files
committed
Implement Item#getHeldByPokemon, Item#getSprite, Pokemon#getHeldItems
1 parent 20e9314 commit e13144d

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

src/main/java/eu/iamgio/pokedex/item/Item.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ public class Item {
6161
*/
6262
private ItemCategory category;
6363

64-
//TODO sprites
64+
/**
65+
* The sprite used to depict this item in the game
66+
*/
67+
private String sprite;
6568

66-
//TODO heldByPokemon
69+
/**
70+
* A list of Pokémon that might be found in the wild holding this item
71+
*/
72+
private List<ItemHold> heldByPokemon;
6773

6874
//TODO babyTriggerFor
6975

@@ -128,6 +134,8 @@ public static Item fromName(String name) throws PokedexException {
128134
flingEffect.isJsonNull() ? null :
129135
ItemFlingEffect.valueOf(new NamedResource(flingEffect).toEnumName()),
130136
ItemCategory.valueOf(new NamedResource(json.get("category")).toEnumName()),
137+
json.getAsJsonObject("sprites").get("default").getAsString(),
138+
ItemHold.fromJson(json.getAsJsonArray("held_by_pokemon"), false),
131139
gameIndices,
132140
machines,
133141
new LocalizedNames(json.getAsJsonArray("names"), "name"),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package eu.iamgio.pokedex.item;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import eu.iamgio.pokedex.util.JsonStream;
6+
import eu.iamgio.pokedex.util.NamedResource;
7+
import eu.iamgio.pokedex.version.Version;
8+
import lombok.AccessLevel;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Getter;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
16+
/**
17+
* Pokémon that might be found in the wild holding a specific item.
18+
* @author Gio
19+
*/
20+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
21+
@Getter
22+
public class ItemHold {
23+
24+
/**
25+
* Name of the Pokémon that holds this item or the item the referenced Pokémon holds
26+
*/
27+
private String name;
28+
29+
/**
30+
* How often the Pokémon holds this item in every version
31+
*/
32+
private Map<Version, Integer> versionDetails;
33+
34+
/**
35+
* @param json JSON containing data
36+
* @return Parsed JSON into {@link ItemHold}
37+
*/
38+
public static List<ItemHold> fromJson(JsonArray json, boolean ofPokemon) {
39+
return new JsonStream(json)
40+
.stream()
41+
.map(JsonElement::getAsJsonObject)
42+
.map(holder -> new ItemHold(
43+
new NamedResource(holder.get(ofPokemon ? "item" : "pokemon")).getName(),
44+
new JsonStream(holder.getAsJsonArray("version_details"))
45+
.stream()
46+
.map(JsonElement::getAsJsonObject)
47+
.collect(Collectors.toMap(
48+
detail -> Version.valueOf(new NamedResource(detail.get("version")).toEnumName()),
49+
detail -> detail.get("rarity").getAsInt()
50+
))
51+
))
52+
.collect(Collectors.toList());
53+
}
54+
}

src/main/java/eu/iamgio/pokedex/pokemon/Pokemon.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonObject;
66
import eu.iamgio.pokedex.connection.HttpConnection;
77
import eu.iamgio.pokedex.exception.PokedexException;
8+
import eu.iamgio.pokedex.item.ItemHold;
89
import eu.iamgio.pokedex.pokemon.move.PokemonPersonalMove;
910
import eu.iamgio.pokedex.util.JsonStream;
1011
import eu.iamgio.pokedex.util.NamedResource;
@@ -62,6 +63,11 @@ public class Pokemon {
6263
*/
6364
private Pair<PokemonType, PokemonType> types;
6465

66+
/**
67+
* A list of items this Pokémon may be holding when encountered
68+
*/
69+
private List<ItemHold> heldItems;
70+
6571
/**
6672
* A list of game indices relevent to Pokémon item by generation
6773
*/
@@ -164,6 +170,7 @@ public static Pokemon fromName(String name) throws PokedexException {
164170
json.get("weight").getAsInt(),
165171
json.get("base_experience").getAsInt(),
166172
new Pair<>(types.get(0), types.size() > 1 ? types.get(1) : null),
173+
ItemHold.fromJson(json.getAsJsonArray("held_items"), true),
167174
new JsonStream(json.getAsJsonArray("game_indices"))
168175
.stream()
169176
.map(JsonElement::getAsJsonObject)

src/test/java/eu/iamgio/pokedex/Tests.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package eu.iamgio.pokedex;
22

3-
import eu.iamgio.pokedex.item.Item;
4-
import eu.iamgio.pokedex.item.ItemAttribute;
5-
import eu.iamgio.pokedex.item.ItemCategory;
3+
import eu.iamgio.pokedex.item.*;
64
import eu.iamgio.pokedex.lang.Language;
75
import eu.iamgio.pokedex.location.PalParkArea;
86
import eu.iamgio.pokedex.machines.Machine;
@@ -34,6 +32,7 @@ void testBulbasaur() {
3432
assertEquals(69, bulbasaur.getWeight());
3533
assertEquals(64, bulbasaur.getBaseExperience());
3634
assertEquals(new Pair<>(PokemonType.GRASS, PokemonType.POISON), bulbasaur.getTypes());
35+
assertEquals(0, bulbasaur.getHeldItems().size());
3736
assertEquals(153, bulbasaur.getGameIndices().get(Version.YELLOW).intValue());
3837
assertEquals("bulbasaur", bulbasaur.getSpeciesName());
3938
assertEquals("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png",
@@ -46,6 +45,14 @@ void testBulbasaur() {
4645
assertEquals(45, bulbasaur.getStat(Stat.Type.SPEED).getBaseStat());
4746
}
4847

48+
@Test
49+
void testChanseyHeldItems() {
50+
Pokemon chansey = Pokemon.fromName("chansey");
51+
ItemHold holder = chansey.getHeldItems().get(0);
52+
assertEquals("oval-stone", holder.getName());
53+
assertEquals(50, holder.getVersionDetails().get(Version.PEARL).intValue());
54+
}
55+
4956
@Test
5057
void testLevitate() {
5158
PokemonAbility levitate = PokemonAbility.fromName("levitate");
@@ -169,10 +176,21 @@ void testMasterBall() {
169176
assertEquals(null, item.getFlingPower());
170177
assertEquals(null, item.getFlingEffect());
171178
assertEquals(ItemCategory.STANDARD_BALLS, item.getCategory());
179+
assertEquals("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/items/master-ball.png",
180+
item.getSprite());
172181
assertEquals(1, item.getGameIndices().get(Generation.SUN_MOON).intValue());
173182
assertEquals(0, item.getMachines().size());
174183
assertEquals("Master Ball", item.getLocalizedNames().get(Language.ENGLISH).getName());
175184
assertEquals("The best BALL that\ncatches a POKéMON\nwithout fail.",
176185
item.getFlavors().filterVersion(VersionGroup.RUBY_SAPPHIRE).get(Language.ENGLISH).getName());
186+
assertEquals(0, item.getHeldByPokemon().size());
187+
}
188+
189+
@Test
190+
void testOvalStoneHolders() {
191+
Item item = Item.fromName("oval-stone");
192+
ItemHold holder = item.getHeldByPokemon().get(0);
193+
assertEquals("chansey", holder.getName());
194+
assertEquals(50, holder.getVersionDetails().get(Version.PEARL).intValue());
177195
}
178196
}

0 commit comments

Comments
 (0)