RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GameSaver.java
1package com.osroyale.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.osroyale.content.WellOfGoodwill;
8import com.osroyale.content.WellOfGoodwill.Contributor;
9import com.osroyale.content.bot.BotUtility;
10import com.osroyale.game.world.entity.mob.player.PlayerRight;
11import com.osroyale.util.parser.GsonParser;
12
13import java.io.FileWriter;
14import java.util.HashMap;
15import java.util.Optional;
16
54
55public final class GameSaver {
56
57 public static int MAX_PLAYERS;
58 public static long PERSONAL_ITEM_WORTH = 0;
59 public static long ITEMS_SOLD = 0;
60
61 public static void save() {
62 Thread.startVirtualThread(() -> {
63 try (FileWriter fw = new FileWriter("./data/content/game/world.json")) {
64 fw.write(GsonUtils.JSON_PRETTY_NO_NULLS.get().toJson(toJson()));
65 } catch (final Exception e) {
66 e.printStackTrace();
67 }
68 });
69 }
70
71 public static JsonObject toJson() {
72 JsonObject object = new JsonObject();
73
74 object.addProperty("max-players", MAX_PLAYERS);
75
76 /* Well of Goodwill */
77 object.addProperty("wog-active-time", WellOfGoodwill.activeTime);
78 object.addProperty("wog-contribution", WellOfGoodwill.CONTRIBUTION);
79 object.addProperty("wog-last-contributor", WellOfGoodwill.lastContributor);
80
81 JsonArray wogArray = new JsonArray();
82 for (Contributor contributor : WellOfGoodwill.contributors) {
83 JsonObject contributorObj = new JsonObject();
84 contributorObj.addProperty("name", contributor.name);
85 contributorObj.addProperty("rank", contributor.rank.getCrown());
86 contributorObj.addProperty("amount", contributor.contribution);
87 wogArray.add(contributorObj);
88 }
89
90 object.add("contributors", wogArray);
91
92 /* Personal Store */
93 object.addProperty("personal-store-worth", PERSONAL_ITEM_WORTH);
94 object.addProperty("personal-store-items-sold", ITEMS_SOLD);
95 object.add("pk-bot-loot", GsonUtils.JSON_PRETTY_ALLOW_NULL.get().toJsonTree(BotUtility.BOOT_LOOT));
96 return object;
97 }
98
99 public static void load() {
100 new GsonParser("content/game/world") {
101 @Override
102 protected void parse(JsonObject object) {
103 MAX_PLAYERS = object.get("max-players").getAsInt();
104
105 /* Well of Goodwill */
106 WellOfGoodwill.activeTime = object.get("wog-active-time").getAsInt();
107 WellOfGoodwill.CONTRIBUTION = object.get("wog-contribution").getAsInt();
108
109 if (object.has("wog-last-contributor")) {
110 WellOfGoodwill.lastContributor = object.get("wog-last-contributor").getAsString();
111 }
112
113 final JsonArray contributors = object.get("contributors").getAsJsonArray();
114 for (JsonElement element : contributors) {
115 final JsonObject contributorObj = (JsonObject) element;
116 final String name = contributorObj.get("name").getAsString();
117 final Optional<PlayerRight> rank = PlayerRight.lookup(contributorObj.get("rank").getAsInt());
118 final int amount = contributorObj.get("amount").getAsInt();
119 WellOfGoodwill.add(name, rank.orElse(PlayerRight.PLAYER), amount);
120 }
121
122 /* Personal Store */
123 PERSONAL_ITEM_WORTH = object.get("personal-store-worth").getAsLong();
124 ITEMS_SOLD = object.get("personal-store-items-sold").getAsLong();
125 BotUtility.BOOT_LOOT = GsonUtils.JSON_PRETTY_ALLOW_NULL.get().fromJson(object.get("pk-bot-loot"), new TypeToken<HashMap<Integer, MutableNumber>>() { }.getType());
126 }
127 }.run();
128 }
129}
static TreeSet< Contributor > contributors