Browse Source

Initial commit

Ian Adam Naval 4 years ago
commit
cc790900f8

+ 7
- 0
.classpath View File

@@ -0,0 +1,7 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" path="src"/>
4
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5
+	<classpathentry kind="lib" path="/home/ian/Downloads/craftbukkit-1.2.5-R1.4-20120512.060225-7.jar"/>
6
+	<classpathentry kind="output" path="bin"/>
7
+</classpath>

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+bin/

+ 17
- 0
.project View File

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>DisciplineRecord</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>org.eclipse.jdt.core.javabuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+	</buildSpec>
14
+	<natures>
15
+		<nature>org.eclipse.jdt.core.javanature</nature>
16
+	</natures>
17
+</projectDescription>

+ 12
- 0
.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,12 @@
1
+#Sat Jul 02 15:06:31 EDT 2011
2
+eclipse.preferences.version=1
3
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6
+org.eclipse.jdt.core.compiler.compliance=1.6
7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
9
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12
+org.eclipse.jdt.core.compiler.source=1.6

+ 11
- 0
config.yml View File

@@ -0,0 +1,11 @@
1
+enabled: true
2
+consequences:
3
+  minor:
4
+  - 1 kick
5
+  - 10 tempban 24 hours
6
+  - 25 ban permanent
7
+  major:
8
+  - 1 kick
9
+  - 2 tempban 24 hours
10
+  - 4 ban permanent
11
+  - 5 ban ip

+ 67
- 0
plugin.yml View File

@@ -0,0 +1,67 @@
1
+name: DisciplineRecord
2
+main: com.ianonavy.disciplinerecord.Main
3
+version: 1.0
4
+website: http://www.ianonavy.com/
5
+description: Allows admins to custom configure consequences for repeat rule offenders and keeps record of the number of offenses.
6
+author: ianonavy
7
+commands:
8
+    dr:
9
+        description: Manages the server's Discipline Record. Type /dr help for more info.
10
+        usage: /dr <action>
11
+permissions:
12
+    disciplinerecord.*:
13
+        description: Gives access to all Discipline Record commands
14
+        children:
15
+            disciplinerecord.minor: true
16
+            disciplinerecord.major: true
17
+            disciplinerecord.check: true
18
+            disciplinerecord.checkminor: true
19
+            disciplinerecord.checkmajor: true
20
+            disciplinerecord.checkmajor.others: true
21
+            disciplinerecord.setminor: true
22
+            disciplinerecord.setmajor: true
23
+            disciplinerecord.pardonminor: true
24
+            disciplinerecord.pardonmajor: true
25
+    disciplinerecord.tallyonly:
26
+        description: Only gives access to /dr major, /dr minor and /dr check others. Users with this permission cannot pardon.
27
+        children:
28
+            disciplinerecord.minor: true
29
+            disciplinerecord.major: true
30
+            disciplinerecord.check: true
31
+            disciplinerecord.checkminor: true
32
+            disciplinerecord.checkmajor: true
33
+            disciplinerecord.checkmajor.others: true
34
+            disciplinerecord.setminor: false
35
+            disciplinerecord.setmajor: false
36
+            disciplinerecord.pardonminor: false
37
+            disciplinerecord.pardonmajor: false
38
+    disciplinerecord.minor:
39
+        description: Allows you to add minor offenses to the the discipline record of a user. 
40
+        default: op
41
+    disciplinerecord.major:
42
+        description: Allows you to add major offenses to the the discipline record of a user.
43
+        default: op
44
+    disciplinerecord.check:
45
+        description: Allows you to check how many offenses you have.
46
+        default: true
47
+    disciplinerecord.check.others:
48
+        description: Allows you to check how many offenses someone else has.
49
+        default: op
50
+    disciplinerecord.checkminor:
51
+        description: Allows you to check how many minor offenses you have.
52
+        default: true
53
+    disciplinerecord.checkmajor:
54
+        description: Allows you to check how many major offenses you have.
55
+        default: true
56
+    disciplinerecord.setminor:
57
+        description: Allows you to set the number of minor offenses of any user to a specific number.
58
+        default: op
59
+    disciplinerecord.setmajor:
60
+        description: Allows you to set the number of major offenses of any user to a specific number.
61
+        default: op
62
+    disciplinerecord.pardonminor:
63
+        description: Allows you to pardon all minor offenses of any user.
64
+        default: op
65
+    disciplinerecord.pardonmajor:
66
+        description: Allows you to pardon all major offenses of any user.
67
+        default: op

+ 0
- 0
record.yml View File


+ 619
- 0
src/com/ianonavy/disciplinerecord/Main.java View File

@@ -0,0 +1,619 @@
1
+package com.ianonavy.disciplinerecord;
2
+
3
+import java.io.File;
4
+import java.io.FileOutputStream;
5
+import java.io.IOException;
6
+import java.io.InputStream;
7
+import java.io.OutputStream;
8
+import java.util.logging.Logger;
9
+
10
+import org.bukkit.Bukkit;
11
+import org.bukkit.ChatColor;
12
+import org.bukkit.Server;
13
+import org.bukkit.command.Command;
14
+import org.bukkit.command.CommandSender;
15
+import org.bukkit.configuration.file.FileConfiguration;
16
+import org.bukkit.configuration.file.YamlConfiguration;
17
+import org.bukkit.entity.Player;
18
+import org.bukkit.plugin.PluginDescriptionFile;
19
+import org.bukkit.plugin.java.JavaPlugin;
20
+
21
+/**
22
+ * Plugin for Minecraft Bukkit servers that manages a discipline record. 
23
+ * 
24
+ * @author ianonavy
25
+ * @version 1.0
26
+ */
27
+public class Main extends JavaPlugin {
28
+
29
+	boolean enabled;
30
+	
31
+	// Get the logger
32
+	public final Logger logger = Logger.getLogger("Minecraft");
33
+	
34
+	
35
+	// Configuration files
36
+	File configFile;
37
+    File recordFile;
38
+    FileConfiguration config;
39
+    FileConfiguration record;
40
+    
41
+    Recorder recorder;
42
+
43
+    // Tag used for server logs.
44
+	static String pluginTag = ChatColor.YELLOW + "[Discipline Record] " + ChatColor.WHITE;
45
+			
46
+	/**
47
+	 * Constructor for the Discipline Record plugin.
48
+	 */
49
+	public Main() {
50
+		enabled = true;
51
+	}
52
+	
53
+	/**
54
+	 * Called when the plugin is disabled.
55
+	 */
56
+	@Override
57
+	public void onDisable() {
58
+		saveYamls();
59
+		PluginDescriptionFile pdfFile = this.getDescription();
60
+		this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " disabled.");		
61
+	}
62
+
63
+	/**
64
+	 * Called when the plugin is enabled.
65
+	 */
66
+	@Override
67
+	public void onEnable() {
68
+		PluginDescriptionFile pdfFile = this.getDescription();
69
+		
70
+		// Open files.
71
+		configFile = new File(getDataFolder(), "config.yml");
72
+	    recordFile = new File(getDataFolder(), "record.yml");
73
+	    
74
+	    try {
75
+            firstRun();
76
+        } catch (Exception e) {
77
+            e.printStackTrace();
78
+        }
79
+	    
80
+	    // Load configuration and record.
81
+	    config = new YamlConfiguration();
82
+	    record = new YamlConfiguration();
83
+	    loadYamls();
84
+	    
85
+	    // Check enabled.
86
+	    if (config.isSet("enabled")) {
87
+	    	if (!config.getBoolean("enabled", true)) {
88
+	    		this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " disabled in config.yml.");
89
+	    		enabled = false;
90
+	    		return;
91
+	    	}
92
+	    } else {
93
+	    	config.set("enabled", true);
94
+	    }
95
+	    Server server = Bukkit.getServer();
96
+	    recorder = new Recorder(server, (YamlConfiguration) config, (YamlConfiguration) record, configFile, recordFile);
97
+	    
98
+	    enabled = true;
99
+		this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " enabled.");
100
+	}
101
+	
102
+	/**
103
+	 * Sets up the default config files for the first run.
104
+	 */
105
+	private void firstRun() throws Exception {
106
+	    if(!configFile.exists()){
107
+	        configFile.getParentFile().mkdirs();
108
+	        copy(getResource("config.yml"), configFile);
109
+	    }
110
+	    if(!recordFile.exists()){
111
+	        recordFile.getParentFile().mkdirs();
112
+	        copy(getResource("record.yml"), recordFile);
113
+	    }
114
+	}
115
+	
116
+	/**
117
+	 * Copies a file from one to another
118
+	 * @param in the input stream to copy the original file from
119
+	 * @param file the output file that serves as the copy destination
120
+	 */
121
+	private void copy(InputStream in, File file) {
122
+	    try {
123
+	        OutputStream out = new FileOutputStream(file);
124
+	        byte[] buf = new byte[1024];
125
+	        int len;
126
+	        while((len=in.read(buf))>0){
127
+	            out.write(buf,0,len);
128
+	        }
129
+	        out.close();
130
+	        in.close();
131
+	    } catch (Exception e) {
132
+	        e.printStackTrace();
133
+	    }
134
+	}
135
+	
136
+	/**
137
+	 * Saves the record YAML.
138
+	 */
139
+	public void saveYamls() {
140
+	    try {
141
+	        record.save(recordFile);
142
+	    } catch (IOException e) {
143
+	        e.printStackTrace();
144
+	    }
145
+	}
146
+	
147
+	/**
148
+	 * Loads the configuration file and record YAML files.
149
+	 */
150
+	public void loadYamls() {
151
+	    try {
152
+	        config.load(configFile);
153
+	        record.load(recordFile);
154
+	    } catch (Exception e) {
155
+	        e.printStackTrace();
156
+	    }
157
+	}
158
+	
159
+	/**
160
+	 * Command listener which passes commands on top separate methods for further execution.
161
+	 * However, it also checks for permissions and whether the plugin is enabled in the config.
162
+	 * @param sender the player who sent the command
163
+	 * @param command the command object to be handled
164
+	 * @param command_string a string version of the command name
165
+	 * @param args the arguments passed to the command
166
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
167
+	 */
168
+	@Override
169
+	public boolean onCommand(CommandSender sender, Command command, String commandString, String[] args) {
170
+		if (enabled && commandString.equalsIgnoreCase("dr")) {
171
+			if (args.length > 0) {
172
+				String effectiveCommand = args[0];
173
+				
174
+				if (effectiveCommand.equalsIgnoreCase("minor")) {
175
+					if (sender.hasPermission("disciplinerecord.minor")) {
176
+						return minorOffense(sender, args);
177
+					}
178
+				} else if (effectiveCommand.equalsIgnoreCase("major")) {
179
+					if (sender.hasPermission("disciplinerecord.major")) {
180
+						return majorOffense(sender, args);
181
+					}
182
+				} else if (effectiveCommand.equalsIgnoreCase("check")) {
183
+					if (sender.hasPermission("disciplinerecord.check")) {
184
+						return check(sender, args);
185
+					}
186
+			    } else if (effectiveCommand.equalsIgnoreCase("checkminor")) {
187
+					if (sender.hasPermission("disciplinerecord.checkminor")) {
188
+						return checkMinor(sender, args);
189
+					}
190
+				} else if (effectiveCommand.equalsIgnoreCase("checkmajor")) {
191
+					if (sender.hasPermission("disciplinerecord.checkmajor")) {
192
+						return checkMajor(sender, args);
193
+					}
194
+				} else if (effectiveCommand.equalsIgnoreCase("setminor")) {
195
+					if (sender.hasPermission("disciplinerecord.setminor")) {
196
+						return setMinor(sender, args);
197
+					}
198
+				} else if (effectiveCommand.equalsIgnoreCase("setmajor")) {
199
+					if (sender.hasPermission("disciplinerecord.setmajor")) {
200
+						return setMajor(sender, args);
201
+					}
202
+				} else if (effectiveCommand.equalsIgnoreCase("pardonminor")) {
203
+					if (sender.hasPermission("disciplinerecord.pardonminor")) {
204
+						return pardonMinor(sender, args);
205
+					}
206
+				} else if (effectiveCommand.equalsIgnoreCase("pardonmajor")) {
207
+					if (sender.hasPermission("disciplinerecord.pardonmajor")) {
208
+						return pardonMajor(sender, args);
209
+					}
210
+				} else {
211
+					return help(sender, args);
212
+				}
213
+			} else {
214
+				return help(sender, args);
215
+			}
216
+		}
217
+		return true;
218
+	}
219
+	
220
+	/**
221
+	 * This command handler parses the arguments for a valid username. It only accesses the record
222
+	 * if the player is currently online and sends messages to both the command sender and the 
223
+	 * player. However, it is very likely that the player will not see this message. In particular,
224
+	 * this handler adds a single minor offense to the record for a player.
225
+	 * @param sender the player who sent the command
226
+	 * @param args the arguments passed to this command
227
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
228
+	 */
229
+	public boolean minorOffense(CommandSender sender, String[] args) {
230
+		Server server = Bukkit.getServer();
231
+		String playerName = "";
232
+		String reason = "";
233
+		
234
+		if (args.length > 1) {
235
+			playerName = args[1];
236
+			if (args.length > 2) {
237
+				for (int i = 2; i < args.length; i++) {
238
+					reason += args[i] + " ";
239
+				}
240
+			}
241
+			Player player = server.getPlayer(playerName);
242
+			if (player != null) {
243
+				playerName = player.getName(); // Gets the full name.
244
+				recorder.incrementMinor(playerName, reason);
245
+				sender.sendMessage(pluginTag + "Minor offense tally added to " + player.getDisplayName());
246
+				player.sendMessage(pluginTag + "A " + ChatColor.YELLOW + "minor offense" + ChatColor.WHITE + " has been added to your discipline record!");
247
+				if (!reason.isEmpty()) {
248
+					player.sendMessage(ChatColor.BLUE + "Reason: " + ChatColor.WHITE + reason);
249
+				}
250
+				int numOffenses = recorder.getMinor(playerName);
251
+				player.sendMessage(pluginTag + "You now have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " minor offense" + ((numOffenses == 1) ? "." : "s."));
252
+			} else {
253
+				sender.sendMessage(pluginTag + ChatColor.RED + playerName + " is not online!");
254
+			}
255
+			return true;
256
+		} else {
257
+			sender.sendMessage(pluginTag + " Usage: /dr minor <name> [reason]");
258
+			return true; // Suppress default usage message.
259
+		}
260
+	}
261
+	
262
+	/**
263
+	 * This command handler parses the arguments for a valid username. It only accesses the record
264
+	 * if the player is currently online and sends messages to both the command sender and the 
265
+	 * player. However, it is very likely that the player will not see this message. In particular,
266
+	 * this handler adds a single major offense to the record for a player.
267
+	 * @param sender the player who sent the command
268
+	 * @param args the arguments passed to this command
269
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
270
+	 */
271
+	public boolean majorOffense(CommandSender sender, String[] args) {
272
+		Server server = Bukkit.getServer();
273
+		String playerName = "";
274
+		String reason = "";
275
+		
276
+		if (args.length > 1) {
277
+			playerName = args[1];
278
+			if (args.length > 2) {
279
+				for (int i = 2; i < args.length; i++) {
280
+					reason += args[i] + " ";
281
+				}
282
+			}
283
+			Player player = server.getPlayer(playerName);
284
+			if (player != null) {
285
+				playerName = player.getName(); // Gets the full name.
286
+				recorder.incrementMajor(playerName, reason);
287
+				sender.sendMessage(pluginTag + " Major offense tally added to " + player.getDisplayName());
288
+				player.sendMessage(pluginTag + "A " + ChatColor.RED + "major offense" + ChatColor.WHITE + " has been added to your discipline record!");
289
+				if (!reason.isEmpty()) {
290
+					player.sendMessage(ChatColor.BLUE + "Reason: " + ChatColor.WHITE + reason);
291
+				}
292
+				int numOffenses = recorder.getMajor(playerName);
293
+				player.sendMessage(pluginTag + "You now have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " major offense" + ((numOffenses == 1) ? "." : "s."));
294
+			} else {
295
+				sender.sendMessage(pluginTag + ChatColor.RED + playerName + " is not online!");
296
+			}
297
+			return true;
298
+		} else {
299
+			sender.sendMessage(pluginTag + " Usage: /dr major <name> [reason]");
300
+			return true; // Suppress default usage message.
301
+		}
302
+	}
303
+	
304
+	/**
305
+	 * This command is intended to be used by any player who wants to check the total number of
306
+	 * offenses they have in the record.
307
+	 * @param sender the player wishing to check his or her record
308
+	 * @param args the arguments passed to this command
309
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
310
+	 */
311
+	public boolean check(CommandSender sender, String[] args) {
312
+		if (args.length > 1) { // If checking another player name,
313
+			String playerName = args[1];
314
+			
315
+			if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
316
+				int numMinor = recorder.getMinor(playerName);
317
+				int numMajor = recorder.getMajor(playerName);
318
+				sender.sendMessage(pluginTag + playerName + " has " + ChatColor.RED + numMinor + ChatColor.WHITE + " minor offense" + ((numMinor == 1) ? "" : "s") + " and " + ChatColor.RED + numMajor + ChatColor.WHITE + " major offense" + ((numMajor == 1) ? "." : "s."));	
319
+			} else {
320
+				sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
321
+			}
322
+			
323
+			return true;
324
+		} else {
325
+			int numMinor = recorder.getMinor(sender.getName());
326
+			int numMajor = recorder.getMajor(sender.getName());
327
+			sender.sendMessage(pluginTag + "You have " + ChatColor.RED + numMinor + ChatColor.WHITE + " minor offense" + ((numMinor == 1) ? "" : "s") + " and " + ChatColor.RED + numMajor + ChatColor.WHITE + " major offense" + ((numMajor == 1) ? "." : "s."));
328
+			return true;
329
+		}
330
+	}
331
+	
332
+	/**
333
+	 * This command is intended to be used by any player who wants to check the number of minor
334
+	 * offenses they have in the record.
335
+	 * @param sender the player wishing to check his or her record
336
+	 * @param args the arguments passed to this command
337
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
338
+	 */
339
+	public boolean checkMinor(CommandSender sender, String[] args) {
340
+		if (args.length > 1) { // If checking another player name,
341
+			String playerName = args[1];
342
+			
343
+			if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
344
+				int numMinor = recorder.getMinor(playerName);
345
+				sender.sendMessage(pluginTag + playerName + " has " + ChatColor.RED + numMinor + ChatColor.WHITE + " minor offense" + ((numMinor == 1) ? "." : "s."));
346
+			} else {
347
+				sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
348
+			}
349
+			return true;
350
+		} else {
351
+			int numOffenses = recorder.getMinor(sender.getName());
352
+			sender.sendMessage(pluginTag + "You have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " minor offense" + ((numOffenses == 1) ? "." : "s."));
353
+			return true;			
354
+		}
355
+	}
356
+	
357
+	/**
358
+	 * This command is intended to be used by any player who wants to check the number of major
359
+	 * offenses they have in the record.
360
+	 * @param sender the player wishing to check his or her record
361
+	 * @param args the arguments passed to this command
362
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
363
+	 */
364
+	public boolean checkMajor(CommandSender sender, String[] args) {
365
+		if (args.length > 1) { // If checking another player name, 
366
+			String playerName = args[1];
367
+			
368
+			if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
369
+				int numMajor = recorder.getMajor(playerName);
370
+				sender.sendMessage(pluginTag + playerName + " has " + ChatColor.RED + numMajor + ChatColor.WHITE + " major offense" + ((numMajor == 1) ? "." : "s."));
371
+				return true;
372
+			} else {
373
+				sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
374
+			}
375
+			return true;
376
+		} else {
377
+			int numOffenses = recorder.getMajor(sender.getName());
378
+			sender.sendMessage(pluginTag + "You have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " major offense" + ((numOffenses == 1) ? "." : "s."));
379
+			return true;
380
+		}
381
+	}
382
+	
383
+	/**
384
+	 * This command specifically sets the number of minor offenses of a player to a particular
385
+	 * number. It does not require the user to be online.
386
+	 * @param sender the sender of the command
387
+	 * @param args the arguments associated with this command
388
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
389
+	 */
390
+	public boolean setMinor(CommandSender sender, String[] args) {
391
+		Server server = Bukkit.getServer();
392
+		String playerName = "";
393
+		int number = 0;
394
+		
395
+		if (args.length > 2) {
396
+			playerName = args[1];
397
+			try {
398
+				number = Integer.parseInt(args[2]);
399
+			} catch (Exception ex) {
400
+				sender.sendMessage(pluginTag + "Invalid number!");
401
+				return false;
402
+			}
403
+			
404
+			Player player = server.getPlayer(playerName);
405
+			if (player != null) {
406
+				playerName = player.getName(); // Gets the full name if online.
407
+				player.sendMessage(pluginTag + "The number of minor offenses you have has been reset to " + ChatColor.RED + number);
408
+			}
409
+			recorder.setMinor(playerName, number);
410
+			sender.sendMessage(pluginTag + " Set number of minor offenses for " + playerName + " to " + number);
411
+			return true;
412
+		} else {
413
+			sender.sendMessage(pluginTag + " Usage: /dr setminor <name> <number>");
414
+			return true; // Suppress default usage message.
415
+		}
416
+	}
417
+	
418
+	/**
419
+	 * This command specifically sets the number of major offenses of a player to a particular
420
+	 * number. It does not require the user to be online.
421
+	 * @param sender the sender of the command
422
+	 * @param args the arguments associated with this command
423
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
424
+	 */
425
+	public boolean setMajor(CommandSender sender, String[] args) {
426
+		Server server = Bukkit.getServer();
427
+		String playerName = "";
428
+		int number = 0;
429
+		
430
+		if (args.length > 2) {
431
+			playerName = args[1];
432
+			try {
433
+				number = Integer.parseInt(args[2]);
434
+			} catch (Exception ex) {
435
+				sender.sendMessage("Invalid number!");
436
+				return false;
437
+			}
438
+			
439
+			Player player = server.getPlayer(playerName);
440
+			if (player != null) {
441
+				playerName = player.getName(); // Gets the full name if online.
442
+				player.sendMessage(pluginTag + "The number of major offenses you have has been reset to " + ChatColor.RED + number);
443
+			}
444
+			recorder.setMajor(playerName, number);
445
+			sender.sendMessage(pluginTag  + " Set number of major offenses for " + playerName + " to " + number);
446
+			return true;
447
+		} else {
448
+			sender.sendMessage(pluginTag + " Usage: /dr setmajor <name> <number>");
449
+			return true; // Suppress default usage message.
450
+		}
451
+	}
452
+	
453
+	/**
454
+	 * This command pardons a player of all of his minor offenses.
455
+	 * @param sender the sender of this command
456
+	 * @param args an array containing a single element which is the username of the player to 
457
+	 * pardon
458
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
459
+	 */
460
+	public boolean pardonMinor(CommandSender sender, String[] args) {
461
+		Server server = Bukkit.getServer();
462
+		String playerName = "";
463
+		
464
+		if (args.length > 1) {
465
+			playerName = args[1];
466
+			Player player = server.getPlayer(playerName);
467
+			if (player != null) {
468
+				playerName = player.getName(); // Gets the full name if online.
469
+				player.sendMessage(pluginTag + "Good news! All of your minor offenses have been pardoned.");
470
+				
471
+			}
472
+			recorder.pardonMinor(playerName);
473
+			sender.sendMessage(pluginTag + "Pardoning all minor offenses for " + playerName);
474
+			return true;
475
+		} else {
476
+			sender.sendMessage(pluginTag + " Usage: /dr pardonminor <name>");
477
+			return true; // Suppress default usage message.
478
+		}
479
+	}
480
+	
481
+	/**
482
+	 * This command pardons a player of all of his major offenses.
483
+	 * @param sender the sender of this command
484
+	 * @param args an array containing a single element which is the username of the player to 
485
+	 * pardon
486
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
487
+	 */
488
+	public boolean pardonMajor(CommandSender sender, String[] args) {
489
+		Server server = Bukkit.getServer();
490
+		String playerName = "";
491
+		
492
+		if (args.length > 1) {
493
+			playerName = args[1];
494
+			Player player = server.getPlayer(playerName);
495
+			if (player != null) {
496
+				playerName = player.getName(); // Gets the full name if online.
497
+				player.sendMessage("Good news! All of your major offenses have been pardoned.");
498
+			}
499
+			recorder.pardonMajor(playerName);
500
+			sender.sendMessage(pluginTag + " Pardoning all major offenses for " + playerName);
501
+			return true;
502
+		} else {
503
+			sender.sendMessage(pluginTag + " Usage: /dr pardonmajor <name>");
504
+			return true; // Suppress default usage message.
505
+		}
506
+	}
507
+	
508
+	/**
509
+	 * Displays the appropriate help context.
510
+	 * @param sender the sender of the command
511
+	 * @param args the arguments passed to the command
512
+	 * @return whether the syntax of the command was correct (i.e. correct number of arguments)
513
+	 */
514
+	public boolean help(CommandSender sender, String[] args) {
515
+		if (args.length > 1 && args[0].equalsIgnoreCase("help")) { // If command passed to help
516
+			String command = args[1];
517
+			String commandName, description, usage = "";
518
+			
519
+			// Check if the user has permission to see that command.
520
+			if (sender.hasPermission("disciplinerecord.minor") && command.equalsIgnoreCase("minor")) {
521
+				commandName = "minor";
522
+				description = "adds a minor offense to a player's Discipline Record. Optionally add a reason.";
523
+			    usage = "/dr minor <player> [reason]";
524
+			    
525
+			} else if (sender.hasPermission("disciplinerecord.major") && command.equalsIgnoreCase("major")) {
526
+				commandName = "major";
527
+				description = "adds a major offense to a player's Discipline Record. Optionally add a reason.";
528
+			    usage = "/dr major <player> [reason]";
529
+			    
530
+			} else if (sender.hasPermission("disciplinerecord.check") && command.equalsIgnoreCase("check")) {
531
+				commandName = "check";
532
+				description = "checks your discipline record and tells you how many offenses you have.";
533
+			    usage = "/dr check";
534
+			    
535
+			    if (sender.hasPermission("disciplinerecord.check.others")) {
536
+			    	description += " Optionally, you can specify another player's name and check his or her record.";
537
+				    usage += " [player]";
538
+			    }
539
+
540
+			} else if (sender.hasPermission("disciplinerecord.checkminor") && command.equalsIgnoreCase("checkminor")) {
541
+				commandName = "checkminor";
542
+				description = "checks your discipline record and tells you how many minor offenses you have.";
543
+			    usage = "/dr checkminor";
544
+			    
545
+			    if (sender.hasPermission("disciplinerecord.check.others")) {
546
+			    	description += " Optionally, you can specify another player's name and check his or her record.";
547
+				    usage += " [player]";
548
+			    }
549
+			    
550
+			} else if (sender.hasPermission("disciplinerecord.checkmajor") && command.equalsIgnoreCase("checkmajor")) {
551
+				commandName = "checkmajor";
552
+				description = "checks your discipline record and tells you how many minor offenses you have.";
553
+			    usage = "/dr checkmajor";
554
+			    
555
+			    if (sender.hasPermission("disciplinerecord.check.others")) {
556
+			    	description += " Optionally, you can specify another player's name and check his or her record.";
557
+				    usage += " [player]";
558
+			    }
559
+			    
560
+			} else if (sender.hasPermission("disciplinerecord.setminor") && command.equalsIgnoreCase("setminor")) {
561
+				commandName = "setminor";
562
+				description = "sets a player's number of minor offenses to a specific number.";
563
+			    usage = "/dr setminor <player> <number>";
564
+			    
565
+			} else if (sender.hasPermission("disciplinerecord.setmajor") && command.equalsIgnoreCase("setmajor")) {
566
+				commandName = "setmajor";
567
+				description = "sets a player's number of major offenses to a specific number.";
568
+			    usage = "/dr setmajor <player> <number>";
569
+			    
570
+			} else if (sender.hasPermission("disciplinerecord.pardonminor") && command.equalsIgnoreCase("pardonminor")) {
571
+				commandName = "pardonminor";
572
+				description = "pardon's a player of all minor offenses.";
573
+			    usage = "/dr pardonminor <player>";
574
+			    
575
+			} else if (sender.hasPermission("disciplinerecord.pardonmajor") && command.equalsIgnoreCase("pardonmajor")) {
576
+				commandName = "pardonmajor";
577
+				description = "pardon's a player of all major offenses.";
578
+			    usage = "/dr pardonmajor <player>";
579
+			    
580
+			} else {
581
+				sendDefaultHelp(sender);
582
+				return true;
583
+			}
584
+
585
+			sender.sendMessage("");
586
+			sender.sendMessage(ChatColor.GREEN + commandName + " " + ChatColor.WHITE + description);
587
+			sender.sendMessage("");
588
+			sender.sendMessage(ChatColor.GRAY + "Usage: " + ChatColor.YELLOW + usage);
589
+			return true;
590
+		} else {
591
+			sendDefaultHelp(sender);
592
+			return true;
593
+		}
594
+	}
595
+	
596
+	public void sendDefaultHelp(CommandSender sender) {
597
+		sender.sendMessage("");
598
+		sender.sendMessage(ChatColor.YELLOW + "Discipline Record" + ChatColor.WHITE + " is a plugin that helps keep a record of the number of times that players break rules. There are " + ChatColor.GREEN + "two" + ChatColor.WHITE + " types of rules: " + ChatColor.YELLOW + "minor" + ChatColor.WHITE + " and " + ChatColor.RED + "major.");
599
+		sender.sendMessage("");
600
+		String availableCommands = "";
601
+		
602
+		ChatColor color = ChatColor.GREEN;
603
+		if (sender.hasPermission("disciplinerecord.minor")) availableCommands += color + "minor" + ChatColor.WHITE + ", ";
604
+		if (sender.hasPermission("disciplinerecord.major")) availableCommands += color + "major" + ChatColor.WHITE + ", ";
605
+		if (sender.hasPermission("disciplinerecord.check")) availableCommands += color + "check" + ChatColor.WHITE + ", ";
606
+		if (sender.hasPermission("disciplinerecord.checkminor")) availableCommands += color + "checkminor" + ChatColor.WHITE + ", ";
607
+		if (sender.hasPermission("disciplinerecord.checkmajor")) availableCommands += color + "checkmajor" + ChatColor.WHITE + ", ";
608
+		if (sender.hasPermission("disciplinerecord.setminor")) availableCommands += color + "setminor" + ChatColor.WHITE + ", ";
609
+		if (sender.hasPermission("disciplinerecord.setmajor")) availableCommands += color + "setmajor" + ChatColor.WHITE + ", ";
610
+		if (sender.hasPermission("disciplinerecord.pardonminor")) availableCommands += color + "pardonminor" + ChatColor.WHITE + ", ";
611
+		if (sender.hasPermission("disciplinerecord.pardonmajor")) availableCommands += color + "pardonmajor" + ChatColor.WHITE + ", ";
612
+		availableCommands = availableCommands.substring(0, availableCommands.length() - 2); // Remove trailing comma.
613
+		   
614
+		sender.sendMessage(ChatColor.RED + "Commands: " + ChatColor.WHITE + availableCommands);
615
+		sender.sendMessage("");
616
+		sender.sendMessage("Type /dr help <command> for more info about each command.");
617
+	}
618
+
619
+}

+ 275
- 0
src/com/ianonavy/disciplinerecord/Recorder.java View File

@@ -0,0 +1,275 @@
1
+package com.ianonavy.disciplinerecord;
2
+
3
+import java.io.File;
4
+import java.io.IOException;
5
+import java.util.List;
6
+
7
+import org.bukkit.Server;
8
+import org.bukkit.command.CommandSender;
9
+import org.bukkit.configuration.file.YamlConfiguration;
10
+import org.bukkit.entity.Player;
11
+
12
+/**
13
+ * This class handles all of the YAML record reading and writing. It also handles kicking and
14
+ * banning events based on the configuration file.
15
+ * @author ianonavy
16
+ */
17
+public class Recorder {
18
+	
19
+	Server server;
20
+	YamlConfiguration yamlConfig;
21
+	YamlConfiguration yamlRecord;
22
+	File configFile;
23
+	File recordFile;
24
+	
25
+	/**
26
+	 * Constructor for the recorder.
27
+	 * @param server the server where all the players currently reside.
28
+	 * @param yamlConfig the YAML configuration object for the config
29
+	 * @param yamlRecord the YAML configuration object for the record
30
+	 * @param configFile the configuration file object
31
+	 * @param recordFile the record file object
32
+	 */
33
+	public Recorder(Server server, YamlConfiguration yamlConfig, YamlConfiguration yamlRecord, File configFile, File recordFile) {
34
+		this.server = server;
35
+		this.yamlConfig = yamlConfig;
36
+		this.yamlRecord = yamlRecord;
37
+		this.configFile = configFile;
38
+		this.recordFile = recordFile;
39
+	}
40
+	
41
+	/**
42
+	 * Increases the number of minor offenses in the record for a particular player by 1.
43
+	 * @param playerName the name of the offending player 
44
+	 * @param reason the reason for the infraction
45
+	 */
46
+	public void incrementMinor(String playerName, String reason) {
47
+		setMinor(playerName, getMinor(playerName) + 1, reason);
48
+	}
49
+	
50
+	/**
51
+	 * Increases the number of major offenses in the record for a particular player by 1.
52
+	 * @param playerName the name of the offending player 
53
+	 * @param reason the reason for the infraction
54
+	 */
55
+	public void incrementMajor(String playerName, String reason) {
56
+		setMajor(playerName, getMajor(playerName) + 1, reason);
57
+	}
58
+	
59
+	/**
60
+	 * Sets the number of minor offenses in the record for a particular player to 0.
61
+	 * @param playerName the name of the offending player 
62
+	 */
63
+	public void pardonMinor(String playerName) {
64
+		setMinor(playerName, 0);
65
+	}
66
+	
67
+	/**
68
+	 * Sets the number of major offenses in the record for a particular player to 0.
69
+	 * @param playerName the name of the offending player 
70
+	 */
71
+	public void pardonMajor(String playerName) {
72
+		setMajor(playerName, 0);
73
+	}
74
+	
75
+	/**
76
+	 * Sets the number of minor offenses in the record for a particular player to a particular
77
+	 * number. Overloads with a blank reason.
78
+	 * @param playerName the name of the offending player
79
+	 * @param number the number of offenses to set in the record 
80
+	 */
81
+	public void setMinor(String playerName, int number) {
82
+		setMinor(playerName, number, null);
83
+	}
84
+	
85
+	/**
86
+	 * Sets the number of minor offenses in the record for a particular player to a particular
87
+	 * number.
88
+	 * @param playerName the name of the offending player
89
+	 * @param number the number of offenses to set in the record 
90
+	 * @string the optional reason for the infraction
91
+	 */
92
+	public void setMinor(String playerName, int number, String reason) {
93
+		reloadYaml();
94
+		String path = playerName + ".minor";
95
+		yamlRecord.set(path, number);
96
+		saveYaml();
97
+		handleConsequences(playerName, true, reason);
98
+	}
99
+	
100
+	/**
101
+	 * Sets the number of major offenses in the record for a particular player to a particular
102
+	 * number. Overloads with a blank reason.
103
+	 * @param playerName the name of the offending player
104
+	 * @param number the number of offenses to set in the record 
105
+	 */
106
+	public void setMajor(String playerName, int number) {
107
+		setMajor(playerName, number, null);
108
+	}
109
+	
110
+	/**
111
+	 * Sets the number of major offenses in the record for a particular player to a particular
112
+	 * number.
113
+	 * @param playerName the name of the offending player
114
+	 * @param number the number of offenses to set in the record 
115
+	 * @string the optional reason for the infraction
116
+	 */
117
+	public void setMajor(String playerName, int number, String reason) {
118
+		reloadYaml();
119
+		String path = playerName + ".major";
120
+		yamlRecord.set(path, number);
121
+		saveYaml();
122
+		handleConsequences(playerName, false, reason);
123
+	}
124
+	
125
+	/**
126
+	 * Reads and returns the number of minor offenses from the record. 
127
+	 * @param playerName the player whose offenses are being looked up
128
+	 * @return the number of minor offenses from the record.
129
+	 */
130
+	public int getMinor(String playerName) {
131
+		reloadYaml();
132
+		// Check existing for record.
133
+		String path = playerName + ".minor";
134
+		if (yamlRecord.isSet(path)) {
135
+			return yamlRecord.getInt(path);
136
+		}
137
+		return 0; // If the record does not exist, return 0.
138
+	}
139
+	
140
+	/**
141
+	 * Reads and returns the number of major offenses from the record. 
142
+	 * @param playerName the player whose offenses are being looked up
143
+	 * @return the number of major offenses from the record.
144
+	 */
145
+	public int getMajor(String playerName) {
146
+		reloadYaml();
147
+		// Check existing for record.
148
+		String path = playerName + ".major";
149
+		if (yamlRecord.isSet(path)) {
150
+			return yamlRecord.getInt(path);
151
+		}
152
+		return 0; // If the record does not exist, return 0.
153
+	}
154
+	
155
+	/**
156
+	 * Reloads the configuration file and record file.
157
+	 */
158
+	public void reloadYaml() {
159
+		try {
160
+			yamlConfig.load(configFile);
161
+			yamlRecord.load(recordFile);
162
+		} catch (Exception e) {
163
+			e.printStackTrace();
164
+		}
165
+	}
166
+	
167
+	/**
168
+	 * Only saves the record YAML file.
169
+	 */
170
+	public void saveYaml() {
171
+		try {
172
+	        yamlRecord.save(recordFile);
173
+	    } catch (IOException e) {
174
+	        e.printStackTrace();
175
+	    }
176
+	}
177
+	
178
+	/**
179
+	 * Handles the consequences from changing the number of offenses for a user. However, it does
180
+	 * not automatically undo any bans or IP bans. 
181
+	 * @param playerName the name of the player to handle
182
+	 * @param minor whether to handle minor offenses instead of major offenses
183
+	 */
184
+	@SuppressWarnings("unchecked")
185
+	public void handleConsequences(String playerName, boolean minor, String reason) {
186
+		int numOffenses = 0;
187
+		String consequence = "";
188
+		List<String> consequenceList;
189
+		if (reason == null) {
190
+			reason = "";
191
+		}
192
+		
193
+		// Get the list of consequences from the YAML file.
194
+		try {
195
+			if (minor) {
196
+				numOffenses = getMinor(playerName);
197
+				consequenceList = (List<String>) yamlConfig.getList("consequences.minor");
198
+			} else {
199
+				numOffenses = getMajor(playerName);
200
+				consequenceList = (List<String>) yamlConfig.getList("consequences.major");
201
+			}
202
+		} catch (Exception ex) {
203
+			System.out.println(Main.pluginTag + " Failed to load consequences!");
204
+			return;
205
+		}
206
+
207
+		// Get the lowest matching consequence for the current number of offenses.
208
+		for (int i = 0; i < consequenceList.size(); i++) {
209
+			String currentConsequence = consequenceList.get(i);
210
+			int consequenceValue = Integer.parseInt(currentConsequence.substring(0, currentConsequence.indexOf(" ")));
211
+			if (numOffenses >= consequenceValue) {
212
+				consequence = currentConsequence;
213
+			}
214
+		}
215
+		
216
+		CommandSender console = server.getConsoleSender();
217
+		// Tell the player the number offenses in the kick message.
218
+		String message = " You committed " + (minor ? "minor" : "major") + " offense #" + numOffenses + ".";
219
+		if (!reason.isEmpty()) {
220
+			message += " Reason: " + reason;			
221
+		}
222
+		
223
+		// Attempt to unban the player if there are no consequences for the current number of
224
+		// offenses, just in case he or she was pardoned.
225
+		if (consequence.isEmpty()) {
226
+			server.dispatchCommand(console, "unban " + playerName);
227
+			return;
228
+		}
229
+		
230
+		// Parses and handles "kick" sending a message.
231
+		if (consequence.contains("kick")) {
232
+			server.dispatchCommand(console, "kick " + playerName + message);
233
+			return;
234
+		}
235
+		// Handle all types of bans.
236
+		if (consequence.contains("ban")) {
237
+			if (consequence.contains("ip")) { 
238
+				// IP Ban
239
+				String ip = getPlayerIP(server.getPlayer(playerName));
240
+				server.banIP(ip);
241
+				server.dispatchCommand(console, "kick " + playerName + message);
242
+			} else if (consequence.contains("tempban")) {
243
+				// Temporary ban
244
+				int time = 1; // default to 1 numerical value
245
+				String unit = "d"; // default to days
246
+				try {
247
+					// Get the numerical value of the time.
248
+					time = Integer.parseInt(consequence.split(" ")[2]); // minutes
249
+				} catch (Exception ex) {
250
+					// Ignore bad formatting.
251
+				}
252
+				
253
+				// Parse the unit from the configuration entry.
254
+				if (consequence.contains("hour")) unit = "h";
255
+				else if (consequence.contains("minutes")) unit = "m";
256
+				else if (consequence.contains("week")) unit = "w";
257
+				
258
+				// Execute the temporary ban.
259
+				server.dispatchCommand(console, "tempban " + playerName + " " + time + unit + " " + message);
260
+			} else if (consequence.contains("permanent")) {
261
+				// Permanent ban has a separate message.
262
+				server.dispatchCommand(console, "ban " + playerName + " You have been permanently banned for breaking the rules.");
263
+			}
264
+		}
265
+	}
266
+	
267
+	/**
268
+	 * Returns a player's IP address for IP banning purposes.
269
+	 * @param player the offending player
270
+	 * @return the IP address of the player
271
+	 */
272
+	public String getPlayerIP(Player player) {
273
+        return player.getAddress().toString().substring(1).split(":")[0];
274
+    }
275
+}

Loading…
Cancel
Save