New Event Processing system.

Pit Report:
Total - Line coverage: 1157/4258 (27%) | Mutation: 399/1978 (20%)
New test areas:
- The new processing system: 55% and 51%
- Database: 53% and 45%

New processing system pools event information and processes it every 60
seconds.
Testing & Timings required
This commit is contained in:
Rsl1122 2017-04-10 09:09:54 +03:00
parent af8616a208
commit d0ee96b3ab
56 changed files with 1612 additions and 753 deletions

View File

@ -37,6 +37,7 @@ import main.java.com.djrapitops.plan.api.API;
import main.java.com.djrapitops.plan.command.PlanCommand;
import main.java.com.djrapitops.plan.data.additional.HookHandler;
import main.java.com.djrapitops.plan.data.cache.*;
import main.java.com.djrapitops.plan.data.handling.InfoPoolProcessor;
import main.java.com.djrapitops.plan.data.listeners.*;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.*;
@ -59,6 +60,7 @@ public class Plan extends JavaPlugin {
private API api;
private DataCacheHandler handler;
private InfoPoolProcessor infoPoolProcessor;
private InspectCacheHandler inspectCache;
private AnalysisCacheHandler analysisCache;
private Database db;
@ -102,6 +104,8 @@ public class Plan extends JavaPlugin {
}
this.handler = new DataCacheHandler(this);
this.infoPoolProcessor = new InfoPoolProcessor(this);
infoPoolProcessor.startPoolTask();
this.inspectCache = new InspectCacheHandler(this);
this.analysisCache = new AnalysisCacheHandler(this);
registerListeners();
@ -148,6 +152,7 @@ public class Plan extends JavaPlugin {
}
Bukkit.getScheduler().cancelTasks(this);
if (handler != null) {
infoPoolProcessor.processPool();
log(Phrase.CACHE_SAVE + "");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.execute(() -> {
@ -327,6 +332,10 @@ public class Plan extends JavaPlugin {
return handler;
}
public InfoPoolProcessor getInfoPoolProcessor() {
return infoPoolProcessor;
}
/**
* @return the Current Database
*/

View File

@ -86,12 +86,11 @@ public class ManageImportCommand extends SubCommand {
uuids.add(p.getUniqueId());
}
HashMap<UUID, Long> numbericData = importPlugins.get(importFromPlugin).grabNumericData(uuids);
DataCacheHandler handler = plugin.getHandler();
BukkitTask asyncImportTask = (new BukkitRunnable() {
@Override
public void run() {
if (importFromPlugin.equals("ontime")) {
if (ManageUtils.importOnTime(numbericData, handler)) {
if (ManageUtils.importOnTime(numbericData, plugin)) {
sender.sendMessage(Phrase.MANAGE_SUCCESS + "");
}
}

View File

@ -211,6 +211,7 @@ public class UserData {
if (nick != null) {
if (!nick.isEmpty()) {
nicknames.add(nick);
lastNick = nick;
return true;
}
}
@ -408,6 +409,9 @@ public class UserData {
* @return
*/
public HashMap<GameMode, Long> getGmTimes() {
if (gmTimes == null) {
gmTimes = new HashMap<>();
}
return gmTimes;
}

View File

@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.data.cache;
import main.java.com.djrapitops.plan.utilities.NewPlayerCreator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@ -11,6 +12,7 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.data.*;
import main.java.com.djrapitops.plan.data.handlers.*;
import main.java.com.djrapitops.plan.data.handling.info.ReloadInfo;
import main.java.com.djrapitops.plan.database.Database;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@ -33,14 +35,8 @@ public class DataCacheHandler {
private final Database db;
// Handlers
private final ActivityHandler activityHandler;
private final GamemodeTimesHandler gamemodeTimesHandler;
private final LocationHandler locationHandler;
private final DemographicsHandler demographicsHandler;
private final BasicInfoHandler basicInfoHandler;
private final RuleBreakingHandler ruleBreakingHandler;
private CommandUseHandler commandUseHandler;
private final KillHandler killHandler;
private final SessionHandler sessionHandler;
// Queues
@ -64,14 +60,7 @@ public class DataCacheHandler {
this.plugin = plugin;
db = plugin.getDB();
dataCache = new HashMap<>();
activityHandler = new ActivityHandler(plugin, this);
gamemodeTimesHandler = new GamemodeTimesHandler(plugin, this);
locationHandler = new LocationHandler(plugin);
demographicsHandler = new DemographicsHandler(plugin, this);
basicInfoHandler = new BasicInfoHandler(plugin, this);
ruleBreakingHandler = new RuleBreakingHandler(plugin, this);
killHandler = new KillHandler(plugin);
sessionHandler = new SessionHandler(plugin);
getTask = new DataCacheGetQueue(plugin);
@ -201,10 +190,16 @@ public class DataCacheHandler {
* @param uuid Player's UUID
*/
public void saveCachedData(UUID uuid) {
UserData data = dataCache.get(uuid);
if (data != null) {
saveTask.scheduleForSave(data);
}
DBCallableProcessor saveProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
data.addLocations(locationHandler.getLocationsForSaving(uuid));
locationHandler.clearLocations(uuid);
saveTask.scheduleForSave(data);
scheludeForClear(uuid);
}
};
getTask.scheduleForGet(uuid, saveProcessor);
}
/**
@ -219,26 +214,17 @@ public class DataCacheHandler {
}
}
// Should only be called from Async thread
/**
*
*/
public void saveHandlerDataToCache() {
Bukkit.getServer().getOnlinePlayers().parallelStream().forEach((p) -> {
saveHandlerDataToCache(p);
});
}
// Should only be called from Async thread
private void saveHandlerDataToCache(Player p) {
DBCallableProcessor cacheUpdater = new DBCallableProcessor() {
@Override
public void process(UserData data) {
activityHandler.saveToCache(data);
gamemodeTimesHandler.saveToCache(p.getGameMode(), data);
}
};
getUserDataForProcessing(cacheUpdater, p.getUniqueId());
private void saveHandlerDataToCache(Player player) {
long time = new Date().getTime();
UUID uuid = player.getUniqueId();
plugin.getInfoPoolProcessor().addToPool(new ReloadInfo(uuid, time, player.getAddress().getAddress(), player.isBanned(), player.getDisplayName(), player.getGameMode()));
}
/**
@ -274,7 +260,12 @@ public class DataCacheHandler {
public boolean isDataAccessed(UUID uuid) {
UserData userData = dataCache.get(uuid);
if (userData != null) {
return userData.isAccessed();
if (userData.isAccessed()) {
return true;
}
if (saveTask.containsUUID(uuid)) {
return true;
}
}
return false;
}
@ -303,13 +294,6 @@ public class DataCacheHandler {
return dataCache;
}
/**
* @return Current instance of the ActivityHandler
*/
public ActivityHandler getActivityHandler() {
return activityHandler;
}
/**
* @return Current instance of the LocationHandler
*/
@ -317,42 +301,6 @@ public class DataCacheHandler {
return locationHandler;
}
/**
* @return Current instance of the DemographicsHandler
*/
public DemographicsHandler getDemographicsHandler() {
return demographicsHandler;
}
/**
* @return Current instance of the BasicInfoHandler
*/
public BasicInfoHandler getBasicInfoHandler() {
return basicInfoHandler;
}
/**
* @return Current instance of the RuleBreakingHandler
*/
public RuleBreakingHandler getRuleBreakingHandler() {
return ruleBreakingHandler;
}
/**
* @return Current instance of the GamemodeTimesHandler
*/
public GamemodeTimesHandler getGamemodeTimesHandler() {
return gamemodeTimesHandler;
}
/**
*
* @return
*/
public KillHandler getKillHandler() {
return killHandler;
}
/**
*
* @return
@ -387,20 +335,11 @@ public class DataCacheHandler {
public void run() {
for (Player player : Bukkit.getOnlinePlayers()) {
UUID uuid = player.getUniqueId();
boolean isNewPlayer = activityHandler.isFirstTimeJoin(uuid);
boolean isNewPlayer = !db.wasSeenBefore(uuid);
if (isNewPlayer) {
newPlayer(player);
}
DBCallableProcessor cacheUpdater = new DBCallableProcessor() {
@Override
public void process(UserData data) {
activityHandler.handleReload(data);
basicInfoHandler.handleReload(player.getDisplayName(), player.getAddress().getAddress(), data);
gamemodeTimesHandler.handleReload(player.getGameMode(), data);
saveCachedUserData();
}
};
getUserDataForProcessing(cacheUpdater, player.getUniqueId());
saveHandlerDataToCache(player);
}
this.cancel();
}

View File

@ -1,9 +1,12 @@
package main.java.com.djrapitops.plan.data.cache;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;
import main.java.com.djrapitops.plan.Phrase;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.Settings;
@ -65,6 +68,10 @@ public class DataCacheSaveQueue {
getPlugin(Plan.class).logError(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber() + ""));
}
}
public boolean containsUUID(UUID uuid) {
return new ArrayList<>(q).stream().map(d -> d.getUuid()).collect(Collectors.toList()).contains(uuid);
}
/**
*

View File

@ -1,98 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import java.util.Date;
import java.util.UUID;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
/**
*
* @author Rsl1122
*/
public class ActivityHandler {
private final Plan plugin;
private final DataCacheHandler handler;
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
* @param h Current instance of DataCacheHandler
*/
public ActivityHandler(Plan plugin, DataCacheHandler h) {
this.plugin = plugin;
this.handler = h;
}
/**
* Checks from Database if the player's data is present.
*
* @param uuid Player's UUID
* @return true if data is not found.
*/
public boolean isFirstTimeJoin(UUID uuid) {
return !plugin.getDB().wasSeenBefore(uuid);
}
/**
* Saves current PlayTime timer and sets lastPlayed.
*
* lastPlayed is set to long matching current Date.
*
* @param data UserData matching the Player
*/
public void saveToCache(UserData data) {
long timeNow = new Date().getTime();
data.setPlayTime(data.getPlayTime() + (timeNow - data.getLastPlayed()));
data.setLastPlayed(timeNow);
}
/**
* Updates UserData about activity related things on Login.
*
* Updates if player is banned or not, Adds one to login times, Adds current
* location to location list.
*
* @param isBanned is the player banned?
* @param data UserData matching the Player
*/
public void handleLogin(boolean isBanned, UserData data) {
data.setLastPlayed(new Date().getTime());
data.updateBanned(isBanned);
data.setLoginTimes(data.getLoginTimes() + 1);
handler.getSessionHandler().startSession(data);
// handler.getLocationHandler().addLocation(player.getUniqueId(), player.getLocation());
}
/**
* Updates UserData about activity related things on Logout.
*
* Saves PlayTime, Set's LastPlayed value to long matching current Date
*
* @param data UserData matching the Player
*/
public void handleLogOut(UserData data) {
Date now = new Date();
long timeNow = now.getTime();
data.setPlayTime(data.getPlayTime() + (timeNow - data.getLastPlayed()));
data.setLastPlayed(timeNow);
handler.getSessionHandler().endSession(data);
}
/**
* Updates UserData about activity related things on /reload.
*
* Updates PlayTime, Sets LastPlayed value to long matching current Date
*
* @param data UserData matching the Player
*/
public void handleReload(UserData data) {
Date now = new Date();
long timeNow = now.getTime();
data.setPlayTime(data.getPlayTime() + (timeNow - data.getLastPlayed()));
data.setLastPlayed(timeNow);
handler.getSessionHandler().startSession(data);
}
}

View File

@ -1,62 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import java.net.InetAddress;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
/**
*
* @author Rsl1122
*/
public class BasicInfoHandler {
private DataCacheHandler handler;
/**
* Class Constructor
*
* @param plugin Current instance of Plan
* @param h Current instance of DataCacheHandler
*/
public BasicInfoHandler(Plan plugin, DataCacheHandler h) {
this.handler = h;
}
/**
* Adds new nicknames and IPs to UserData
*
* @param nickname Displayname of player
* @param ip IP of player
* @param data UserData matching the Player
*/
public void handleLogin(String nickname, InetAddress ip, UserData data) {
addNickname(nickname, data);
data.addIpAddress(ip);
}
/**
* Adds new nicknames and IPs to UserData in case of /reload
*
* @param nickname Displayname of player
* @param ip IP of player
* @param data UserData matching the Player
*/
public void handleReload(String nickname, InetAddress ip, UserData data) {
addNickname(nickname, data);
data.addIpAddress(ip);
}
/**
*
* @param nickname
* @param data
*/
public void addNickname(String nickname, UserData data) {
if (!nickname.isEmpty()) {
if (data.addNickname(nickname)) {
data.setLastNick(nickname);
}
}
}
}

View File

@ -1,131 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import main.java.com.djrapitops.plan.Phrase;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
/**
*
* @author Rsl1122
*/
public class DemographicsHandler {
private final DataCacheHandler handler;
private final Plan plugin;
/**
* Class Constructor
*
* @param plugin Current instance of Plan.class
* @param h Current instance of DataCacheHandler h
*/
public DemographicsHandler(Plan plugin, DataCacheHandler h) {
this.handler = h;
this.plugin = plugin;
}
/**
* Checks the message for Demographics relevant data
*
* If message contains triggerwords and words that define data important,
* informatino will be saved in the DemographicsData of UserData provided
*
* @param message Chat Message
* @param data UserData corresponding to player of this event.
*/
public void handleChatEvent(String message, UserData data) {
List<String> triggers = Arrays.asList(Settings.DEM_TRIGGERS.toString().split(", "));
List<String> female = Arrays.asList(Settings.DEM_FEMALE.toString().split(", "));
List<String> male = Arrays.asList(Settings.DEM_MALE.toString().split(", "));
List<String> ignore = Arrays.asList(Settings.DEM_IGNORE.toString().split(", "));
String[] messageA = message.toLowerCase().split("\\s+");
boolean trigger = false;
boolean gender = false;
// Does message contain important data?
for (String string : messageA) {
if (ignore.contains(string)) {
trigger = false;
break;
}
if (triggers.contains(string)) {
trigger = true;
}
if (female.contains(string) || male.contains(string)) {
gender = true;
}
}
// if not end
if (!trigger) {
return;
}
// Manage important data
int ageNum = -1;
for (String string : messageA) {
try {
ageNum = Integer.parseInt(string);
if (ageNum != -1) {
break;
}
} catch (Exception e) {
}
}
if (ageNum != -1 && ageNum < 100) {
data.getDemData().setAge(ageNum);
}
if (gender) {
for (String string : messageA) {
if (female.contains(string)) {
data.getDemData().setGender(Gender.FEMALE);
} else if (male.contains(string)) {
data.getDemData().setGender(Gender.MALE);
}
}
}
}
/**
* Locates the player upon login
*
* Uses ip-api.com to locate the IP address If too many calls are made to
* the API the IP will be blocked from further calls.
*
* @param ip Player's IP address
* @param data UserData corresponding the player
*/
public void handleLogin(InetAddress ip, UserData data) {
DemographicsData demData = data.getDemData();
try {
String result = "";
URL url = new URL("http://freegeoip.net/csv/" + ip.getHostAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String resultline;
while ((resultline = in.readLine()) != null) {
result += resultline + ",";
}
in.close();
String[] results = result.split(",");
if (!results[2].isEmpty()) {
demData.setGeoLocation(results[2]);
}
} catch (Exception e) {
demData.setGeoLocation(Phrase.DEM_UNKNOWN + "");
}
}
}

View File

@ -1,109 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import java.util.HashMap;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import org.bukkit.GameMode;
/**
*
* @author Rsl1122
*/
public class GamemodeTimesHandler {
private final Plan plugin;
private final DataCacheHandler handler;
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
* @param h Current instance of DataCacheHandler
*/
public GamemodeTimesHandler(Plan plugin, DataCacheHandler h) {
this.plugin = plugin;
handler = h;
}
/**
* Updates lastGamemode to current gamemode on Login
*
* @param gm Gamemode upon login
* @param data UserData matching the Player
*/
public void handleLogin(GameMode gm, UserData data) {
handleChangeEvent(gm, data);
}
/**
* Updates the GameModeTimes HashMap.
*
* Updates GMTimes with new values and sets lastSwap and lastGM.
*
* @param newGM the GameMode player changed to
* @param data UserData matching the Player
*/
public void handleChangeEvent(GameMode newGM, UserData data) {
if (newGM == null) {
return;
}
if (data.getLastGamemode() == null) {
data.setLastGamemode(GameMode.SURVIVAL);
}
GameMode oldGM = data.getLastGamemode();
HashMap<GameMode, Long> times = data.getGmTimes();
Long currentGMTime = times.get(oldGM);
if (currentGMTime == null) {
currentGMTime = 0L;
}
handler.getActivityHandler().saveToCache(data);
long lastSwap = data.getLastGmSwapTime();
long playTime = data.getPlayTime();
data.setGMTime(oldGM, currentGMTime + (playTime - lastSwap));
data.setLastGamemode(newGM);
data.setLastGmSwapTime(playTime);
}
/**
* Updates GMTimes with new values and saves it to cache.
*
* @param currentGM Current Gamemode of the Player
* @param data UserData matching the Player
*/
public void saveToCache(GameMode currentGM, UserData data) {
if (currentGM == null) {
return;
}
HashMap<GameMode, Long> times = data.getGmTimes();
handler.getActivityHandler().saveToCache(data);
Long currentGMTime = times.get(currentGM);
if (currentGMTime == null) {
currentGMTime = 0L;
}
long lastSwap = data.getLastGmSwapTime();
long playtime = data.getPlayTime();
data.setGMTime(currentGM, currentGMTime + (playtime - lastSwap));
data.setLastGmSwapTime(playtime);
}
/**
* Updates GMTImes for player who is online when /reload is run.
*
* @param currentGM Gamemode if online during reload
* @param data UserData matching Player
*/
public void handleReload(GameMode currentGM, UserData data) {
saveToCache(currentGM, data);
}
/**
* Updates GMTimes on Logout.
*
* @param currentGM Current gamemode at logout
* @param data UserData matching Player
*/
public void handleLogOut(GameMode currentGM, UserData data) {
saveToCache(currentGM, data);
}
}

View File

@ -1,60 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Rsl1122
*/
public class KillHandler {
private Plan plugin;
/**
*
* @param plugin
*/
public KillHandler(Plan plugin) {
this.plugin = plugin;
}
/**
*
* @param killerData
* @param victimUUID
* @param weapon
*/
public void handlePlayerKill(UserData killerData, UUID victimUUID, String weapon) {
long now = new Date().toInstant().getEpochSecond() * (long) 1000;
int victimID;
try {
victimID = plugin.getDB().getUserId(victimUUID + "");
} catch (SQLException e) {
plugin.toLog(this.getClass().getName(), e);
return;
}
killerData.addPlayerKill(new KillData(victimUUID, victimID, weapon, now));
}
/**
*
* @param data
*/
public void handlePlayerDeath(UserData data) {
data.setDeaths(data.getDeaths() + 1);
}
/**
*
* @param data
*/
public void handleMobKill(UserData data) {
data.setMobKills(data.getMobKills() + 1);
}
}

View File

@ -1,44 +0,0 @@
package main.java.com.djrapitops.plan.data.handlers;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
/**
*
* @author Rsl1122
*/
public class RuleBreakingHandler {
private final DataCacheHandler handler;
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
* @param h Current instance of DataCacheHandler
*/
public RuleBreakingHandler(Plan plugin, DataCacheHandler h) {
this.handler = h;
}
/**
* Update if player is banned or not on logout.
*
* @param isBanned
* @param data UserData matching Player
*/
public void handleLogout(boolean isBanned, UserData data) {
data.updateBanned(isBanned);
}
/**
* Update if player is banned or not on kick.
*
* @param data UserData matching Player
*/
public void handleKick(UserData data) {
data.setTimesKicked(data.getTimesKicked() + 1);
handler.getActivityHandler().handleLogOut(data);
}
}

View File

@ -32,11 +32,10 @@ public class SessionHandler {
*
* @param data
*/
public void startSession(UserData data) {
public void startSession(UUID uuid) {
long now = new Date().toInstant().getEpochSecond() * (long) 1000;
SessionData session = new SessionData(now);
activeSessions.put(data.getUuid(), session);
data.setCurrentSession(session);
activeSessions.put(uuid, session);
}
/**

View File

@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import java.util.Arrays;
import java.util.List;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Risto
*/
public class ChatHandling {
public static void processChatInfo(UserData data, String nickname, String msg) {
data.addNickname(nickname);
updateDemographicInformation(msg, data);
}
public static void updateDemographicInformation(String msg, UserData data) {
List<String> triggers = Arrays.asList(Settings.DEM_TRIGGERS.toString().split(", "));
List<String> female = Arrays.asList(Settings.DEM_FEMALE.toString().split(", "));
List<String> male = Arrays.asList(Settings.DEM_MALE.toString().split(", "));
List<String> ignore = Arrays.asList(Settings.DEM_IGNORE.toString().split(", "));
String[] messageA = msg.toLowerCase().split("\\s+");
boolean trigger = false;
boolean gender = false;
for (String string : messageA) {
if (ignore.contains(string)) {
trigger = false;
break;
}
if (triggers.contains(string)) {
trigger = true;
}
if (female.contains(string) || male.contains(string)) {
gender = true;
}
}
if (!trigger) {
return;
}
int ageNum = -1;
for (String string : messageA) {
try {
ageNum = Integer.parseInt(string);
if (ageNum != -1) {
break;
}
} catch (Exception e) {
}
}
if (ageNum != -1 && ageNum < 100) {
data.getDemData().setAge(ageNum);
}
if (gender) {
for (String string : messageA) {
if (female.contains(string)) {
data.getDemData().setGender(Gender.FEMALE);
} else if (male.contains(string)) {
data.getDemData().setGender(Gender.MALE);
}
}
}
}
}

View File

@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import java.util.HashMap;
import main.java.com.djrapitops.plan.data.UserData;
import org.bukkit.GameMode;
/**
*
* @author Risto
*/
public class GamemodeHandling {
public static void processGamemodeInfo(UserData data, long time, GameMode newGM) {
if (newGM == null) {
return;
}
GameMode lastGamemode = data.getLastGamemode();
if (lastGamemode == null) {
data.setLastGamemode(newGM);
}
lastGamemode = data.getLastGamemode();
HashMap<GameMode, Long> times = data.getGmTimes();
Long currentGMTime = times.get(lastGamemode);
if (currentGMTime == null) {
currentGMTime = 0L;
}
data.setPlayTime(data.getPlayTime() + (time - data.getLastPlayed()));
data.setLastPlayed(time);
long lastSwap = data.getLastGmSwapTime();
long playtime = data.getPlayTime();
data.setGMTime(lastGamemode, currentGMTime + (playtime - lastSwap));
data.setLastGmSwapTime(playtime);
data.setLastGamemode(newGM);
}
}

View File

@ -0,0 +1,98 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handling.info.*;
import main.java.com.djrapitops.plan.utilities.comparators.HandlingInfoTimeComparator;
import org.bukkit.scheduler.BukkitRunnable;
/**
*
* @author Risto
*/
public class InfoPoolProcessor {
private Plan plugin;
private DataCacheHandler handler;
private List<HandlingInfo> pool;
public InfoPoolProcessor(Plan plugin) {
this.plugin = plugin;
handler = plugin.getHandler();
pool = new ArrayList<>();
}
public void startPoolTask() {
new BukkitRunnable() {
@Override
public void run() {
processPool();
}
}.runTaskTimerAsynchronously(plugin, 20*60, 20*60);
}
public void processPool() {
List<HandlingInfo> toProcess = new ArrayList<>(pool);
try {
pool.removeAll(toProcess);
List<UUID> uuids = toProcess.parallelStream().map(i -> i.getUuid()).distinct().collect(Collectors.toList());
Map<UUID, UserData> userData = getAffectedUserData(uuids);
Collections.sort(toProcess, new HandlingInfoTimeComparator());
for (HandlingInfo r : toProcess) {
UserData data = userData.get(r.getUuid());
if (data == null) {
pool.add(r);
continue;
}
r.process(data);
}
} catch (Exception e) {
plugin.toLog(this.getClass().getName(), e);
pool.addAll(toProcess);
}
}
public Map<UUID, UserData> getAffectedUserData(List<UUID> uuids) {
Map<UUID, UserData> userData = new HashMap<>();
for (UUID uuid : uuids) {
DBCallableProcessor processor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
userData.put(data.getUuid(), data);
}
};
handler.getUserDataForProcessing(processor, uuid);
}
int waitAttempts = 0;
while (uuids.size() < userData.size()) {
if (waitAttempts >= 15) {
break;
}
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
waitAttempts++;
}
return userData;
}
public void addToPool(HandlingInfo info) {
pool.add(info);
}
}

View File

@ -0,0 +1,39 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import java.sql.SQLException;
import java.util.UUID;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.data.UserData;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
/**
*
* @author Risto
*/
public class KillHandling {
public static void processKillInfo(UserData data, long time, LivingEntity dead, String weaponName) {
Plan plugin = getPlugin(Plan.class);
if (dead instanceof Player) {
Player deadPlayer = (Player) dead;
int victimID;
try {
UUID victimUUID = deadPlayer.getUniqueId();
victimID = plugin.getDB().getUserId(victimUUID + "");
data.addPlayerKill(new KillData(victimUUID, victimID, weaponName, time));
} catch (SQLException e) {
plugin.toLog("main.java.com.djrapitops.plan.KillHandling", e);
}
} else {
data.setMobKills(data.getMobKills() + 1);
}
}
}

View File

@ -0,0 +1,52 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import main.java.com.djrapitops.plan.Phrase;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Risto
*/
public class LoginHandling {
public static void processLoginInfo(UserData data, long time, InetAddress ip, boolean banned, String nickname, int loginTimes) {
data.setLastPlayed(time);
data.updateBanned(banned);
data.setLoginTimes(data.getLoginTimes() + loginTimes);
data.addNickname(nickname);
data.addIpAddress(ip);
updateGeolocation(ip, data);
}
public static void updateGeolocation(InetAddress ip, UserData data) {
DemographicsData demData = data.getDemData();
try {
String result = "";
URL url = new URL("http://freegeoip.net/csv/" + ip.getHostAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String resultline;
while ((resultline = in.readLine()) != null) {
result += resultline + ",";
}
in.close();
String[] results = result.split(",");
if (!results[2].isEmpty()) {
demData.setGeoLocation(results[2]);
}
} catch (Exception e) {
demData.setGeoLocation(Phrase.DEM_UNKNOWN + "");
}
}
}

View File

@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
/**
*
* @author Risto
*/
public class LogoutHandling {
public static void processLogoutInfo(UserData data, long time, boolean banned) {
data.setPlayTime(data.getPlayTime() + (time - data.getLastPlayed()));
data.setLastPlayed(time);
data.updateBanned(banned);
getPlugin(Plan.class).getHandler().getSessionHandler().endSession(data);
}
}

View File

@ -0,0 +1,37 @@
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.ChatHandling;
/**
*
* @author Rsl1122
*/
public class ChatInfo extends HandlingInfo {
private String nickname;
private String message;
public ChatInfo(UUID uuid, String nickname, String message) {
super(uuid, InfoType.CHAT, 0L);
this.nickname = nickname;
this.message = message;
}
public String getNickname() {
return nickname;
}
public String getMessage() {
return message;
}
@Override
public boolean process(UserData uData) {
if (!uData.getUuid().equals(super.uuid)) {
return false;
}
ChatHandling.processChatInfo(uData, nickname, message);
return true;
}
}

View File

@ -0,0 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Risto
*/
public class DeathInfo extends HandlingInfo{
public DeathInfo(UUID uuid) {
super(uuid, InfoType.DEATH, 0L);
}
@Override
public boolean process(UserData uData) {
if (uData.getUuid() != uuid) {
return false;
}
uData.setDeaths(uData.getDeaths()+1);
return true;
}
}

View File

@ -0,0 +1,36 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.GamemodeHandling;
import org.bukkit.GameMode;
/**
*
* @author Risto
*/
public class GamemodeInfo extends HandlingInfo{
private GameMode currentGamemode;
public GamemodeInfo(UUID uuid, long time, GameMode gm) {
super(uuid, InfoType.GM, time);
currentGamemode = gm;
}
@Override
public boolean process(UserData uData) {
if (currentGamemode == null) {
return false;
}
if (uData.getUuid() != uuid) {
return false;
}
GamemodeHandling.processGamemodeInfo(uData, time, currentGamemode);
return true;
}
}

View File

@ -0,0 +1,35 @@
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Rsl1122
*/
public abstract class HandlingInfo {
UUID uuid;
InfoType type;
long time;
public HandlingInfo(UUID uuid, InfoType type, long time) {
this.uuid = uuid;
this.type = type;
this.time = time;
}
public UUID getUuid() {
return uuid;
}
public InfoType getType() {
return type;
}
public long getTime() {
return time;
}
public abstract boolean process(UserData uData);
}

View File

@ -0,0 +1,14 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
/**
*
* @author Risto
*/
public enum InfoType {
CHAT, DEATH, KILL, GM, LOGIN, LOGOUT, KICK, RELOAD
}

View File

@ -0,0 +1,27 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
/**
*
* @author Risto
*/
public class KickInfo extends HandlingInfo {
public KickInfo(UUID uuid) {
super(uuid, InfoType.KICK, 0L);
}
@Override
public boolean process(UserData uData) {
uData.setTimesKicked(uData.getTimesKicked()+1);
return true;
}
}

View File

@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.KillHandling;
import org.bukkit.entity.LivingEntity;
/**
*
* @author Rsl1122
*/
public class KillInfo extends HandlingInfo {
private LivingEntity dead;
private String weaponName;
public KillInfo(UUID uuid, long time, LivingEntity dead, String weaponName) {
super(uuid, InfoType.KILL, time);
this.dead = dead;
this.weaponName = weaponName;
}
@Override
public boolean process(UserData uData) {
KillHandling.processKillInfo(uData, time, dead, weaponName);
return true;
}
}

View File

@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.net.InetAddress;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.LoginHandling;
import org.bukkit.GameMode;
/**
*
* @author Risto
*/
public class LoginInfo extends HandlingInfo{
private InetAddress ip;
private boolean banned;
private String nickname;
private GamemodeInfo gmInfo;
private int loginTimes;
public LoginInfo(UUID uuid, long time, InetAddress ip, boolean banned, String nickname, GameMode gm, int loginTimes) {
super(uuid, InfoType.LOGIN, time);
this.ip = ip;
this.banned = banned;
this.nickname = nickname;
this.gmInfo = new GamemodeInfo(uuid, time, gm);
this.loginTimes = loginTimes;
}
public LoginInfo(UUID uuid, long time, InetAddress ip, boolean banned, String nickname, GameMode gm) {
this(uuid, time, ip, banned, nickname, gm, 0);
}
@Override
public boolean process(UserData uData) {
if (uData.getUuid() != uuid) {
return false;
}
LoginHandling.processLoginInfo(uData, getTime(), ip, banned, nickname, loginTimes);
gmInfo.process(uData);
return true;
}
}

View File

@ -0,0 +1,37 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.LogoutHandling;
import org.bukkit.GameMode;
/**
*
* @author Risto
*/
public class LogoutInfo extends HandlingInfo{
private boolean banned;
private GamemodeInfo gmInfo;
public LogoutInfo(UUID uuid, long time, boolean banned, GameMode gm) {
super(uuid, InfoType.LOGOUT, time);
this.banned = banned;
this.gmInfo = new GamemodeInfo(uuid, time, gm);
}
@Override
public boolean process(UserData uData) {
if (uData.getUuid() != uuid) {
return false;
}
LogoutHandling.processLogoutInfo(uData, time, banned);
gmInfo.process(uData);
return true;
}
}

View File

@ -0,0 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.data.handling.info;
import java.net.InetAddress;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.UserData;
import org.bukkit.GameMode;
/**
*
* @author Risto
*/
public class ReloadInfo extends HandlingInfo {
private LoginInfo info;
public ReloadInfo(UUID uuid, long time, InetAddress ip, boolean banned, String nickname, GameMode gm) {
super(uuid, InfoType.RELOAD, time);
info = new LoginInfo(uuid, time, ip, banned, nickname, gm);
}
@Override
public boolean process(UserData uData) {
return info.process(uData);
}
}

View File

@ -1,11 +1,8 @@
package main.java.com.djrapitops.plan.data.listeners;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handlers.BasicInfoHandler;
import main.java.com.djrapitops.plan.data.handlers.DemographicsHandler;
import main.java.com.djrapitops.plan.data.handling.InfoPoolProcessor;
import main.java.com.djrapitops.plan.data.handling.info.ChatInfo;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -19,9 +16,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
public class PlanChatListener implements Listener {
private final Plan plugin;
private final DataCacheHandler handler;
private final DemographicsHandler demographicsHandler;
private final BasicInfoHandler basicInfoH;
private final InfoPoolProcessor processor;
/**
* Class Constructor.
@ -30,9 +25,7 @@ public class PlanChatListener implements Listener {
*/
public PlanChatListener(Plan plugin) {
this.plugin = plugin;
handler = plugin.getHandler();
demographicsHandler = handler.getDemographicsHandler();
basicInfoH = handler.getBasicInfoHandler();
processor = plugin.getInfoPoolProcessor();
}
/**
@ -46,13 +39,6 @@ public class PlanChatListener implements Listener {
return;
}
Player p = event.getPlayer();
DBCallableProcessor chatProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
basicInfoH.addNickname(p.getDisplayName(), data);
demographicsHandler.handleChatEvent(event.getMessage(), data);
}
};
handler.getUserDataForProcessing(chatProcessor, p.getUniqueId());
processor.addToPool(new ChatInfo(p.getUniqueId(), p.getDisplayName(), event.getMessage()));
}
}

View File

@ -1,10 +1,10 @@
package main.java.com.djrapitops.plan.data.listeners;
import java.util.Date;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handlers.KillHandler;
import main.java.com.djrapitops.plan.data.handling.InfoPoolProcessor;
import main.java.com.djrapitops.plan.data.handling.info.DeathInfo;
import main.java.com.djrapitops.plan.data.handling.info.KillInfo;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -19,8 +19,7 @@ import org.bukkit.event.entity.EntityDeathEvent;
public class PlanDeathEventListener implements Listener {
private final Plan plugin;
private final DataCacheHandler handler;
private final KillHandler kH;
private final InfoPoolProcessor processor;
/**
*
@ -28,8 +27,7 @@ public class PlanDeathEventListener implements Listener {
*/
public PlanDeathEventListener(Plan plugin) {
this.plugin = plugin;
this.handler = plugin.getHandler();
this.kH = this.handler.getKillHandler();
this.processor = plugin.getInfoPoolProcessor();
}
/**
@ -39,45 +37,15 @@ public class PlanDeathEventListener implements Listener {
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onDeath(EntityDeathEvent event) {
long time = new Date().getTime();
LivingEntity dead = event.getEntity();
Player killer = dead.getKiller();
boolean killerIsPlayer = killer != null;
if (killerIsPlayer) {
DBCallableProcessor deathProcess = new DBCallableProcessor() {
@Override
public void process(UserData killersData) {
continueProcessing(dead, killerIsPlayer, killer, killersData);
}
};
handler.getUserDataForProcessing(deathProcess, killer.getUniqueId());
} else {
continueProcessing(dead, false, null, null);
processor.addToPool(new KillInfo(killer.getUniqueId(), time, dead, killer.getInventory().getItemInMainHand().getType().name()));
}
}
/**
*
* @param dead
* @param killerIsPlayer
* @param killer
* @param killersData
*/
public void continueProcessing(LivingEntity dead, boolean killerIsPlayer, Player killer, UserData killersData) {
if (dead instanceof Player) {
Player killed = (Player) dead;
DBCallableProcessor deathProcess = new DBCallableProcessor() {
@Override
public void process(UserData killedsData) {
if (killerIsPlayer) {
String weaponName = killer.getInventory().getItemInMainHand().getType().name();
kH.handlePlayerKill(killersData, killed.getUniqueId(), weaponName);
}
kH.handlePlayerDeath(killedsData);
}
};
handler.getUserDataForProcessing(deathProcess, killed.getUniqueId());
} else if (killerIsPlayer) {
kH.handleMobKill(killersData);
processor.addToPool(new DeathInfo(((Player) dead).getUniqueId()));
}
}
}

View File

@ -1,10 +1,9 @@
package main.java.com.djrapitops.plan.data.listeners;
import java.util.Date;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handlers.GamemodeTimesHandler;
import main.java.com.djrapitops.plan.data.handling.InfoPoolProcessor;
import main.java.com.djrapitops.plan.data.handling.info.GamemodeInfo;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -18,8 +17,7 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent;
public class PlanGamemodeChangeListener implements Listener {
private final Plan plugin;
private final DataCacheHandler handler;
private final GamemodeTimesHandler gmTimesH;
private final InfoPoolProcessor processor;
/**
* Class Constructor.
@ -28,8 +26,8 @@ public class PlanGamemodeChangeListener implements Listener {
*/
public PlanGamemodeChangeListener(Plan plugin) {
this.plugin = plugin;
handler = plugin.getHandler();
gmTimesH = handler.getGamemodeTimesHandler();
processor = plugin.getInfoPoolProcessor();
}
/**
@ -43,12 +41,6 @@ public class PlanGamemodeChangeListener implements Listener {
return;
}
Player p = event.getPlayer();
DBCallableProcessor gmProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
gmTimesH.handleChangeEvent(event.getNewGameMode(), data);
}
};
handler.getUserDataForProcessing(gmProcessor, p.getUniqueId());
processor.addToPool(new GamemodeInfo(p.getUniqueId(), new Date().getTime(), event.getNewGameMode()));
}
}

View File

@ -1,12 +1,14 @@
package main.java.com.djrapitops.plan.data.listeners;
import java.net.InetAddress;
import java.util.Date;
import java.util.UUID;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.handlers.*;
import main.java.com.djrapitops.plan.data.handling.InfoPoolProcessor;
import main.java.com.djrapitops.plan.data.handling.info.KickInfo;
import main.java.com.djrapitops.plan.data.handling.info.LoginInfo;
import main.java.com.djrapitops.plan.data.handling.info.LogoutInfo;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -25,11 +27,7 @@ public class PlanPlayerListener implements Listener {
private final Plan plugin;
private final DataCacheHandler handler;
private final ActivityHandler activityH;
private final BasicInfoHandler basicInfoH;
private final GamemodeTimesHandler gmTimesH;
private final DemographicsHandler demographicH;
private final RuleBreakingHandler rulebreakH;
private final InfoPoolProcessor processor;
private final LocationHandler locationH;
/**
@ -43,11 +41,7 @@ public class PlanPlayerListener implements Listener {
public PlanPlayerListener(Plan plugin) {
this.plugin = plugin;
handler = plugin.getHandler();
activityH = handler.getActivityHandler();
basicInfoH = handler.getBasicInfoHandler();
gmTimesH = handler.getGamemodeTimesHandler();
demographicH = handler.getDemographicsHandler();
rulebreakH = handler.getRuleBreakingHandler();
processor = plugin.getInfoPoolProcessor();
locationH = handler.getLocationHandler();
}
@ -63,25 +57,15 @@ public class PlanPlayerListener implements Listener {
public void onPlayerLogin(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
BukkitTask asyncLoginSaveTask = (new BukkitRunnable() {
processor.addToPool(new LoginInfo(uuid, new Date().getTime(), player.getAddress().getAddress(), player.isBanned(), player.getDisplayName(), player.getGameMode(), 1));
handler.getSessionHandler().startSession(uuid);
BukkitTask asyncNewPlayerCheckTask = (new BukkitRunnable() {
@Override
public void run() {
boolean isNewPlayer = activityH.isFirstTimeJoin(uuid);
boolean isNewPlayer = !plugin.getDB().wasSeenBefore(uuid);
if (isNewPlayer) {
handler.newPlayer(player);
}
DBCallableProcessor loginProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
activityH.handleLogin(player.isBanned(), data);
InetAddress ip = player.getAddress().getAddress();
basicInfoH.handleLogin(player.getDisplayName(), ip, data);
gmTimesH.handleLogin(player.getGameMode(), data);
demographicH.handleLogin(ip, data);
handler.saveCachedData(uuid);
}
};
handler.getUserDataForProcessing(loginProcessor, uuid);
this.cancel();
}
}).runTaskAsynchronously(plugin);
@ -99,17 +83,8 @@ public class PlanPlayerListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
DBCallableProcessor logoutProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
activityH.handleLogOut(data);
gmTimesH.handleLogOut(player.getGameMode(), data);
data.addLocations(locationH.getLocationsForSaving(uuid));
handler.saveCachedData(uuid);
locationH.clearLocations(uuid);
}
};
handler.getUserDataForProcessing(logoutProcessor, uuid);
processor.addToPool(new LogoutInfo(uuid, new Date().getTime(), player.isBanned(), player.getGameMode()));
handler.saveCachedData(uuid);
}
/**
@ -124,17 +99,10 @@ public class PlanPlayerListener implements Listener {
if (event.isCancelled()) {
return;
}
UUID uuid = event.getPlayer().getUniqueId();
DBCallableProcessor kickProcessor = new DBCallableProcessor() {
@Override
public void process(UserData data) {
rulebreakH.handleKick(data);
data.addLocations(locationH.getLocationsForSaving(uuid));
handler.saveCachedData(uuid);
locationH.clearLocations(uuid);
handler.scheludeForClear(uuid);
}
};
handler.getUserDataForProcessing(kickProcessor, uuid);
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
processor.addToPool(new LogoutInfo(uuid, new Date().getTime(), player.isBanned(), player.getGameMode()));
processor.addToPool(new KickInfo(uuid));
handler.saveCachedData(uuid);
}
}

View File

@ -161,6 +161,10 @@ public abstract class SQLDB extends Database {
versionName = "plan_version";
startConnectionPingTask(plugin);
}
public void startConnectionPingTask(Plan plugin) throws IllegalArgumentException, IllegalStateException {
// Maintains Connection.
(new BukkitRunnable() {
@Override

View File

@ -1,6 +1,5 @@
package main.java.com.djrapitops.plan.utilities;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import main.java.com.djrapitops.plan.ui.Html;

View File

@ -50,13 +50,15 @@ public class ManageUtils {
* cache.
*
* @param onTimeData PlayTime data of Ontime
* @param handler Cache the data will be cached to.
* @param plugin Current instance of Plan
* @return success?
*/
public static boolean importOnTime(HashMap<UUID, Long> onTimeData, DataCacheHandler handler) {
public static boolean importOnTime(HashMap<UUID, Long> onTimeData, Plan plugin) {
DataCacheHandler handler = plugin.getHandler();
for (UUID uuid : onTimeData.keySet()) {
OfflinePlayer player = getOfflinePlayer(uuid);
if (handler.getActivityHandler().isFirstTimeJoin(uuid)) {
if (!plugin.getDB().wasSeenBefore(uuid)) {
handler.newPlayer(player);
}
DBCallableProcessor importer = new DBCallableProcessor() {

View File

@ -1,7 +1,6 @@
package main.java.com.djrapitops.plan.utilities;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -0,0 +1,22 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.java.com.djrapitops.plan.utilities.comparators;
import java.util.Comparator;
import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo;
/**
*
* @author Risto
*/
public class HandlingInfoTimeComparator implements Comparator<HandlingInfo> {
@Override
public int compare(HandlingInfo o1, HandlingInfo o2) {
return ((Long) o1.getTime()).compareTo((Long) o2.getTime());
}
}

View File

@ -10,7 +10,6 @@ import main.java.com.djrapitops.plan.Plan;
import org.bukkit.ChatColor;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

View File

@ -9,13 +9,11 @@ import main.java.com.djrapitops.plan.Phrase;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;

View File

@ -0,0 +1,50 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.KillData;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Risto
*/
public class KillDataTest {
public KillDataTest() {
}
@Test
public void testGetVictim() {
UUID uuid = UUID.fromString("71cfb6f0-c3ef-4954-8abe-13fa07afc340");
KillData k = new KillData(uuid, 1, "TestWeapon", 100L);
assertEquals(k.getVictim(), uuid);
}
@Test
public void testGetDate() {
UUID uuid = UUID.fromString("71cfb6f0-c3ef-4954-8abe-13fa07afc340");
KillData k = new KillData(uuid, 1, "TestWeapon", 100L);
assertEquals(k.getDate(), 100L);
}
@Test
public void testGetWeapon() {
UUID uuid = UUID.fromString("71cfb6f0-c3ef-4954-8abe-13fa07afc340");
KillData k = new KillData(uuid, 1, "TestWeapon", 100L);
assertEquals(k.getWeapon(), "TestWeapon");
}
@Test
public void testGetVictimUserID() {
UUID uuid = UUID.fromString("71cfb6f0-c3ef-4954-8abe-13fa07afc340");
KillData k = new KillData(uuid, 1, "TestWeapon", 100L);
assertEquals(k.getVictimUserID(), 1);
}
}

View File

@ -134,9 +134,9 @@ public class UserDataTest {
public void testAddNickname() {
String one = "Test1";
String two = "Test2";
boolean n = test.addNickname(one);
boolean n1 = test.addNickname(two);
boolean n2 = test.addNickname(two);
boolean n = test.addNickname(one);
test.addNickname(null);
assertTrue("Didn't add 1", test.getNicknames().contains(one));
assertTrue("Didn't add 2", test.getNicknames().contains(two));
@ -145,6 +145,7 @@ public class UserDataTest {
assertTrue("2 is not new", !n2);
assertTrue("Added null", !test.getNicknames().contains(null));
assertTrue("Added multiples", test.getNicknames().size() == 2);
assertTrue("Last nickname was not one", test.getLastNick().equals(one));
}
@Test

View File

@ -0,0 +1,76 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.ChatHandling;
import org.bukkit.GameMode;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class ChatHandlingTest {
public ChatHandlingTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcessChatInfoAddedNickname() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
String expected = "TestNicknameChatHandling";
ChatHandling.processChatInfo(data, expected, "");
assertTrue("Didn't add nickname", data.getNicknames().contains(expected));
}
@Test
public void testUpdateDemographicInformationMale() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
ChatHandling.updateDemographicInformation("I'm male", data);
assertTrue("Didn't update gender", data.getDemData().getGender() == Gender.MALE);
}
@Test
public void testUpdateDemographicInformationAge() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
ChatHandling.updateDemographicInformation("im 18", data);
assertTrue("Didn't update age", data.getDemData().getAge() == 18);
}
}

View File

@ -0,0 +1,155 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.GamemodeHandling;
import org.bukkit.GameMode;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class GamemodeHandlingTest {
public GamemodeHandlingTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcessGamemodeInfo() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setPlayTime(100L);
data.setLastGamemode(GameMode.CREATIVE);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeHandling.processGamemodeInfo(data, time, GameMode.SURVIVAL);
Long result = data.getGmTimes().get(GameMode.CREATIVE);
assertTrue("Gamemode time was "+result, result == 1050L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 1100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 2000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 1100L);
}
@Test
public void testProcessGamemodeInfoSameGM() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setPlayTime(100L);
data.setLastGamemode(GameMode.SURVIVAL);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeHandling.processGamemodeInfo(data, time, GameMode.SURVIVAL);
Long result = data.getGmTimes().get(GameMode.SURVIVAL);
assertTrue("Gamemode time was "+result, result == 1050L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 1100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 2000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 1100L);
}
@Test
public void testProcessGamemodeInfoNullNewGM() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setPlayTime(100L);
data.setLastGamemode(GameMode.SURVIVAL);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeHandling.processGamemodeInfo(data, time, null);
Long result = data.getGmTimes().get(GameMode.SURVIVAL);
assertTrue("Gamemode time was "+result, result == 0L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 1000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 50L);
}
@Test
public void testProcessGamemodeInfoNullOldGM() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setPlayTime(100L);
data.setLastGamemode(null);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeHandling.processGamemodeInfo(data, time, GameMode.SURVIVAL);
Long result = data.getGmTimes().get(GameMode.SURVIVAL);
assertTrue("Gamemode time was "+result, result == 1050L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 1100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 2000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 1100L);
}
@Test
public void testProcessGamemodeInfoNullGMTimes() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setGmTimes(null);
data.setPlayTime(100L);
data.setLastGamemode(null);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeHandling.processGamemodeInfo(data, time, GameMode.SURVIVAL);
Long result = data.getGmTimes().get(GameMode.SURVIVAL);
assertTrue("Gamemode time was "+result, result == 1050L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 1100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 2000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 1100L);
}
}

View File

@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling;
import org.junit.Test;
import org.junit.Ignore;
/**
*
* @author Risto
*/
public class KillHandlingTest {
public KillHandlingTest() {
}
@Ignore("Get DB tests to work")@Test
public void testProcessKillInfo() {
}
}

View File

@ -0,0 +1,76 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling;
import java.net.InetAddress;
import java.net.UnknownHostException;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.LoginHandling;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class LoginHandlingTest {
public LoginHandlingTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcessLoginInfo() throws UnknownHostException {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
InetAddress ip = InetAddress.getByName("137.19.188.146");
long time = 10L;
int loginTimes = data.getLoginTimes();
String nick = "TestProcessLoginInfo";
LoginHandling.processLoginInfo(data, time, ip, false, nick, 1);
assertTrue("LastPlayed wrong", data.getLastPlayed() == time);
assertTrue("Ip not added", data.getIps().contains(ip));
assertTrue("Logintimes not +1", data.getLoginTimes() == loginTimes + 1);
assertTrue("Nick not added", data.getNicknames().contains(nick));
assertTrue("Nick not last nick", data.getLastNick().equals(nick));
String geo = data.getDemData().getGeoLocation();
assertTrue("Wrong location " + geo, geo.equals("United States"));
}
@Test
public void testUpdateGeolocation() throws UnknownHostException {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
InetAddress ip = InetAddress.getByName("137.19.188.146");
LoginHandling.updateGeolocation(ip, data);
String result = data.getDemData().getGeoLocation();
assertTrue("Wrong location " + result, result.equals("United States"));
}
}

View File

@ -0,0 +1,101 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling.info;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.ChatHandling;
import main.java.com.djrapitops.plan.data.handling.info.ChatInfo;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class ChatInfoTest {
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
public ChatInfoTest() {
}
@Test
public void testGetNickname() {
ChatInfo i = new ChatInfo(null, "Test", "Message");
assertTrue("Nick get wrong", i.getNickname().equals("Test"));
}
@Test
public void testGetMessage() {
ChatInfo i = new ChatInfo(null, "Test", "Message");
assertTrue("Message get wrong", i.getMessage().equals("Message"));
}
@Test
public void testProcessNick() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
String expected = "TestNicknameChatInfo";
ChatInfo i = new ChatInfo(data.getUuid(), expected, "im 18 male");
assertTrue("Didn't succeed", i.process(data));
assertTrue("Didn't add nickname", data.getNicknames().contains(expected));
assertTrue("Didn't update gender", data.getDemData().getGender() == Gender.MALE);
}
@Test
public void testProcessAge() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
String expected = "TestNicknameChatInfo";
ChatInfo i = new ChatInfo(data.getUuid(), expected, "im 18 male");
assertTrue("Didn't succeed", i.process(data));
assertTrue("Didn't update age", data.getDemData().getAge() == 18);
}
@Test
public void testProcessGender() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
String expected = "TestNicknameChatInfo";
ChatInfo i = new ChatInfo(data.getUuid(), expected, "im 18 male");
assertTrue("Didn't succeed", i.process(data));
assertTrue("Didn't update gender", data.getDemData().getGender() == Gender.MALE);
}
@Test
public void testProcessWrongUUID() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
String expected = "TestNicknameChatInfo";
ChatInfo i = new ChatInfo(null, expected, "im 18 male");
assertTrue("Succeeded.", !i.process(data));
}
}

View File

@ -0,0 +1,64 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling.info;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.info.DeathInfo;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class DeathInfoTest {
public DeathInfoTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcess() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
DeathInfo i = new DeathInfo(data.getUuid());
i.process(data);
assertEquals(1, data.getDeaths());
}
@Test
public void testProcessWrongUUID() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
DeathInfo i = new DeathInfo(null);
assertTrue(!i.process(data));
assertEquals(0, data.getDeaths());
}
}

View File

@ -0,0 +1,71 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling.info;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.info.GamemodeInfo;
import org.bukkit.GameMode;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class GamemodeInfoTest {
public GamemodeInfoTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcess() {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
data.setPlayTime(100L);
data.setLastGamemode(GameMode.CREATIVE);
data.setLastGmSwapTime(50L);
data.setLastPlayed(1000L);
long time = 2000L;
GamemodeInfo i = new GamemodeInfo(data.getUuid(), time, GameMode.SURVIVAL);
i.process(data);
Long result = data.getGmTimes().get(GameMode.CREATIVE);
assertTrue("Gamemode time was "+result, result == 1050L);
result = data.getPlayTime();
assertTrue("Playtime was"+result, result == 1100L);
result = data.getLastPlayed();
assertTrue("Last Played was"+result, result == 2000L);
GameMode lastGM = data.getLastGamemode();
assertTrue("Last gm not Survival", lastGM == GameMode.SURVIVAL);
result = data.getLastGmSwapTime();
assertTrue("Last swaptime was "+result, result == 1100L);
}
}

View File

@ -0,0 +1,88 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.handling.info;
import java.net.InetAddress;
import java.net.UnknownHostException;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.LoginHandling;
import main.java.com.djrapitops.plan.data.handling.info.LoginInfo;
import org.bukkit.GameMode;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class LoginInfoTest {
public LoginInfoTest() {
}
@Before
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@Test
public void testProcess() throws UnknownHostException {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
InetAddress ip = InetAddress.getByName("137.19.188.146");
long time = 10L;
int loginTimes = data.getLoginTimes();
String nick = "TestProcessLoginInfo";
LoginInfo i = new LoginInfo(data.getUuid(), time, ip, true, nick, GameMode.CREATIVE, 1);
assertTrue(i.process(data));
assertTrue("LastPlayed wrong: " + data.getLastPlayed(), data.getLastPlayed() == time);
assertTrue("Ip not added", data.getIps().contains(ip));
assertTrue("Logintimes not +1", data.getLoginTimes() == loginTimes + 1);
assertTrue("Nick not added", data.getNicknames().contains(nick));
assertTrue("Nick not last nick", data.getLastNick().equals(nick));
String geo = data.getDemData().getGeoLocation();
assertTrue("Wrong location " + geo, geo.equals("United States"));
assertTrue("Didn't process gamemode", data.getLastGamemode() == GameMode.CREATIVE);
}
@Test
public void testProcessWrongUUID() throws UnknownHostException {
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
InetAddress ip = InetAddress.getByName("137.19.188.146");
long time = 10L;
String nick = "TestProcessLoginInfo";
LoginInfo i = new LoginInfo(null, time, ip, true, nick, GameMode.CREATIVE, 1);
assertTrue(!i.process(data));
assertTrue("LastPlayed wrong: " + data.getLastPlayed(), data.getLastPlayed() == 0L);
assertTrue("Ip not added", !data.getIps().contains(ip));
assertTrue("Logintimes not +1", data.getLoginTimes() == 0);
assertTrue("Nick not added", !data.getNicknames().contains(nick));
String geo = data.getDemData().getGeoLocation();
assertTrue("Wrong location " + geo, geo.equals("Not Known"));
assertTrue("Didn't process gamemode", data.getLastGamemode() == GameMode.SURVIVAL);
}
}

View File

@ -5,33 +5,38 @@
*/
package test.java.main.java.com.djrapitops.plan.database;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.DemographicsData;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;
import test.java.utils.TestInit;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.scheduler.*;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.bukkit.scheduler.BukkitScheduler;
import org.easymock.EasyMock;
import static org.easymock.EasyMock.anyLong;
import org.junit.Ignore;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import static org.junit.Assert.assertTrue;
import test.java.utils.MockUtils;
/**
*
@ -42,6 +47,7 @@ import static org.powermock.api.mockito.PowerMockito.whenNew;
public class DatabaseTest {
private Plan plan;
private Database db;
private int rows;
public DatabaseTest() {
@ -53,36 +59,131 @@ public class DatabaseTest {
assertTrue("Not set up", t.setUp());
plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = 0;
if (f.exists()) {
rows = Files.readLines(f, Charset.defaultCharset()).size();
rows = Files.lines(f.toPath(), Charset.defaultCharset()).collect(Collectors.toList()).size();
}
BukkitRunnable mockRunnable = PowerMockito.mock(BukkitRunnable.class);
when(mockRunnable.runTaskTimerAsynchronously(plan, anyLong(), anyLong())).thenReturn(null);
whenNew(BukkitRunnable.class).withNoArguments().thenReturn(mockRunnable);
db = new SQLiteDB(plan, "debug") {
@Override
public void startConnectionPingTask(Plan plugin) {
}
};
// BukkitRunnable mockRunnable = PowerMockito.mock(BukkitRunnable.class);
// when(mockRunnable.runTaskTimerAsynchronously(plan, anyLong(), anyLong())).thenReturn(null);
// whenNew(BukkitRunnable.class).withNoArguments().thenReturn(mockRunnable);
//
PowerMock.mockStatic(Bukkit.class);
// PowerMock.replay(Bukkit.class);
BukkitScheduler mockScheduler = Mockito.mock(BukkitScheduler.class);
EasyMock.expect(Bukkit.getScheduler()).andReturn(mockScheduler);
OfflinePlayer op = MockUtils.mockPlayer();
EasyMock.expect(Bukkit.getOfflinePlayer(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"))).andReturn(op);
op = MockUtils.mockPlayer2();
EasyMock.expect(Bukkit.getOfflinePlayer(UUID.fromString("ec94a954-1fa1-445b-b09b-9b698519af80"))).andReturn(op);
PowerMock.replay(Bukkit.class);
// BukkitScheduler mockScheduler = Mockito.mock(BukkitScheduler.class);
// EasyMock.expect(Bukkit.getScheduler()).andReturn(mockScheduler);
}
@After
public void tearDown() throws IOException {
public void tearDown() throws IOException, SQLException {
db.close();
File f = new File(plan.getDataFolder(), "Errors.txt");
int rowsAgain = 0;
if (f.exists()) {
rowsAgain = Files.readLines(f, Charset.defaultCharset()).size();
rowsAgain = Files.lines(f.toPath(), Charset.defaultCharset()).collect(Collectors.toList()).size();
}
assertTrue("Errors were caught.", rows == rowsAgain);
}
@Ignore @Test
@Test
public void testInit() {
Database db = new SQLiteDB(plan, "debug.db");
assertTrue("Database failed to init.", db.init());
}
@Test
public void testRemoveAll() throws SQLException {
db.init();
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
db.saveUserData(data.getUuid(), data);
HashMap<String, Integer> c = new HashMap<>();
c.put("/plan", 1);
c.put("/tp", 4);
c.put("/pla", 7);
c.put("/help", 21);
c.put("/roiergbnougbierubieugbeigubeigubgierbgeugeg", 3);
db.saveCommandUse(c);
db.removeAllData();
assertTrue("Contains the user", db.getUserId(data.getUuid().toString()) == -1);
assertTrue("Contains commandUse", db.getCommandUse().isEmpty());
}
@Test
public void testSaveCommandUse() throws SQLException {
db.init();
HashMap<String, Integer> c = new HashMap<>();
c.put("/plan", 1);
c.put("/tp", 4);
c.put("/pla", 7);
c.put("/help", 21);
c.put("/roiergbnougbierubieugbeigubeigubgierbgeugeg", 3);
db.saveCommandUse(c);
assertTrue("Doesn't contain /plan", db.getCommandUse().containsKey("/plan"));
assertTrue("Doesn't contain /tp", db.getCommandUse().containsKey("/tp"));
assertTrue("Doesn't contain /pla", db.getCommandUse().containsKey("/pla"));
assertTrue("Doesn't contain /help", db.getCommandUse().containsKey("/help"));
assertTrue("Contains too long cmd", !db.getCommandUse().containsKey("/roiergbnougbierubieugbeigubeigubgierbgeugeg"));
}
@Test
public void testSaveUserData() throws SQLException {
db.init();
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
db.saveUserData(data.getUuid(), data);
DBCallableProcessor process = new DBCallableProcessor() {
@Override
public void process(UserData d) {
assertTrue("Not Equals", data.equals(d));
}
};
db.giveUserDataToProcessors(data.getUuid(), process);
}
@Test
public void testSaveMultipleUserData() throws SQLException {
db.init();
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
UserData data2 = new UserData(MockUtils.mockPlayer2(), new DemographicsData());
List<UserData> list = new ArrayList<>();
list.add(data);
list.add(data2);
db.saveMultipleUserData(list);
DBCallableProcessor process = new DBCallableProcessor() {
@Override
public void process(UserData d) {
assertTrue("Not Equals", data.equals(d));
}
};
db.giveUserDataToProcessors(data.getUuid(), process);
DBCallableProcessor process2 = new DBCallableProcessor() {
@Override
public void process(UserData d) {
assertTrue("Not Equals", data2.equals(d));
}
};
db.giveUserDataToProcessors(data2.getUuid(), process2);
}
@Test
public void testRemove() throws SQLException {
db.init();
UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
db.saveUserData(data.getUuid(), data);
db.removeAccount(data.getUuid().toString());
assertTrue("Contains the user", !db.wasSeenBefore(data.getUuid()));
}
}

View File

@ -5,7 +5,6 @@
*/
package test.java.main.java.com.djrapitops.plan.ui;
import java.io.File;
import main.java.com.djrapitops.plan.ui.Html;
import org.junit.Test;
import static org.junit.Assert.*;

View File

@ -13,7 +13,6 @@ import main.java.com.djrapitops.plan.data.SessionData;
import main.java.com.djrapitops.plan.ui.graphs.PlayerActivityGraphCreator;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;
/**
*

View File

@ -17,7 +17,6 @@ import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;

View File

@ -14,7 +14,6 @@ import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;

View File

@ -34,6 +34,19 @@ public class MockUtils {
when(p.getName()).thenReturn("TestName");
return p;
}
public static Player mockPlayer2() {
Player p = PowerMockito.mock(Player.class);
when(p.getGameMode()).thenReturn(GameMode.SPECTATOR);
when(p.getUniqueId()).thenReturn(UUID.fromString("ec94a954-1fa1-445b-b09b-9b698519af80"));
when(p.getFirstPlayed()).thenReturn(3423434L);
World mockWorld = mockWorld();
when(p.getLocation()).thenReturn(new Location(mockWorld, 1, 0, 1));
when(p.isOp()).thenReturn(false);
when(p.isBanned()).thenReturn(false);
when(p.isOnline()).thenReturn(false);
when(p.getName()).thenReturn("TestName2");
return p;
}
public static Player mockBrokenPlayer() {
Player p = PowerMockito.mock(Player.class);

View File

@ -32,8 +32,14 @@ public class TestInit {
YamlConfiguration configuration = new YamlConfiguration();
configuration.load(configfile.getAbsolutePath());
when(planMock.getConfig()).thenReturn(configuration);
Files.deleteIfExists(new File("temporaryTestFolder").toPath());
File testFolder = new File("temporaryTestFolder");
if (testFolder.exists()) {
for (File f : testFolder.listFiles()) {
Files.deleteIfExists(f.toPath());
}
}
Files.deleteIfExists(new File("temporaryTestFolder").toPath());
testFolder = new File("temporaryTestFolder");
testFolder.mkdir();
//
when(planMock.getDataFolder()).thenReturn(testFolder);