Avoid parsing UUID in sponge world calculator

This commit is contained in:
Luck 2017-03-30 18:34:45 +01:00
parent ae82807139
commit 4d0499a61e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 10 additions and 18 deletions

View File

@ -265,7 +265,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
contextManager = new ContextManager<>();
contextManager.registerCalculator(new ServerCalculator<>(configuration));
contextManager.registerCalculator(new WorldCalculator(this));
contextManager.registerCalculator(new WorldCalculator());
// register the PermissionService with Sponge
getLog().info("Registering PermissionService...");

View File

@ -26,46 +26,38 @@ import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.api.context.ContextCalculator;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.sponge.LPSpongePlugin;
import org.spongepowered.api.command.CommandSource;
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 implements ContextCalculator<Subject> {
private final LPSpongePlugin plugin;
@Override
public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) {
UUID uuid = Util.parseUuid(subject.getIdentifier());
if (uuid == null) {
CommandSource source = subject.getCommandSource().orElse(null);
if (source == null || !(source instanceof Player)) {
return accumulator;
}
Optional<Player> p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid));
if (!p.isPresent()) {
return accumulator;
}
accumulator.add(Context.WORLD_KEY, p.get().getWorld().getName());
Player p = ((Player) source);
accumulator.add(Context.WORLD_KEY, p.getWorld().getName());
return accumulator;
}
@Override
public boolean isContextApplicable(Subject subject, Map.Entry<String, String> context) {
UUID uuid = Util.parseUuid(subject.getIdentifier());
if (uuid == null) {
CommandSource source = subject.getCommandSource().orElse(null);
if (source == null || !(source instanceof Player)) {
return false;
}
Optional<Player> p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid));
return p.map(player -> context.getKey().equals(Context.WORLD_KEY) && player.getWorld().getName().equals(context.getValue())).orElse(false);
Player p = ((Player) source);
return context.getKey().equals(Context.WORLD_KEY) && p.getWorld().getName().equals(context.getValue());
}
}