v1.4.3 Bugfix and Added comments

Fixed AAHook using wrong method. (Coding when tired is not good!)
Flipped operation for usingUUID

Added comments for later in case I forget what happens in DataUtils and
DataFormatUtils
This commit is contained in:
Rsl1122 2016-12-10 12:39:06 +02:00
parent ce8490f814
commit 19d688f498
8 changed files with 84 additions and 86 deletions

View File

@ -21,6 +21,7 @@ public class AdvancedAchievementsHook implements Hook {
public AdvancedAchievementsHook(Plan plugin) throws Exception, NoClassDefFoundError {
this.plugin = plugin;
this.aAPlugin = getPlugin(AdvancedAchievements.class);
// Version was important because 4.0.3 added required method for Offline players
String[] aAVersion = aAPlugin.getDescription().getVersion().split(".");
try {
double versionNumber = Double.parseDouble(aAVersion[0] + "." + aAVersion[1] + aAVersion[2]);
@ -31,6 +32,7 @@ public class AdvancedAchievementsHook implements Hook {
plugin.logError("Advanced Achievements 4.0.3 or above required for Offline players");
}
} catch (Exception e) {
// Some versions were formatted with two numbers
try {
double versionNumber = Double.parseDouble(aAVersion[0] + "." + aAVersion[1]);
if (versionNumber >= 4.03) {
@ -43,7 +45,7 @@ public class AdvancedAchievementsHook implements Hook {
plugin.logToFile("AAHOOK\nError getting version number.\n" + e2);
}
}
// Get total number of Achievements
for (NormalAchievements category : NormalAchievements.values()) {
String categoryName = category.toString();
if (aAPlugin.getDisabledCategorySet().contains(categoryName)) {
@ -65,15 +67,21 @@ public class AdvancedAchievementsHook implements Hook {
if (!aAPlugin.getDisabledCategorySet().contains("Commands")) {
totalAchievements += aAPlugin.getPluginConfig().getConfigurationSection("Commands").getKeys(false).size();
}
//
}
@Override
public HashMap<String, String> getData(String player) throws Exception {
HashMap<String, String> data = new HashMap<>();
// Check if achievements exist
if (totalAchievements > 0) {
UUID uuid = UUIDFetcher.getUUIDOf(player);
try {
// Check if correct method is present
if (this.usingUUID) {
data.put("AAC-ACHIEVEMENTS", aAPlugin.getDb().getPlayerAchievementsAmount(uuid.toString()) + " / " + totalAchievements);
} else {
// Fallback method for older versions, only returns Online player data
Player p = getPlayer(player);
if (uuid != null) {
p = getPlayer(uuid);
@ -81,8 +89,6 @@ public class AdvancedAchievementsHook implements Hook {
if (p != null) {
data.put("AAC-ACHIEVEMENTS", aAPlugin.getDb().getPlayerAchievementsAmount(p) + " / " + totalAchievements);
}
} else {
data.put("AAC-ACHIEVEMENTS", aAPlugin.getDb().getPlayerAchievementsAmount(uuid.toString()) + " / " + totalAchievements);
}
} catch (Exception e) {
plugin.logToFile("AAHOOK-GetData\nFailed to get data\n" + e + "\nfor: " + player);

View File

@ -7,7 +7,6 @@ import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import net.ess3.api.IEssentials;
import com.earth2me.essentials.craftbukkit.BanLookup;
import com.gmail.nossr50.util.uuid.UUIDFetcher;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
import org.bukkit.BanList;
import org.bukkit.Location;
@ -22,6 +21,7 @@ public class EssentialsHook implements Hook {
this.plugin = p;
}
// Gets data with Essentials own User methods
@Override
public HashMap<String, String> getData(String player) throws Exception {
HashMap<String, String> data = new HashMap<>();

View File

@ -8,9 +8,7 @@ import java.util.HashMap;
import com.massivecraft.factions.entity.MPlayer;
import java.util.UUID;
import static org.bukkit.Bukkit.getOfflinePlayer;
import static org.bukkit.Bukkit.getPlayer;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
public class FactionsHook implements Hook {
@ -28,11 +26,16 @@ public class FactionsHook implements Hook {
HashMap<String, String> data = new HashMap<>();
MPlayer mplayer;
UUID uuid = UUIDFetcher.getUUIDOf(player);
OfflinePlayer p = getOfflinePlayer(player);
OfflinePlayer p;
if (uuid != null) {
p = getOfflinePlayer(uuid);
mplayer = MPlayer.get(uuid);
} else {
// Fallback method if UUID is not found
p = getOfflinePlayer(player);
mplayer = MPlayer.get(p.getUniqueId());
}
mplayer = MPlayer.get(p.getUniqueId());
// Check if player has played on server
if (p.hasPlayedBefore()) {
if (mplayer.hasFaction()) {
data.put("FAC-FACTION", mplayer.getFactionName());

View File

@ -10,8 +10,6 @@ import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.util.StringMgmt;
import java.util.HashMap;
import java.util.List;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;

View File

@ -24,9 +24,11 @@ public class VaultHook implements Hook {
HashMap<String, String> data = new HashMap<>();
try {
UUID uuid = UUIDFetcher.getUUIDOf(player);
OfflinePlayer p = getOfflinePlayer(player);
OfflinePlayer p;
if (uuid != null) {
p = getOfflinePlayer(uuid);
} else {
p = getOfflinePlayer(player);
}
if (p.hasPlayedBefore()) {
data.put("ECO-BALANCE", this.econ.format(this.econ.getBalance(p)));

View File

@ -18,11 +18,13 @@ public class DataFormatUtils {
Plan plugin = getPlugin(Plan.class);
for (String key : data.keySet()) {
try {
// Process OnTime empty data (returns -1 if empty)
if (key.subSequence(0, 3).equals("ONT")) {
if ((data.get(key)).equals("-1") || (data.get(key)).equals("-1.0")) {
remove.add(key);
}
}
// Process failed PlaceholderAPI requests (%string%)
if (key.subSequence(0, 3).equals("PHA")) {
if ((data.get(key)).contains("%")) {
remove.add(key);
@ -32,10 +34,12 @@ public class DataFormatUtils {
plugin.logToFile("FORMAT-Remove\n" + e + "\n" + key);
}
}
// Remove faulty data to prevent TOW-LAST LOGIN from being removed with empty data
for (String removedKey : remove) {
data.remove(removedKey);
}
remove.clear();
// Process Towny data (Empty returns Java Epoch date 1970 for REGISTERED)
if (data.get("TOW-REGISTERED") != null) {
if (data.get("TOW-REGISTERED").contains("1970")) {
remove.add("TOW-REGISTERED");
@ -48,14 +52,31 @@ public class DataFormatUtils {
remove.add("TOW-PLOT OPTIONS");
}
}
// If both OnTime and Towny data found, OnTime priority.
if (data.get("ONT-LAST LOGIN") != null) {
remove.add("TOW-LAST LOGIN");
}
}
// Remove faulty Towny data
for (String removedKey : remove) {
data.remove(removedKey);
}
// Remove faulty Essentials SINCE data, reload turns data to 0
String[] keysRemoveIfZero = {"ESS-ONLINE SINCE", "ESS-OFFLINE SINCE"};
for (String key : keysRemoveIfZero) {
if (data.get(key) != null) {
if (data.get(key).equals("0")) {
data.remove(key);
}
}
}
// Remove OnTime Total Votes if SuperbVote is present
if (data.get("SVO-VOTES") != null) {
if (data.get("ONT-TOTAL VOTES") != null) {
data.remove("ONT-TOTAL VOTES");
}
}
// Format TimeStamps
String[] keysTimestamp = {"ONT-LAST LOGIN"};
for (String key : keysTimestamp) {
if (data.get(key) != null) {
@ -70,15 +91,7 @@ public class DataFormatUtils {
}
}
}
String[] keysRemoveIfZero = {"ESS-ONLINE SINCE", "ESS-OFFLINE SINCE"};
for (String key : keysRemoveIfZero) {
if (data.get(key) != null) {
if (data.get(key).equals("0")) {
data.remove(key);
}
}
}
// Format Milliseconds to readable format
String[] keysTimeAmount = {"ONT-TOTAL PLAY", "ESS-ONLINE SINCE", "ESS-OFFLINE SINCE"};
for (String key : keysTimeAmount) {
if (data.get(key) != null) {
@ -93,81 +106,41 @@ public class DataFormatUtils {
data.replace(key, formatted);
}
} catch (NumberFormatException e) {
plugin.logToFile("FORMAT-Since\nError Parsing number.\n" + e + "\n" + data.get(key));
data.remove(key);
}
}
}
if (data.get("SVO-VOTES") != null) {
if (data.get("ONT-TOTAL VOTES") != null) {
data.remove("ONT-TOTAL VOTES");
}
}
return data;
}
// Creates a new Date with Epoch second and returns Date and Time String
public static String formatTimeStamp(String string) throws NumberFormatException {
long ms = Long.parseLong(string);
Date sfd = new Date(ms);
return ("" + sfd).substring(4, 19);
}
// Formats Time Since (0 -> string)
public static String formatTimeAmount(String string) throws NumberFormatException {
String returnValue = "";
long ms = Long.parseLong(string);
long x = ms / 1000;
long seconds = x % 60;
x /= 60;
long minutes = x % 60;
x /= 60;
long hours = x % 24;
x /= 24;
long days = x;
if (days != 0) {
returnValue += days + "d ";
}
if (hours != 0) {
returnValue += hours + "h ";
}
if (minutes != 0) {
returnValue += minutes + "m ";
}
if (seconds != 0) {
returnValue += seconds + "s";
}
return returnValue;
return turnMsLongToString(ms);
}
// Formats Time Difference String before -> Date now
public static String formatTimeAmountSinceString(String string, Date date) throws NumberFormatException {
String returnValue = "";
long ms = (date.toInstant().getEpochSecond() * 1000) - Long.parseLong(string);
long x = ms / 1000;
long seconds = x % 60;
x /= 60;
long minutes = x % 60;
x /= 60;
long hours = x % 24;
x /= 24;
long days = x;
if (days != 0) {
returnValue += days + "d ";
}
if (hours != 0) {
returnValue += hours + "h ";
}
if (minutes != 0) {
returnValue += minutes + "m ";
}
if (seconds != 0) {
returnValue += seconds + "s";
}
return returnValue;
return turnMsLongToString(ms);
}
// Formats Time Difference Date before -> Date now
public static String formatTimeAmountSinceDate(Date before, Date now) throws NumberFormatException {
String returnValue = "";
long ms = (now.toInstant().getEpochSecond() * 1000) - (before.toInstant().getEpochSecond() * 1000);
return turnMsLongToString(ms);
}
// Formats long in milliseconds into d:h:m:s string
private static String turnMsLongToString(long ms) {
String returnValue = "";
long x = ms / 1000;
long seconds = x % 60;
x /= 60;
@ -191,10 +164,12 @@ public class DataFormatUtils {
return returnValue;
}
// Analysis data Formatting, will be updated after more analysis is added
public static HashMap<String, String> formatAnalyzed(HashMap<String, String> analyzedData) {
return removeExtraDataPoints(analyzedData);
}
// Removes letters from a string leaving only numbers and dots.
public static String removeLetters(String dataPoint) {
String numbers = "0123456789.";
List<Character> numList = new ArrayList<>();
@ -211,6 +186,7 @@ public class DataFormatUtils {
return returnString;
}
// Sorts HashMap into Sorted List of Arrays
public static List<String[]> turnDataHashMapToSortedListOfArrays(HashMap<String, String> data) {
List<String[]> dataList = new ArrayList<>();
for (String key : data.keySet()) {

View File

@ -16,6 +16,8 @@ import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
public class DataUtils {
// allData defined by -a argument in InspectCommand
// returns data given by each Hook
public static HashMap<String, String> getData(boolean allData, String playerName) {
HashMap<String, String> data = new HashMap<>();
Plan plugin = getPlugin(Plan.class);
@ -27,7 +29,6 @@ public class DataUtils {
data.putAll(plugin.getHooks().get(hook).getData(playerName));
}
} catch (Exception e) {
String toLog = "UTILS-GetData"
+ "\nFailed to getData from " + hook
+ "\n" + e
@ -36,18 +37,18 @@ public class DataUtils {
toLog += "\n " + element;
}
plugin.logToFile(toLog);
}
}
return data;
}
// Returns data HashMaps for all pplayers in a HashMap.
public static HashMap<UUID, HashMap<String, String>> getTotalData() {
HashMap<UUID, HashMap<String, String>> playerData = new HashMap<>();
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (playerData.get(player.getUniqueId()) == null) {
playerData.put(player.getUniqueId(), getData(true, player.getName()));
}
}
}
return playerData;
}
@ -71,10 +72,11 @@ public class DataUtils {
}
return null;
}
public static HashMap<String, String> analyze(HashMap<UUID, HashMap<String, String>> playerData) {
Plan plugin = getPlugin(Plan.class);
HashMap<String, List<String>> playerDataLists = new HashMap<>();
// Ignore following keys (Strings, unprocessable or irrelevant data)
String[] ignore = {"ESS-BAN REASON", "ESS-OPPED", "ESS-MUTE TIME", "ESS-LOCATION", "ESS-HUNGER", "ESS-LOCATION WORLD",
"ESS-NICKNAME", "ESS-UUID", "FAC-FACTION", "ONT-LAST LOGIN", "TOW-TOWN", "TOW-REGISTERED",
"TOW-LAST LOGIN", "TOW-OWNER OF", "TOW-PLOT PERMS", "TOW-PLOT OPTIONS", "TOW-FRIENDS", "ESS-ONLINE SINCE",
@ -89,7 +91,8 @@ public class DataUtils {
ignoreKeys.add("AAC-ACHIEVEMENTS");
}
ignoreKeys.addAll(Arrays.asList(ignore));
// Turn playerData into Hashmap of Lists sorted by keys.
for (UUID key : playerData.keySet()) {
for (String dataKey : playerData.get(key).keySet()) {
if (ignoreKeys.contains(dataKey)) {
@ -102,26 +105,32 @@ public class DataUtils {
}
}
String[] numbers = {"AAC-ACHIEVEMENTS","ESS-HEALTH", "ESS-XP LEVEL", "FAC-POWER", "FAC-POWER PER HOUR",
// Define analysis method for keys
String[] numbers = {"AAC-ACHIEVEMENTS", "ESS-HEALTH", "ESS-XP LEVEL", "FAC-POWER", "FAC-POWER PER HOUR",
"FAC-POWER PER DEATH", "SVO-VOTES", "ONT-TOTAL VOTES", "ONT-TOTAL REFERRED", "ECO-BALANCE"};
List<String> numberKeys = new ArrayList<>();
numberKeys.addAll(Arrays.asList(numbers));
String[] booleanValues = {"ESS-BANNED", "ESS-JAILED", "ESS-MUTED", "ESS-FLYING", "TOW-ONLINE"};
List<String> boolKeys = new ArrayList<>();
boolKeys.addAll(Arrays.asList(booleanValues));
String[] timeValues = {"ONT-TOTAL PLAY"};
List<String> numberKeys = new ArrayList<>();
List<String> boolKeys = new ArrayList<>();
List<String> timeKeys = new ArrayList<>();
numberKeys.addAll(Arrays.asList(numbers));
boolKeys.addAll(Arrays.asList(booleanValues));
timeKeys.addAll(Arrays.asList(timeValues));
// TODO: Add extrahook analysis methods here
HashMap<String, String> analyzedData = new HashMap<>();
int errors = 0;
HashSet<String> errorTypes = new HashSet<>();
// Analyze - Go through each key - Go through each point of data in the list.
for (String dataKey : playerDataLists.keySet()) {
if (numberKeys.contains(dataKey)) {
double sum = 0;
for (String dataPoint : playerDataLists.get(dataKey)) {
// Special cases separated.
try {
if (dataKey.equals("FAC-POWER") || dataKey.equals("AAC-ACHIEVEMENTS")) {
sum += Double.parseDouble(dataPoint.split(" ")[0]);
@ -135,6 +144,7 @@ public class DataUtils {
errorTypes.add("" + e);
}
}
// Average
analyzedData.put(dataKey, "" + (sum * 1.0 / playerData.size()));
} else if (boolKeys.contains(dataKey)) {
@ -149,6 +159,7 @@ public class DataUtils {
errorTypes.add("" + e);
}
}
// Average
analyzedData.put(dataKey, "" + ((amount * 1.0 / playerData.size()) * 100) + "%");
} else if (timeKeys.contains(dataKey)) {
Long time = Long.parseLong("0");
@ -160,9 +171,11 @@ public class DataUtils {
errorTypes.add("" + e);
}
}
// Average
analyzedData.put(dataKey, "" + (time * 1.0 / playerData.size()));
}
}
// Log errors
if (errors > 0) {
String log = "ANALYZE\n" + errors + " error(s) occurred while analyzing total data.\nFollowing types:";
for (String errorType : errorTypes) {

View File

@ -1,6 +1,6 @@
name: Plan
main: com.djrapitops.plan.Plan
version: 1.4.2
version: 1.4.3
commands:
plan: