Search in sources :

Example 1 with Profile

use of com.sk89q.worldguard.util.profile.Profile in project WorldGuard by EngineHub.

the class DefaultDomain method toPlayersComponent.

private Component toPlayersComponent(ProfileCache cache) {
    List<String> uuids = Lists.newArrayList();
    Map<String, UUID> profileMap = Maps.newHashMap();
    for (String name : playerDomain.getPlayers()) {
        profileMap.put(name, null);
    }
    if (cache != null) {
        ImmutableMap<UUID, Profile> results = cache.getAllPresent(playerDomain.getUniqueIds());
        for (UUID uuid : playerDomain.getUniqueIds()) {
            Profile profile = results.get(uuid);
            if (profile != null) {
                profileMap.put(profile.getName(), uuid);
            } else {
                uuids.add(uuid.toString());
            }
        }
    } else {
        for (UUID uuid : playerDomain.getUniqueIds()) {
            uuids.add(uuid.toString());
        }
    }
    final TextComponent.Builder builder = TextComponent.builder("");
    final Iterator<TextComponent> profiles = profileMap.keySet().stream().sorted().map(name -> {
        final UUID uuid = profileMap.get(name);
        final TextComponent component = TextComponent.of(name, TextColor.YELLOW).hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, uuid == null ? TextComponent.of("Name only", TextColor.GRAY) : TextComponent.of("Last known name of uuid: ", TextColor.GRAY).append(TextComponent.of(uuid.toString(), TextColor.WHITE))));
        if (uuid == null) {
            return component;
        }
        return component.clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, uuid.toString()));
    }).iterator();
    while (profiles.hasNext()) {
        builder.append(profiles.next());
        if (profiles.hasNext() || !uuids.isEmpty()) {
            builder.append(TextComponent.of(", "));
        }
    }
    if (!uuids.isEmpty()) {
        builder.append(TextComponent.of(uuids.size() + " unknown uuid" + (uuids.size() == 1 ? "" : "s"), TextColor.GRAY).hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of(String.join("\n", uuids)).append(TextComponent.newline().append(TextComponent.of("Click to select"))))).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, String.join(",", uuids))));
    }
    return builder.build();
}
Also used : TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) LocalPlayer(com.sk89q.worldguard.LocalPlayer) UUID(java.util.UUID) HoverEvent(com.sk89q.worldedit.util.formatting.text.event.HoverEvent) Maps(com.google.common.collect.Maps) Component(com.sk89q.worldedit.util.formatting.text.Component) ArrayList(java.util.ArrayList) List(java.util.List) Lists(com.google.common.collect.Lists) TextColor(com.sk89q.worldedit.util.formatting.text.format.TextColor) ClickEvent(com.sk89q.worldedit.util.formatting.text.event.ClickEvent) Map(java.util.Map) Profile(com.sk89q.worldguard.util.profile.Profile) ChangeTracked(com.sk89q.worldguard.util.ChangeTracked) ProfileCache(com.sk89q.worldguard.util.profile.cache.ProfileCache) Nullable(javax.annotation.Nullable) UUID(java.util.UUID) Profile(com.sk89q.worldguard.util.profile.Profile)

Example 2 with Profile

use of com.sk89q.worldguard.util.profile.Profile in project WorldGuard by EngineHub.

the class DomainInputResolver method call.

@Override
public DefaultDomain call() throws UnresolvedNamesException {
    DefaultDomain domain = new DefaultDomain();
    List<String> namesToQuery = new ArrayList<>();
    for (String s : input) {
        Matcher m = GROUP_PATTERN.matcher(s);
        if (m.matches()) {
            domain.addGroup(m.group(1));
        } else {
            UUID uuid = parseUUID(s);
            if (uuid != null) {
                // Try to add any UUIDs given
                domain.addPlayer(UUID.fromString(UUIDs.addDashes(s.replaceAll("^uuid:", ""))));
            } else {
                switch(locatorPolicy) {
                    case NAME_ONLY:
                        domain.addPlayer(s);
                        break;
                    case UUID_ONLY:
                        namesToQuery.add(s.toLowerCase());
                        break;
                    case UUID_AND_NAME:
                        domain.addPlayer(s);
                        namesToQuery.add(s.toLowerCase());
                }
            }
        }
    }
    if (!namesToQuery.isEmpty()) {
        try {
            for (Profile profile : profileService.findAllByName(namesToQuery)) {
                namesToQuery.remove(profile.getName().toLowerCase());
                domain.addPlayer(profile.getUniqueId());
            }
        } catch (IOException e) {
            throw new UnresolvedNamesException("The UUID lookup service failed so the names entered could not be turned into UUIDs");
        } catch (InterruptedException e) {
            throw new UnresolvedNamesException("UUID lookup was interrupted");
        }
    }
    if (!namesToQuery.isEmpty()) {
        throw new UnresolvedNamesException("Unable to resolve the names " + Joiner.on(", ").join(namesToQuery));
    }
    return domain;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UUID(java.util.UUID) DefaultDomain(com.sk89q.worldguard.domains.DefaultDomain) Profile(com.sk89q.worldguard.util.profile.Profile)

Example 3 with Profile

use of com.sk89q.worldguard.util.profile.Profile in project WorldGuard by EngineHub.

the class WorldGuardPlayerListener method onPlayerJoin.

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    World world = player.getWorld();
    ConfigurationManager cfg = getConfig();
    WorldConfiguration wcfg = getWorldConfig(world);
    if (cfg.activityHaltToggle) {
        player.sendMessage(ChatColor.YELLOW + "Intensive server activity has been HALTED.");
        int removed = 0;
        for (Entity entity : world.getEntities()) {
            if (Entities.isIntensiveEntity(BukkitAdapter.adapt(entity))) {
                entity.remove();
                removed++;
            }
        }
        if (removed > 10) {
            log.info("Halt-Act: " + removed + " entities (>10) auto-removed from " + player.getWorld());
        }
    }
    if (wcfg.fireSpreadDisableToggle) {
        player.sendMessage(ChatColor.YELLOW + "Fire spread is currently globally disabled for this world.");
    }
    Events.fire(new ProcessPlayerEvent(player));
    WorldGuard.getInstance().getExecutorService().submit(() -> WorldGuard.getInstance().getProfileCache().put(new Profile(player.getUniqueId(), player.getName())));
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) ProcessPlayerEvent(com.sk89q.worldguard.bukkit.event.player.ProcessPlayerEvent) World(org.bukkit.World) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) Profile(com.sk89q.worldguard.util.profile.Profile) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Profile

use of com.sk89q.worldguard.util.profile.Profile in project WorldGuard by EngineHub.

the class UUIDMigration method migrate.

private boolean migrate(Collection<ProtectedRegion> regions) throws MigrationException {
    // Name scan pass
    Set<String> names = getNames(regions);
    if (!names.isEmpty()) {
        // This task logs the progress of conversion (% converted...)
        // periodically
        TimerTask task = new ResolvedNamesTimerTask();
        try {
            timer.schedule(task, LOG_DELAY, LOG_DELAY);
            log.log(Level.INFO, "Resolving " + names.size() + " name(s) into UUIDs... this may take a while.");
            // Don't lookup names that we already looked up for previous
            // worlds -- note: all names are lowercase in these collections
            Set<String> lookupNames = new HashSet<>(names);
            lookupNames.removeAll(resolvedNames.keySet());
            // Ask Mojang for names
            profileService.findAllByName(lookupNames, new Predicate<Profile>() {

                @Override
                public boolean apply(Profile profile) {
                    resolvedNames.put(profile.getName().toLowerCase(), profile.getUniqueId());
                    return true;
                }
            });
        } catch (IOException e) {
            throw new MigrationException("The name -> UUID service failed", e);
        } catch (InterruptedException e) {
            throw new MigrationException("The migration was interrupted");
        } finally {
            // Stop showing the % converted messages
            task.cancel();
        }
        // Name -> UUID in all regions
        log.log(Level.INFO, "UUIDs resolved... now migrating all regions to UUIDs where possible...");
        convert(regions);
        return true;
    } else {
        return false;
    }
}
Also used : TimerTask(java.util.TimerTask) IOException(java.io.IOException) Profile(com.sk89q.worldguard.util.profile.Profile) HashSet(java.util.HashSet)

Example 5 with Profile

use of com.sk89q.worldguard.util.profile.Profile in project WorldGuard by EngineHub.

the class DefaultDomain method toPlayersString.

public String toPlayersString(@Nullable ProfileCache cache) {
    StringBuilder str = new StringBuilder();
    List<String> output = new ArrayList<>();
    for (String name : playerDomain.getPlayers()) {
        output.add("name:" + name);
    }
    if (cache != null) {
        ImmutableMap<UUID, Profile> results = cache.getAllPresent(playerDomain.getUniqueIds());
        for (UUID uuid : playerDomain.getUniqueIds()) {
            Profile profile = results.get(uuid);
            if (profile != null) {
                output.add(profile.getName() + "*");
            } else {
                output.add("uuid:" + uuid);
            }
        }
    } else {
        for (UUID uuid : playerDomain.getUniqueIds()) {
            output.add("uuid:" + uuid);
        }
    }
    output.sort(String.CASE_INSENSITIVE_ORDER);
    for (Iterator<String> it = output.iterator(); it.hasNext(); ) {
        str.append(it.next());
        if (it.hasNext()) {
            str.append(", ");
        }
    }
    return str.toString();
}
Also used : ArrayList(java.util.ArrayList) UUID(java.util.UUID) Profile(com.sk89q.worldguard.util.profile.Profile)

Aggregations

Profile (com.sk89q.worldguard.util.profile.Profile)5 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 LocalPlayer (com.sk89q.worldguard.LocalPlayer)2 IOException (java.io.IOException)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Component (com.sk89q.worldedit.util.formatting.text.Component)1 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)1 ClickEvent (com.sk89q.worldedit.util.formatting.text.event.ClickEvent)1 HoverEvent (com.sk89q.worldedit.util.formatting.text.event.HoverEvent)1 TextColor (com.sk89q.worldedit.util.formatting.text.format.TextColor)1 ProcessPlayerEvent (com.sk89q.worldguard.bukkit.event.player.ProcessPlayerEvent)1 ConfigurationManager (com.sk89q.worldguard.config.ConfigurationManager)1 WorldConfiguration (com.sk89q.worldguard.config.WorldConfiguration)1 DefaultDomain (com.sk89q.worldguard.domains.DefaultDomain)1 ChangeTracked (com.sk89q.worldguard.util.ChangeTracked)1 ProfileCache (com.sk89q.worldguard.util.profile.cache.ProfileCache)1