RuneHive-Game
Loading...
Searching...
No Matches
CollectionLogSaving.java
Go to the documentation of this file.
1
package
com.runehive.content.collectionlog;
2
3
import
com.google.gson.Gson;
4
import
com.google.gson.GsonBuilder;
5
import
com.google.gson.JsonObject;
6
import
com.google.gson.JsonParser;
7
import
com.runehive.game.world.entity.mob.player.Player;
8
9
import
java.io.File;
10
import
java.io.FileReader;
11
import
java.io.FileWriter;
12
import
java.nio.file.Path;
13
import
java.nio.file.Paths;
14
15
public
class
CollectionLogSaving
{
16
17
public
static
void
save
(
Player
player) {
18
Path path = Paths.get(
"./data/profile/save/collectionLogs/"
+player.
getUsername
()+
".json"
);
19
File file = path.toFile();
20
file.getParentFile().setWritable(
true
);
21
if
(!file.getParentFile().exists()) {
22
try
{
23
file.getParentFile().mkdirs();
24
}
catch
(SecurityException e) {
25
System.out.println(
"Unable to create directory for player data!"
);
26
}
27
}
28
try
(FileWriter
writer
=
new
FileWriter(file)) {
29
Gson builder =
new
GsonBuilder().setPrettyPrinting().create();
30
JsonObject
object
=
new
JsonObject();
31
32
object
.add(
"collectionLog"
, builder.toJsonTree(player.
getCollectionLog
()));
33
34
writer
.write(builder.toJson(
object
));
35
}
catch
(Exception e) {
36
}
37
}
38
39
public
static
CollectionLog
load
(
Player
player) {
40
CollectionLog
log =
new
CollectionLog
();
41
42
Path path = Paths.get(
"./data/profile/save/collectionLogs/"
+player.
getUsername
()+
".json"
);
43
File file = path.toFile();
44
if
(!file.exists()) {
45
return
log;
46
}
47
try
(FileReader fileReader =
new
FileReader(file)) {
48
JsonParser fileParser =
new
JsonParser();
49
Gson builder =
new
GsonBuilder().create();
50
JsonObject reader = (JsonObject) fileParser.parse(fileReader);
51
if
(reader.has(
"collectionLog"
)) {
52
log = builder.fromJson(reader.get(
"collectionLog"
),
CollectionLog
.class);
53
}
54
}
catch
(Exception e) {
55
e.printStackTrace();
56
}
57
58
return
log;
59
}
60
61
}
com.runehive.content.collectionlog.CollectionLog
Definition
CollectionLog.java:13
com.runehive.content.collectionlog.CollectionLogSaving
Definition
CollectionLogSaving.java:15
com.runehive.content.collectionlog.CollectionLogSaving.save
static void save(Player player)
Definition
CollectionLogSaving.java:17
com.runehive.content.collectionlog.CollectionLogSaving.load
static CollectionLog load(Player player)
Definition
CollectionLogSaving.java:39
com.runehive.game.world.entity.mob.player.Player
This class represents a character controlled by a player.
Definition
Player.java:125
com.runehive.game.world.entity.mob.player.Player.getUsername
String getUsername()
Definition
Player.java:917
com.runehive.game.world.entity.mob.player.Player.getCollectionLog
CollectionLog getCollectionLog()
Definition
Player.java:204
com.runehive.content.writer