Search in sources :

Example 1 with UserStorageService

use of org.spongepowered.api.service.user.UserStorageService in project RedProtect by FabioZumbi12.

the class RPUtil method PlayerToUUID.

public static String PlayerToUUID(String PlayerName) {
    if (PlayerName == null || PlayerName.equals("")) {
        return null;
    }
    // check if is already UUID
    if (isUUIDs(PlayerName) || isDefaultServer(PlayerName) || (PlayerName.startsWith("[") && PlayerName.endsWith("]"))) {
        return PlayerName;
    }
    String uuid = PlayerName;
    if (!RedProtect.get().OnlineMode) {
        uuid = uuid.toLowerCase();
        return uuid;
    }
    UserStorageService uss = Sponge.getGame().getServiceManager().provide(UserStorageService.class).get();
    Optional<GameProfile> ogpName = uss.getAll().stream().filter(f -> f.getName().isPresent() && f.getName().get().equalsIgnoreCase(PlayerName)).findFirst();
    if (ogpName.isPresent()) {
        return ogpName.get().getUniqueId().toString();
    } else {
        Optional<Player> p = RedProtect.get().serv.getPlayer(PlayerName);
        if (p.isPresent()) {
            return p.get().getUniqueId().toString();
        }
    }
    return uuid;
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) Connection(java.sql.Connection) Date(java.util.Date) ConfigurationLoader(ninja.leaping.configurate.loader.ConfigurationLoader) UserStorageService(org.spongepowered.api.service.user.UserStorageService) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ResultSet(java.sql.ResultSet) Map(java.util.Map) GameProfile(org.spongepowered.api.profile.GameProfile) ParseException(java.text.ParseException) ZipEntry(java.util.zip.ZipEntry) DateFormat(java.text.DateFormat) Location(org.spongepowered.api.world.Location) User(org.spongepowered.api.entity.living.player.User) Set(java.util.Set) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) PreparedStatement(java.sql.PreparedStatement) BlockState(org.spongepowered.api.block.BlockState) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) List(java.util.List) World(org.spongepowered.api.world.World) Optional(java.util.Optional) HandTypes(org.spongepowered.api.data.type.HandTypes) Player(org.spongepowered.api.entity.living.player.Player) ZipOutputStream(java.util.zip.ZipOutputStream) Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) SimpleDateFormat(java.text.SimpleDateFormat) DatabaseMetaData(java.sql.DatabaseMetaData) HashMap(java.util.HashMap) TypeToken(com.google.common.reflect.TypeToken) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Text(org.spongepowered.api.text.Text) LinkedList(java.util.LinkedList) HoconConfigurationLoader(ninja.leaping.configurate.hocon.HoconConfigurationLoader) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) WEListener(br.net.fabiozumbi12.RedProtect.Sponge.hooks.WEListener) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Entity(org.spongepowered.api.entity.Entity) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) RPLang(br.net.fabiozumbi12.RedProtect.Sponge.config.RPLang) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) EntityType(org.spongepowered.api.entity.EntityType) Collections(java.util.Collections) ItemType(org.spongepowered.api.item.ItemType) DriverManager(java.sql.DriverManager) Player(org.spongepowered.api.entity.living.player.Player) GameProfile(org.spongepowered.api.profile.GameProfile)

Example 2 with UserStorageService

use of org.spongepowered.api.service.user.UserStorageService in project core by CubeEngine.

the class UserMatcher method match.

public Optional<User> match(String name, boolean searchOffline) {
    Game game = Sponge.getGame();
    if (name == null) {
        return null;
    }
    // Direct Match Online Players:
    Optional<Player> player = game.getServer().getPlayer(name);
    if (player.isPresent()) {
        return Optional.of(player.get());
    }
    // Find Online Players with similar name
    Map<String, Player> onlinePlayerMap = new HashMap<>();
    for (Player onlineUser : game.getServer().getOnlinePlayers()) {
        onlinePlayerMap.put(onlineUser.getName(), onlineUser);
    }
    String foundUser = sm.matchString(name, onlinePlayerMap.keySet());
    if (foundUser != null) {
        return Optional.of(onlinePlayerMap.get(foundUser));
    }
    UserStorageService storage = game.getServiceManager().provideUnchecked(UserStorageService.class);
    Optional<User> directMatchOffline;
    try {
        directMatchOffline = storage.get(name);
    } catch (IllegalArgumentException ignore) {
        return Optional.empty();
    }
    if (directMatchOffline.isPresent()) {
        return directMatchOffline;
    }
    if (searchOffline) {
        String match = sm.matchString(name, storage.getAll().stream().map(GameProfile::getName).filter(Optional::isPresent).map(Optional::get).collect(toList()));
        if (match != null) {
            return storage.get(match);
        }
    }
    return Optional.empty();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) Player(org.spongepowered.api.entity.living.player.Player) Game(org.spongepowered.api.Game) User(org.spongepowered.api.entity.living.player.User) Optional(java.util.Optional) HashMap(java.util.HashMap) GameProfile(org.spongepowered.api.profile.GameProfile)

Example 3 with UserStorageService

use of org.spongepowered.api.service.user.UserStorageService in project modules-extra by CubeEngine.

the class Vote method onVote.

@Listener
public void onVote(VotifierEvent event) throws ExecutionException, InterruptedException {
    final com.vexsoftware.votifier.model.Vote vote = event.getVote();
    UserStorageService uss = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    Optional<User> user = uss.get(vote.getUsername());
    if (!user.isPresent()) {
        if (vote.getUsername() == null || vote.getUsername().trim().isEmpty()) {
            logger.info("{} voted but is not known to the server!", vote.getUsername());
        }
        return;
    }
    final DSLContext dsl = db.getDSL();
    db.queryOne(dsl.selectFrom(TABLE_VOTE).where(TABLE_VOTE.ID.eq(user.get().getUniqueId()))).thenAcceptAsync((voteModel) -> {
        if (voteModel != null) {
            if (voteModel.timePassed(config.voteBonusTime.toMillis())) {
                voteModel.setVotes(1);
            } else {
                voteModel.addVote();
            }
            voteModel.update();
        } else {
            voteModel = dsl.newRecord(TABLE_VOTE).newVote(user.get());
            voteModel.insert();
        }
        UniqueAccount acc = economy.getOrCreateAccount(user.get().getUniqueId()).get();
        final int voteAmount = voteModel.getVotes();
        double money = this.config.voteReward * pow(1 + 1.5 / voteAmount, voteAmount - 1);
        acc.deposit(economy.getDefaultCurrency(), new BigDecimal(money), Cause.of(EventContext.empty(), event.getVote()));
        Text moneyFormat = economy.getDefaultCurrency().format(new BigDecimal(money));
        bc.broadcastMessage(NONE, ChatFormat.parseFormats(this.config.voteBroadcast).replace("{PLAYER}", vote.getUsername()).replace("{MONEY}", moneyFormat.toPlain()).replace("{AMOUNT}", String.valueOf(voteAmount)).replace("{VOTEURL}", this.config.voteUrl));
        user.get().getPlayer().ifPresent(p -> p.sendMessage(Text.of(ChatFormat.parseFormats(this.config.voteMessage.replace("{PLAYER}", vote.getUsername()).replace("{MONEY}", moneyFormat.toPlain()).replace("{AMOUNT}", String.valueOf(voteAmount)).replace("{VOTEURL}", this.config.voteUrl)))));
    });
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) DSLContext(org.jooq.DSLContext) Text(org.spongepowered.api.text.Text) BigDecimal(java.math.BigDecimal) Listener(org.spongepowered.api.event.Listener)

Example 4 with UserStorageService

use of org.spongepowered.api.service.user.UserStorageService in project Nucleus by NucleusPowered.

the class MailFilterArgument method player.

private Optional<UUID> player(String text) {
    if (text.equalsIgnoreCase("server") || (text.equalsIgnoreCase("console"))) {
        return Optional.of(Util.consoleFakeUUID);
    }
    UserStorageService uss = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    Optional<User> ou = uss.get(text);
    return ou.map(Identifiable::getUniqueId);
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) Identifiable(org.spongepowered.api.util.Identifiable)

Example 5 with UserStorageService

use of org.spongepowered.api.service.user.UserStorageService in project Skree by Skelril.

the class RegionListMembersCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    UserStorageService userService = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    List<Text> result = ref.getMembers().stream().map(userService::get).filter(Optional::isPresent).map(Optional::get).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).map(a -> Text.of((a.isOnline() ? TextColors.GREEN : TextColors.RED), a.getName())).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Members")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) UserStorageService(org.spongepowered.api.service.user.UserStorageService) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Aggregations

UserStorageService (org.spongepowered.api.service.user.UserStorageService)13 User (org.spongepowered.api.entity.living.player.User)10 Text (org.spongepowered.api.text.Text)5 Optional (java.util.Optional)4 Player (org.spongepowered.api.entity.living.player.Player)4 GameProfile (org.spongepowered.api.profile.GameProfile)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Sponge (org.spongepowered.api.Sponge)3 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 PaginationService (org.spongepowered.api.service.pagination.PaginationService)2 TextColors (org.spongepowered.api.text.format.TextColors)2 RPLang (br.net.fabiozumbi12.RedProtect.Sponge.config.RPLang)1 WEListener (br.net.fabiozumbi12.RedProtect.Sponge.hooks.WEListener)1