diff --git a/src/de/diddiz/LogBlock/AreaBlockSearch.java b/src/de/diddiz/LogBlock/AreaBlockSearch.java index a5212f1..e6bdd6a 100644 --- a/src/de/diddiz/LogBlock/AreaBlockSearch.java +++ b/src/de/diddiz/LogBlock/AreaBlockSearch.java @@ -9,6 +9,7 @@ import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.logging.Level; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -22,8 +23,7 @@ public class AreaBlockSearch implements Runnable private Connection conn = null; private String table; - AreaBlockSearch(Connection conn, Player player, int type, int size, String table) - { + AreaBlockSearch(Connection conn, Player player, int type, int size, String table) { this.player = player; this.location = player.getLocation(); this.type = type; @@ -31,8 +31,8 @@ public class AreaBlockSearch implements Runnable this.conn = conn; this.table = table; } - public void run() - { + + public void run() { boolean hist = false; PreparedStatement ps = null; ResultSet rs = null; @@ -52,7 +52,7 @@ public class AreaBlockSearch implements Runnable ps.setInt(8, location.getBlockZ() + size); rs = ps.executeQuery(); - player.sendMessage("§3Block history within " + size + " blocks of " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + ": "); + player.sendMessage(ChatColor.DARK_AQUA + "Block history for " + getMaterialName(type) + " within " + size + " blocks of " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + ": "); while (rs.next()) { @@ -60,11 +60,11 @@ public class AreaBlockSearch implements Runnable String datestr = formatter.format(date); String msg = datestr + " " + rs.getString("player") + " (" + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + ") "; if (rs.getInt("type") == 0) - msg = msg + "destroyed " + Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' '); + msg = msg + "destroyed " + getMaterialName(rs.getInt("replaced")); else if (rs.getInt("replaced") == 0) - msg = msg + "created " + Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '); + msg = msg + "created " + getMaterialName(rs.getInt("type")); else - msg = msg + "replaced " + Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' ') + " with " + Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '); + msg = msg + "replaced " + getMaterialName(rs.getInt("replaced")) + " with " + getMaterialName(rs.getInt("type")); player.sendMessage("§6" + msg); hist = true; } @@ -85,4 +85,8 @@ public class AreaBlockSearch implements Runnable if (!hist) player.sendMessage("§3None."); } + + private String getMaterialName(int type) { + return Material.getMaterial(type).toString().toLowerCase().replace('_', ' '); + } } diff --git a/src/de/diddiz/LogBlock/Config.java b/src/de/diddiz/LogBlock/Config.java new file mode 100644 index 0000000..8b8b5f1 --- /dev/null +++ b/src/de/diddiz/LogBlock/Config.java @@ -0,0 +1,83 @@ +package de.diddiz.LogBlock; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.util.config.Configuration; + +public class Config { + static List worldNames; + static List worldTables; + static String dbDriver; + static String dbUrl; + static String dbUsername; + static String dbPassword; + static int keepLogDays; + static int delay; + static int defaultDist; + static int defaultTime; + static int toolID; + static int toolblockID; + static boolean toolblockRemove; + static boolean logExplosions; + static boolean logFire; + static boolean usePermissions; + + static boolean Load(Configuration config) { + config.load(); + List keys = config.getKeys(null); + if (!keys.contains("worldNames")) + config.setProperty("worldNames", Arrays.asList(new String[]{"world"})); + if (!keys.contains("worldTables")) + config.setProperty("worldTables", Arrays.asList(new String[]{"lb-main"})); + if (!keys.contains("driver")) + config.setProperty("driver", "com.mysql.jdbc.Driver"); + if (!keys.contains("url")) + config.setProperty("url", "jdbc:mysql://localhost:3306/db"); + if (!keys.contains("username")) + config.setProperty("username", "user"); + if (!keys.contains("password")) + config.setProperty("password", "pass"); + if (!keys.contains("keepLogDays")) + config.setProperty("keepLogDays", -1); + if (!keys.contains("delay")) + config.setProperty("delay", 6); + if (!keys.contains("defaultDist")) + config.setProperty("defaultDist", 20); + if (!keys.contains("defaultTime")) + config.setProperty("defaultTime", "30 minutes"); + if (!keys.contains("toolID")) + config.setProperty("toolID", 270); + if (!keys.contains("toolblockID")) + config.setProperty("toolblockID", 7); + if (!keys.contains("toolblockRemove")) + config.setProperty("toolblockRemove", true); + if (!keys.contains("logExplosions")) + config.setProperty("logExplosions", false); + if (!keys.contains("logFire")) + config.setProperty("logFire", false); + if (!keys.contains("usePermissions")) + config.setProperty("usePermissions", false); + if (!config.save()){ + LogBlock.log.severe("[LogBlock] Error while writing to config.yml"); + return false; + } + worldNames = config.getStringList("worldNames", null); + worldTables = config.getStringList("worldTables", null); + dbDriver = config.getString("driver"); + dbUrl = config.getString("url"); + dbUsername = config.getString("username"); + dbPassword = config.getString("password"); + keepLogDays = config.getInt("keepLogDays", -1); + delay = config.getInt("delay", 6); + defaultDist = config.getInt("defaultDist", 20); + defaultTime = LogBlock.parseTimeSpec(config.getString("defaultTime")); + toolID = config.getInt("toolID", 270); + toolblockID = config.getInt("toolblockID", 7); + toolblockRemove = config.getBoolean("toolblockRemove", true); + logExplosions = config.getBoolean("logExplosions", false); + logFire = config.getBoolean("logFire", false); + usePermissions = config.getBoolean("usePermissions", false); + return true; + } +} diff --git a/src/de/diddiz/LogBlock/LogBlock.java b/src/de/diddiz/LogBlock/LogBlock.java index 01b3351..327fd91 100644 --- a/src/de/diddiz/LogBlock/LogBlock.java +++ b/src/de/diddiz/LogBlock/LogBlock.java @@ -1,6 +1,5 @@ package de.diddiz.LogBlock; -import java.io.File; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; @@ -10,13 +9,14 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.text.SimpleDateFormat; -import java.util.List; +import java.util.ArrayList; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.command.Command; @@ -44,55 +44,22 @@ import com.nijikokun.bukkit.Permissions.Permissions; public class LogBlock extends JavaPlugin { static Logger log; - private List worldNames; - private List worldTables; - private boolean usePermissions = false; - private String dbDriver = "com.mysql.jdbc.Driver"; - private String dbUrl = ""; - private String dbUsername = ""; - private String dbPassword = ""; - private int delay = 6; - private int defaultDist = 20; - private int toolID = 270; - private int toolblockID = 7; - private int keepLogDays = -1; - private boolean toolblockRemove = true; - private boolean logExplosions = false; - private boolean logFire = false; private Consumer consumer = null; private LinkedBlockingQueue bqueue = new LinkedBlockingQueue(); + private ArrayList sessions = new ArrayList(); @Override public void onEnable() { log = getServer().getLogger(); try { - if (!new File (getDataFolder(), "config.yml").exists()) { - log.log(Level.SEVERE, "[LogBlock] Config not found"); - getServer().getPluginManager().disablePlugin(this); - return; - } - getConfiguration().load(); - dbDriver = getConfiguration().getString("driver", "com.mysql.jdbc.Driver"); - dbUrl = getConfiguration().getString("url", "jdbc:mysql://localhost:3306/db"); - dbUsername = getConfiguration().getString("username", "user"); - dbPassword = getConfiguration().getString("password", "pass"); - delay = getConfiguration().getInt("delay", 6); - toolID = getConfiguration().getInt("tool-id", 270); - toolblockID = getConfiguration().getInt("tool-block-id", 7); - toolblockRemove = getConfiguration().getBoolean("tool-block-remove", true); - defaultDist = getConfiguration().getInt("default-distance", 20); - keepLogDays = getConfiguration().getInt("keepLogDays", -1); - worldNames = getConfiguration().getStringList("worldNames", null); - worldTables = getConfiguration().getStringList("worldTables", null); - logExplosions = getConfiguration().getBoolean("logExplosions", false); - logFire = getConfiguration().getBoolean("logFire", false); - if (getConfiguration().getBoolean("usePermissions", false)) { + Config.Load(getConfiguration()); + if (Config.usePermissions) { if (getServer().getPluginManager().getPlugin("Permissions") != null) { - usePermissions = true; + Config.usePermissions = true; log.info("[LogBlock] Permissions enabled"); } else - log.info("[LogBlock] Permissions plugin not found. Use default permissions."); + log.info("[LogBlock] Permissions plugin not found. Using default permissions."); } } catch (Exception e) { log.log(Level.SEVERE, "[LogBlock] Exception while reading config.yml", e); @@ -100,25 +67,27 @@ public class LogBlock extends JavaPlugin return; } try { - new JDCConnectionDriver(dbDriver, dbUrl, dbUsername, dbPassword); + new JDCConnectionDriver(Config.dbDriver, Config.dbUrl, Config.dbUsername, Config.dbPassword); + Connection conn = getConnection(); + conn.close(); } catch (Exception ex) { log.log(Level.SEVERE, "[LogBlock] Exception while creation database connection", ex); getServer().getPluginManager().disablePlugin(this); return; } - if (worldNames == null || worldTables == null || worldNames.size() == 0 || worldNames.size() != worldTables.size()) { + if (Config.worldNames == null || Config.worldTables == null || Config.worldNames.size() == 0 || Config.worldNames.size() != Config.worldTables.size()) { log.log(Level.SEVERE, "[LogBlock] worldNames or worldTables not set porperly"); getServer().getPluginManager().disablePlugin(this); return; } - for (int i = 0; i < worldNames.size(); i++) { - if (!checkTables(worldTables.get(i))) { + for (int i = 0; i < Config.worldNames.size(); i++) { + if (!checkTables(Config.worldTables.get(i))) { log.log(Level.SEVERE, "[LogBlock] Errors while checking tables. They may not exist."); getServer().getPluginManager().disablePlugin(this); return; } } - if (keepLogDays >= 0) + if (Config.keepLogDays >= 0) dropOldLogs(); LBLBlockListener lblBlockListener = new LBLBlockListener(); PluginManager pm = getServer().getPluginManager(); @@ -127,9 +96,9 @@ public class LogBlock extends JavaPlugin pm.registerEvent(Type.BLOCK_PLACED, lblBlockListener, Event.Priority.Monitor, this); pm.registerEvent(Type.BLOCK_BREAK, lblBlockListener, Event.Priority.Monitor, this); pm.registerEvent(Type.SIGN_CHANGE, lblBlockListener, Event.Priority.Monitor, this); - if (logFire) + if (Config.logFire) pm.registerEvent(Type.BLOCK_BURN, lblBlockListener, Event.Priority.Monitor, this); - if (logExplosions) + if (Config.logExplosions) pm.registerEvent(Type.ENTITY_EXPLODE, new LBLEntityListener(), Event.Priority.Monitor, this); consumer = new Consumer(); new Thread(consumer).start(); @@ -149,87 +118,134 @@ public class LogBlock extends JavaPlugin @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("lb")) { - if ((sender instanceof Player)) { - Player player = (Player)sender; - Connection conn = getConnection(); - if (conn != null) { - String table = GetTable(player.getWorld().getName()); - if (table != null) { - if (CheckPermission(player,"logblock.area")) { - if (args.length == 0) { - player.sendMessage(ChatColor.RED + "No argument. Type /mt help for help"); - } else if (args[0].equalsIgnoreCase("area")) { - int radius = defaultDist; - if (args.length == 2 && isInt(args[1])) - radius = Integer.parseInt(args[1]); - AreaStats th = new AreaStats(conn, player, radius, table); - new Thread(th).start(); - } else if (args[0].equalsIgnoreCase("world")) { - PlayerWorldStats th = new PlayerWorldStats(conn, player, table); - new Thread(th).start(); - } else if (args[0].equalsIgnoreCase("player")) { - if (args.length >= 2) { - int radius = defaultDist; - if (args.length == 3 && isInt(args[2])) - radius = Integer.parseInt(args[2]); - PlayerAreaStats th = new PlayerAreaStats(conn, player, args[1], radius, table); - new Thread(th).start(); - } else - player.sendMessage(ChatColor.RED + "Usage: /lb player [name] "); - } else if (args[0].equalsIgnoreCase("block")) { - if (args.length >= 2) { - if (Material.matchMaterial(args[1]) != null) { - int type = Material.matchMaterial(args[1]).getId(); - int radius = defaultDist; + try { + if ((sender instanceof Player)) { + Player player = (Player)sender; + Connection conn = getConnection(); + if (conn != null) { + String table = getTable(player.getWorld().getName()); + if (table != null) { + if (CheckPermission(player,"logblock.area")) { + if (args.length == 0) { + player.sendMessage(ChatColor.RED + "No argument. Type /lb help for help"); + } else if (args[0].equalsIgnoreCase("area")) { + int radius = Config.defaultDist; + if (args.length == 2 && isInt(args[1])) + radius = Integer.parseInt(args[1]); + new Thread(new AreaStats(conn, player, radius, table)).start(); + } else if (args[0].equalsIgnoreCase("world")) { + new Thread(new PlayerWorldStats(conn, player, table)).start(); + } else if (args[0].equalsIgnoreCase("player")) { + if (args.length >= 2) { + int radius = Config.defaultDist; if (args.length == 3 && isInt(args[2])) radius = Integer.parseInt(args[2]); - AreaBlockSearch th = new AreaBlockSearch(conn, player, type, radius, table); - new Thread(th).start(); + new Thread(new PlayerAreaStats(conn, player, args[1], radius, table)).start(); } else - player.sendMessage(ChatColor.RED + "Can't find any item like '" + args[1] + "'"); - } else - player.sendMessage(ChatColor.RED + "Usage: /lb block [type] "); - } else if (args[0].equalsIgnoreCase("rollback")) { - if (args.length == 5) { - int minutes = parseTimeSpec(args[3], args[4]); - if (args[1].equalsIgnoreCase("player")) { - player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " by " + minutes + " minutes."); - Rollback rb = new Rollback(player, conn, args[2], minutes, table); - new Thread(rb).start(); - } else if (args[1].equalsIgnoreCase("area")) { - - if (isInt(args[2])) { - player.sendMessage(ChatColor.GREEN + "Rolling back area within " + args[2] + " blocks of you by " + minutes + " minutes."); - Rollback rb = new Rollback(player, conn, Integer.parseInt(args[2]), minutes, table); - new Thread(rb).start(); + player.sendMessage(ChatColor.RED + "Usage: /lb player [name] "); + } else if (args[0].equalsIgnoreCase("block")) { + if (args.length >= 2) { + if (Material.matchMaterial(args[1]) != null) { + int type = Material.matchMaterial(args[1]).getId(); + int radius = Config.defaultDist; + if (args.length == 3 && isInt(args[2])) + radius = Integer.parseInt(args[2]); + new Thread(new AreaBlockSearch(conn, player, type, radius, table)).start(); } else - player.sendMessage(ChatColor.RED + "Can't cast into an int: " + args[2]); + player.sendMessage(ChatColor.RED + "Can't find any item like '" + args[1] + "'"); } else - player.sendMessage(ChatColor.RED + "Wrong argument. Try player or area."); - } else { - player.sendMessage(ChatColor.RED + "Usage:"); - player.sendMessage(ChatColor.RED + "/lb rollback player [name] [time] [minutes|hours|days]"); - player.sendMessage(ChatColor.RED + "/lb rollback area [radius] [time] [minutes|hours|days]"); - } - } else if (args[0].equalsIgnoreCase("help")) { - player.sendMessage("§dLogBlock Commands:"); - player.sendMessage("§d/lb area "); - player.sendMessage("§d/lb world"); - player.sendMessage("§d/lb player [name] "); - player.sendMessage("§d/lb block [type] "); - player.sendMessage("§d/lb rollback area [radius] [time] [minutes|hours|days]"); - player.sendMessage("§d/lb rollback player [name] [time] [minutes|hours|days]"); + player.sendMessage(ChatColor.RED + "Usage: /lb block [type] "); + } else if (args[0].equalsIgnoreCase("setpos")) { + Session session = getSession(player); + Location loc = player.getTargetBlock(null, Integer.MAX_VALUE).getLocation(); + if (args.length == 1) { + if (!session.isloc1Set()) { + session.loc1 = loc; + player.sendMessage(ChatColor.GREEN + "Pos 1 set."); + } else if (!session.isloc2Set()) { + session.loc2 = loc; + player.sendMessage(ChatColor.GREEN + "Pos 2 set."); + } else { + session.loc1 = loc; + session.loc2 = null; + player.sendMessage(ChatColor.GREEN + "Positions cleared."); + player.sendMessage(ChatColor.GREEN + "Pos 1 set."); + } + } else if (args.length == 2) { + if (args[1].equalsIgnoreCase("1")) { + session.loc1 = loc; + player.sendMessage(ChatColor.GREEN + "Pos 1 set."); + } else if (args[1].equalsIgnoreCase("2")) { + session.loc2 = loc; + player.sendMessage(ChatColor.GREEN + "Pos 2 set."); + } else + player.sendMessage(ChatColor.RED + "Usage: /lb setpos <1|2>"); + } else { + player.sendMessage(ChatColor.RED + "Usage: /lb setpos <1|2>"); + } + } else if (args[0].equalsIgnoreCase("rollback")) { + if (args.length >= 2) { + int minutes; + if (args[1].equalsIgnoreCase("player")) { + if (args.length == 3) + minutes = Config.defaultTime; + else + minutes = parseTimeSpec(args[3], args[4]); + player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " by " + minutes + " minutes."); + new Thread(new Rollback(player, conn, args[2], minutes, table)).start(); + } else if (args[1].equalsIgnoreCase("area")) { + if (args.length == 3) + minutes = Config.defaultTime; + else + minutes = parseTimeSpec(args[3], args[4]); + if (isInt(args[2])) { + player.sendMessage(ChatColor.GREEN + "Rolling back area within " + args[2] + " blocks of you by " + minutes + " minutes."); + new Thread(new Rollback(player, conn, Integer.parseInt(args[2]), minutes, table)).start(); + } else + player.sendMessage(ChatColor.RED + "Can't cast into an int: " + args[2]); + } else if (args[1].equalsIgnoreCase("cuboid")) { + if (args.length == 2) + minutes = Config.defaultTime; + else + minutes = parseTimeSpec(args[2], args[3]); + Session session = getSession(player); + if (session.isloc1Set() && session.isloc2Set()) { + player.sendMessage(ChatColor.GREEN + "Rolling back selected cuboid by " + minutes + " minutes."); + new Thread(new Rollback(player, conn, session.loc1, session.loc2, minutes, table)).start(); + } else + player.sendMessage(ChatColor.RED + "No cuboid selected. Use /lb setpos"); + } else + player.sendMessage(ChatColor.RED + "Wrong rollback mode"); + } else { + player.sendMessage(ChatColor.RED + "Usage:"); + player.sendMessage(ChatColor.RED + "/lb rollback player [name]