blob: 3c152f30602cacffc4d7460cf3ba5af0bd11f290 (
plain)
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
|
package com.redstoner.bungeeBans;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.redstoner.bungeeBans.json.Ban;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BanManager<T extends Ban> {
private Gson gson = new Gson();
private File file;
private Class<T> type;
private List<T> bans = new ArrayList<>();
BanManager(File file, Class<T> type) {
this.file = file;
this.type = type;
}
public void loadBans() throws JsonSyntaxException, IOException {
String json = new String(Files.readAllBytes(file.toPath()));
try {
@SuppressWarnings ("unchecked")
T[] bans = gson.fromJson(json, (Class<T[]>) java.lang.reflect.Array.newInstance(type, 0).getClass());
if (bans != null) this.bans.addAll(Arrays.asList(bans));
} catch (ClassCastException e) {
//IGNORE
e.printStackTrace();
}
}
public void saveBans() throws IOException {
String json = gson.toJson(bans);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(json.getBytes());
outputStream.close();
}
public void addBan(T ban) {
this.bans.add(ban);
}
public void removeBan(T ban) {
this.bans.remove(ban);
}
public T getBan(String identifier) {
for (T ban : bans) {
if (ban.getIdentifier().equals(identifier)) {
return ban;
}
}
return null;
}
}
|