Search in sources :

Example 41 with GameProfile

use of org.spongepowered.api.profile.GameProfile in project Nucleus by NucleusPowered.

the class BanCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    final String r = args.<String>getOne(reason).orElse(plugin.getMessageProvider().getMessageWithFormat("ban.defaultreason"));
    Optional<GameProfile> ou = Optional.ofNullable(args.<GameProfile>getOne(uuid).orElseGet(() -> args.<GameProfile>getOne(user).orElse(null)));
    if (ou.isPresent()) {
        Optional<User> optionalUser = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(ou.get());
        if ((!optionalUser.isPresent() || !optionalUser.get().isOnline()) && !permissions.testSuffix(src, "offline")) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.offline.noperms"));
        }
        if (optionalUser.isPresent() && permissions.testSuffix(optionalUser.get(), "exempt.target", src, false)) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.exempt", optionalUser.get().getName()));
        }
        return executeBan(src, ou.get(), r);
    }
    if (!permissions.testSuffix(src, "offline")) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.offline.noperms"));
    }
    final String userToFind = args.<String>getOne(name).get();
    // Get the profile async.
    Sponge.getScheduler().createAsyncExecutor(plugin).execute(() -> {
        GameProfileManager gpm = Sponge.getServer().getGameProfileManager();
        try {
            GameProfile gp = gpm.get(userToFind).get();
            // Ban the user sync.
            Sponge.getScheduler().createSyncExecutor(plugin).execute(() -> {
                // Create the user.
                UserStorageService uss = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
                User user = uss.getOrCreate(gp);
                src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("gameprofile.new", user.getName()));
                try {
                    executeBan(src, gp, r);
                } catch (Exception e) {
                    Nucleus.getNucleus().printStackTraceIfDebugMode(e);
                }
            });
        } catch (Exception e) {
            Nucleus.getNucleus().printStackTraceIfDebugMode(e);
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.profileerror", userToFind));
        }
    });
    return CommandResult.empty();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) GameProfile(org.spongepowered.api.profile.GameProfile) GameProfileManager(org.spongepowered.api.profile.GameProfileManager) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)

Example 42 with GameProfile

use of org.spongepowered.api.profile.GameProfile in project Nucleus by NucleusPowered.

the class CheckBanCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    GameProfile gp;
    if (args.hasAny(key)) {
        gp = args.<GameProfile>getOne(key).get();
    } else {
        gp = args.<GameProfile>getOne(key2).get();
    }
    BanService service = Sponge.getServiceManager().provideUnchecked(BanService.class);
    Optional<Ban.Profile> obp = service.getBanFor(gp);
    if (!obp.isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkban.notset", gp.getName().orElse(plugin.getMessageProvider().getMessageWithFormat("standard.unknown"))));
        return CommandResult.success();
    }
    Ban.Profile bp = obp.get();
    String name;
    if (bp.getBanSource().isPresent()) {
        name = bp.getBanSource().get().toPlain();
    } else {
        name = plugin.getMessageProvider().getMessageWithFormat("standard.unknown");
    }
    if (bp.getExpirationDate().isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkban.bannedfor", gp.getName().orElse(plugin.getMessageProvider().getMessageWithFormat("standard.unknown")), name, Util.getTimeToNow(bp.getExpirationDate().get())));
    } else {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkban.bannedperm", gp.getName().orElse(plugin.getMessageProvider().getMessageWithFormat("standard.unknown")), name));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkban.created", Util.FULL_TIME_FORMATTER.withLocale(src.getLocale()).format(bp.getCreationDate())));
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("standard.reasoncoloured", TextSerializers.FORMATTING_CODE.serialize(bp.getReason().orElse(plugin.getMessageProvider().getTextMessageWithFormat("ban.defaultreason")))));
    return CommandResult.success();
}
Also used : GameProfile(org.spongepowered.api.profile.GameProfile) BanService(org.spongepowered.api.service.ban.BanService) GameProfile(org.spongepowered.api.profile.GameProfile) Ban(org.spongepowered.api.util.ban.Ban)

Example 43 with GameProfile

use of org.spongepowered.api.profile.GameProfile in project Nucleus by NucleusPowered.

the class ServerListListener method onServerListPing.

@Listener
public void onServerListPing(ClientPingServerEvent event, @Getter("getResponse") ClientPingServerEvent.Response response) {
    if (this.config == null) {
        try {
            onReload();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
    if (this.config.isModifyServerList()) {
        List<NucleusTextTemplateImpl> list = null;
        Optional<Text> ott = plugin.getGeneralService().get(ServerListGeneralDataModule.class).getMessage();
        if (ott.isPresent()) {
            response.setDescription(ott.get());
        } else {
            if (Sponge.getServer().hasWhitelist() && !this.config.getWhitelist().isEmpty()) {
                list = this.config.getWhitelist();
            } else if (!this.config.getMessages().isEmpty()) {
                list = this.config.getMessages();
            }
            if (list != null) {
                NucleusTextTemplate template = list.get(this.random.nextInt(list.size()));
                response.setDescription(template.getForCommandSource(Sponge.getServer().getConsole()));
            }
        }
    }
    if (this.config.isHidePlayerCount()) {
        response.setHidePlayers(true);
    } else if (this.config.isHideVanishedPlayers()) {
        Collection<GameProfile> players = Sponge.getServer().getOnlinePlayers().stream().filter(x -> !x.get(Keys.VANISH).orElse(false)).map(User::getProfile).collect(Collectors.toList());
        response.getPlayers().ifPresent(y -> {
            y.getProfiles().clear();
            y.getProfiles().addAll(players);
            y.setOnline(players.size());
        });
    }
}
Also used : Getter(org.spongepowered.api.event.filter.Getter) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Keys(org.spongepowered.api.data.key.Keys) User(org.spongepowered.api.entity.living.player.User) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Collection(java.util.Collection) ServerListModule(io.github.nucleuspowered.nucleus.modules.serverlist.ServerListModule) Sponge(org.spongepowered.api.Sponge) Random(java.util.Random) ClientPingServerEvent(org.spongepowered.api.event.server.ClientPingServerEvent) ServerListConfigAdapter(io.github.nucleuspowered.nucleus.modules.serverlist.config.ServerListConfigAdapter) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) ServerListConfig(io.github.nucleuspowered.nucleus.modules.serverlist.config.ServerListConfig) List(java.util.List) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Optional(java.util.Optional) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) GameProfile(org.spongepowered.api.profile.GameProfile) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Listener(org.spongepowered.api.event.Listener) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) User(org.spongepowered.api.entity.living.player.User) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Collection(java.util.Collection) Text(org.spongepowered.api.text.Text) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Listener(org.spongepowered.api.event.Listener)

Example 44 with GameProfile

use of org.spongepowered.api.profile.GameProfile in project Nucleus by NucleusPowered.

the class NicknameArgumentTests method getMockUserStorageService.

private UserStorageService getMockUserStorageService() {
    GameProfile gp1 = Mockito.mock(GameProfile.class);
    GameProfile gp2 = Mockito.mock(GameProfile.class);
    Mockito.when(gp1.getName()).thenReturn(Optional.of("test"));
    Mockito.when(gp2.getName()).thenReturn(Optional.of("testtest"));
    UserStorageService mockUss = Mockito.mock(UserStorageService.class);
    Mockito.when(mockUss.getAll()).thenReturn(Lists.newArrayList(gp1, gp2));
    User u1 = Mockito.mock(User.class);
    Mockito.when(u1.getName()).thenAnswer(g -> gp1.getName().get());
    Mockito.when(u1.getPlayer()).thenAnswer(g -> Optional.empty());
    User u2 = Mockito.mock(User.class);
    Mockito.when(u2.getName()).thenAnswer(g -> gp2.getName().get());
    Mockito.when(u2.getPlayer()).thenAnswer(g -> Optional.empty());
    Mockito.when(mockUss.get(gp1)).thenReturn(Optional.of(u1));
    Mockito.when(mockUss.get(gp2)).thenReturn(Optional.of(u2));
    return mockUss;
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) GameProfile(org.spongepowered.api.profile.GameProfile)

Aggregations

GameProfile (org.spongepowered.api.profile.GameProfile)44 Optional (java.util.Optional)12 UUID (java.util.UUID)12 Text (org.spongepowered.api.text.Text)9 User (org.spongepowered.api.entity.living.player.User)8 IOException (java.io.IOException)7 List (java.util.List)6 Sponge (org.spongepowered.api.Sponge)6 Cause (org.spongepowered.api.event.cause.Cause)6 World (org.spongepowered.api.world.World)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 Collectors (java.util.stream.Collectors)5 Keys (org.spongepowered.api.data.key.Keys)5 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 ImmutableList (com.google.common.collect.ImmutableList)4 Lists (com.google.common.collect.Lists)4 Collection (java.util.Collection)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4