RuneHive-Game
Loading...
Searching...
No Matches
StoreParser.java
Go to the documentation of this file.
1package com.runehive.util.parser.impl;
2
3import com.google.gson.JsonObject;
4import com.runehive.content.store.SellType;
5import com.runehive.content.store.Store;
6import com.runehive.content.store.StoreItem;
7import com.runehive.content.store.currency.CurrencyType;
8import com.runehive.content.store.impl.DefaultStore;
9import com.runehive.util.parser.GsonParser;
10
11import java.util.*;
12
13//import com.runehive.content.shop.Store;
14//import com.runehive.content.shop.StoreItem;
15//import com.runehive.content.shop.StoreRepository;
16
17/**
18 * Parses throug the shops files and creates in-game shop object for the game
19 * on startup.
20 *
21 * @author Daniel | Obey
22 */
23public class StoreParser extends GsonParser {
24
25 public StoreParser() {
26 super("def/store/stores", false);
27 }
28
29 @Override
30 protected void parse(JsonObject data) {
31 final String name = Objects.requireNonNull(data.get("name").getAsString());
32 final CurrencyType currency = builder.fromJson(data.get("currency"), CurrencyType.class);
33 final boolean restock = data.get("restock").getAsBoolean();
34 final String sellType = data.get("sellType").getAsString().toUpperCase();
35 final LoadedItem[] loadedItems = builder.fromJson(data.get("items"), LoadedItem[].class);
36
37 final List<StoreItem> storeItems = new ArrayList<>(loadedItems.length);
38
39 for(LoadedItem loadedItem : loadedItems) {
40 if (loadedItem.value == 0) {
41 loadedItem.value = -1;
42 }
43 storeItems.add(new StoreItem(loadedItem.id, loadedItem.amount, OptionalInt.of(loadedItem.value), Optional.ofNullable(loadedItem.type)));
44 }
45
46 StoreItem[] items = storeItems.toArray(new StoreItem[storeItems.size()]);
47 Store.STORES.put(name, new DefaultStore(items, name, SellType.valueOf(sellType), restock, currency));
48 }
49
50 private static final class LoadedItem {
51
52 private final int id;
53
54 private final int amount;
55
56 private int value;
57
58 private final CurrencyType type;
59
60 public LoadedItem(int id, int amount, int value, CurrencyType type) {
61 this.id = id;
62 this.amount = amount;
63 this.value = value;
64 this.type = type;
65 }
66 }
67
68}
The class which holds support for further abstraction for shops.
Definition Store.java:20
static Map< String, Store > STORES
A mapping of each shop by it's name.
Definition Store.java:23
A simple wrapper class which holds extra attributes for the item object.
The default shop which are owned by the server.
GsonParser(String path)
Creates a new GsonParser.
transient Gson builder
The Gson object.
LoadedItem(int id, int amount, int value, CurrencyType type)
void parse(JsonObject data)
The method allows a user to modify the data as its being parsed.
Represents ways items can be sold in a shop.
Definition SellType.java:8
The enumerated type whom holds all the currencies usable for a server.