Initial commit

This commit is contained in:
Ian Adam Naval 2014-05-22 04:23:55 -07:00
commit cc790900f8
9 changed files with 1009 additions and 0 deletions

7
.classpath Normal file
View File

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

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DisciplineRecord</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

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

11
config.yml Normal file
View File

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

67
plugin.yml Normal file
View File

@ -0,0 +1,67 @@
name: DisciplineRecord
main: com.ianonavy.disciplinerecord.Main
version: 1.0
website: http://www.ianonavy.com/
description: Allows admins to custom configure consequences for repeat rule offenders and keeps record of the number of offenses.
author: ianonavy
commands:
dr:
description: Manages the server's Discipline Record. Type /dr help for more info.
usage: /dr <action>
permissions:
disciplinerecord.*:
description: Gives access to all Discipline Record commands
children:
disciplinerecord.minor: true
disciplinerecord.major: true
disciplinerecord.check: true
disciplinerecord.checkminor: true
disciplinerecord.checkmajor: true
disciplinerecord.checkmajor.others: true
disciplinerecord.setminor: true
disciplinerecord.setmajor: true
disciplinerecord.pardonminor: true
disciplinerecord.pardonmajor: true
disciplinerecord.tallyonly:
description: Only gives access to /dr major, /dr minor and /dr check others. Users with this permission cannot pardon.
children:
disciplinerecord.minor: true
disciplinerecord.major: true
disciplinerecord.check: true
disciplinerecord.checkminor: true
disciplinerecord.checkmajor: true
disciplinerecord.checkmajor.others: true
disciplinerecord.setminor: false
disciplinerecord.setmajor: false
disciplinerecord.pardonminor: false
disciplinerecord.pardonmajor: false
disciplinerecord.minor:
description: Allows you to add minor offenses to the the discipline record of a user.
default: op
disciplinerecord.major:
description: Allows you to add major offenses to the the discipline record of a user.
default: op
disciplinerecord.check:
description: Allows you to check how many offenses you have.
default: true
disciplinerecord.check.others:
description: Allows you to check how many offenses someone else has.
default: op
disciplinerecord.checkminor:
description: Allows you to check how many minor offenses you have.
default: true
disciplinerecord.checkmajor:
description: Allows you to check how many major offenses you have.
default: true
disciplinerecord.setminor:
description: Allows you to set the number of minor offenses of any user to a specific number.
default: op
disciplinerecord.setmajor:
description: Allows you to set the number of major offenses of any user to a specific number.
default: op
disciplinerecord.pardonminor:
description: Allows you to pardon all minor offenses of any user.
default: op
disciplinerecord.pardonmajor:
description: Allows you to pardon all major offenses of any user.
default: op

0
record.yml Normal file
View File

View File

@ -0,0 +1,619 @@
package com.ianonavy.disciplinerecord;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Plugin for Minecraft Bukkit servers that manages a discipline record.
*
* @author ianonavy
* @version 1.0
*/
public class Main extends JavaPlugin {
boolean enabled;
// Get the logger
public final Logger logger = Logger.getLogger("Minecraft");
// Configuration files
File configFile;
File recordFile;
FileConfiguration config;
FileConfiguration record;
Recorder recorder;
// Tag used for server logs.
static String pluginTag = ChatColor.YELLOW + "[Discipline Record] " + ChatColor.WHITE;
/**
* Constructor for the Discipline Record plugin.
*/
public Main() {
enabled = true;
}
/**
* Called when the plugin is disabled.
*/
@Override
public void onDisable() {
saveYamls();
PluginDescriptionFile pdfFile = this.getDescription();
this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " disabled.");
}
/**
* Called when the plugin is enabled.
*/
@Override
public void onEnable() {
PluginDescriptionFile pdfFile = this.getDescription();
// Open files.
configFile = new File(getDataFolder(), "config.yml");
recordFile = new File(getDataFolder(), "record.yml");
try {
firstRun();
} catch (Exception e) {
e.printStackTrace();
}
// Load configuration and record.
config = new YamlConfiguration();
record = new YamlConfiguration();
loadYamls();
// Check enabled.
if (config.isSet("enabled")) {
if (!config.getBoolean("enabled", true)) {
this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " disabled in config.yml.");
enabled = false;
return;
}
} else {
config.set("enabled", true);
}
Server server = Bukkit.getServer();
recorder = new Recorder(server, (YamlConfiguration) config, (YamlConfiguration) record, configFile, recordFile);
enabled = true;
this.logger.info("[" + pdfFile.getName() + "] v. " + pdfFile.getVersion() + " enabled.");
}
/**
* Sets up the default config files for the first run.
*/
private void firstRun() throws Exception {
if(!configFile.exists()){
configFile.getParentFile().mkdirs();
copy(getResource("config.yml"), configFile);
}
if(!recordFile.exists()){
recordFile.getParentFile().mkdirs();
copy(getResource("record.yml"), recordFile);
}
}
/**
* Copies a file from one to another
* @param in the input stream to copy the original file from
* @param file the output file that serves as the copy destination
*/
private void copy(InputStream in, File file) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Saves the record YAML.
*/
public void saveYamls() {
try {
record.save(recordFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Loads the configuration file and record YAML files.
*/
public void loadYamls() {
try {
config.load(configFile);
record.load(recordFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Command listener which passes commands on top separate methods for further execution.
* However, it also checks for permissions and whether the plugin is enabled in the config.
* @param sender the player who sent the command
* @param command the command object to be handled
* @param command_string a string version of the command name
* @param args the arguments passed to the command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String commandString, String[] args) {
if (enabled && commandString.equalsIgnoreCase("dr")) {
if (args.length > 0) {
String effectiveCommand = args[0];
if (effectiveCommand.equalsIgnoreCase("minor")) {
if (sender.hasPermission("disciplinerecord.minor")) {
return minorOffense(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("major")) {
if (sender.hasPermission("disciplinerecord.major")) {
return majorOffense(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("check")) {
if (sender.hasPermission("disciplinerecord.check")) {
return check(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("checkminor")) {
if (sender.hasPermission("disciplinerecord.checkminor")) {
return checkMinor(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("checkmajor")) {
if (sender.hasPermission("disciplinerecord.checkmajor")) {
return checkMajor(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("setminor")) {
if (sender.hasPermission("disciplinerecord.setminor")) {
return setMinor(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("setmajor")) {
if (sender.hasPermission("disciplinerecord.setmajor")) {
return setMajor(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("pardonminor")) {
if (sender.hasPermission("disciplinerecord.pardonminor")) {
return pardonMinor(sender, args);
}
} else if (effectiveCommand.equalsIgnoreCase("pardonmajor")) {
if (sender.hasPermission("disciplinerecord.pardonmajor")) {
return pardonMajor(sender, args);
}
} else {
return help(sender, args);
}
} else {
return help(sender, args);
}
}
return true;
}
/**
* This command handler parses the arguments for a valid username. It only accesses the record
* if the player is currently online and sends messages to both the command sender and the
* player. However, it is very likely that the player will not see this message. In particular,
* this handler adds a single minor offense to the record for a player.
* @param sender the player who sent the command
* @param args the arguments passed to this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean minorOffense(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
String reason = "";
if (args.length > 1) {
playerName = args[1];
if (args.length > 2) {
for (int i = 2; i < args.length; i++) {
reason += args[i] + " ";
}
}
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name.
recorder.incrementMinor(playerName, reason);
sender.sendMessage(pluginTag + "Minor offense tally added to " + player.getDisplayName());
player.sendMessage(pluginTag + "A " + ChatColor.YELLOW + "minor offense" + ChatColor.WHITE + " has been added to your discipline record!");
if (!reason.isEmpty()) {
player.sendMessage(ChatColor.BLUE + "Reason: " + ChatColor.WHITE + reason);
}
int numOffenses = recorder.getMinor(playerName);
player.sendMessage(pluginTag + "You now have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " minor offense" + ((numOffenses == 1) ? "." : "s."));
} else {
sender.sendMessage(pluginTag + ChatColor.RED + playerName + " is not online!");
}
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr minor <name> [reason]");
return true; // Suppress default usage message.
}
}
/**
* This command handler parses the arguments for a valid username. It only accesses the record
* if the player is currently online and sends messages to both the command sender and the
* player. However, it is very likely that the player will not see this message. In particular,
* this handler adds a single major offense to the record for a player.
* @param sender the player who sent the command
* @param args the arguments passed to this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean majorOffense(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
String reason = "";
if (args.length > 1) {
playerName = args[1];
if (args.length > 2) {
for (int i = 2; i < args.length; i++) {
reason += args[i] + " ";
}
}
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name.
recorder.incrementMajor(playerName, reason);
sender.sendMessage(pluginTag + " Major offense tally added to " + player.getDisplayName());
player.sendMessage(pluginTag + "A " + ChatColor.RED + "major offense" + ChatColor.WHITE + " has been added to your discipline record!");
if (!reason.isEmpty()) {
player.sendMessage(ChatColor.BLUE + "Reason: " + ChatColor.WHITE + reason);
}
int numOffenses = recorder.getMajor(playerName);
player.sendMessage(pluginTag + "You now have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " major offense" + ((numOffenses == 1) ? "." : "s."));
} else {
sender.sendMessage(pluginTag + ChatColor.RED + playerName + " is not online!");
}
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr major <name> [reason]");
return true; // Suppress default usage message.
}
}
/**
* This command is intended to be used by any player who wants to check the total number of
* offenses they have in the record.
* @param sender the player wishing to check his or her record
* @param args the arguments passed to this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean check(CommandSender sender, String[] args) {
if (args.length > 1) { // If checking another player name,
String playerName = args[1];
if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
int numMinor = recorder.getMinor(playerName);
int numMajor = recorder.getMajor(playerName);
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."));
} else {
sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
}
return true;
} else {
int numMinor = recorder.getMinor(sender.getName());
int numMajor = recorder.getMajor(sender.getName());
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."));
return true;
}
}
/**
* This command is intended to be used by any player who wants to check the number of minor
* offenses they have in the record.
* @param sender the player wishing to check his or her record
* @param args the arguments passed to this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean checkMinor(CommandSender sender, String[] args) {
if (args.length > 1) { // If checking another player name,
String playerName = args[1];
if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
int numMinor = recorder.getMinor(playerName);
sender.sendMessage(pluginTag + playerName + " has " + ChatColor.RED + numMinor + ChatColor.WHITE + " minor offense" + ((numMinor == 1) ? "." : "s."));
} else {
sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
}
return true;
} else {
int numOffenses = recorder.getMinor(sender.getName());
sender.sendMessage(pluginTag + "You have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " minor offense" + ((numOffenses == 1) ? "." : "s."));
return true;
}
}
/**
* This command is intended to be used by any player who wants to check the number of major
* offenses they have in the record.
* @param sender the player wishing to check his or her record
* @param args the arguments passed to this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean checkMajor(CommandSender sender, String[] args) {
if (args.length > 1) { // If checking another player name,
String playerName = args[1];
if (sender.hasPermission("disciplinerecord.check.others")) { // check permissions
int numMajor = recorder.getMajor(playerName);
sender.sendMessage(pluginTag + playerName + " has " + ChatColor.RED + numMajor + ChatColor.WHITE + " major offense" + ((numMajor == 1) ? "." : "s."));
return true;
} else {
sender.sendMessage(pluginTag + ChatColor.RED + "You do not have permission to check others' discipline record!");
}
return true;
} else {
int numOffenses = recorder.getMajor(sender.getName());
sender.sendMessage(pluginTag + "You have " + ChatColor.RED + numOffenses + ChatColor.WHITE + " major offense" + ((numOffenses == 1) ? "." : "s."));
return true;
}
}
/**
* This command specifically sets the number of minor offenses of a player to a particular
* number. It does not require the user to be online.
* @param sender the sender of the command
* @param args the arguments associated with this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean setMinor(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
int number = 0;
if (args.length > 2) {
playerName = args[1];
try {
number = Integer.parseInt(args[2]);
} catch (Exception ex) {
sender.sendMessage(pluginTag + "Invalid number!");
return false;
}
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name if online.
player.sendMessage(pluginTag + "The number of minor offenses you have has been reset to " + ChatColor.RED + number);
}
recorder.setMinor(playerName, number);
sender.sendMessage(pluginTag + " Set number of minor offenses for " + playerName + " to " + number);
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr setminor <name> <number>");
return true; // Suppress default usage message.
}
}
/**
* This command specifically sets the number of major offenses of a player to a particular
* number. It does not require the user to be online.
* @param sender the sender of the command
* @param args the arguments associated with this command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean setMajor(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
int number = 0;
if (args.length > 2) {
playerName = args[1];
try {
number = Integer.parseInt(args[2]);
} catch (Exception ex) {
sender.sendMessage("Invalid number!");
return false;
}
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name if online.
player.sendMessage(pluginTag + "The number of major offenses you have has been reset to " + ChatColor.RED + number);
}
recorder.setMajor(playerName, number);
sender.sendMessage(pluginTag + " Set number of major offenses for " + playerName + " to " + number);
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr setmajor <name> <number>");
return true; // Suppress default usage message.
}
}
/**
* This command pardons a player of all of his minor offenses.
* @param sender the sender of this command
* @param args an array containing a single element which is the username of the player to
* pardon
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean pardonMinor(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
if (args.length > 1) {
playerName = args[1];
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name if online.
player.sendMessage(pluginTag + "Good news! All of your minor offenses have been pardoned.");
}
recorder.pardonMinor(playerName);
sender.sendMessage(pluginTag + "Pardoning all minor offenses for " + playerName);
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr pardonminor <name>");
return true; // Suppress default usage message.
}
}
/**
* This command pardons a player of all of his major offenses.
* @param sender the sender of this command
* @param args an array containing a single element which is the username of the player to
* pardon
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean pardonMajor(CommandSender sender, String[] args) {
Server server = Bukkit.getServer();
String playerName = "";
if (args.length > 1) {
playerName = args[1];
Player player = server.getPlayer(playerName);
if (player != null) {
playerName = player.getName(); // Gets the full name if online.
player.sendMessage("Good news! All of your major offenses have been pardoned.");
}
recorder.pardonMajor(playerName);
sender.sendMessage(pluginTag + " Pardoning all major offenses for " + playerName);
return true;
} else {
sender.sendMessage(pluginTag + " Usage: /dr pardonmajor <name>");
return true; // Suppress default usage message.
}
}
/**
* Displays the appropriate help context.
* @param sender the sender of the command
* @param args the arguments passed to the command
* @return whether the syntax of the command was correct (i.e. correct number of arguments)
*/
public boolean help(CommandSender sender, String[] args) {
if (args.length > 1 && args[0].equalsIgnoreCase("help")) { // If command passed to help
String command = args[1];
String commandName, description, usage = "";
// Check if the user has permission to see that command.
if (sender.hasPermission("disciplinerecord.minor") && command.equalsIgnoreCase("minor")) {
commandName = "minor";
description = "adds a minor offense to a player's Discipline Record. Optionally add a reason.";
usage = "/dr minor <player> [reason]";
} else if (sender.hasPermission("disciplinerecord.major") && command.equalsIgnoreCase("major")) {
commandName = "major";
description = "adds a major offense to a player's Discipline Record. Optionally add a reason.";
usage = "/dr major <player> [reason]";
} else if (sender.hasPermission("disciplinerecord.check") && command.equalsIgnoreCase("check")) {
commandName = "check";
description = "checks your discipline record and tells you how many offenses you have.";
usage = "/dr check";
if (sender.hasPermission("disciplinerecord.check.others")) {
description += " Optionally, you can specify another player's name and check his or her record.";
usage += " [player]";
}
} else if (sender.hasPermission("disciplinerecord.checkminor") && command.equalsIgnoreCase("checkminor")) {
commandName = "checkminor";
description = "checks your discipline record and tells you how many minor offenses you have.";
usage = "/dr checkminor";
if (sender.hasPermission("disciplinerecord.check.others")) {
description += " Optionally, you can specify another player's name and check his or her record.";
usage += " [player]";
}
} else if (sender.hasPermission("disciplinerecord.checkmajor") && command.equalsIgnoreCase("checkmajor")) {
commandName = "checkmajor";
description = "checks your discipline record and tells you how many minor offenses you have.";
usage = "/dr checkmajor";
if (sender.hasPermission("disciplinerecord.check.others")) {
description += " Optionally, you can specify another player's name and check his or her record.";
usage += " [player]";
}
} else if (sender.hasPermission("disciplinerecord.setminor") && command.equalsIgnoreCase("setminor")) {
commandName = "setminor";
description = "sets a player's number of minor offenses to a specific number.";
usage = "/dr setminor <player> <number>";
} else if (sender.hasPermission("disciplinerecord.setmajor") && command.equalsIgnoreCase("setmajor")) {
commandName = "setmajor";
description = "sets a player's number of major offenses to a specific number.";
usage = "/dr setmajor <player> <number>";
} else if (sender.hasPermission("disciplinerecord.pardonminor") && command.equalsIgnoreCase("pardonminor")) {
commandName = "pardonminor";
description = "pardon's a player of all minor offenses.";
usage = "/dr pardonminor <player>";
} else if (sender.hasPermission("disciplinerecord.pardonmajor") && command.equalsIgnoreCase("pardonmajor")) {
commandName = "pardonmajor";
description = "pardon's a player of all major offenses.";
usage = "/dr pardonmajor <player>";
} else {
sendDefaultHelp(sender);
return true;
}
sender.sendMessage("");
sender.sendMessage(ChatColor.GREEN + commandName + " " + ChatColor.WHITE + description);
sender.sendMessage("");
sender.sendMessage(ChatColor.GRAY + "Usage: " + ChatColor.YELLOW + usage);
return true;
} else {
sendDefaultHelp(sender);
return true;
}
}
public void sendDefaultHelp(CommandSender sender) {
sender.sendMessage("");
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.");
sender.sendMessage("");
String availableCommands = "";
ChatColor color = ChatColor.GREEN;
if (sender.hasPermission("disciplinerecord.minor")) availableCommands += color + "minor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.major")) availableCommands += color + "major" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.check")) availableCommands += color + "check" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.checkminor")) availableCommands += color + "checkminor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.checkmajor")) availableCommands += color + "checkmajor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.setminor")) availableCommands += color + "setminor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.setmajor")) availableCommands += color + "setmajor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.pardonminor")) availableCommands += color + "pardonminor" + ChatColor.WHITE + ", ";
if (sender.hasPermission("disciplinerecord.pardonmajor")) availableCommands += color + "pardonmajor" + ChatColor.WHITE + ", ";
availableCommands = availableCommands.substring(0, availableCommands.length() - 2); // Remove trailing comma.
sender.sendMessage(ChatColor.RED + "Commands: " + ChatColor.WHITE + availableCommands);
sender.sendMessage("");
sender.sendMessage("Type /dr help <command> for more info about each command.");
}
}

View File

@ -0,0 +1,275 @@
package com.ianonavy.disciplinerecord;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
/**
* This class handles all of the YAML record reading and writing. It also handles kicking and
* banning events based on the configuration file.
* @author ianonavy
*/
public class Recorder {
Server server;
YamlConfiguration yamlConfig;
YamlConfiguration yamlRecord;
File configFile;
File recordFile;
/**
* Constructor for the recorder.
* @param server the server where all the players currently reside.
* @param yamlConfig the YAML configuration object for the config
* @param yamlRecord the YAML configuration object for the record
* @param configFile the configuration file object
* @param recordFile the record file object
*/
public Recorder(Server server, YamlConfiguration yamlConfig, YamlConfiguration yamlRecord, File configFile, File recordFile) {
this.server = server;
this.yamlConfig = yamlConfig;
this.yamlRecord = yamlRecord;
this.configFile = configFile;
this.recordFile = recordFile;
}
/**
* Increases the number of minor offenses in the record for a particular player by 1.
* @param playerName the name of the offending player
* @param reason the reason for the infraction
*/
public void incrementMinor(String playerName, String reason) {
setMinor(playerName, getMinor(playerName) + 1, reason);
}
/**
* Increases the number of major offenses in the record for a particular player by 1.
* @param playerName the name of the offending player
* @param reason the reason for the infraction
*/
public void incrementMajor(String playerName, String reason) {
setMajor(playerName, getMajor(playerName) + 1, reason);
}
/**
* Sets the number of minor offenses in the record for a particular player to 0.
* @param playerName the name of the offending player
*/
public void pardonMinor(String playerName) {
setMinor(playerName, 0);
}
/**
* Sets the number of major offenses in the record for a particular player to 0.
* @param playerName the name of the offending player
*/
public void pardonMajor(String playerName) {
setMajor(playerName, 0);
}
/**
* Sets the number of minor offenses in the record for a particular player to a particular
* number. Overloads with a blank reason.
* @param playerName the name of the offending player
* @param number the number of offenses to set in the record
*/
public void setMinor(String playerName, int number) {
setMinor(playerName, number, null);
}
/**
* Sets the number of minor offenses in the record for a particular player to a particular
* number.
* @param playerName the name of the offending player
* @param number the number of offenses to set in the record
* @string the optional reason for the infraction
*/
public void setMinor(String playerName, int number, String reason) {
reloadYaml();
String path = playerName + ".minor";
yamlRecord.set(path, number);
saveYaml();
handleConsequences(playerName, true, reason);
}
/**
* Sets the number of major offenses in the record for a particular player to a particular
* number. Overloads with a blank reason.
* @param playerName the name of the offending player
* @param number the number of offenses to set in the record
*/
public void setMajor(String playerName, int number) {
setMajor(playerName, number, null);
}
/**
* Sets the number of major offenses in the record for a particular player to a particular
* number.
* @param playerName the name of the offending player
* @param number the number of offenses to set in the record
* @string the optional reason for the infraction
*/
public void setMajor(String playerName, int number, String reason) {
reloadYaml();
String path = playerName + ".major";
yamlRecord.set(path, number);
saveYaml();
handleConsequences(playerName, false, reason);
}
/**
* Reads and returns the number of minor offenses from the record.
* @param playerName the player whose offenses are being looked up
* @return the number of minor offenses from the record.
*/
public int getMinor(String playerName) {
reloadYaml();
// Check existing for record.
String path = playerName + ".minor";
if (yamlRecord.isSet(path)) {
return yamlRecord.getInt(path);
}
return 0; // If the record does not exist, return 0.
}
/**
* Reads and returns the number of major offenses from the record.
* @param playerName the player whose offenses are being looked up
* @return the number of major offenses from the record.
*/
public int getMajor(String playerName) {
reloadYaml();
// Check existing for record.
String path = playerName + ".major";
if (yamlRecord.isSet(path)) {
return yamlRecord.getInt(path);
}
return 0; // If the record does not exist, return 0.
}
/**
* Reloads the configuration file and record file.
*/
public void reloadYaml() {
try {
yamlConfig.load(configFile);
yamlRecord.load(recordFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Only saves the record YAML file.
*/
public void saveYaml() {
try {
yamlRecord.save(recordFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Handles the consequences from changing the number of offenses for a user. However, it does
* not automatically undo any bans or IP bans.
* @param playerName the name of the player to handle
* @param minor whether to handle minor offenses instead of major offenses
*/
@SuppressWarnings("unchecked")
public void handleConsequences(String playerName, boolean minor, String reason) {
int numOffenses = 0;
String consequence = "";
List<String> consequenceList;
if (reason == null) {
reason = "";
}
// Get the list of consequences from the YAML file.
try {
if (minor) {
numOffenses = getMinor(playerName);
consequenceList = (List<String>) yamlConfig.getList("consequences.minor");
} else {
numOffenses = getMajor(playerName);
consequenceList = (List<String>) yamlConfig.getList("consequences.major");
}
} catch (Exception ex) {
System.out.println(Main.pluginTag + " Failed to load consequences!");
return;
}
// Get the lowest matching consequence for the current number of offenses.
for (int i = 0; i < consequenceList.size(); i++) {
String currentConsequence = consequenceList.get(i);
int consequenceValue = Integer.parseInt(currentConsequence.substring(0, currentConsequence.indexOf(" ")));
if (numOffenses >= consequenceValue) {
consequence = currentConsequence;
}
}
CommandSender console = server.getConsoleSender();
// Tell the player the number offenses in the kick message.
String message = " You committed " + (minor ? "minor" : "major") + " offense #" + numOffenses + ".";
if (!reason.isEmpty()) {
message += " Reason: " + reason;
}
// Attempt to unban the player if there are no consequences for the current number of
// offenses, just in case he or she was pardoned.
if (consequence.isEmpty()) {
server.dispatchCommand(console, "unban " + playerName);
return;
}
// Parses and handles "kick" sending a message.
if (consequence.contains("kick")) {
server.dispatchCommand(console, "kick " + playerName + message);
return;
}
// Handle all types of bans.
if (consequence.contains("ban")) {
if (consequence.contains("ip")) {
// IP Ban
String ip = getPlayerIP(server.getPlayer(playerName));
server.banIP(ip);
server.dispatchCommand(console, "kick " + playerName + message);
} else if (consequence.contains("tempban")) {
// Temporary ban
int time = 1; // default to 1 numerical value
String unit = "d"; // default to days
try {
// Get the numerical value of the time.
time = Integer.parseInt(consequence.split(" ")[2]); // minutes
} catch (Exception ex) {
// Ignore bad formatting.
}
// Parse the unit from the configuration entry.
if (consequence.contains("hour")) unit = "h";
else if (consequence.contains("minutes")) unit = "m";
else if (consequence.contains("week")) unit = "w";
// Execute the temporary ban.
server.dispatchCommand(console, "tempban " + playerName + " " + time + unit + " " + message);
} else if (consequence.contains("permanent")) {
// Permanent ban has a separate message.
server.dispatchCommand(console, "ban " + playerName + " You have been permanently banned for breaking the rules.");
}
}
}
/**
* Returns a player's IP address for IP banning purposes.
* @param player the offending player
* @return the IP address of the player
*/
public String getPlayerIP(Player player) {
return player.getAddress().toString().substring(1).split(":")[0];
}
}