RuneHive-Game
Loading...
Searching...
No Matches
GameSaver.java
Go to the documentation of this file.
1package com.runehive.util;
2
3import com.google.gson.JsonArray;
4import com.google.gson.JsonElement;
5import com.google.gson.JsonObject;
6import com.google.gson.reflect.TypeToken;
7import com.runehive.content.WellOfGoodwill;
8import com.runehive.content.WellOfGoodwill.Contributor;
9import com.runehive.content.bot.BotUtility;
10import com.runehive.game.world.entity.mob.player.PlayerRight;
11import com.runehive.util.parser.GsonParser;
12
13import java.io.FileWriter;
14import java.util.HashMap;
15import java.util.Optional;
16
17/**
18 * Created by Daniel on 2017-11-05.
19 */
20public final class GameSaver {
21
22 public static int MAX_PLAYERS;
23 public static long PERSONAL_ITEM_WORTH = 0;
24 public static long ITEMS_SOLD = 0;
25
26 public static void save() {
27 Thread.startVirtualThread(() -> {
28 try (FileWriter fw = new FileWriter("./data/content/game/world.json")) {
29 fw.write(GsonUtils.JSON_PRETTY_NO_NULLS.get().toJson(toJson()));
30 } catch (final Exception e) {
31 e.printStackTrace();
32 }
33 });
34 }
35
36 public static JsonObject toJson() {
37 JsonObject object = new JsonObject();
38
39 object.addProperty("max-players", MAX_PLAYERS);
40
41 /* Well of Goodwill */
42 object.addProperty("wog-active-time", WellOfGoodwill.activeTime);
43 object.addProperty("wog-contribution", WellOfGoodwill.CONTRIBUTION);
44 object.addProperty("wog-last-contributor", WellOfGoodwill.lastContributor);
45
46 JsonArray wogArray = new JsonArray();
47 for (Contributor contributor : WellOfGoodwill.contributors) {
48 JsonObject contributorObj = new JsonObject();
49 contributorObj.addProperty("name", contributor.name);
50 contributorObj.addProperty("rank", contributor.rank.getCrown());
51 contributorObj.addProperty("amount", contributor.contribution);
52 wogArray.add(contributorObj);
53 }
54
55 object.add("contributors", wogArray);
56
57 /* Personal Store */
58 object.addProperty("personal-store-worth", PERSONAL_ITEM_WORTH);
59 object.addProperty("personal-store-items-sold", ITEMS_SOLD);
60 object.add("pk-bot-loot", GsonUtils.JSON_PRETTY_ALLOW_NULL.get().toJsonTree(BotUtility.BOOT_LOOT));
61 return object;
62 }
63
64 public static void load() {
65 new GsonParser("content/game/world") {
66 @Override
67 protected void parse(JsonObject object) {
68 MAX_PLAYERS = object.get("max-players").getAsInt();
69
70 /* Well of Goodwill */
71 WellOfGoodwill.activeTime = object.get("wog-active-time").getAsInt();
72 WellOfGoodwill.CONTRIBUTION = object.get("wog-contribution").getAsInt();
73
74 if (object.has("wog-last-contributor")) {
75 WellOfGoodwill.lastContributor = object.get("wog-last-contributor").getAsString();
76 }
77
78 final JsonArray contributors = object.get("contributors").getAsJsonArray();
79 for (JsonElement element : contributors) {
80 final JsonObject contributorObj = (JsonObject) element;
81 final String name = contributorObj.get("name").getAsString();
82 final Optional<PlayerRight> rank = PlayerRight.lookup(contributorObj.get("rank").getAsInt());
83 final int amount = contributorObj.get("amount").getAsInt();
84 WellOfGoodwill.add(name, rank.orElse(PlayerRight.PLAYER), amount);
85 }
86
87 /* Personal Store */
88 PERSONAL_ITEM_WORTH = object.get("personal-store-worth").getAsLong();
89 ITEMS_SOLD = object.get("personal-store-items-sold").getAsLong();
90 BotUtility.BOOT_LOOT = GsonUtils.JSON_PRETTY_ALLOW_NULL.get().fromJson(object.get("pk-bot-loot"), new TypeToken<HashMap<Integer, MutableNumber>>() { }.getType());
91 }
92 }.run();
93 }
94}
Handles contribution towards the well of goodwill.
static String lastContributor
The last contributor to the well.
static int CONTRIBUTION
The current contribution amount for the well.
static TreeSet< Contributor > contributors
The array list of contributors.
static void add(String name, PlayerRight rank, int amount)
Holds all the constants used by bot.
static Map< Integer, MutableNumber > BOOT_LOOT
Created by Daniel on 2017-11-05.
static JsonObject toJson()
This class provides an easy to use google gson parser specifically designed for parsing JSON files.
static Optional< PlayerRight > lookup(int id)
static String getCrown(Player player)
Gets the crown display.
static final ThreadLocal< Gson > JSON_PRETTY_NO_NULLS
static final ThreadLocal< Gson > JSON_PRETTY_ALLOW_NULL