-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathButtonWarp.java
More file actions
368 lines (321 loc) · 12.3 KB
/
ButtonWarp.java
File metadata and controls
368 lines (321 loc) · 12.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
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
package com.codisimus.plugins.buttonwarp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import java.util.TreeMap;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import net.milkbowl.vault.economy.Economy;
/**
* Loads Plugin and manages Data/Permissions
*
* @author Codisimus
*/
public class ButtonWarp extends JavaPlugin {
static Server server;
static Logger logger;
static PluginManager pm;
static Plugin plugin;
static int defaultDays;
static int defaultHours;
static int defaultMinutes;
static int defaultSeconds;
static boolean defaultTakeItems;
static boolean defaultRestricted;
static int defaultMax;
static String dataFolder;
private static TreeMap<String, Warp> warps = new TreeMap<String, Warp>();
/**
* Loads this Plugin by doing the following:
* Loads the settings from the Config file
* Finds the Permission and Economy Plugins to use
* Loads the saved ButtonWarp data
* Registers the Events to listen for
*/
@Override
public void onEnable () {
//Metrics hook
try { new Metrics(this).start(); } catch (IOException e) {}
server = getServer();
logger = getLogger();
pm = server.getPluginManager();
plugin = this;
File dir = this.getDataFolder();
if (!dir.isDirectory()) {
dir.mkdir();
}
dataFolder = dir.getPath();
dir = new File(dataFolder+"/Warps");
if (!dir.isDirectory()) {
dir.mkdir();
}
ButtonWarpConfig.load();
//Find Economy
RegisteredServiceProvider<Economy> economyProvider =
getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
if (economyProvider != null) {
Econ.economy = economyProvider.getProvider();
}
//Load Warps Data
loadData();
//Register Events
pm.registerEvents(new ButtonWarpListener(), this);
pm.registerEvents(new ButtonWarpVehicleListener(), this);
if (ButtonWarpListener.delay > 0) {
pm.registerEvents(new ButtonWarpDelayListener(), this);
}
//Register the command found in the plugin.yml
ButtonWarpCommand.command = (String)this.getDescription().getCommands().keySet().toArray()[0];
getCommand(ButtonWarpCommand.command).setExecutor(new ButtonWarpCommand());
Properties version = new Properties();
try {
version.load(this.getResource("version.properties"));
} catch (Exception ex) {
logger.warning("version.properties file not found within jar");
}
logger.info("ButtonWarp "+this.getDescription().getVersion()+" (Build "+version.getProperty("Build")+") is enabled!");
}
/**
* Returns boolean value of whether the given player has the specific permission
*
* @param player The Player who is being checked for permission
* @param node The String of the permission, ex. admin
* @return true if the given player has the specific permission
*/
public static boolean hasPermission(Player player, String node) {
return player.hasPermission("buttonwarp." + node);
}
/**
* Loads Warps from the save file
* Saving is turned off if an error occurs
*/
public static void loadData() {
File[] files = plugin.getDataFolder().listFiles();
//Organize files
if (files != null) {
for (File file: files) {
String name = file.getName();
if (name.endsWith(".dat")) {
File dest = new File(dataFolder+"/Warps/"+name.substring(0, name.length() - 4)+".properties");
file.renameTo(dest);
}
}
}
files = new File(dataFolder+"/Warps/").listFiles();
FileInputStream fis = null;
for (File file: files) {
String name = file.getName();
if (name.endsWith(".properties")) {
try {
//Load the Properties file for reading
Properties p = new Properties();
fis = new FileInputStream(file);
p.load(fis);
//Construct a new Warp using the file name and values of message, amount, and source
Warp warp = new Warp(name.substring(0, name.length() - 11), ButtonWarpMessages.format(p.getProperty("Message")),
Double.parseDouble(p.getProperty("Amount")), p.getProperty("Source"));
if (p.containsKey("Location")) {
//Set the Location data
String[] location = p.getProperty("Location").split("'");
warp.world = location[0];
warp.x = Double.parseDouble(location[1]);
warp.y = Double.parseDouble(location[2]);
warp.z = Double.parseDouble(location[3]);
warp.pitch = Float.parseFloat(location[4]);
warp.yaw = Float.parseFloat(location[5]);
if (p.containsKey("IgnorePitch")) {
warp.ignorePitch = Boolean.parseBoolean(p.getProperty("IgnorePitch"));
}
if (p.containsKey("IgnoreYaw")) {
warp.ignoreYaw = Boolean.parseBoolean(p.getProperty("IgnoreYaw"));
}
}
if (p.containsKey("Commands")) {
String command = p.getProperty("Commands");
if (!command.equals("none")) {
warp.commands.addAll(Arrays.asList(command.split(", ")));
}
}
//Set the reset time
String[] resetTime = p.getProperty("ResetTime").split("'");
warp.days = Integer.parseInt(resetTime[0]);
warp.hours = Integer.parseInt(resetTime[1]);
warp.minutes = Integer.parseInt(resetTime[2]);
warp.seconds = Integer.parseInt(resetTime[3]);
//Set the reset type
warp.global = p.getProperty("ResetType").equals("global");
//Set whether the Warp is restricted
if (p.containsKey("Restricted")) {
warp.restricted = Boolean.parseBoolean(p.getProperty("Restricted"));
}
//Load the data of all the Buttons
if (p.containsKey("Buttons")) {
warp.setButtons(p.getProperty("Buttons"));
} else {
warp.setButtonsOld(p.getProperty("ButtonsData"));
}
warps.put(warp.name, warp);
file = new File(dataFolder + "/Warps/"
+ warp.name + ".warptimes");
if (file.exists()) {
fis = new FileInputStream(file);
warp.activationTimes.load(fis);
} else {
warp.save();
}
} catch (Exception loadFailed) {
logger.severe("Failed to load " + name);
loadFailed.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e) {
}
}
}
}
}
/**
* Invokes save() method for each Warp
*/
public static void saveAll() {
for (Warp warp: warps.values()) {
saveWarp(warp);
}
}
/**
* Writes the given Warp to its save file
* If the file already exists, it is overwritten
*
* @param warp The given Warp
*/
static void saveWarp(Warp warp) {
FileOutputStream fos = null;
try {
Properties p = new Properties();
if (warp.commands.isEmpty()) {
p.setProperty("Commands", "none");
} else {
String command = warp.commands.toString();
p.setProperty("Commands", command.substring(1, command.length() - 1));
}
p.setProperty("Message", ButtonWarpMessages.unformat(warp.msg));
p.setProperty("Amount", String.valueOf(warp.amount));
p.setProperty("Source", warp.source);
if (warp.world != null) {
p.setProperty("Location", warp.world+"'"+warp.x+"'"+warp.y+"'"+warp.z+"'"+warp.pitch+"'"+warp.yaw);
p.setProperty("IgnorePitch", String.valueOf(warp.ignorePitch));
p.setProperty("IgnoreYaw", String.valueOf(warp.ignoreYaw));
}
p.setProperty("ResetTime", warp.days+"'"+warp.hours+"'"+warp.minutes+"'"+warp.seconds);
p.setProperty("ResetType", warp.global ? "global" : "player");
p.setProperty("Restricted", String.valueOf(warp.restricted));
String value = "";
for (Button button: warp.buttons) {
value += "; " + button.toString();
}
if (!value.isEmpty()) {
value = value.substring(2);
}
p.setProperty("Buttons", value);
//Write the Warp Properties to file
fos = new FileOutputStream(dataFolder + "/Warps/" + warp.name + ".properties");
p.store(fos, null);
fos.close();
//Write the Warp activation times to file
fos = new FileOutputStream(dataFolder + "/Warps/" + warp.name + ".warptimes");
warp.activationTimes.store(fos, null);
} catch (Exception saveFailed) {
logger.severe("Save Failed!");
saveFailed.printStackTrace();
} finally {
try {
fos.close();
} catch (Exception e) {
}
}
}
/**
* Returns the Collection of all Warps
*
* @return The Collection of all Warps
*/
public static Collection<Warp> getWarps() {
return warps.values();
}
/**
* Adds the given Warp to the collection of Warps
*
* @param warp The given Warp
*/
public static void addWarp(Warp warp) {
warps.put(warp.name, warp);
warp.save();
}
/**
* Removes the given Warp from the collection of Warps
*
* @param warp The given Warp
*/
public static void removeWarp(Warp warp) {
warps.remove(warp.name);
File trash = new File(dataFolder+"/Warps/"+warp.name+".properties");
trash.delete();
trash = new File(dataFolder+"/Warps/"+warp.name+".warptimes");
trash.delete();
}
/**
* Reloads ButtonWarp data
*/
public static void rl() {
rl(null);
}
/**
* Reloads ButtonWarp data
*
* @param player The Player reloading the data
*/
public static void rl(Player player) {
warps.clear();
loadData();
logger.info("reloaded");
if (player != null) {
player.sendMessage("§5ButtonWarp reloaded");
}
}
/**
* Returns the Warp with the given name
*
* @param name The name of the Warp you wish to find
* @return The Warp with the given name or null if not found
*/
public static Warp findWarp(String name) {
return warps.get(name);
}
/**
* Returns the Warp that contains the given Block
*
* @param block The Block that is part of the Warp
* @return The Warp that contains the given Block or null if not found
*/
public static Warp findWarp(Block block) {
//Iterate through all Warps to find the one with the given Block
for (Warp warp: warps.values()) {
if (warp.findButton(block) != null) {
return warp;
}
}
//Return null because the Warp does not exist
return null;
}
}