From 500a9ac0dd2e14b5407c47804a2a1670ff00a944 Mon Sep 17 00:00:00 2001 From: Zach Iverson Date: Mon, 4 Jul 2011 00:58:01 -0400 Subject: [PATCH] 1. Fixed a bunch of issues with disabling forced registration 2. Database connection will be reestablised if the previous connection is closed 3. Location protection can now be turned on/off 4. Cleaned up code --- src/main/java/com/cypherx/xauth/Util.java | 26 ----- .../cypherx/xauth/commands/xAuthCommand.java | 6 +- .../xauth/datamanager/DataManager.java | 106 ++++++++++++++---- .../xauth/listeners/xAuthPlayerListener.java | 11 +- src/main/java/com/cypherx/xauth/xAuth.java | 26 +---- .../java/com/cypherx/xauth/xAuthPlayer.java | 2 - .../java/com/cypherx/xauth/xAuthSettings.java | 4 +- src/main/resources/config.yml | 4 +- 8 files changed, 106 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/cypherx/xauth/Util.java b/src/main/java/com/cypherx/xauth/Util.java index 7768c86..5ec2c52 100644 --- a/src/main/java/com/cypherx/xauth/Util.java +++ b/src/main/java/com/cypherx/xauth/Util.java @@ -3,7 +3,6 @@ package com.cypherx.xauth; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -//import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; @@ -20,10 +19,6 @@ import org.bukkit.entity.Player; import com.avaje.ebean.validation.factory.EmailValidatorFactory; -/* - * Miscellaneous methods - */ - public class Util { public static void writeConfig(File file, Class c) { String fileName = file.getName(); @@ -80,27 +75,6 @@ public class Util { return sb.toString(); } - /*public static boolean getOnlineMode() { - BufferedReader br = null; - String value = null; - String line; - - try { - br = new BufferedReader(new FileReader("server.properties")); - while ((line = br.readLine()).indexOf("online-mode") == -1); - value = line.split("=")[1]; - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (br != null) - br.close(); - } catch (IOException e) {} - } - - return Boolean.parseBoolean(value); - }*/ - public static boolean isValidName(Player player) { String playerName = player.getName().toLowerCase(); if (playerName.length() < xAuthSettings.filterMinLength) diff --git a/src/main/java/com/cypherx/xauth/commands/xAuthCommand.java b/src/main/java/com/cypherx/xauth/commands/xAuthCommand.java index fc6e312..b69bc79 100644 --- a/src/main/java/com/cypherx/xauth/commands/xAuthCommand.java +++ b/src/main/java/com/cypherx/xauth/commands/xAuthCommand.java @@ -235,7 +235,8 @@ public class xAuthCommand implements CommandExecutor { Player target = xPlayer.getPlayer(); if (target != null) { - plugin.createGuest(xPlayer); + if (xPlayer.mustRegister()) + plugin.createGuest(xPlayer); xAuthMessages.send("admnUnregSuccessTgt", target); } @@ -259,7 +260,8 @@ public class xAuthCommand implements CommandExecutor { Player target = xPlayer.getPlayer(); if (target != null) { - plugin.createGuest(xPlayer); + if (xPlayer.mustRegister()) + plugin.createGuest(xPlayer); target.sendMessage("You have been unregistered and logged out!"); } diff --git a/src/main/java/com/cypherx/xauth/datamanager/DataManager.java b/src/main/java/com/cypherx/xauth/datamanager/DataManager.java index 589ec88..8fd6564 100644 --- a/src/main/java/com/cypherx/xauth/datamanager/DataManager.java +++ b/src/main/java/com/cypherx/xauth/datamanager/DataManager.java @@ -32,6 +32,10 @@ public class DataManager { private ConcurrentHashMap teleLocations = new ConcurrentHashMap(); public DataManager() { + connect(); + } + + private void connect() { if (xAuthSettings.datasource.equals("mysql")) connectMySQL(); else @@ -83,6 +87,9 @@ public class DataManager { "WHERE NOW() > DATEADD('SECOND', " + xAuthSettings.sessionLength + ", `logintime`)"; } + if (!isConnected()) + connect(); + // delete expired sessions try { stmt.executeUpdate(sql); @@ -91,22 +98,10 @@ public class DataManager { } } - public void printStats() { - try { - rs = stmt.executeQuery( - "SELECT" + - " (SELECT COUNT(*) FROM `" + xAuthSettings.tblAccount + "`) AS accounts," + - " (SELECT COUNT(*) FROM `" + xAuthSettings.tblSession + "`) AS sessions" - ); - - if (rs.next()) - xAuthLog.info("Accounts: " + rs.getInt("accounts") + ", Sessions: " + rs.getInt("sessions")); - } catch (SQLException e) { - xAuthLog.severe("Could not fetch xAuth statistics!", e); - } - } - public void createTables() { + if (!isConnected()) + connect(); + try { stmt.execute( "CREATE TABLE IF NOT EXISTS `" + xAuthSettings.tblAccount + "` (" + @@ -167,7 +162,28 @@ public class DataManager { } } + public void printStats() { + if (!isConnected()) + connect(); + + try { + rs = stmt.executeQuery( + "SELECT" + + " (SELECT COUNT(*) FROM `" + xAuthSettings.tblAccount + "`) AS accounts," + + " (SELECT COUNT(*) FROM `" + xAuthSettings.tblSession + "`) AS sessions" + ); + + if (rs.next()) + xAuthLog.info("Accounts: " + rs.getInt("accounts") + ", Sessions: " + rs.getInt("sessions")); + } catch (SQLException e) { + xAuthLog.severe("Could not fetch xAuth statistics!", e); + } + } + public void insertInventory(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "SELECT *" + @@ -241,6 +257,9 @@ public class DataManager { } public ItemStack[] getInventory(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "SELECT *" + @@ -269,6 +288,9 @@ public class DataManager { } public void deleteInventory(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "DELETE FROM `" + xAuthSettings.tblInventory + "`" + @@ -310,6 +332,9 @@ public class DataManager { } public xAuthPlayer getPlayerFromDb(String playerName) { + if (!isConnected()) + connect(); + xAuthPlayer xPlayer = null; try { @@ -333,6 +358,9 @@ public class DataManager { } public xAuthPlayer reloadPlayer(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "SELECT a.*, s.*" + @@ -356,6 +384,9 @@ public class DataManager { } public int getActive(String playerName) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "SELECT `active`" + @@ -382,6 +413,9 @@ public class DataManager { } protected void insertAccount(Account account) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "INSERT INTO `" + xAuthSettings.tblAccount + "`" + @@ -408,6 +442,9 @@ public class DataManager { } public void insertAccounts(List accounts) { + if (!isConnected()) + connect(); + StringBuilder sb = new StringBuilder(); Account account; sb.append("INSERT INTO `" + xAuthSettings.tblAccount + "` (`playername`, `password`) VALUES"); @@ -431,6 +468,9 @@ public class DataManager { } protected void updateAccount(Account account) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "UPDATE `" + xAuthSettings.tblAccount + "`" + @@ -461,6 +501,9 @@ public class DataManager { } public void deleteAccount(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + Account account = xPlayer.getAccount(); try { @@ -479,6 +522,9 @@ public class DataManager { } public void insertSession(Session session) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "INSERT INTO `" + xAuthSettings.tblSession + "`" + @@ -495,6 +541,9 @@ public class DataManager { } public void deleteSession(xAuthPlayer xPlayer) { + if (!isConnected()) + connect(); + Session session = xPlayer.getSession(); try { @@ -512,7 +561,8 @@ public class DataManager { } public void loadTeleLocations() { - //List teleLocations = new ArrayList(); + if (!isConnected()) + connect(); try { rs = stmt.executeQuery( @@ -529,16 +579,16 @@ public class DataManager { teleLocation.setYaw(rs.getFloat("yaw")); teleLocation.setPitch(rs.getFloat("pitch")); teleLocations.put(teleLocation.getWorldName(), teleLocation); - //teleLocations.add(teleLocation); } } catch (SQLException e) { xAuthLog.severe("Could not load TeleLocations from database!", e); } - - //return teleLocations; } public void insertTeleLocation(TeleLocation teleLocation) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "INSERT INTO `" + xAuthSettings.tblLocation + "`" + @@ -559,6 +609,9 @@ public class DataManager { } public void updateTeleLocation(TeleLocation teleLocation) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "UPDATE `" + xAuthSettings.tblLocation + "`" + @@ -583,6 +636,9 @@ public class DataManager { } public void deleteTeleLocation(TeleLocation teleLocation) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "DELETE FROM `" + xAuthSettings.tblLocation + "`" + @@ -597,6 +653,9 @@ public class DataManager { } public StrikeBan loadStrikeBan(String host) { + if (!isConnected()) + connect(); + StrikeBan ban = null; try { @@ -622,6 +681,9 @@ public class DataManager { } public void insertStrikeBan(StrikeBan ban) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "INSERT INTO `" + xAuthSettings.tblStrike + "`" + @@ -639,6 +701,9 @@ public class DataManager { } public void deleteStrikeBan(StrikeBan ban) { + if (!isConnected()) + connect(); + try { stmt = connection.prepareStatement( "DELETE FROM `" + xAuthSettings.tblStrike + "`" + @@ -653,6 +718,9 @@ public class DataManager { } public boolean isHostUsed(String host) { + if (!isConnected()) + connect(); + try { prepStmt = connection.prepareStatement( "SELECT `id`" + diff --git a/src/main/java/com/cypherx/xauth/listeners/xAuthPlayerListener.java b/src/main/java/com/cypherx/xauth/listeners/xAuthPlayerListener.java index 9f987af..ff1e85e 100644 --- a/src/main/java/com/cypherx/xauth/listeners/xAuthPlayerListener.java +++ b/src/main/java/com/cypherx/xauth/listeners/xAuthPlayerListener.java @@ -201,16 +201,13 @@ public class xAuthPlayerListener extends PlayerListener { //return; if (xPlayer.isGuest()) { - //Location loc = plugin.getLocationToTeleport(player.getWorld()); - //player.teleport(loc); - - //event.setFrom(loc); - event.setTo(plugin.getLocationToTeleport(player.getWorld())); + if (xAuthSettings.protectLoc) + event.setTo(plugin.getLocationToTeleport(player.getWorld())); + else + event.setTo(xPlayer.getLocation()); if (xPlayer.canNotify()) xPlayer.sendIllegalActionNotice(); - - //event.setCancelled(true); } } diff --git a/src/main/java/com/cypherx/xauth/xAuth.java b/src/main/java/com/cypherx/xauth/xAuth.java index d31b3e5..b402599 100644 --- a/src/main/java/com/cypherx/xauth/xAuth.java +++ b/src/main/java/com/cypherx/xauth/xAuth.java @@ -52,7 +52,6 @@ public class xAuth extends JavaPlugin { xAuthSettings.setup(dataFolder); xAuthMessages.setup(dataFolder); - // Util.getOnlineMode() -> getServer().getOnlineMode() if (xAuthSettings.autoDisable && getServer().getOnlineMode()) { xAuthLog.warning("Disabling - Server is running in online-mode"); getServer().getPluginManager().disablePlugin(this); @@ -159,6 +158,9 @@ public class xAuth extends JavaPlugin { } public void removeGuest(xAuthPlayer xPlayer) { + if (!xPlayer.isGuest()) + return; + getServer().getScheduler().cancelTask(xPlayer.getTimeoutTaskId()); restore(xPlayer); xPlayer.setGuest(false); @@ -169,8 +171,6 @@ public class xAuth extends JavaPlugin { PlayerInventory playerInv = player.getInventory(); dataManager.insertInventory(xPlayer); - //xPlayer.setInventory(playerInv.getContents()); - //xPlayer.setArmor(playerInv.getArmorContents()); playerInv.clear(); playerInv.setHelmet(null); playerInv.setChestplate(null); @@ -181,7 +181,8 @@ public class xAuth extends JavaPlugin { if (player.getHealth() > 0) xPlayer.setLocation(player.getLocation()); - player.teleport(getLocationToTeleport(player.getWorld())); + if (xAuthSettings.protectLoc) + player.teleport(getLocationToTeleport(player.getWorld())); } public void restore(xAuthPlayer xPlayer) { @@ -215,21 +216,6 @@ public class xAuth extends JavaPlugin { playerInv.setArmorContents(armor); dataManager.deleteInventory(xPlayer); - /*ItemStack[] inv = xPlayer.getInventory(); - //Backpack fix - if (playerInv.getSize() > inv.length) { - ItemStack[] newInv = new ItemStack[playerInv.getSize()]; - - for(int i = 0; i < inv.length; i++) - newInv[i] = inv[i]; - - inv = newInv; - } - //end Backpack fix - - playerInv.setContents(inv); - playerInv.setArmorContents(xPlayer.getArmor());*/ - if (xPlayer.getLocation() != null) xPlayer.getPlayer().teleport(xPlayer.getLocation()); player.saveData(); @@ -324,8 +310,6 @@ public class xAuth extends JavaPlugin { public void reload() { xAuthSettings.setup(dataFolder); xAuthMessages.setup(dataFolder); - //dataManager.close(); - //dataManager = new DataManager(); } public DataManager getDataManager() { diff --git a/src/main/java/com/cypherx/xauth/xAuthPlayer.java b/src/main/java/com/cypherx/xauth/xAuthPlayer.java index 5096182..1873b97 100644 --- a/src/main/java/com/cypherx/xauth/xAuthPlayer.java +++ b/src/main/java/com/cypherx/xauth/xAuthPlayer.java @@ -13,8 +13,6 @@ public class xAuthPlayer { private Session session; private boolean guest = false; private Location location; - //private ItemStack[] inventory; - //private ItemStack[] armor; private Timestamp lastNotifyTime; private int strikes = 0; private int timeoutTaskId; diff --git a/src/main/java/com/cypherx/xauth/xAuthSettings.java b/src/main/java/com/cypherx/xauth/xAuthSettings.java index 3a42eec..7981a88 100644 --- a/src/main/java/com/cypherx/xauth/xAuthSettings.java +++ b/src/main/java/com/cypherx/xauth/xAuthSettings.java @@ -62,6 +62,7 @@ public class xAuthSettings { public static int guestTimeout = 300; public static int notifyCooldown = 5; public static List allowedCmds = Arrays.asList(new String[]{"register", "login", "l"}); + public static boolean protectLoc = true; // session public static int sessionLength = 3600; @@ -88,7 +89,7 @@ public class xAuthSettings { /* * REMEMBER TO CHANGE VERSION AFTER MODIFYING DEFAULT SETTINGS */ - public static int version = 2; + public static int version = 3; public static void setup(File dataFolder) { file = new File(dataFolder, "config.yml"); @@ -150,6 +151,7 @@ public class xAuthSettings { guestTimeout = getInt("guest.timeout", guestTimeout); notifyCooldown = getInt("guest.notify-cooldown", notifyCooldown); allowedCmds = getStrList("guest.allowed-commands", allowedCmds); + protectLoc = getBool("guest.protect-location", protectLoc); sessionLength = getInt("session.length", sessionLength); verifyIp = getBool("session.verifyip", verifyIp); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 43866cc..f03124b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -10,7 +10,7 @@ main: auto-disable: [autoDisable] # When set to true, if a player connects with the same name as someone who is # already online, the player connecting will be kicked instead of the online player - reverse-enforce-single-session: true + reverse-enforce-single-session: [reverseESS] mysql: # Location of the MySQL server. Can be either a host name or IP address @@ -77,6 +77,8 @@ guest: notify-cooldown: [notifyCooldown] # Commands that players who are not registered or logged in may execute allowed-commands: [allowedCmds] + # Turn on/off location protection + protect-location: [protectLoc] session: # Amount of time, in seconds, that a session will remain valid