1
0
mirror of https://github.com/moparisthebest/xAuth synced 2024-12-22 07:08:51 -05:00

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
This commit is contained in:
Zach Iverson 2011-07-04 00:58:01 -04:00
parent 69aeb90688
commit 500a9ac0dd
8 changed files with 106 additions and 79 deletions

View File

@ -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)

View File

@ -235,6 +235,7 @@ public class xAuthCommand implements CommandExecutor {
Player target = xPlayer.getPlayer();
if (target != null) {
if (xPlayer.mustRegister())
plugin.createGuest(xPlayer);
xAuthMessages.send("admnUnregSuccessTgt", target);
}
@ -259,6 +260,7 @@ public class xAuthCommand implements CommandExecutor {
Player target = xPlayer.getPlayer();
if (target != null) {
if (xPlayer.mustRegister())
plugin.createGuest(xPlayer);
target.sendMessage("You have been unregistered and logged out!");
}

View File

@ -32,6 +32,10 @@ public class DataManager {
private ConcurrentHashMap<String, TeleLocation> teleLocations = new ConcurrentHashMap<String, TeleLocation>();
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<Account> 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<TeleLocation> teleLocations = new ArrayList<TeleLocation>();
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`" +

View File

@ -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);
if (xAuthSettings.protectLoc)
event.setTo(plugin.getLocationToTeleport(player.getWorld()));
else
event.setTo(xPlayer.getLocation());
if (xPlayer.canNotify())
xPlayer.sendIllegalActionNotice();
//event.setCancelled(true);
}
}

View File

@ -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,6 +181,7 @@ public class xAuth extends JavaPlugin {
if (player.getHealth() > 0)
xPlayer.setLocation(player.getLocation());
if (xAuthSettings.protectLoc)
player.teleport(getLocationToTeleport(player.getWorld()));
}
@ -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() {

View File

@ -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;

View File

@ -62,6 +62,7 @@ public class xAuthSettings {
public static int guestTimeout = 300;
public static int notifyCooldown = 5;
public static List<String> 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);

View File

@ -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