Search in sources :

Example 1 with Subject

use of org.spongepowered.api.service.permission.Subject in project SpongeCommon by SpongePowered.

the class MixinDedicatedPlayerList method onBypassPlayerLimit.

@Inject(method = "bypassesPlayerLimit", at = @At("HEAD"), cancellable = true)
private void onBypassPlayerLimit(GameProfile profile, CallbackInfoReturnable<Boolean> ci) {
    final PermissionService permissionService = Sponge.getServiceManager().provideUnchecked(PermissionService.class);
    final Subject subject = permissionService.getUserSubjects().getSubject(profile.getId().toString()).orElse(permissionService.getDefaults());
    final Tristate tristate = subject.getPermissionValue(SubjectData.GLOBAL_CONTEXT, LoginPermissions.BYPASS_PLAYER_LIMIT_PERMISSION);
    // if we are still using the default permission service
    if (tristate == Tristate.UNDEFINED && permissionService instanceof SpongePermissionService) {
        return;
    }
    ci.setReturnValue(tristate.asBoolean());
}
Also used : SpongePermissionService(org.spongepowered.common.service.permission.SpongePermissionService) PermissionService(org.spongepowered.api.service.permission.PermissionService) Tristate(org.spongepowered.api.util.Tristate) SpongePermissionService(org.spongepowered.common.service.permission.SpongePermissionService) Subject(org.spongepowered.api.service.permission.Subject) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with Subject

use of org.spongepowered.api.service.permission.Subject in project SpongeCommon by SpongePowered.

the class MixinCommandSenderWrapper method onInit.

@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(ICommandSender delegateIn, Vec3d positionVectorIn, BlockPos positionIn, Integer permissionLevelIn, Entity entityIn, Boolean sendCommandFeedbackIn, CallbackInfo ci) {
    CommandSource wrappedDelegate = WrapperCommandSource.of(this.delegate);
    Subject subjectDelegate;
    if (this.permissionLevel == null) {
        subjectDelegate = wrappedDelegate;
    } else {
        subjectDelegate = new AndPermissionLevelSubject(this, wrappedDelegate);
    }
    if (this.positionVector != null || wrappedDelegate instanceof Locatable) {
        this.sponge = new SpongeProxySource.Located(this, wrappedDelegate, subjectDelegate);
    } else {
        this.sponge = new SpongeProxySource(this, wrappedDelegate, subjectDelegate);
    }
}
Also used : SpongeProxySource(org.spongepowered.common.command.SpongeProxySource) WrapperCommandSource(org.spongepowered.common.command.WrapperCommandSource) CommandSource(org.spongepowered.api.command.CommandSource) AndPermissionLevelSubject(org.spongepowered.common.command.AndPermissionLevelSubject) AndPermissionLevelSubject(org.spongepowered.common.command.AndPermissionLevelSubject) Subject(org.spongepowered.api.service.permission.Subject) Locatable(org.spongepowered.api.world.Locatable) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with Subject

use of org.spongepowered.api.service.permission.Subject in project modules-extra by CubeEngine.

the class ChatFormatListener method onPlayerChat.

@Listener(order = Order.EARLY)
public void onPlayerChat(MessageChannelEvent.Chat event, @Root Player player) {
    // TODO format on the messagechannel instead
    String msg = event.getRawMessage().toPlain();
    if (!msg.equals("+") && msg.endsWith("+") && player.hasPermission(module.perms().LONGER.getId())) {
        msg = accumulated.getOrDefault(player.getUniqueId(), "") + msg.substring(0, msg.length() - 1);
        msg = msg.substring(0, Math.min(msg.length(), 50 * 20));
        module.getI18n().send(ChatTypes.ACTION_BAR, player, NEUTRAL, "{amount} characters in buffer.", msg.length());
        accumulated.put(player.getUniqueId(), msg);
        event.setCancelled(true);
        return;
    }
    msg = accumulated.getOrDefault(player.getUniqueId(), "") + msg;
    accumulated.remove(player.getUniqueId());
    msg = msg.substring(0, Math.min(msg.length(), 50 * 20));
    if (module.getConfig().allowColors) {
        if (!player.hasPermission(module.perms().COLOR.getId())) {
            msg = chatColors.matcher(msg).replaceAll("");
        }
    }
    if (player.hasPermission(module.perms().NEWLINE.getId())) {
        msg = msg.replace("\\n", "\n");
    }
    try {
        Subject subject = module.getPermissionService().getUserSubjects().loadSubject(player.getUniqueId().toString()).get();
        Map<String, Text> replacements = new HashMap<>();
        String name = player.getName();
        replacements.put("{NAME}", Text.of(name));
        Text displayName = player.get(DisplayNameData.class).isPresent() ? player.getDisplayNameData().displayName().get() : Text.of(name);
        if (!displayName.toPlain().equals(name)) {
            displayName = Text.builder().append(displayName).onHover(TextActions.showText(Text.of(DARK_GREEN, name))).build();
        }
        replacements.put("{DISPLAY_NAME}", displayName);
        replacements.put("{WORLD}", Text.of(player.getWorld().getName()));
        replacements.put("{MESSAGE}", fromLegacy(msg, '&'));
        replacements.put("{PREFIX}", Text.of());
        replacements.put("{SUFFIX}", Text.of());
        replacements.put("{PREFIX}", fromLegacy(subject.getOption("chat-prefix").orElse(""), '&'));
        replacements.put("{SUFFIX}", fromLegacy(subject.getOption("chat-suffix").orElse(""), '&'));
        event.setMessage(fromLegacy(this.getFormat(subject), replacements, '&'));
    } catch (ExecutionException | InterruptedException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HashMap(java.util.HashMap) Text(org.spongepowered.api.text.Text) ExecutionException(java.util.concurrent.ExecutionException) Subject(org.spongepowered.api.service.permission.Subject) Listener(org.spongepowered.api.event.Listener)

Example 4 with Subject

use of org.spongepowered.api.service.permission.Subject in project UltimateChat by FabioZumbi12.

the class UCMessages method formatTags.

public static String formatTags(String tag, String text, Object cmdSender, Object receiver, String msg, UCChannel ch) {
    if (receiver instanceof CommandSource && tag.equals("message")) {
        text = text.replace("{message}", mention(cmdSender, receiver, msg));
        if (UChat.get().getConfig().root().general.item_hand.enable) {
            text = text.replace(UChat.get().getConfig().root().general.item_hand.placeholder, formatTags("", UCUtil.toColor(UChat.get().getConfig().root().general.item_hand.format), cmdSender, receiver, msg, ch));
        }
    } else {
        text = text.replace("{message}", msg);
    }
    if (tag.equals("message") && !UChat.get().getConfig().root().general.enable_tags_on_messages) {
        return text;
    }
    text = text.replace("{ch-color}", ch.getColor()).replace("{ch-name}", ch.getName()).replace("{ch-alias}", ch.getAlias());
    if (UChat.get().getConfig().root().jedis.enable && ch.useJedis()) {
        text = text.replace("{jedis-id}", UChat.get().getConfig().root().jedis.server_id);
    }
    if (cmdSender instanceof CommandSource) {
        text = text.replace("{playername}", ((CommandSource) cmdSender).getName());
        if (receiver instanceof CommandSource) {
            text = text.replace("{receivername}", ((CommandSource) receiver).getName());
        }
        if (receiver instanceof String) {
            text = text.replace("{receivername}", receiver.toString());
        }
    } else {
        text = text.replace("{playername}", (String) cmdSender);
        if (receiver instanceof CommandSource)
            text = text.replace("{receivername}", ((CommandSource) receiver).getName());
        if (receiver instanceof String)
            text = text.replace("{receivername}", (String) receiver);
    }
    for (String repl : registeredReplacers.keySet()) {
        if (registeredReplacers.get(repl).equals(repl)) {
            text = text.replace(repl, "");
            continue;
        }
        text = text.replace(repl, registeredReplacers.get(repl));
    }
    if (defFormat.length > 0) {
        StringBuilder all = new StringBuilder();
        for (int i = 0; i < defFormat.length; i++) {
            if (i == 0)
                text = text.replace("{chat_header}", defFormat[i]);
            if (i == 1)
                text = text.replace("{chat_body}", defFormat[i]);
            if (i == 2)
                text = text.replace("{chat_footer}", defFormat[i]);
            all.append(defFormat[i]);
        }
        text = text.replace("{chat_all}", all.toString());
    }
    if (text.contains("{time-now}")) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        text = text.replace("{time-now}", sdf.format(cal.getTime()));
    }
    if (cmdSender instanceof Player) {
        Player sender = (Player) cmdSender;
        if (text.contains("{nickname}") && sender.get(Keys.DISPLAY_NAME).isPresent()) {
            String nick = sender.get(Keys.DISPLAY_NAME).get().toPlain();
            if (!nick.equals(sender.getName())) {
                text = text.replace("{nickname}", nick);
                text = text.replace("{nick-symbol}", UChat.get().getConfig().root().general.nick_symbol);
            } else if (Sponge.getPluginManager().getPlugin("nucleus").isPresent() && NucleusAPI.getNicknameService().isPresent()) {
                Optional<Text> opNick = NucleusAPI.getNicknameService().get().getNickname(sender);
                if (opNick.isPresent()) {
                    nick = TextSerializers.FORMATTING_CODE.serialize(opNick.get());
                    text = text.replace("{nick-symbol}", UChat.get().getConfig().root().general.nick_symbol);
                }
            }
            text = text.replace("{nickname}", nick);
        }
        // replace item hand
        text = text.replace(UChat.get().getConfig().root().general.item_hand.placeholder, UCUtil.toColor(UChat.get().getConfig().root().general.item_hand.format));
        ItemStack item = null;
        if (sender.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
            item = sender.getItemInHand(HandTypes.MAIN_HAND).get();
        } else if (sender.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
            item = sender.getItemInHand(HandTypes.OFF_HAND).get();
        }
        if (text.contains("{hand-") && item != null) {
            text = text.replace("{hand-durability}", item.get(Keys.ITEM_DURABILITY).isPresent() ? String.valueOf(item.get(Keys.ITEM_DURABILITY).get()) : "").replace("{hand-name}", item.getItem().getTranslation().get());
            if (item.get(Keys.ITEM_LORE).isPresent()) {
                StringBuilder lorestr = new StringBuilder();
                for (Text line : item.get(Keys.ITEM_LORE).get()) {
                    lorestr.append("\n ").append(line.toPlain());
                }
                if (lorestr.length() >= 2) {
                    text = text.replace("{hand-lore}", lorestr.toString().substring(0, lorestr.length() - 1));
                }
            }
            if (item.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
                StringBuilder str = new StringBuilder();
                str.append(UChat.get().getVHelper().getEnchantments(str, item));
                if (str.length() >= 2) {
                    text = text.replace("{hand-enchants}", str.toString().substring(0, str.length() - 1));
                }
            }
            text = text.replace("{hand-amount}", String.valueOf(item.getQuantity()));
            text = text.replace("{hand-name}", item.getItem().getName());
            text = text.replace("{hand-type}", item.getItem().getTranslation().get());
        } else {
            text = text.replace("{hand-name}", UChat.get().getLang().get("chat.emptyslot"));
            text = text.replace("{hand-type}", "Air");
        }
        if (text.contains("{world}")) {
            String world = sender.getWorld().getName();
            text = text.replace("{world}", UChat.get().getConfig().root().general.world_names.getOrDefault(world, world));
        }
        if (text.contains("{balance}") && UChat.get().getEco() != null) {
            UniqueAccount acc = UChat.get().getEco().getOrCreateAccount(sender.getUniqueId()).get();
            text = text.replace("{balance}", "" + acc.getBalance(UChat.get().getEco().getDefaultCurrency()).intValue());
        }
        // parse permissions options
        try {
            // player options
            Pattern pp = Pattern.compile("\\{player_option_(.+?)\\}");
            Matcher pm = pp.matcher(text);
            while (pm.find()) {
                if (sender.getOption(pm.group(1)).isPresent() && !text.contains(sender.getOption(pm.group(1)).get())) {
                    text = text.replace("{player_option_" + pm.group(1) + "}", sender.getOption(pm.group(1)).get());
                    pm = pp.matcher(text);
                }
            }
            // group options
            Subject sub = UChat.get().getPerms().getGroupAndTag(sender);
            if (sub != null) {
                text = text.replace("{option_group}", UChat.get().getConfig().root().general.group_names.getOrDefault(sub.getIdentifier(), sub.getIdentifier()));
                if (sub.getOption("display_name").isPresent()) {
                    text = text.replace("{option_display_name}", sub.getOption("display_name").get());
                } else {
                    text = text.replace("{option_display_name}", UChat.get().getConfig().root().general.group_names.getOrDefault(sub.getIdentifier(), sub.getIdentifier()));
                }
                Pattern gp = Pattern.compile("\\{option_(.+?)\\}");
                Matcher gm = gp.matcher(text);
                while (gm.find()) {
                    if (sub.getOption(gm.group(1)).isPresent() && !text.contains(sub.getOption(gm.group(1)).get())) {
                        text = text.replace("{option_" + gm.group(1) + "}", sub.getOption(gm.group(1)).get());
                        gm = gp.matcher(text);
                    }
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        if (text.contains("{clan_") && UChat.get().getConfig().root().hooks.MCClans.enable) {
            Optional<ClanService> clanServiceOpt = Sponge.getServiceManager().provide(ClanService.class);
            if (clanServiceOpt.isPresent()) {
                ClanService clan = clanServiceOpt.get();
                ClanPlayer cp = clan.getClanPlayer(sender.getUniqueId());
                if (cp != null && cp.isMemberOfAClan()) {
                    text = text.replace("{clan_name}", checkEmpty(cp.getClan().getName())).replace("{clan_tag}", cp.getClan().getTag()).replace("{clan_tag_color}", TextSerializers.FORMATTING_CODE.serialize(cp.getClan().getTagColored())).replace("{clan_kdr}", "" + cp.getClan().getKDR()).replace("{clan_player_rank}", checkEmpty(cp.getRank().getName())).replace("{clan_player_kdr}", "" + cp.getKillDeath().getKDR()).replace("{clan_player_ffprotected}", String.valueOf(cp.isFfProtected())).replace("{clan_player_isowner}", String.valueOf(cp.getClan().getOwner().equals(cp)));
                }
            }
        }
        if (Sponge.getPluginManager().getPlugin("placeholderapi").isPresent()) {
            Optional<PlaceholderService> phapiOpt = Sponge.getServiceManager().provide(PlaceholderService.class);
            Pattern p = Pattern.compile("\\%([^\\%]*)\\%");
            if (phapiOpt.isPresent() && p.matcher(text).find()) {
                PlaceholderService phapi = phapiOpt.get();
                // fix last color from string
                String lastcolor = text.length() > 1 ? text.substring(text.length() - 2, text.length()) : "";
                if (!Pattern.compile("(&([A-Fa-fK-Ok-oRr0-9]))").matcher(lastcolor).find())
                    lastcolor = "";
                text = TextSerializers.FORMATTING_CODE.serialize(phapi.replacePlaceholders(text, sender, receiver, p)) + lastcolor;
            }
        }
    }
    text = text.replace("{option_suffix}", "&r: ");
    if (cmdSender instanceof CommandSource) {
        text = text.replace("{nickname}", UChat.get().getConfig().root().general.console_tag.replace("{console}", ((CommandSource) cmdSender).getName()));
    } else {
        text = text.replace("{nickname}", UChat.get().getConfig().root().general.console_tag.replace("{console}", (String) cmdSender));
    }
    // remove blank items
    text = text.replaceAll("\\{.*\\}", "");
    if (!tag.equals("message")) {
        for (String rpl : UChat.get().getConfig().root().general.remove_from_chat) {
            text = text.replace(UCUtil.toColor(rpl), "");
        }
    }
    if (text.equals(" ") || text.equals("  ")) {
        return text = "";
    }
    return text;
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) PlaceholderService(me.rojo8399.placeholderapi.PlaceholderService) Subject(org.spongepowered.api.service.permission.Subject) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ExecutionException(java.util.concurrent.ExecutionException) SimpleDateFormat(java.text.SimpleDateFormat) ClanService(nl.riebie.mcclans.api.ClanService)

Example 5 with Subject

use of org.spongepowered.api.service.permission.Subject in project modules-extra by CubeEngine.

the class SpawnListener method onJoin.

public void onJoin(ClientConnectionEvent.Login event) {
    if (event.getTargetUser().get(Keys.LAST_DATE_PLAYED).isPresent()) {
        return;
    }
    Subject subject = pm.getUserSubjects().loadSubject(event.getTargetUser().getIdentifier()).join();
    subject.getOption(SpawnCommands.ROLESPAWN).ifPresent(s -> event.setToTransform(getSpawnLocation(s)));
}
Also used : Subject(org.spongepowered.api.service.permission.Subject)

Aggregations

Subject (org.spongepowered.api.service.permission.Subject)32 SubjectReference (org.spongepowered.api.service.permission.SubjectReference)12 List (java.util.List)10 Test (org.junit.Test)10 Player (org.spongepowered.api.entity.living.player.Player)9 PermissionService (org.spongepowered.api.service.permission.PermissionService)9 Optional (java.util.Optional)6 Context (org.spongepowered.api.service.context.Context)6 Inject (org.spongepowered.asm.mixin.injection.Inject)6 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Util (io.github.nucleuspowered.nucleus.Util)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CommandSource (org.spongepowered.api.command.CommandSource)4 Text (org.spongepowered.api.text.Text)4 Tristate (org.spongepowered.api.util.Tristate)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 ListPlayerCommand (io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand)3