mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2026-02-18 01:42:32 +01:00
Compare commits
3 Commits
commandson
...
dev-asyncL
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ffd11f6e0e | ||
|
|
f1f9ffc10b | ||
|
|
8fe18be79b |
@@ -89,10 +89,14 @@ public class McMMOPlayer {
|
||||
private boolean isUsingUnarmed;
|
||||
|
||||
public McMMOPlayer(Player player) {
|
||||
this(player, mcMMO.getDatabaseManager().loadPlayerProfile(player.getName(), true));
|
||||
}
|
||||
|
||||
public McMMOPlayer(Player player, PlayerProfile profile) {
|
||||
String playerName = player.getName();
|
||||
|
||||
this.player = player;
|
||||
profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true);
|
||||
this.profile = profile;
|
||||
party = PartyManager.getPlayerParty(playerName);
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,12 +15,14 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerFishEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
import org.bukkit.event.player.PlayerPickupItemEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
@@ -264,6 +266,24 @@ public class PlayerListener implements Listener {
|
||||
BleedTimerTask.bleedOut(player); // Bleed it out
|
||||
}
|
||||
|
||||
/**
|
||||
* Start user data prefetch.
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onLoginStart(PlayerLoginEvent event) {
|
||||
UserManager.prefetchUserData(event.getPlayer().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel user data prefetch if a plugin kicks them.
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false)
|
||||
public void onLoginComplete(PlayerLoginEvent event) {
|
||||
if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
|
||||
UserManager.discardPrefetch(event.getPlayer().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Monitor PlayerJoin events.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.gmail.nossr50.runnables.player;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
|
||||
public class PlayerProfileLoader implements Callable<PlayerProfile> {
|
||||
private final String playerName;
|
||||
|
||||
public PlayerProfileLoader(String player) {
|
||||
this.playerName = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerProfile call() {
|
||||
return mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,55 @@
|
||||
package com.gmail.nossr50.util.player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
import com.gmail.nossr50.runnables.player.PlayerProfileLoader;
|
||||
|
||||
public final class UserManager {
|
||||
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
|
||||
private final static Map<String, Future<PlayerProfile>> loadTasks = new HashMap<String, Future<PlayerProfile>>();
|
||||
private final static ExecutorService loadExecutor = Executors.newCachedThreadPool();
|
||||
|
||||
private UserManager() {};
|
||||
|
||||
/**
|
||||
* Add a new user.
|
||||
* Asynchronously pre-fetch information about the player. This is intended
|
||||
* to expedite the PlayerJoinEvent.
|
||||
*
|
||||
* @param playerName The player name
|
||||
*/
|
||||
public static void prefetchUserData(String playerName) {
|
||||
loadTasks.put(playerName, loadExecutor.submit(new PlayerProfileLoader(playerName)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard the information from the prefetch - for example, due to the
|
||||
* user being banned.
|
||||
*
|
||||
* @param playerName The player name
|
||||
*/
|
||||
public static void discardPrefetch(String playerName) {
|
||||
Future<PlayerProfile> oldTask = loadTasks.remove(playerName);
|
||||
if (oldTask != null) {
|
||||
oldTask.cancel(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new user. If the prefetched player information is available, it
|
||||
* will be used.
|
||||
*
|
||||
* @param player The player to create a user record for
|
||||
* @return the player's {@link McMMOPlayer} object
|
||||
@@ -29,7 +62,22 @@ public final class UserManager {
|
||||
mcMMOPlayer.setPlayer(player); // The player object is different on each reconnection and must be updated
|
||||
}
|
||||
else {
|
||||
Future<PlayerProfile> task = loadTasks.remove(playerName);
|
||||
if (task != null && !task.isCancelled()) {
|
||||
try {
|
||||
mcMMOPlayer = new McMMOPlayer(player, task.get());
|
||||
// TODO copy any additional post-processing here
|
||||
players.put(playerName, mcMMOPlayer);
|
||||
return mcMMOPlayer;
|
||||
}
|
||||
catch (ExecutionException e) {
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
// Did not return - load on main thread
|
||||
mcMMOPlayer = new McMMOPlayer(player);
|
||||
// (start post-processing that must be copied above)
|
||||
players.put(playerName, mcMMOPlayer);
|
||||
}
|
||||
|
||||
@@ -43,12 +91,14 @@ public final class UserManager {
|
||||
*/
|
||||
public static void remove(String playerName) {
|
||||
players.remove(playerName);
|
||||
discardPrefetch(playerName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all users.
|
||||
*/
|
||||
public static void clearAll() {
|
||||
discardAllPrefetch();
|
||||
players.clear();
|
||||
}
|
||||
|
||||
@@ -56,11 +106,23 @@ public final class UserManager {
|
||||
* Save all users.
|
||||
*/
|
||||
public static void saveAll() {
|
||||
discardAllPrefetch();
|
||||
for (McMMOPlayer mcMMOPlayer : players.values()) {
|
||||
mcMMOPlayer.getProfile().save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard / cancel all data prefetching.
|
||||
*/
|
||||
public static void discardAllPrefetch() {
|
||||
Iterator<Future<PlayerProfile>> taskIter = loadTasks.values().iterator();
|
||||
while (taskIter.hasNext()) {
|
||||
taskIter.next().cancel(false);
|
||||
taskIter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, McMMOPlayer> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user