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 = new ContextManager<>();
contextManager.registerCalculator(new ServerCalculator<>(configuration)); contextManager.registerCalculator(new ServerCalculator<>(configuration));
contextManager.registerCalculator(new WorldCalculator(this)); contextManager.registerCalculator(new WorldCalculator());
// register the PermissionService with Sponge // register the PermissionService with Sponge
getLog().info("Registering PermissionService..."); 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.ContextCalculator;
import me.lucko.luckperms.api.context.MutableContextSet; 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.entity.living.player.Player;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.service.permission.Subject;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@RequiredArgsConstructor @RequiredArgsConstructor
public class WorldCalculator implements ContextCalculator<Subject> { public class WorldCalculator implements ContextCalculator<Subject> {
private final LPSpongePlugin plugin;
@Override @Override
public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) { public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) {
UUID uuid = Util.parseUuid(subject.getIdentifier()); CommandSource source = subject.getCommandSource().orElse(null);
if (uuid == null) { if (source == null || !(source instanceof Player)) {
return accumulator; return accumulator;
} }
Optional<Player> p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid)); Player p = ((Player) source);
if (!p.isPresent()) { accumulator.add(Context.WORLD_KEY, p.getWorld().getName());
return accumulator;
}
accumulator.add(Context.WORLD_KEY, p.get().getWorld().getName());
return accumulator; return accumulator;
} }
@Override @Override
public boolean isContextApplicable(Subject subject, Map.Entry<String, String> context) { public boolean isContextApplicable(Subject subject, Map.Entry<String, String> context) {
UUID uuid = Util.parseUuid(subject.getIdentifier()); CommandSource source = subject.getCommandSource().orElse(null);
if (uuid == null) { if (source == null || !(source instanceof Player)) {
return false; return false;
} }
Optional<Player> p = plugin.getGame().getServer().getPlayer(plugin.getUuidCache().getExternalUUID(uuid)); Player p = ((Player) source);
return p.map(player -> context.getKey().equals(Context.WORLD_KEY) && player.getWorld().getName().equals(context.getValue())).orElse(false); return context.getKey().equals(Context.WORLD_KEY) && p.getWorld().getName().equals(context.getValue());
} }
} }