#1113 Handle LimboPlayer tasks via LimboService
- Add methods to LimboService for handling messages to make it the only relevant Limbo class for outside classes - Move LimboPlayerTaskManager to limbo package and make it package-private - Create MessageTask and TimeoutTask immediately when LimboPlayer is created - #1112 MessageTask: improve efficiency by keeping reference to Player
This commit is contained in:
@@ -10,6 +10,7 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@@ -18,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
public class LimboService {
|
||||
|
||||
private Map<String, LimboPlayer> entries = new ConcurrentHashMap<>();
|
||||
private final Map<String, LimboPlayer> entries = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
private SpawnLoader spawnLoader;
|
||||
@@ -29,6 +30,9 @@ public class LimboService {
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
@Inject
|
||||
private LimboPlayerTaskManager taskManager;
|
||||
|
||||
LimboService() {
|
||||
}
|
||||
|
||||
@@ -62,6 +66,7 @@ public class LimboService {
|
||||
player.setWalkSpeed(walkSpeed);
|
||||
player.setFlySpeed(flySpeed);
|
||||
limbo.clearTasks();
|
||||
ConsoleLogger.debug("Restored LimboPlayer stats for `{0}`", lowerName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,8 +94,9 @@ public class LimboService {
|
||||
* Creates a LimboPlayer for the given player and revokes all "limbo data" from the player.
|
||||
*
|
||||
* @param player the player to process
|
||||
* @param isRegistered whether or not the player is registered
|
||||
*/
|
||||
public void createLimboPlayer(Player player) {
|
||||
public void createLimboPlayer(Player player, boolean isRegistered) {
|
||||
final String name = player.getName().toLowerCase();
|
||||
|
||||
LimboPlayer existingLimbo = entries.remove(name);
|
||||
@@ -100,10 +106,53 @@ public class LimboService {
|
||||
}
|
||||
|
||||
LimboPlayer limboPlayer = newLimboPlayer(player);
|
||||
taskManager.registerMessageTask(player, limboPlayer, isRegistered);
|
||||
taskManager.registerTimeoutTask(player, limboPlayer);
|
||||
revokeLimboStates(player);
|
||||
entries.put(name, limboPlayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new tasks for the given player and cancels the old ones for a newly registered player.
|
||||
* This resets his time to log in (TimeoutTask) and updates the messages he is shown (MessageTask).
|
||||
*
|
||||
* @param player the player to reset the tasks for
|
||||
*/
|
||||
public void replaceTasksAfterRegistration(Player player) {
|
||||
getLimboOrLogError(player, "reset tasks")
|
||||
.ifPresent(limbo -> {
|
||||
taskManager.registerTimeoutTask(player, limbo);
|
||||
taskManager.registerMessageTask(player, limbo, true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the message task associated with the player's LimboPlayer.
|
||||
*
|
||||
* @param player the player to set a new message task for
|
||||
* @param isRegistered whether or not the player is registered
|
||||
*/
|
||||
public void resetMessageTask(Player player, boolean isRegistered) {
|
||||
getLimboOrLogError(player, "reset message task")
|
||||
.ifPresent(limbo -> taskManager.registerMessageTask(player, limbo, isRegistered));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player the player whose message task should be muted
|
||||
*/
|
||||
public void muteMessageTask(Player player) {
|
||||
getLimboOrLogError(player, "mute message task")
|
||||
.ifPresent(limbo -> LimboPlayerTaskManager.setMuted(limbo.getMessageTask(), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player the player whose message task should be unmuted
|
||||
*/
|
||||
public void unmuteMessageTask(Player player) {
|
||||
getLimboOrLogError(player, "unmute message task")
|
||||
.ifPresent(limbo -> LimboPlayerTaskManager.setMuted(limbo.getMessageTask(), false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a LimboPlayer with the given player's details.
|
||||
*
|
||||
@@ -143,4 +192,19 @@ public class LimboService {
|
||||
player.setWalkSpeed(0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the limbo player for the given player or logs an error.
|
||||
*
|
||||
* @param player the player to retrieve the limbo player for
|
||||
* @param context the action for which the limbo player is being retrieved (for logging)
|
||||
* @return Optional with the limbo player
|
||||
*/
|
||||
private Optional<LimboPlayer> getLimboOrLogError(Player player, String context) {
|
||||
LimboPlayer limbo = entries.get(player.getName().toLowerCase());
|
||||
if (limbo == null) {
|
||||
ConsoleLogger.debug("No LimboPlayer found for `{0}`. Action: {1}", player.getName(), context);
|
||||
}
|
||||
return Optional.ofNullable(limbo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user