-
Notifications
You must be signed in to change notification settings - Fork 17
/
History.java
479 lines (393 loc) · 11.5 KB
/
History.java
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.model;
import com.griefcraft.lwc.LWC;
import com.griefcraft.util.StringUtil;
import java.util.Arrays;
import java.util.List;
public class History {
/**
* The history type defines what this History object is for, such as TRANSACTION.
* <p/>
* Ordering <b>must</b> remain constant as internally, the ordinal is used and
* if ordering is changed, LWC will experience undefined behaviour.
*/
public enum Type {
/**
* Designates that this history object is for a transaction - e.g when a protection is removed
*/
TRANSACTION
}
/**
* The status of this History object; most often ACTIVE or INACTIVE.
* <p/>
* As with {@link Type}, the ordering <b>must</b> remain constant and not change.
*/
public enum Status {
/**
* This type is still active
*/
ACTIVE,
/**
* For some reason the type is now inactive. Most likely the
* protection was removed by the player
*/
INACTIVE
}
/**
* History id in the database
*/
private int id;
/**
* Affected protection id
*/
private int protectionId;
/**
* The protection known for this history object
*/
private Protection protection;
/**
* The player that caused the history action to be created
*/
private String player;
/**
* The x coordinate of the history item
*/
private int x;
/**
* The y coordinate of the history item
*/
private int y;
/**
* The z coordinate of the history item
*/
private int z;
/**
* The history type, e.g TRANSACTION
*/
private Type type = Type.TRANSACTION;
/**
* The status (ACTIVE or INACTIVE normally)
*/
private Status status = Status.INACTIVE;
/**
* Metadata about the transaction. An example of one entry would be
* for iConomy prices to be pushed in here. Any module can modify the
* meta data and add their own data about the transaction.
*/
private String[] metadata;
/**
* The seconds (since linux epoch) this History object was created
*/
private long timestamp;
/**
* If the history val exists in the database
*/
private boolean exists = false;
/**
* If the history object was modified
*/
private boolean modified = false;
/**
* If the History object is waiting to be flushed to the database
*/
private boolean saving = false;
public History() {
// set some defaults to account for stupidness
status = Status.INACTIVE;
metadata = new String[0];
}
/**
* @return true if the history object should be synced to the database
*/
public boolean wasModified() {
return modified;
}
/**
* @return the Protection this history value is associated with
*/
public Protection getProtection() {
// attempt to load the protection if it hasn't been loaded yet
if (protection == null) {
this.protection = LWC.getInstance().getPhysicalDatabase().loadProtection(protectionId);
}
return protection;
}
/**
* Add a string of data to the stored metadata
*
* @param data
*/
public void addMetaData(String data) {
String[] temp = new String[metadata.length + 1];
System.arraycopy(metadata, 0, temp, 0, metadata.length);
// push the data to the end of the temporary array
// array.length doesn't start at 0, so we can be sure this is valid
temp[metadata.length] = data;
// we're okey
this.metadata = temp;
this.modified = true;
}
/**
* Check if the metadata contains a given key
*
* @param key
* @return
*/
public boolean hasKey(String key) {
return getMetaDataStartsWith(key + "=") != null;
}
/**
* Get a boolean value from the metadata using the key (key=value)
*
* @param key
* @return
*/
public boolean getBoolean(String key) {
String metadata = getMetaDataStartsWith(key + "=");
return metadata != null && Boolean.parseBoolean(metadata.substring((key + "=").length()));
}
/**
* Get a String value from the metadata using the key (key=value)
*
* @param key
* @return
*/
public String getString(String key) {
String metadata = getMetaDataStartsWith(key + "=");
if (metadata == null) {
return "";
}
return metadata.substring((key + "=").length());
}
/**
* Get an integer value from the metadata using the key (key=value)
*
* @param key
* @return
*/
public int getInteger(String key) {
String metadata = getMetaDataStartsWith(key + "=");
if (metadata == null) {
return 0;
}
return Integer.parseInt(metadata.substring((key + "=").length()));
}
/**
* Get a double value from the metadata using the key (key=value)
*
* @param key
* @return
*/
public double getDouble(String key) {
String metadata = getMetaDataStartsWith(key + "=");
if (metadata == null) {
return 0;
}
return Double.parseDouble(metadata.substring((key + "=").length()));
}
/**
* Get a metadata that starts with a specific string
*
* @param startsWith
* @return the full metadata if a match is found, otherwise NULL
*/
public String getMetaDataStartsWith(String startsWith) {
for (String temp : metadata) {
if (temp.startsWith(startsWith)) {
return temp;
}
}
return null;
}
/**
* Remove a string of known data from the stored metadata
*
* @param data
* @return true if the given metadata was successfully removed
*/
public boolean removeMetaData(String data) {
// sorry
List<String> temp = Arrays.asList(metadata);
int expected = metadata.length - 1;
temp.remove(data);
// that went better than expected
this.metadata = temp.toArray(new String[temp.size()]);
this.modified = true;
return metadata.length == expected;
}
/**
* Set the cached protection this History object belongs to save a query or two later on
*
* @param protection
*/
public void setProtection(Protection protection) {
if (protection == null) {
return;
}
this.protection = protection;
this.protectionId = protection.getId();
}
/**
* @return true if this History object exists in the database
*/
public boolean doesExist() {
return exists;
}
/**
* Set if the History object exists in the database
*
* @param exists
*/
public void setExists(boolean exists) {
this.exists = exists;
this.modified = true;
}
/**
* @return a STRING representation of the metadata for use in the database
*/
public String getSafeMetaData() {
return StringUtil.join(metadata, 0, ",");
}
/**
* Sync this history object to the database when possible
*/
public void save() {
// if it was not modified, no point in saving it :-)
if (!modified || saving) {
return;
}
LWC lwc = LWC.getInstance();
// find the protection the history object is attached to
Protection protection = getProtection();
// no protection? weird, just sync anyway
if (protection == null) {
saveNow();
return;
}
// wait!
this.saving = true;
// ensure the protection knows about us
protection.checkHistory(this);
// save it when possible
protection.save();
}
/**
* Force the history object to be saved immediately
*/
public void saveNow() {
LWC.getInstance().getPhysicalDatabase().saveHistory(this);
this.modified = false;
this.saving = false;
}
/**
* Alias for {@see save}
*/
public void sync() {
save();
}
/**
* Remove this history object from the database
* TODO: broadcast an event
*/
public void remove() {
LWC.getInstance().getPhysicalDatabase().removeHistory(id);
this.modified = false;
}
public int getId() {
return id;
}
public int getProtectionId() {
return protectionId;
}
public String getPlayer() {
return player;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Type getType() {
return type;
}
public Status getStatus() {
return status;
}
public String[] getMetaData() {
return metadata;
}
public long getTimestamp() {
return timestamp;
}
public void setId(int id) {
this.id = id;
this.exists = true;
this.modified = true;
}
public void setProtectionId(int protectionId) {
this.protectionId = protectionId;
this.modified = true;
}
public void setPlayer(String player) {
this.player = player;
this.modified = true;
}
public void setX(int x) {
this.x = x;
this.modified = true;
}
public void setY(int y) {
this.y = y;
this.modified = true;
}
public void setZ(int z) {
this.z = z;
this.modified = true;
}
public void setType(Type type) {
this.type = type;
this.modified = true;
}
public void setStatus(Status status) {
this.status = status;
this.modified = true;
}
public void setMetaData(String[] metadata) {
this.metadata = metadata;
this.modified = true;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
this.modified = true;
}
}