-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathFlatDataSource.java
More file actions
127 lines (106 loc) · 3.51 KB
/
FlatDataSource.java
File metadata and controls
127 lines (106 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package nl.martenm.servertutorialplus.data;
import nl.martenm.servertutorialplus.ServerTutorialPlus;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author MartenM
* @since 24-12-2017.
*/
public class FlatDataSource implements DataSource {
private ServerTutorialPlus plugin;
public FlatDataSource(ServerTutorialPlus plugin){
this.plugin = plugin;
}
@Override
public List<String> getPlayedTutorials(UUID uuid) {
return (List<String>) getDataFromUUID(uuid).getOrDefault("tutorials", new ArrayList<>());
}
@Override
public String getQuitTutorial(UUID uuid) {
return (String) getDataFromUUID(uuid).get("quit_tutorial");
}
private JSONObject getDataFromUUID(UUID uuid) {
File hostlocation = new File(plugin.getDataFolder() + "/data/playerdata");
hostlocation.mkdirs();
JSONObject data = new JSONObject();
File file = new File(plugin.getDataFolder() + "/data/playerdata/" + uuid + ".json");
if(file.exists()) {
JSONParser parser = new JSONParser();
FileReader reader = null;
try {
reader = new FileReader(file.getPath());
Object obj = parser.parse(reader);
data = (JSONObject) obj;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return data;
}
@Override
public boolean addPlayedTutorial(UUID uuid, String id) {
List<String> played = getPlayedTutorials(uuid);
played.add(id);
return changeDataInFile(uuid, "tutorials", played);
}
@Override
public void setQuitTutorial(UUID uuid, String id) {
changeDataInFile(uuid, "quit_tutorial", id);
}
public boolean changeDataInFile(UUID uuid, String key, Object value) {
JSONObject data = getDataFromUUID(uuid);
if (value == null) {
data.remove(key);
} else {
data.put(key, value);
}
File file = new File(plugin.getDataFolder() + "/data/playerdata/" + uuid + ".json");
FileWriter writer = null;
try{
writer = new FileWriter(file);
writer.write(data.toJSONString());
} catch (Exception ex){
ex.printStackTrace();
return false;
} finally {
if(writer != null){
try {
writer.flush();
writer.close();
} catch (Exception ex){
ex.printStackTrace();
}
}
}
return true;
}
@Override
public boolean removePlayedTutorial(UUID uuid, String id) {
List<String> played = getPlayedTutorials(uuid);
played.remove(id);
return changeDataInFile(uuid, "tutorials", played);
}
@Override
public boolean removeQuitTutorial(UUID uuid) {
return changeDataInFile(uuid, "quit_tutorial", null);
}
@Override
public boolean hasPlayedTutorial(UUID uuid, String id) {
return getPlayedTutorials(uuid).contains(id);
}
}