From 6109718c67bad2d781f97486668492d1280892d0 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 13 Oct 2016 21:16:34 +0100 Subject: [PATCH] Cleanup some remaining TODOs --- .../luckperms/bukkit/LPBukkitPlugin.java | 15 +++++--- .../luckperms/bungee/LPBungeePlugin.java | 15 +++++--- .../luckperms/common/LuckPermsPlugin.java | 24 +++++++++++-- .../me/lucko/luckperms/common/users/User.java | 5 +-- .../luckperms/common/users/UserManager.java | 14 ++++++-- .../luckperms/sponge/LPSpongePlugin.java | 20 +++++++---- .../sponge/contexts/SpongeCalculatorLink.java | 7 ++-- .../sponge/contexts/WorldCalculator.java | 36 ++++++++++++++++--- .../sponge/service/LuckPermsUserSubject.java | 13 ++----- 9 files changed, 107 insertions(+), 42 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index e2c4144e..8298aec0 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -220,11 +220,6 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { return getDataFolder(); } - @Override - public Message getPlayerStatus(UUID uuid) { - return getServer().getPlayer(getUuidCache().getExternalUUID(uuid)) != null ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; - } - @Override public int getPlayerCount() { return getServer().getOnlinePlayers().size(); @@ -235,6 +230,16 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { return getServer().getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); } + @Override + public Set getOnlinePlayers() { + return getServer().getOnlinePlayers().stream().map(Player::getUniqueId).collect(Collectors.toSet()); + } + + @Override + public boolean isOnline(UUID external) { + return getServer().getPlayer(external) != null; + } + @Override public List getNotifyListeners() { return getServer().getOnlinePlayers().stream() diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 727f1890..22bbebe3 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -171,11 +171,6 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { return getDataFolder(); } - @Override - public Message getPlayerStatus(UUID uuid) { - return getProxy().getPlayer(getUuidCache().getExternalUUID(uuid)) != null ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; - } - @Override public int getPlayerCount() { return getProxy().getOnlineCount(); @@ -186,6 +181,16 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { return getProxy().getPlayers().stream().map(ProxiedPlayer::getName).collect(Collectors.toList()); } + @Override + public Set getOnlinePlayers() { + return getProxy().getPlayers().stream().map(ProxiedPlayer::getUniqueId).collect(Collectors.toSet()); + } + + @Override + public boolean isOnline(UUID external) { + return getProxy().getPlayer(external) != null; + } + @Override public List getNotifyListeners() { return getProxy().getPlayers().stream() diff --git a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java index 273a75c3..4c378b9f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java @@ -93,7 +93,10 @@ public interface LuckPermsPlugin { * @param uuid The player's uuid * @return a formatted status string */ - Message getPlayerStatus(UUID uuid); + default Message getPlayerStatus(UUID uuid) { + UUID external = getUuidCache().getExternalUUID(uuid); + return isOnline(external) ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; + } /** * Gets the number of users online on the platform @@ -107,6 +110,19 @@ public interface LuckPermsPlugin { */ List getPlayerList(); + /** + * Gets the UUIDs of the users online on the platform + * @return a {@link Set} of UUIDs + */ + Set getOnlinePlayers(); + + /** + * Checks if a user is online + * @param external the users external uuid + * @return true if the user is online + */ + boolean isOnline(UUID external); + /** * @return a {@link List} of senders online on the platform */ @@ -117,7 +133,11 @@ public interface LuckPermsPlugin { */ Sender getConsoleSender(); - // TODO javadoc + /** + * Gets a set of Contexts that should be pre-processed in advance + * @param op if the user being processed is op + * @return a set of contexts + */ Set getPreProcessContexts(boolean op); /** diff --git a/common/src/main/java/me/lucko/luckperms/common/users/User.java b/common/src/main/java/me/lucko/luckperms/common/users/User.java index dc296e9c..219c3de3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/users/User.java +++ b/common/src/main/java/me/lucko/luckperms/common/users/User.java @@ -27,9 +27,11 @@ import lombok.Getter; import lombok.Setter; import lombok.ToString; import me.lucko.luckperms.api.event.events.GroupAddEvent; +import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.api.internal.GroupLink; import me.lucko.luckperms.common.api.internal.PermissionHolderLink; +import me.lucko.luckperms.common.api.internal.UserLink; import me.lucko.luckperms.common.caching.UserData; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.groups.Group; @@ -109,7 +111,6 @@ public class User extends PermissionHolder implements Identifiable { * @param user The user to be cleaned up */ public void cleanup(User user) { - // TODO + if (!plugin.isOnline(plugin.getUuidCache().getExternalUUID(user.getUuid()))) { + unload(user); + } } /** * Reloads the data of all online users */ public void updateAllUsers() { - // TODO + plugin.doSync(() -> { + Set players = plugin.getOnlinePlayers(); + plugin.doAsync(() -> { + for (UUID uuid : players) { + plugin.getDatastore().loadUser(plugin.getUuidCache().getUUID(uuid), "null"); + } + }); + }); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 8effe5f0..53a43294 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -65,6 +65,7 @@ import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.service.permission.PermissionDescription; import org.spongepowered.api.service.permission.PermissionService; +import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.text.Text; import java.io.File; @@ -103,7 +104,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { private ConsecutiveExecutor consecutiveExecutor; private LuckPermsService service; private LocaleManager localeManager; - private ContextManager contextManager; // TODO convert this to use Subject instead of Player + private ContextManager contextManager; private CalculatorFactory calculatorFactory; @Listener @@ -146,7 +147,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { contextManager = new ContextManager<>(); contextManager.registerCalculator(new ServerCalculator<>(getConfiguration().getServer())); - contextManager.registerCalculator(new WorldCalculator()); + contextManager.registerCalculator(new WorldCalculator(this)); getLog().info("Registering PermissionService..."); Sponge.getServiceManager().setProvider(this, PermissionService.class, (service = new LuckPermsService(this))); @@ -227,11 +228,6 @@ public class LPSpongePlugin implements LuckPermsPlugin { return PlatformType.SPONGE; } - @Override - public Message getPlayerStatus(UUID uuid) { - return game.getServer().getPlayer(getUuidCache().getExternalUUID(uuid)).isPresent() ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; - } - @Override public int getPlayerCount() { return game.getServer().getOnlinePlayers().size(); @@ -242,6 +238,16 @@ public class LPSpongePlugin implements LuckPermsPlugin { return game.getServer().getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); } + @Override + public Set getOnlinePlayers() { + return game.getServer().getOnlinePlayers().stream().map(Player::getUniqueId).collect(Collectors.toSet()); + } + + @Override + public boolean isOnline(UUID external) { + return game.getServer().getPlayer(external).isPresent(); + } + @Override public List getNotifyListeners() { return game.getServer().getOnlinePlayers().stream() diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java index efc72cee..84e17d54 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java @@ -24,7 +24,6 @@ package me.lucko.luckperms.sponge.contexts; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.context.ContextCalculator; -import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; @@ -33,11 +32,11 @@ import java.util.Set; import java.util.stream.Collectors; @AllArgsConstructor -public class SpongeCalculatorLink extends ContextCalculator { +public class SpongeCalculatorLink extends ContextCalculator { private final org.spongepowered.api.service.context.ContextCalculator calculator; @Override - public Map giveApplicableContext(Player subject, Map accumulator) { + public Map giveApplicableContext(Subject subject, Map accumulator) { Set contexts = accumulator.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); calculator.accumulateContexts(subject, contexts); @@ -46,7 +45,7 @@ public class SpongeCalculatorLink extends ContextCalculator { } @Override - public boolean isContextApplicable(Player subject, Map.Entry context) { + public boolean isContextApplicable(Subject subject, Map.Entry context) { Context c = new Context(context.getKey(), context.getValue()); return calculator.matches(c, subject); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/WorldCalculator.java b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/WorldCalculator.java index 2ae81f30..c31b9cfc 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/WorldCalculator.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/WorldCalculator.java @@ -24,23 +24,49 @@ package me.lucko.luckperms.sponge.contexts; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.api.context.ContextCalculator; +import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.sponge.LPSpongePlugin; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.Subject; import java.util.Map; +import java.util.Optional; +import java.util.UUID; @RequiredArgsConstructor -public class WorldCalculator extends ContextCalculator { +public class WorldCalculator extends ContextCalculator { + private final LPSpongePlugin plugin; @Override - public Map giveApplicableContext(Player subject, Map accumulator) { - accumulator.put(Context.WORLD_KEY, subject.getWorld().getName()); + public Map giveApplicableContext(Subject subject, Map accumulator) { + UUID uuid = Util.parseUuid(subject.getIdentifier()); + if (uuid == null) { + return accumulator; + } + + Optional p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid)); + if (!p.isPresent()) { + return accumulator; + } + + accumulator.put(Context.WORLD_KEY, p.get().getWorld().getName()); return accumulator; } @Override - public boolean isContextApplicable(Player subject, Map.Entry context) { - return context.getKey().equals(Context.WORLD_KEY) && subject.getWorld().getName().equals(context.getValue()); + public boolean isContextApplicable(Subject subject, Map.Entry context) { + UUID uuid = Util.parseUuid(subject.getIdentifier()); + if (uuid == null) { + return false; + } + + Optional p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid)); + if (!p.isPresent()) { + return false; + } + + return context.getKey().equals(Context.WORLD_KEY) && p.get().getWorld().getName().equals(context.getValue()); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java index e4007e8d..a21646ea 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsUserSubject.java @@ -172,15 +172,8 @@ public class LuckPermsUserSubject implements Subject { @Override public Set getActiveContexts() { - final UUID uuid = service.getPlugin().getUuidCache().getExternalUUID(user.getUuid()); - Optional player = Sponge.getServer().getPlayer(uuid); - - if (!player.isPresent()) { - return SubjectData.GLOBAL_CONTEXT; - } - - Map context = new HashMap<>(); - service.getPlugin().getContextManager().giveApplicableContext(player.get(), context); - return context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); + return service.getPlugin().getContextManager().giveApplicableContext(this, new HashMap<>()).entrySet().stream() + .map(e -> new Context(e.getKey(), e.getValue())) + .collect(Collectors.toSet()); } }