Finishing touches to Listener-Process system

This commit is contained in:
Rsl1122 2017-08-24 13:23:44 +03:00
parent 11ba16d918
commit dced471adb
11 changed files with 123 additions and 91 deletions

View File

@ -37,6 +37,7 @@ import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.cache.PageCache;
import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.listeners.*;
import main.java.com.djrapitops.plan.systems.processing.Processor;
@ -73,7 +74,6 @@ public class Plan extends BukkitPlugin<Plan> {
private API api;
private ProcessingQueue processingQueue;
private DataCache dataCache;
private HookHandler hookHandler; // Manages 3rd party data sources
private Database db;
@ -81,6 +81,7 @@ public class Plan extends BukkitPlugin<Plan> {
private WebServer uiServer;
private InformationManager infoManager;
private ServerInfoManager serverInfoManager;
private ServerVariableHolder serverVariableHolder;
@ -159,7 +160,7 @@ public class Plan extends BukkitPlugin<Plan> {
Benchmark.stop("Enable", "Init Database");
Benchmark.start("Init DataCache");
this.dataCache = new DataCache(this);
infoManager = new InformationManager(this);
Benchmark.stop("Enable", "Init DataCache");
registerListeners();
@ -283,7 +284,7 @@ public class Plan extends BukkitPlugin<Plan> {
getServer().getScheduler().cancelTasks(this);
if (Verify.notNull(dataCache, db)) {
if (Verify.notNull(infoManager, db)) {
// Saves the DataCache to the database without Bukkit's Schedulers.
Log.info(Locale.get(Msg.DISABLE_CACHE_SAVE).toString());
@ -359,7 +360,7 @@ public class Plan extends BukkitPlugin<Plan> {
* @return Current instance of the DataCache
*/
public DataCache getDataCache() {
return dataCache;
return getInfoManager().getDataCache();
}
/**
@ -447,4 +448,8 @@ public class Plan extends BukkitPlugin<Plan> {
public static UUID getServerUUID() {
return getInstance().getServerInfoManager().getServerUUID();
}
public InformationManager getInfoManager() {
return infoManager;
}
}

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.systems.cache;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.database.Database;
import java.util.*;
/**
* This Class contains the Cache.
* <p>
@ -18,6 +20,11 @@ public class DataCache extends SessionCache {
private final Database db;
private final Map<UUID, String> playerNames;
private final Map<UUID, String> displayNames;
private final Set<UUID> playersWithFirstSession;
/**
* Class Constructor.
* <p>
@ -27,8 +34,36 @@ public class DataCache extends SessionCache {
* @param plugin Current instance of Plan
*/
public DataCache(Plan plugin) {
super(plugin); // Initializes Session & Location cache.
super(plugin);
db = plugin.getDB();
playerNames = new HashMap<>();
displayNames = new HashMap<>();
playersWithFirstSession = new HashSet<>();
}
public void updateNames(UUID uuid, String playerName, String displayName) {
playerNames.put(uuid, playerName);
displayNames.put(uuid, displayName);
}
public String getName(UUID uuid) {
return playerNames.get(uuid);
}
public String getDisplayName(UUID uuid) {
return displayNames.get(uuid);
}
public void addFirstLeaveCheck(UUID uuid) {
playersWithFirstSession.add(uuid);
}
public boolean isFirstSession(UUID uuid) {
return playersWithFirstSession.contains(uuid);
}
public void clearFromFirstLeaveCheck(UUID uuid) {
playersWithFirstSession.remove(uuid);
}
}

View File

@ -23,7 +23,6 @@ public class InformationManager {
private final Database db;
private final DataCache dataCache;
private final SessionCache sessionCache;
private boolean usingBungeeWebServer;
private String webServerAddress;
@ -36,7 +35,6 @@ public class InformationManager {
.ifPresent(address -> webServerAddress = address);
dataCache = new DataCache(plugin);
sessionCache = new SessionCache(plugin);
if (webServerAddress != null) {
attemptBungeeConnection();
@ -69,6 +67,6 @@ public class InformationManager {
}
public SessionCache getSessionCache() {
return sessionCache;
return dataCache;
}
}

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.systems.listeners;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.processing.player.NameProcessor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -8,6 +9,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.UUID;
/**
* Event Listener for AsyncPlayerChatEvents.
*
@ -16,6 +19,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
public class PlanChatListener implements Listener {
private final Plan plugin;
private final DataCache dataCache;
/**
* Class Constructor.
@ -24,6 +28,7 @@ public class PlanChatListener implements Listener {
*/
public PlanChatListener(Plan plugin) {
this.plugin = plugin;
dataCache = plugin.getDataCache();
}
/**
@ -38,7 +43,10 @@ public class PlanChatListener implements Listener {
}
Player p = event.getPlayer();
// TODO NameCache to DataCache
plugin.addToProcessQueue(new NameProcessor(p.getUniqueId(), p.getName(), p.getDisplayName()));
UUID uuid = p.getUniqueId();
String name = p.getName();
String displayName = p.getDisplayName();
plugin.addToProcessQueue(new NameProcessor(uuid, name, displayName));
}
}

View File

@ -16,8 +16,6 @@ import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
@ -31,8 +29,6 @@ public class PlanPlayerListener implements Listener {
private final Plan plugin;
private final DataCache cache;
private final Set<UUID> playersWithFirstSession;
/**
* Class Constructor.
*
@ -41,17 +37,17 @@ public class PlanPlayerListener implements Listener {
public PlanPlayerListener(Plan plugin) {
this.plugin = plugin;
cache = plugin.getDataCache();
playersWithFirstSession = new HashSet<>();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogin(PlayerLoginEvent event) {
PlayerLoginEvent.Result result = event.getResult();
UUID uuid = event.getPlayer().getUniqueId();
boolean op = event.getPlayer().isOp();
if (result == PlayerLoginEvent.Result.KICK_BANNED) {
plugin.addToProcessQueue(new BanProcessor(uuid, true));
plugin.addToProcessQueue(new BanAndOpProcessor(uuid, true, op));
} else {
plugin.addToProcessQueue(new BanProcessor(uuid, false));
plugin.addToProcessQueue(new BanAndOpProcessor(uuid, false, op));
}
}
@ -102,7 +98,7 @@ public class PlanPlayerListener implements Listener {
plugin.addToProcessQueue(
new RegisterProcessor(this, uuid, player.getFirstPlayed(), playerName, playersOnline),
new IPUpdateProcessor(uuid, ip),
new NameProcessor(uuid, playerName, displayName), // TODO NameCache to DataCache
new NameProcessor(uuid, playerName, displayName),
new DBCommitProcessor(plugin.getDB())
);
}
@ -122,19 +118,14 @@ public class PlanPlayerListener implements Listener {
UUID uuid = player.getUniqueId();
plugin.addToProcessQueue(
new BanProcessor(uuid, player.isBanned()),
new BanAndOpProcessor(uuid, player.isBanned(), player.isOp()),
new EndSessionProcessor(uuid, time)
);
int messagesSent = 0; // TODO messages Sent on first session
if (playersWithFirstSession.contains(uuid)) {
if (cache.isFirstSession(uuid)) {
plugin.addToProcessQueue(new FirstLeaveProcessor(uuid, time, messagesSent));
}
}
// TODO MOVE TO DATACACHE
public void addFirstLeaveCheck(UUID uuid) {
playersWithFirstSession.add(uuid);
}
}

View File

@ -0,0 +1,38 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.processing.player;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import java.sql.SQLException;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class BanAndOpProcessor extends PlayerProcessor {
private final boolean banned;
private final boolean opped;
public BanAndOpProcessor(UUID uuid, boolean banned, boolean op) {
super(uuid);
this.banned = banned;
opped = op;
}
@Override
public void process() {
UUID uuid = getUUID();
try {
Plan.getInstance().getDB().getUserInfoTable().updateOpAndBanStatus(uuid, opped, banned);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}

View File

@ -1,28 +0,0 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.processing.player;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class BanProcessor extends PlayerProcessor {
private final boolean banned;
public BanProcessor(UUID uuid, boolean banned) {
super(uuid);
this.banned = banned;
}
@Override
public void process() {
UUID uuid = getUUID();
// TODO DB Update Ban status
}
}

View File

@ -29,10 +29,14 @@ public class FirstLeaveProcessor extends PlayerProcessor {
@Override
public void process() {
Plan plugin = Plan.getInstance();
UUID uuid = getUUID();
try {
Plan.getInstance().getDB().getActionsTable().insertAction(getUUID(), leaveAction);
plugin.getDB().getActionsTable().insertAction(uuid, leaveAction);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
} finally {
plugin.getDataCache().clearFromFirstLeaveCheck(uuid);
}
}
}

View File

@ -7,12 +7,13 @@ package main.java.com.djrapitops.plan.systems.processing.player;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.systems.cache.DataCache;
import java.sql.SQLException;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
* Processor for updating name in the database if the player has changed it.
*
* @author Rsl1122
*/
@ -30,12 +31,23 @@ public class NameProcessor extends PlayerProcessor {
@Override
public void process() {
UUID uuid = getUUID();
Database db = Plan.getInstance().getDB();
Plan plugin = Plan.getInstance();
DataCache dataCache = plugin.getDataCache();
String cachedName = dataCache.getName(uuid);
String cachedDisplayName = dataCache.getDisplayName(uuid);
if (playerName.equals(cachedName) && displayName.equals(cachedDisplayName)) {
return;
}
Database db = plugin.getDB();
try {
db.getUsersTable().updateName(uuid, playerName);
db.getNicknamesTable().saveUserName(uuid, displayName);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
dataCache.updateNames(uuid, playerName, displayName);
}
}

View File

@ -1,28 +0,0 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.processing.player;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class OPProcessor extends PlayerProcessor {
private final boolean banned;
public OPProcessor(UUID uuid, boolean banned) {
super(uuid);
this.banned = banned;
}
@Override
public void process() {
UUID uuid = getUUID();
// TODO DB Update Ban status
}
}

View File

@ -9,7 +9,6 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.Actions;
import main.java.com.djrapitops.plan.systems.listeners.PlanPlayerListener;
import java.sql.SQLException;
import java.util.UUID;
@ -25,11 +24,8 @@ public class RegisterProcessor extends PlayerProcessor {
private final int playersOnline;
private final String name;
private final PlanPlayerListener listener;
public RegisterProcessor(PlanPlayerListener listener, UUID uuid, long time, String name, int playersOnline) {
public RegisterProcessor(UUID uuid, long time, String name, int playersOnline) {
super(uuid);
this.listener = listener;
this.time = time;
this.playersOnline = playersOnline;
this.name = name;
@ -38,11 +34,12 @@ public class RegisterProcessor extends PlayerProcessor {
@Override
public void process() {
UUID uuid = getUUID();
Database db = Plan.getInstance().getDB();
Plan plugin = Plan.getInstance();
Database db = plugin.getDB();
if (db.wasSeenBefore(uuid)) {
return;
}
listener.addFirstLeaveCheck(uuid);
plugin.getDataCache().addFirstLeaveCheck(uuid);
try {
db.getUsersTable().registerUser(uuid, time, name);
db.getActionsTable().insertAction(uuid, new Action(time, Actions.REGISTERED, "Online: " + playersOnline + " Players"));