-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathIFPlayer.java
More file actions
116 lines (98 loc) · 4.3 KB
/
Copy pathIFPlayer.java
File metadata and controls
116 lines (98 loc) · 4.3 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
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2025. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2025. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.objectholders;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class IFPlayer {
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
public static IFPlayer create(IFPlayerManager manager, UUID player) throws Exception {
IFPlayer ifPlayer = new IFPlayer(manager, player, Collections.emptyMap());
ifPlayer.save();
return ifPlayer;
}
public static IFPlayer load(IFPlayerManager manager, JsonObject json) {
UUID uuid = UUID.fromString(json.get("uuid").getAsString());
Map<IFPlayerPreference<?>, Object> preferences = new HashMap<>();
JsonObject preferenceJson = json.get("preferences").getAsJsonObject();
for (IFPlayerPreference<?> preference : IFPlayerPreference.values()) {
JsonElement element = preferenceJson.get(preference.getJsonName());
if (element != null) {
preferences.put(preference, preference.getDeserializer().apply(element));
}
}
return new IFPlayer(manager, uuid, preferences);
}
private final IFPlayerManager manager;
private final UUID uuid;
private final Map<IFPlayerPreference<?>, Object> preferences;
private IFPlayer(IFPlayerManager manager, UUID uuid, Map<IFPlayerPreference<?>, ?> preferences) {
this.manager = manager;
this.uuid = uuid;
this.preferences = new ConcurrentHashMap<>(preferences);
}
public OfflinePlayer getLocalPlayer() {
return Bukkit.getOfflinePlayer(uuid);
}
public String getName() {
return getLocalPlayer().getName();
}
public UUID getUniqueId() {
return uuid;
}
public Object getPreference(IFPlayerPreference<?> preference) {
return preferences.computeIfAbsent(preference, k -> preference.getDefaultValue(this));
}
@SuppressWarnings("unchecked")
public <T> T getPreference(IFPlayerPreference<?> preference, Class<T> type) {
return (T) preferences.computeIfAbsent(preference, k -> preference.getDefaultValue(this, type));
}
public void setPreference(IFPlayerPreference<?> preference, Object value) {
preferences.put(preference, value);
}
public void save() throws Exception {
manager.getDataFolder().mkdirs();
File file = new File(manager.getDataFolder(), uuid + ".json");
JsonObject json = new JsonObject();
json.addProperty("uuid", uuid.toString());
JsonObject preferenceJson = new JsonObject();
for (Map.Entry<IFPlayerPreference<?>, Object> entry : preferences.entrySet()) {
IFPlayerPreference<?> preference = entry.getKey();
final Object value = entry.getValue();
if (!Objects.equals(value, preference.getDefaultValue(this))) preferenceJson.add(preference.getJsonName(), preference.getSerializer(Object.class).apply(value));
}
if (preferenceJson.size() == 0) return;
json.add("preferences", preferenceJson);
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8))) {
pw.println(GSON.toJson(json));
pw.flush();
}
}
}