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();
}
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();
}
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());
});
}
}
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;
}
Aggregations