From 6ae61230dfedee604ea24692bcbfa13279fe7e82 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 31 Dec 2016 22:41:10 +0200 Subject: [PATCH] More Bugfixes [2.0.0-DEV] Fixed Bugs: - database now contains LoginTimes - Commandlistener now gives proper command. - Logintimes no longer null on inspect - ServerData load SQL format fixed - NickList and IpList changed to Set Known bugs: - (Id while saving might be faulty) - (Database saves multiples of known things.) - LastGameMode is set to null when player joins - (Playtime is negative) - ServerData not updating properly - (DataBase saves non-existent ids) - (LoginTimes is not updated) - Locations not saved when player moves - Location ID not primary key --- .../src/com/djrapitops/plan/Plan.java | 10 +++--- .../plan/command/commands/InfoCommand.java | 12 ++++--- .../plan/command/commands/InspectCommand.java | 2 +- .../plan/command/commands/ReloadCommand.java | 3 +- .../djrapitops/plan/database/UserData.java | 31 ++++++++-------- .../plan/database/databases/SQLDB.java | 35 ++++++++++++------- .../plan/datahandlers/ActivityHandler.java | 10 +++--- .../plan/datahandlers/DataHandler.java | 4 +++ .../datahandlers/GamemodeTimesHandler.java | 19 ++++++---- .../PlanCommandPreprocessListener.java | 2 +- .../listeners/PlanPlayerMoveListener.java | 4 +-- 11 files changed, 80 insertions(+), 52 deletions(-) diff --git a/Plan Advanced/src/com/djrapitops/plan/Plan.java b/Plan Advanced/src/com/djrapitops/plan/Plan.java index 5ad047f5f..6c88227df 100644 --- a/Plan Advanced/src/com/djrapitops/plan/Plan.java +++ b/Plan Advanced/src/com/djrapitops/plan/Plan.java @@ -33,6 +33,8 @@ public class Plan extends JavaPlugin { @Override public void onEnable() { + logToFile("-- Server Start/Reload --"); + getDataFolder().mkdirs(); databases = new HashSet<>(); @@ -75,8 +77,6 @@ public class Plan extends JavaPlugin { getCommand("plan").setExecutor(new PlanCommand(this)); handler.handleReload(); - - logToFile("-- Server Start/Reload --"); log("Player Analytics Enabled."); } @@ -86,7 +86,7 @@ public class Plan extends JavaPlugin { planLiteHook = new PlanLiteHook(this); } } catch (NoClassDefFoundError | Exception e) { - + } } @@ -117,7 +117,7 @@ public class Plan extends JavaPlugin { if (!folder.exists()) { folder.mkdir(); } - File log = new File(getDataFolder(), "Errors.txt"); + File log = new File(getDataFolder(), "Debug.txt"); try { if (!log.exists()) { log.createNewFile(); @@ -128,7 +128,7 @@ public class Plan extends JavaPlugin { pw.flush(); } } catch (IOException e) { - logError("Failed to create Errors.txt file"); + logError("Failed to create Debug.txt file"); } } } diff --git a/Plan Advanced/src/com/djrapitops/plan/command/commands/InfoCommand.java b/Plan Advanced/src/com/djrapitops/plan/command/commands/InfoCommand.java index c5252fcda..b2e051fc9 100644 --- a/Plan Advanced/src/com/djrapitops/plan/command/commands/InfoCommand.java +++ b/Plan Advanced/src/com/djrapitops/plan/command/commands/InfoCommand.java @@ -1,5 +1,6 @@ package com.djrapitops.plan.command.commands; +import com.djrapitops.plan.Phrase; import com.djrapitops.plan.Plan; import com.djrapitops.plan.command.CommandType; import com.djrapitops.plan.command.SubCommand; @@ -21,11 +22,12 @@ public class InfoCommand extends SubCommand { @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { plugin.reloadConfig(); - ChatColor operatorColor = ChatColor.DARK_GREEN; - ChatColor textColor = ChatColor.GRAY; - sender.sendMessage(textColor +"--["+operatorColor+"PLAN - Info"+textColor+"]--"); - sender.sendMessage(operatorColor+"Version: "+textColor+plugin.getDescription().getVersion()); - sender.sendMessage(textColor+MiscUtils.checkVersion()); + ChatColor oColor = Phrase.COLOR_MAIN.color(); + ChatColor tColor = Phrase.COLOR_SEC.color(); + sender.sendMessage(tColor +"--["+oColor+"PLAN - Info"+tColor+"]--"); + sender.sendMessage(oColor+"Version: "+tColor+plugin.getDescription().getVersion()); + sender.sendMessage(tColor+MiscUtils.checkVersion()); + sender.sendMessage(oColor+"Cache\n"+tColor+"Cached users: "+plugin.getHandler().getDataCache().keySet().size()); return true; } diff --git a/Plan Advanced/src/com/djrapitops/plan/command/commands/InspectCommand.java b/Plan Advanced/src/com/djrapitops/plan/command/commands/InspectCommand.java index 41f8e8a79..5c247f3d5 100644 --- a/Plan Advanced/src/com/djrapitops/plan/command/commands/InspectCommand.java +++ b/Plan Advanced/src/com/djrapitops/plan/command/commands/InspectCommand.java @@ -55,7 +55,7 @@ public class InspectCommand extends SubCommand { } Date refreshDate = new Date(); - UserData data = plugin.getHandler().getCurrentData(uuid); + UserData data = plugin.getHandler().getCurrentData(uuid); ChatColor operatorColor = Phrase.COLOR_MAIN.color(); ChatColor textColor = Phrase.COLOR_SEC.color(); diff --git a/Plan Advanced/src/com/djrapitops/plan/command/commands/ReloadCommand.java b/Plan Advanced/src/com/djrapitops/plan/command/commands/ReloadCommand.java index 745d3396b..cdf821f45 100644 --- a/Plan Advanced/src/com/djrapitops/plan/command/commands/ReloadCommand.java +++ b/Plan Advanced/src/com/djrapitops/plan/command/commands/ReloadCommand.java @@ -17,7 +17,7 @@ public class ReloadCommand extends SubCommand { private Plan plugin; public ReloadCommand(Plan plugin) { - super("reload", "plan.reload", "Reload plugin config & Hooks", CommandType.CONSOLE); + super("reload", "plan.reload", "Reload plugin config & save cached data", CommandType.CONSOLE); this.plugin = plugin; } @@ -25,6 +25,7 @@ public class ReloadCommand extends SubCommand { @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { plugin.reloadConfig(); + plugin.getHandler().saveCachedData(); plugin.hookPlanLite(); ChatColor operatorColor = Phrase.COLOR_MAIN.color(); ChatColor textColor = Phrase.COLOR_SEC.color(); diff --git a/Plan Advanced/src/com/djrapitops/plan/database/UserData.java b/Plan Advanced/src/com/djrapitops/plan/database/UserData.java index a69b77a48..67b053e6d 100644 --- a/Plan Advanced/src/com/djrapitops/plan/database/UserData.java +++ b/Plan Advanced/src/com/djrapitops/plan/database/UserData.java @@ -4,6 +4,7 @@ import java.net.InetAddress; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.UUID; import org.bukkit.Bukkit; @@ -20,8 +21,8 @@ public class UserData { private Location location; private List locations; private Location bedLocation; - private List ips; - private List nicknames; + private HashSet ips; + private HashSet nicknames; private long registered; private long lastPlayed; private long playTime; @@ -38,14 +39,14 @@ public class UserData { uuid = player.getUniqueId(); bedLocation = player.getBedSpawnLocation(); if (bedLocation == null) { - bedLocation = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0 ,0); + bedLocation = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0, 0); } registered = player.getFirstPlayed(); location = player.getLocation(); isOp = player.isOp(); locations = new ArrayList<>(); - nicknames = new ArrayList<>(); - ips = new ArrayList<>(); + nicknames = new HashSet<>(); + ips = new HashSet<>(); gmTimes = new HashMap<>(); long zero = Long.parseLong("0"); gmTimes.put(GameMode.SURVIVAL, zero); @@ -61,13 +62,13 @@ public class UserData { uuid = player.getUniqueId(); bedLocation = player.getBedSpawnLocation(); if (bedLocation == null) { - bedLocation = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0 ,0); + bedLocation = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0, 0); } registered = player.getFirstPlayed(); isOp = player.isOp(); locations = new ArrayList<>(); - nicknames = new ArrayList<>(); - ips = new ArrayList<>(); + nicknames = new HashSet<>(); + ips = new HashSet<>(); gmTimes = new HashMap<>(); long zero = Long.parseLong("0"); gmTimes.put(GameMode.SURVIVAL, zero); @@ -90,12 +91,14 @@ public class UserData { public void addLocation(Location loc) { locations.add(loc); - location = locations.get(locations.size() - 1); + location = loc; } public void addLocations(Collection addLocs) { locations.addAll(addLocs); - location = locations.get(locations.size() - 1); + if (!locations.isEmpty()) { + location = locations.get(locations.size() - 1); + } } public void addNickname(String nick) { @@ -149,11 +152,11 @@ public class UserData { return bedLocation; } - public List getIps() { + public HashSet getIps() { return ips; } - public List getNicknames() { + public HashSet getNicknames() { return nicknames; } @@ -218,11 +221,11 @@ public class UserData { this.bedLocation = bedLocation; } - public void setIps(List ips) { + public void setIps(HashSet ips) { this.ips = ips; } - public void setNicknames(List nicknames) { + public void setNicknames(HashSet nicknames) { this.nicknames = nicknames; } diff --git a/Plan Advanced/src/com/djrapitops/plan/database/databases/SQLDB.java b/Plan Advanced/src/com/djrapitops/plan/database/databases/SQLDB.java index 8a642a10b..4ccac3862 100644 --- a/Plan Advanced/src/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan Advanced/src/com/djrapitops/plan/database/databases/SQLDB.java @@ -14,15 +14,14 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Calendar; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; -import static org.bukkit.Bukkit.getOfflinePlayer; import org.bukkit.GameMode; import org.bukkit.Location; import static org.bukkit.Bukkit.getOfflinePlayer; @@ -51,6 +50,7 @@ public abstract class SQLDB extends Database { private String userColumnDemGender; private String userColumnLastGM; private String userColumnLastGMSwapTime; + private String userColumnLoginTimes; private final String locationColumnUserID; private String locationColumnID; private String locationColumnCoordinatesX; @@ -98,6 +98,7 @@ public abstract class SQLDB extends Database { userColumnLastGM = "last_gamemode"; userColumnLastGMSwapTime = "last_gamemode_swap"; userColumnPlayTime = "play_time"; + userColumnLoginTimes = "login_times"; locationColumnCoordinatesX = "x"; locationColumnCoordinatesZ = "z"; @@ -162,7 +163,8 @@ public abstract class SQLDB extends Database { + userColumnDemGeoLocation + " varchar(50) NOT NULL, " + userColumnLastGM + " varchar(15) NOT NULL, " + userColumnLastGMSwapTime + " bigint NOT NULL, " - + userColumnPlayTime + " bigint NOT NULL" + + userColumnPlayTime + " bigint NOT NULL, " + + userColumnLoginTimes + " int NOT NULL" + ")" ); @@ -315,7 +317,7 @@ public abstract class SQLDB extends Database { String id = null; while (set.next()) { - id = set.getString(userColumnID); + id = ""+set.getInt(userColumnID); if (id == null) { return null; } @@ -325,6 +327,7 @@ public abstract class SQLDB extends Database { data.setLastGamemode(MiscUtils.parseGM(set.getString(userColumnLastGM))); data.setLastGmSwapTime(set.getLong(userColumnLastGMSwapTime)); data.setPlayTime(set.getLong(userColumnPlayTime)); + data.setLoginTimes(set.getInt(userColumnLoginTimes)); } set.close(); statement.close(); @@ -408,15 +411,15 @@ public abstract class SQLDB extends Database { HashMap commandUse = getCommandUse(); int newPlayers = 0; Date now = new Date(); - Date startOfToday = new Date(now.getTime() - now.getTime() % 86400000); + Date startOfToday = new Date(now.getTime() - (now.getTime() % 86400000)); try { PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + serverdataName - + "ORDER BY " + serverdataColumnDate + " ASC"); + + " ORDER BY " + serverdataColumnDate + " ASC LIMIT 1"); ResultSet set = statement.executeQuery(); while (set.next()) { Date lastSave = new Date(set.getLong(serverdataColumnDate)); - Date startOfSaveDay = new Date(lastSave.getTime() - lastSave.getTime() % 86400000); + Date startOfSaveDay = new Date(lastSave.getTime() - (lastSave.getTime() % 86400000)); if (startOfSaveDay == startOfToday) { newPlayers = set.getInt(serverdataColumnNewPlayers); } @@ -539,7 +542,8 @@ public abstract class SQLDB extends Database { + userColumnDemGeoLocation + "=?, " + userColumnLastGM + "=?, " + userColumnLastGMSwapTime + "=?, " - + userColumnPlayTime + "=? " + + userColumnPlayTime + "=?, " + + userColumnLoginTimes + "=? " + "WHERE UPPER(" + userColumnUUID + ") LIKE UPPER(?)"; PreparedStatement statement = connection.prepareStatement(sql); @@ -554,6 +558,8 @@ public abstract class SQLDB extends Database { } statement.setLong(5, data.getLastGmSwapTime()); statement.setLong(6, data.getPlayTime()); + statement.setInt(7, data.getLoginTimes()); + statement.setString(8, uuid.toString()); if (statement.executeUpdate() == 0) { statement = connection.prepareStatement("INSERT INTO " + userName + " (" @@ -563,8 +569,9 @@ public abstract class SQLDB extends Database { + userColumnDemGeoLocation + ", " + userColumnLastGM + ", " + userColumnLastGMSwapTime + ", " - + userColumnPlayTime - + ") VALUES (?, ?, ?, ?, ?, ?, ?)"); + + userColumnPlayTime + ", " + + userColumnLoginTimes + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); statement.setString(1, uuid.toString()); statement.setInt(2, data.getDemData().getAge()); @@ -577,10 +584,12 @@ public abstract class SQLDB extends Database { } statement.setLong(6, data.getLastGmSwapTime()); statement.setLong(7, data.getPlayTime()); + statement.setInt(8, data.getLoginTimes()); statement.execute(); statement.close(); - } + userId = getUserId(uuid.toString()); + } saveLocationList(userId, data.getLocations()); saveNickList(userId, data.getNicknames()); saveIPList(userId, data.getIps()); @@ -629,7 +638,7 @@ public abstract class SQLDB extends Database { } } - public void saveNickList(int userId, List names) { + public void saveNickList(int userId, HashSet names) { try { PreparedStatement statement = connection.prepareStatement( "DELETE FROM " + nicknamesName + " WHERE UPPER(" + nicknamesColumnUserID + ") LIKE UPPER(?)"); @@ -653,7 +662,7 @@ public abstract class SQLDB extends Database { } } - public void saveIPList(int userId, List ips) { + public void saveIPList(int userId, HashSet ips) { try { PreparedStatement statement = connection.prepareStatement( "DELETE FROM " + ipsName + " WHERE UPPER(" + ipsColumnUserID + ") LIKE UPPER(?)"); diff --git a/Plan Advanced/src/com/djrapitops/plan/datahandlers/ActivityHandler.java b/Plan Advanced/src/com/djrapitops/plan/datahandlers/ActivityHandler.java index 9b52205ca..5d22912b6 100644 --- a/Plan Advanced/src/com/djrapitops/plan/datahandlers/ActivityHandler.java +++ b/Plan Advanced/src/com/djrapitops/plan/datahandlers/ActivityHandler.java @@ -24,23 +24,25 @@ public class ActivityHandler { public void saveToCache(Player player, UserData data) { long timeNow = new Date().getTime(); - data.setPlayTime(data.getPlayTime() + (data.getLastPlayed() - timeNow)); + data.setPlayTime(data.getPlayTime() + (timeNow - data.getLastPlayed())); data.setLastPlayed(timeNow); } public void handleLogIn(PlayerLoginEvent event, UserData data) { data.setLastPlayed(new Date().getTime()); - data.updateBanned(event.getPlayer()); + Player player = event.getPlayer(); + data.updateBanned(player); + handler.getLocationHandler().addLocation(player.getUniqueId(), player.getLocation()); } public void handleLogOut(PlayerQuitEvent event, UserData data) { Player player = event.getPlayer(); - data.setPlayTime(data.getPlayTime() + (data.getLastPlayed() - new Date().getTime())); + data.setPlayTime(data.getPlayTime() + (new Date().getTime() - data.getLastPlayed())); data.setLastPlayed(player.getLastPlayed()); } void handleReload(Player player, UserData data) { - data.setPlayTime(data.getPlayTime() + (data.getLastPlayed() - new Date().getTime())); + data.setPlayTime(data.getPlayTime() + (new Date().getTime() - data.getLastPlayed())); data.setLastPlayed(player.getLastPlayed()); } } diff --git a/Plan Advanced/src/com/djrapitops/plan/datahandlers/DataHandler.java b/Plan Advanced/src/com/djrapitops/plan/datahandlers/DataHandler.java index f948d5fd2..f086853f3 100644 --- a/Plan Advanced/src/com/djrapitops/plan/datahandlers/DataHandler.java +++ b/Plan Advanced/src/com/djrapitops/plan/datahandlers/DataHandler.java @@ -151,6 +151,10 @@ public class DataHandler { dataCache.put(player.getUniqueId(), data); } + public HashMap getDataCache() { + return dataCache; + } + public ActivityHandler getActivityHandler() { return activityHandler; } diff --git a/Plan Advanced/src/com/djrapitops/plan/datahandlers/GamemodeTimesHandler.java b/Plan Advanced/src/com/djrapitops/plan/datahandlers/GamemodeTimesHandler.java index 4c389384a..352de4901 100644 --- a/Plan Advanced/src/com/djrapitops/plan/datahandlers/GamemodeTimesHandler.java +++ b/Plan Advanced/src/com/djrapitops/plan/datahandlers/GamemodeTimesHandler.java @@ -24,33 +24,40 @@ public class GamemodeTimesHandler { public void handleChangeEvent(PlayerGameModeChangeEvent event, UserData data) { HashMap times = data.getGmTimes(); - long lastSwap = data.getLastGmSwapTime(); handler.getActivityHandler().saveToCache(event.getPlayer(), data); + + long lastSwap = data.getLastGmSwapTime(); long now = data.getPlayTime(); GameMode oldGM = data.getLastGamemode(); - data.setGMTime(oldGM, times.get(oldGM) + (lastSwap - now)); + data.setGMTime(oldGM, times.get(oldGM) + (now - lastSwap)); + GameMode newGM = event.getNewGameMode(); data.setLastGamemode(newGM); + data.setLastGmSwapTime(now); } void saveToCache(Player p, UserData data) { HashMap times = data.getGmTimes(); - long lastSwap = data.getLastGmSwapTime(); handler.getActivityHandler().saveToCache(p, data); + + long lastSwap = data.getLastGmSwapTime(); long now = data.getPlayTime(); GameMode currentGM = p.getGameMode(); - data.setGMTime(currentGM, times.get(currentGM) + (lastSwap - now)); + data.setGMTime(currentGM, times.get(currentGM) + (now - lastSwap)); + data.setLastGmSwapTime(now); } void handleReload(Player p, UserData data) { HashMap times = data.getGmTimes(); - long lastSwap = data.getLastGmSwapTime(); handler.getActivityHandler().saveToCache(p, data); + + long lastSwap = data.getLastGmSwapTime(); long now = data.getPlayTime(); GameMode currentGM = p.getGameMode(); - data.setGMTime(currentGM, times.get(currentGM) + (lastSwap - now)); + data.setGMTime(currentGM, times.get(currentGM) + (now - lastSwap)); + data.setLastGmSwapTime(now); } diff --git a/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanCommandPreprocessListener.java b/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanCommandPreprocessListener.java index cd9ee4f4a..abdc9fdb8 100644 --- a/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanCommandPreprocessListener.java +++ b/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanCommandPreprocessListener.java @@ -25,6 +25,6 @@ public class PlanCommandPreprocessListener implements Listener { if (event.isCancelled()) { return; } - serverH.handleCommand(event.getMessage().split("\\s*")[0]); + serverH.handleCommand(event.getMessage().split(" ")[0]); } } diff --git a/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanPlayerMoveListener.java b/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanPlayerMoveListener.java index 6697f4711..3fec68cc6 100644 --- a/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanPlayerMoveListener.java +++ b/Plan Advanced/src/com/djrapitops/plan/datahandlers/listeners/PlanPlayerMoveListener.java @@ -29,8 +29,8 @@ public class PlanPlayerMoveListener implements Listener { } Player p = event.getPlayer(); Location from = event.getFrom(); - Location to = event.getTo(); - if (from.getBlockX() == to.getBlockX() && from.getBlockZ() == to.getBlockZ() && from.getWorld() == to.getWorld()) { + Location to = event.getTo(); + if (from.getX() == to.getX() && from.getZ() == to.getZ()) { return; } locationH.addLocation(p.getUniqueId(), to);