forked from MLG-Fortress/AutomaticInventory
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayerConfig.java
More file actions
306 lines (252 loc) · 8.66 KB
/
Copy pathPlayerConfig.java
File metadata and controls
306 lines (252 loc) · 8.66 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package dev.chaws.automaticinventory.configuration;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.UUID;
import dev.chaws.automaticinventory.AutomaticInventory;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
public class PlayerConfig {
private final static String METADATA_TAG = "AI_PlayerData";
private static File playerConfigFolder;
private final File playerConfigFile;
private final String playerName;
private final UUID playerID;
private Thread loadingThread;
private Thread savingThread;
private boolean receivedChestSortInfo = false;
private boolean receivedInventorySortInfo = false;
private boolean receivedRestackInfo = false;
private boolean usedQuickDeposit = false;
private boolean gotQuickDepositInfo = false;
private boolean receivedDepositAllInfo = false;
private boolean usedDepositAll = false;
public int firstEmptySlot = -1;
private boolean isDirty = false;
private PlayerConfig(Player player) {
this.playerName = player.getName();
this.playerID = player.getUniqueId();
this.playerConfigFile = new File(PlayerConfig.playerConfigFolder.getPath() + File.separator + this.playerID);
this.loadingThread = new Thread(new DataLoader());
this.loadingThread.start();
player.setMetadata(METADATA_TAG, new FixedMetadataValue(AutomaticInventory.instance, this));
}
public static void initialize(File playerConfigFolder) {
PlayerConfig.playerConfigFolder = playerConfigFolder;
}
public static void initializePlayer(Player player) {
new PlayerConfig(player);
}
public static PlayerConfig fromPlayer(Player player) {
var data = player.getMetadata(METADATA_TAG);
if (data.isEmpty()) {
return new PlayerConfig(player);
} else {
return (PlayerConfig) data.get(0).value();
}
}
public boolean hasUsedQuickDeposit() {
return usedQuickDeposit;
}
public void setUsedQuickDeposit(boolean usedQuickDeposit) {
this.usedQuickDeposit = usedQuickDeposit;
this.isDirty = true;
}
public boolean hasUsedDepositAll() {
return usedDepositAll;
}
public void setUsedDepositAll(boolean usedDepositAll) {
this.usedDepositAll = usedDepositAll;
this.isDirty = true;
}
public boolean hasReceivedChestSortInfo() {
return receivedChestSortInfo;
}
public void setReceivedChestSortInfo(boolean receivedChestSortInfo) {
this.receivedChestSortInfo = receivedChestSortInfo;
this.isDirty = true;
}
public boolean hasReceivedInventorySortInfo() {
return receivedInventorySortInfo;
}
public void setReceivedInventorySortInfo(boolean receivedInventorySortInfo) {
this.receivedInventorySortInfo = receivedInventorySortInfo;
this.isDirty = true;
}
public boolean hasReceivedRestackInfo() {
return receivedRestackInfo;
}
public void setReceivedRestackInfo(boolean receivedRestackInfo) {
this.receivedRestackInfo = receivedRestackInfo;
this.isDirty = true;
}
public static boolean featureEnabled(Features feature, Player player) {
if (!AutomaticInventory.hasPermission(feature, player)) {
return false;
}
var playerConfig = PlayerConfig.fromPlayer(player);
return switch (feature) {
case SortInventory -> playerConfig.isSortInventory();
case SortChests -> playerConfig.isSortChests();
case QuickDeposit -> playerConfig.isQuickDepositEnabled();
case RefillStacks -> playerConfig.isAutoRefillEnabled();
case DepositAll -> true;
};
}
private boolean sortChests = GlobalConfig.autosortEnabledByDefault;
public boolean isSortChests() {
this.waitForLoadComplete();
return sortChests;
}
public void setSortChests(boolean sortChests) {
this.isDirty = true;
this.sortChests = sortChests;
}
private boolean sortInventory = GlobalConfig.autosortEnabledByDefault;
public boolean isSortInventory() {
this.waitForLoadComplete();
return sortInventory;
}
public void setSortInventory(boolean sortInventory) {
this.isDirty = true;
this.sortInventory = sortInventory;
}
private boolean quickDepositEnabled = GlobalConfig.quickDepositEnabledByDefault;
public boolean isQuickDepositEnabled() {
this.waitForLoadComplete();
return quickDepositEnabled;
}
public void setQuickDepositEnabled(boolean quickDepositEnabled) {
this.isDirty = true;
this.quickDepositEnabled = quickDepositEnabled;
}
private boolean autoRefillEnabled = GlobalConfig.autoRefillEnabledByDefault;
public boolean isAutoRefillEnabled() {
this.waitForLoadComplete();
return autoRefillEnabled;
}
public void setAutoRefillEnabled(boolean autoRefillEnabled) {
this.isDirty = true;
this.autoRefillEnabled = autoRefillEnabled;
}
public boolean isGotQuickDepositInfo() {
return gotQuickDepositInfo;
}
public void setGotQuickDepositInfo(boolean newValue) {
this.gotQuickDepositInfo = newValue;
}
public void saveChanges() {
if (!this.isDirty) {
return;
}
this.waitForLoadComplete();
this.savingThread = new Thread(new DataSaver());
this.savingThread.start();
}
private void waitForLoadComplete() {
if (this.loadingThread != null) {
try {
this.loadingThread.join();
} catch (InterruptedException ignored) {
}
this.loadingThread = null;
}
}
public void waitForSaveComplete() {
if (this.savingThread != null) {
try {
this.savingThread.join();
} catch (InterruptedException ignored) {
}
}
}
private void writeDataToFile() {
try {
FileConfiguration config = new YamlConfiguration();
config.set("Player Name", this.playerName);
config.set("Sort Chests", this.sortChests);
config.set("Sort Personal Inventory", this.sortInventory);
config.set("Quick Deposit Enabled", this.quickDepositEnabled);
config.set("Auto Refill Enabled", this.autoRefillEnabled);
config.set("Used Quick Deposit", this.usedQuickDeposit);
config.set("Received Messages.Personal Inventory", this.receivedInventorySortInfo);
config.set("Received Messages.Chest Inventory", this.receivedChestSortInfo);
config.set("Received Messages.Restacker", this.receivedRestackInfo);
config.set("Received Messages.Deposit All", this.receivedDepositAllInfo);
config.save(this.playerConfigFile);
} catch (Exception e) {
var errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
AutomaticInventory.log.info("Failed to save player data for " + playerID + " " + errors);
}
this.savingThread = null;
this.isDirty = false;
}
private void readDataFromFile() {
if (!this.playerConfigFile.exists()) {
return;
}
var needRetry = false;
var retriesRemaining = 5;
Exception latestException = null;
do {
try {
needRetry = false;
FileConfiguration config = YamlConfiguration.loadConfiguration(this.playerConfigFile);
this.sortChests = config.getBoolean("Sort Chests", GlobalConfig.autosortEnabledByDefault);
this.sortInventory = config.getBoolean("Sort Personal Inventory",
GlobalConfig.autosortEnabledByDefault);
this.quickDepositEnabled = config.getBoolean("Quick Deposit Enabled",
GlobalConfig.quickDepositEnabledByDefault);
this.autoRefillEnabled = config.getBoolean("Auto Refill Enabled",
GlobalConfig.autoRefillEnabledByDefault);
this.usedQuickDeposit = config.getBoolean("Used Quick Deposit", false);
this.receivedChestSortInfo = config.getBoolean("Received Messages.Chest Inventory", false);
this.receivedInventorySortInfo = config.getBoolean("Received Messages.Personal Inventory", false);
this.receivedRestackInfo = config.getBoolean("Received Messages.Restacker", false);
this.receivedDepositAllInfo = config.getBoolean("Received Messages.Deposit All", false);
}
// if there's any problem with the file's content, retry up to 5 times with 5
// milliseconds between
catch (Exception e) {
latestException = e;
needRetry = true;
retriesRemaining--;
}
try {
if (needRetry) {
Thread.sleep(5);
}
} catch (InterruptedException ignored) {
}
} while (needRetry && retriesRemaining >= 0);
// if last attempt failed, log information about the problem
if (needRetry) {
var errors = new StringWriter();
if (latestException != null)
latestException.printStackTrace(new PrintWriter(errors));
AutomaticInventory.log.info("Failed to load data for " + playerID + " " + errors);
}
}
private class DataSaver implements Runnable {
@Override
public void run() {
writeDataToFile();
}
}
private class DataLoader implements Runnable {
@Override
public void run() {
readDataFromFile();
}
}
public boolean isReceivedDepositAllInfo() {
return this.receivedDepositAllInfo;
}
public void setReceivedDepositAllInfo(boolean status) {
this.receivedDepositAllInfo = status;
this.isDirty = true;
}
}