-
Notifications
You must be signed in to change notification settings - Fork 17
/
HistoryModule.java
282 lines (238 loc) · 10.2 KB
/
HistoryModule.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
/*
* 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 TYLER BLAIR ''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 TYLER BLAIR 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 should not be interpreted as representing official policies, either expressed
* or implied, of Tyler Blair.
*/
package com.griefcraft.modules.history;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.History;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.JavaModule;
import com.griefcraft.scripting.event.LWCCommandEvent;
import com.griefcraft.util.Colors;
import com.griefcraft.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Date;
import java.util.List;
public class HistoryModule extends JavaModule {
/**
* Amount of history items to show per page
*/
public static final int ITEMS_PER_PAGE = 15;
/**
* Called when /lwc details or /lwc d is used.
*
* @param event
*/
private void doDetailsCommand(LWCCommandEvent event) {
LWC lwc = event.getLWC();
CommandSender sender = event.getSender();
String[] args = event.getArgs();
event.setCancelled(true);
// We MUST have an argument ..!
if (args.length < 1) {
lwc.sendSimpleUsage(sender, "/lwc details <HistoryId>");
return;
}
// Load it ..
int historyId;
try {
historyId = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
sender.sendMessage(Colors.Red + "No results found.");
return;
}
// Try and load the history object
History history = lwc.getPhysicalDatabase().loadHistory(historyId);
if (history == null) {
sender.sendMessage(Colors.Red + "No results found.");
return;
}
// Can they access it?
if (!lwc.isAdmin(sender)) {
if (sender instanceof Player) {
// verify they actually OWN the history object
if (!history.getPlayer().equalsIgnoreCase(((Player) sender).getName())) {
// Make them think no results were found
sender.sendMessage(Colors.Red + "No results found.");
return;
}
}
}
// Now we can start telling them about it! wee
Protection protection = history.getProtection(); // If the protection still exists, that is
// if it was removed, hide the protection (if they're using SQLite, primary keys are reused)
if (history.hasKey("destroyer")) {
protection = null;
}
sender.sendMessage(" ");
sender.sendMessage("Created by: " + Colors.Yellow + history.getPlayer());
sender.sendMessage("Status: " + Colors.Yellow + history.getStatus());
sender.sendMessage("Type: " + Colors.Yellow + history.getType());
sender.sendMessage(" ");
sender.sendMessage("Protection: " + Colors.Yellow + (protection == null ? "Removed" : protection));
sender.sendMessage("Created by: " + Colors.Yellow + history.getString("creator"));
// if its been removed, it most likely will have this key
if (history.hasKey("destroyer")) {
sender.sendMessage("Removed by: " + Colors.Yellow + history.getString("destroyer"));
}
// If it had an economy charge, show it
if (history.hasKey("charge")) {
sender.sendMessage("Economy charge: " + Colors.Yellow + history.getDouble("charge") + " " + lwc.getCurrency().getMoneyName());
}
if (history.hasKey("discount")) {
sender.sendMessage(Colors.Yellow + "(discounted price)");
sender.sendMessage("Discount Id: " + Colors.Yellow + (history.hasKey("discountId") ? history.getString("discountId") : "n/a"));
}
String creation = null;
if (history.getTimestamp() > 0) {
creation = new Date(history.getTimestamp() * 1000L).toString();
}
sender.sendMessage(" ");
sender.sendMessage("Created on: " + Colors.Yellow + (creation == null ? "Unknown" : creation));
// Only show how long ago it was if we know when it was created
if (creation != null) {
sender.sendMessage(Colors.Yellow + StringUtils.timeToString((System.currentTimeMillis() / 1000L) - history.getTimestamp()) + " ago");
}
}
/**
* Called when /lwc history or /lwc h is used.
*
* @param event
*/
private void doHistoryCommand(LWCCommandEvent event) {
LWC lwc = event.getLWC();
CommandSender sender = event.getSender();
String[] args = event.getArgs();
event.setCancelled(true);
// Some vars we'll use more later on
boolean isWildcard = false;
int page = 1;
int historyCount = 0;
int pageCount = 0;
// If it's the console without arguments, lookup for every player
if (!(sender instanceof Player) && args.length == 0) {
isWildcard = true;
}
// The player name to try to parse
String playerName = null;
// check for page number
if (args.length > 0) {
try {
page = Integer.parseInt(args[0]);
if (args.length > 1) {
playerName = args[1];
}
} catch (NumberFormatException e) {
playerName = args[0];
// look for page number
if (args.length > 1) {
try {
page = Integer.parseInt(args[1]);
} catch (NumberFormatException ex) {
}
}
}
}
// check for player string
if (playerName != null) {
// Are they an admin, to be able to do that?
if (!lwc.isAdmin(sender)) {
// Nope, go away!
lwc.sendLocale(sender, "protection.accessdenied");
return;
} else {
// Using a wildcard?
if (playerName.equals("*")) {
isWildcard = true;
}
}
} else {
// if they're a player, look themselves up
if (sender instanceof Player) {
playerName = ((Player) sender).getName();
}
}
// Check if a player was STILL not found and just apply defaults if so
if (playerName == null) {
if (!(sender instanceof Player)) {
isWildcard = true;
} else {
playerName = ((Player) sender).getName();
}
}
// Get the first page
List<History> relatedHistory;
if (isWildcard) {
relatedHistory = lwc.getPhysicalDatabase().loadHistory((page - 1) * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
historyCount = lwc.getPhysicalDatabase().getHistoryCount();
} else {
relatedHistory = lwc.getPhysicalDatabase().loadHistory(playerName, (page - 1) * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
historyCount = lwc.getPhysicalDatabase().getHistoryCount(playerName);
}
// Calculate page count
if (historyCount > 0) {
pageCount = (int) Math.floor(historyCount / (page * ITEMS_PER_PAGE));
// compensate for what's left
while ((pageCount * ITEMS_PER_PAGE) < historyCount) {
pageCount ++;
}
}
// Were there any usable results?
if (relatedHistory.size() == 0) {
sender.sendMessage(Colors.Red + "No results found.");
return;
}
// Is it a valid page? (this normally will NOT happen, with the previous statement in place!)
if (page > pageCount) {
sender.sendMessage(Colors.Red + "Page not found.");
return;
}
// Send it to them
String format = "%4s%12s%12s%12s";
// Header
sender.sendMessage(" ");
sender.sendMessage("To view extended details on a history item, use " + Colors.Yellow + "/lwc details <HistoryId>");
sender.sendMessage(" ");
sender.sendMessage(Colors.Yellow + String.format(format, "Id", "Player", "Type", "Status"));
sender.sendMessage(Colors.Yellow + "Showing " + relatedHistory.size() + " results on page: " + page + "/" + pageCount + " (" + historyCount + " total)");
// Send all that is found to them
for (History history : relatedHistory) {
sender.sendMessage(String.format(format, ("" + history.getId()), history.getPlayer(), history.getType(), history.getStatus()));
}
}
@Override
public void onCommand(LWCCommandEvent event) {
if (event.isCancelled()) {
return;
}
if (event.hasFlag("h", "history")) {
doHistoryCommand(event);
} else if (event.hasFlag("d", "details")) {
doDetailsCommand(event);
}
}
}