-
Notifications
You must be signed in to change notification settings - Fork 17
/
History.java
258 lines (211 loc) · 6.31 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
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.griefcraft.model;
import com.griefcraft.lwc.LWC;
import com.griefcraft.util.StringUtils;
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 history type, e.g TRANSACTION
*/
private Type type;
/**
* The status (ACTIVE or INACTIVE normally)
*/
private Status status;
/**
* 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;
/**
* We use a protected constructor to only strictly allow History objects be created by their owner (which would
* be a Protection object.) It is not necessary to do <pre>new History()</pre> and as such it is recommended to
* instead use <pre>protection.createHistoryObject()</pre>
*/
protected History() {
// set some defaults to account for stupidness
status = Status.INACTIVE;
metadata = new String[0];
}
/**
* @return the Protection this history value is associated with
*/
public Protection getProtection() {
if (protectionId < 0) {
return null;
}
return LWC.getInstance().getPhysicalDatabase().loadProtection(protectionId);
}
/**
* 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;
}
/**
* 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()]);
return metadata.length == expected;
}
/**
* @return true if this History object exists in the database
*/
public boolean doesExist() {
return exists;
}
/**
* @return a STRING representation of the metadata for use in the database
*/
public String getSafeMetaData() {
return StringUtils.join(metadata, 0, ",");
}
/**
* Sync this history object to the database
*/
public void save() {
LWC.getInstance().getPhysicalDatabase().saveHistory(this);
}
/**
* 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().unregisterHistory(id);
}
public int getId() {
return id;
}
public int getProtectionId() {
return protectionId;
}
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;
}
protected void setProtectionId(int protectionId) {
this.protectionId = protectionId;
}
public void setType(Type type) {
this.type = type;
}
public void setStatus(Status status) {
this.status = status;
}
public void setMetaData(String[] metadata) {
this.metadata = metadata;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}