org.spongepowered.api.service.context.Context Java Examples

The following examples show how to use org.spongepowered.api.service.context.Context. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClaimContextCalculator.java    From GriefDefender with MIT License 6 votes vote down vote up
@Override
public boolean matches(Context context, Subject subject) {
    if (context.getKey().equals("gd_claim")) {
        if (subject.getCommandSource().isPresent() && subject.getCommandSource().get() instanceof Player) {
            Player player = (Player) subject.getCommandSource().get();
            GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getPlayerData(player.getWorld(), player.getUniqueId());
            if (playerData == null) {
                return false;
            }

            GDClaim playerClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
            if (playerClaim != null && playerClaim.getUniqueId().equals(UUID.fromString(context.getValue()))) {
                return true;
            }
        }
    }

    return false;
}
 
Example #2
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean matches(Context context, Subject subject)
{
    if (CONTEXT_KEY.equals(context.getKey()))
    {
        try
        {
            UUID value = UUID.fromString(context.getValue());
            VirtualChestActions actions = this.plugin.getVirtualChestActions();
            return actions.getActivatedActions(subject.getIdentifier()).contains(value);
        }
        catch (IllegalArgumentException e)
        {
            return false;
        }
    }
    return false;
}
 
Example #3
Source File: SubjectCollectionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> set, @NonNull String s) {
    return (Map) this.handle.getAllWithPermission(CompatibilityUtil.convertContexts(set), s)
            .thenApply(map -> map.entrySet().stream()
                    .collect(ImmutableCollectors.toMap(
                            e -> new SubjectProxy(this.service, e.getKey()),
                            Map.Entry::getValue
                    ))
            ).join();
}
 
Example #4
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean setOption(@NonNull Set<Context> contexts, @NonNull String key, String value) {
    if (value == null) {
        handle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key));
    } else {
        handle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value));
    }
    return true;
}
 
Example #5
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator)
{
    this.plugin.getVirtualChestActions().getActivatedActions(subject.getIdentifier()).forEach(actionUUID ->
    {
        SubjectData data = subject.getTransientSubjectData();
        Context context = new Context(CONTEXT_KEY, actionUUID.toString());
        Map<String, Boolean> permissions = data.getPermissions(Collections.singleton(context));
        this.logger.debug("Ignored {} permission(s) for action {} (context):", permissions.size(), actionUUID);
        permissions.forEach((permission, state) -> this.logger.debug("- {} ({})", permission, state));
        accumulator.add(context);
    });
}
 
Example #6
Source File: GPSpongeMigrator.java    From GriefDefender with MIT License 5 votes vote down vote up
private Set<com.griefdefender.api.permission.Context> getGDContexts(Set<Context> spongeContexts) {
    Set<com.griefdefender.api.permission.Context> gdContexts = new HashSet<>();
    for (Context context : spongeContexts) {
        gdContexts.add(new com.griefdefender.api.permission.Context(context.getKey(), context.getValue()));
    }
    return gdContexts;
}
 
Example #7
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CompletableFuture<?> clearIgnored(Player player, UUID actionUUID)
{
    SubjectData data = player.getTransientSubjectData();
    Set<Context> contexts = Collections.singleton(new Context(CONTEXT_KEY, actionUUID.toString()));

    return this.clear(data, contexts)
            .thenRun(() -> this.logIgnoredPermissionsCleared(actionUUID, player.getName()));
}
 
Example #8
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CompletableFuture<?> setIgnored(Player player, UUID actionUUID, Collection<String> permissions)
{
    SubjectData data = player.getTransientSubjectData();
    Set<Context> contexts = Collections.singleton(new Context(CONTEXT_KEY, actionUUID.toString()));

    return this.clear(data, contexts)
            .thenCompose(succeed -> CompletableFuture.allOf(this.set(data, contexts, permissions)))
            .thenRun(() -> this.logIgnoredPermissionsAdded(permissions, actionUUID, player.getName()));
}
 
Example #9
Source File: SpongeUtil.java    From GriefDefender with MIT License 5 votes vote down vote up
public static Set<com.griefdefender.api.permission.Context> fromSpongeContexts(Set<Context> contexts) {
    final Set<com.griefdefender.api.permission.Context> gpContexts = new HashSet<>();
    for (Context spongeContext : contexts) {
        gpContexts.add(new com.griefdefender.api.permission.Context(spongeContext.getKey(), spongeContext.getValue()));
    }
    return gpContexts;
}
 
Example #10
Source File: SubjectProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
    return handle().thenApply(handle -> handle.isChildOf(
            CompatibilityUtil.convertContexts(contexts),
            this.service.getReferenceFactory().obtain(parent)
    )).join();
}
 
Example #11
Source File: PEXActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public void removeParent(Player player, Subject subject, Set<Context> contexts, SubjectReference parent) {
    command(player, subjContext(subject, contexts)
            .append("parent remove ").append(parent.getCollectionIdentifier()).append(' ')
            .append(parent.getSubjectIdentifier())
            .toString());
}
 
Example #12
Source File: PEXActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Boolean> setOption(Player player, Subject subject, Set<Context> contexts, String key, String value) {
    CommandResult res = command(player, subjContext(subject, contexts)
            .append("options ").append(key).append(' ')
            .append(value == null ? "" : value).toString());
    return CompletableFuture.completedFuture(res.getSuccessCount().isPresent() && res.getSuccessCount().get() > 0);
}
 
Example #13
Source File: PEXActions.java    From ChatUI with MIT License 5 votes vote down vote up
private static StringBuilder subjContext(Subject subject, Set<Context> contexts) {
    StringBuilder b = new StringBuilder("pex ");
    for (Context context : contexts) {
        b.append("--contexts ").append(context.getKey()).append('=').append(context.getValue()).append(' ');
    }
    return b.append(subject.getContainingCollection().getIdentifier())
            .append(' ')
            .append(subject.getIdentifier())
            .append(' ');
}
 
Example #14
Source File: FallbackPermActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Boolean> setPermission(Player player, Subject subject, Set<Context> contexts, String permission, Tristate value) {
    Boolean perm = subject.getSubjectData().getPermissions(contexts).get(permission);
    if ((perm == null && value == Tristate.UNDEFINED)
            || (perm == Boolean.TRUE && value == Tristate.TRUE)
            || (perm == Boolean.FALSE && value == Tristate.FALSE)) {
        return CompletableFuture.completedFuture(true);
    }
    return subject.getSubjectData().setPermission(contexts, permission, value);
}
 
Example #15
Source File: SubjectViewer.java    From ChatUI with MIT License 5 votes vote down vote up
private UIComponent createTopBar() {
    return new UIComponent() {

        @Override
        public void draw(PlayerContext ctx, LineFactory lineFactory) {
            lineFactory.appendNewLine(Text.of(TextStyles.BOLD, TextColors.RED, SubjectViewer.this.activeSubj.getIdentifier()), ctx);
            Text.Builder builder = Text.builder("Parents: ");
            if (SubjectViewer.this.activeSubj.getParents(SubjectViewer.this.activeContext).isEmpty()) {
                builder.append(Text.of("None"));
            }
            for (SubjectReference parent : SubjectViewer.this.activeSubj.getParents(SubjectViewer.this.activeContext)) {
                builder.append(hyperlink(parent.getSubjectIdentifier(), () -> setActive(parent.resolve().join(), false)));
                builder.append(Text.builder("[x]").color(TextColors.RED)
                        .onClick(ExtraUtils.clickAction((Consumer<PlayerChatView>) view -> removeParent(view.getPlayer(), parent),
                                SubjectViewer.this.tab))
                        .build());
                builder.append(Text.of(", "));
            }
            lineFactory.appendNewLine(builder.build(), ctx);
            builder = Text.builder("Current Context: ");
            if (SubjectViewer.this.activeContext.isEmpty()) {
                builder.append(Text.of("global"));
            }
            for (Context context : SubjectViewer.this.activeContext) {
                builder.append(Text.of(context.getKey() + "[" + context.getValue() + "], "));
            }
            lineFactory.appendNewLine(builder.build(), ctx);
        }
    };
}
 
Example #16
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Set<Context>, Map<String, Boolean>> getAllPermissions() {
    return (Map) handle().thenApply(handle -> handle.getAllPermissions().entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> CompatibilityUtil.convertContexts(e.getKey()),
                    Map.Entry::getValue
            ))).join();
}
 
Example #17
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Set<Context>, List<org.spongepowered.api.service.permission.SubjectReference>> getAllParents() {
    return (Map) handle().thenApply(handle -> handle.getAllParents().entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> CompatibilityUtil.convertContexts(e.getKey()),
                    Map.Entry::getValue
            ))).join();
}
 
Example #18
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Set<Context>, Map<String, String>> getAllOptions() {
    return (Map) handle().thenApply(handle -> handle.getAllOptions().entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> CompatibilityUtil.convertContexts(e.getKey()),
                    Map.Entry::getValue
            ))).join();
}
 
Example #19
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull CompletableFuture<Boolean> setOption(@NonNull Set<Context> contexts, @NonNull String key, @Nullable String value) {
    if (value == null) {
        return handle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key));
    } else {
        return handle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value));
    }
}
 
Example #20
Source File: SubjectCollectionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull Map<Subject, Boolean> getLoadedWithPermission(@NonNull Set<Context> set, @NonNull String s) {
    return this.handle.getLoadedWithPermission(CompatibilityUtil.convertContexts(set), s).entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    sub -> sub.getKey().sponge(),
                    Map.Entry::getValue
            ));
}
 
Example #21
Source File: AbstractForwardingContextSet.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean contains(Object o) {
    if (o instanceof Context) {
        Context context = (Context) o;
        return !context.getKey().isEmpty() && !context.getValue().isEmpty() && delegate().contains(context.getKey(), context.getValue());
    }
    return false;
}
 
Example #22
Source File: ContextCalculatorProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean add(Context context) {
    if (!net.luckperms.api.context.Context.isValidKey(context.getKey()) ||
            !net.luckperms.api.context.Context.isValidValue(context.getValue())) {
        return false;
    }
    this.consumer.accept(context.getKey(), context.getValue());
    return true;
}
 
Example #23
Source File: ContextCalculatorProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean addAll(@NonNull Collection<? extends Context> c) {
    for (Context context : c) {
        add(context);
    }
    return true;
}
 
Example #24
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Set<Context>, Map<String, Boolean>> getAllPermissions() {
    return (Map) handle().thenApply(handle -> handle.getAllPermissions().entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> CompatibilityUtil.convertContexts(e.getKey()),
                    Map.Entry::getValue
            ))).join();
}
 
Example #25
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean setPermission(@NonNull Set<Context> contexts, @NonNull String permission, @NonNull Tristate value) {
    handle().thenCompose(handle -> handle.setPermission(
            CompatibilityUtil.convertContexts(contexts),
            permission,
            CompatibilityUtil.convertTristate(value)
    ));
    return true;
}
 
Example #26
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull List<Subject> getParents(@NonNull Set<Context> contexts) {
    return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream()
            .map(s -> new SubjectProxy(this.service, s))
            .collect(ImmutableCollectors.toList())).join();
}
 
Example #27
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean addParent(@NonNull Set<Context> contexts, @NonNull Subject parent) {
    handle().thenCompose(handle -> handle.addParent(
            CompatibilityUtil.convertContexts(contexts),
            this.service.getReferenceFactory().obtain(parent)
    ));
    return true;
}
 
Example #28
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean removeParent(@NonNull Set<Context> contexts, @NonNull Subject parent) {
    handle().thenCompose(handle -> handle.removeParent(
            CompatibilityUtil.convertContexts(contexts),
            this.service.getReferenceFactory().obtain(parent)
    ));
    return true;
}
 
Example #29
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Set<Context>, Map<String, String>> getAllOptions() {
    return (Map) handle().thenApply(handle -> handle.getAllOptions().entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> CompatibilityUtil.convertContexts(e.getKey()),
                    Map.Entry::getValue
            ))).join();
}
 
Example #30
Source File: ClaimContextCalculator.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public void accumulateContexts(Subject calculable, Set<Context> accumulator) {
    if (calculable.getCommandSource().isPresent() && calculable.getCommandSource().get() instanceof Player) {
        Player player = (Player) calculable.getCommandSource().get();
        GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getPlayerData(player.getWorld(), player.getUniqueId());
        if (playerData == null) {
            return;
        }
        if (playerData.ignoreActiveContexts) {
            playerData.ignoreActiveContexts = false;
            return;
        }

        GDClaim sourceClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
        if (sourceClaim != null) {
            if (playerData == null || playerData.canIgnoreClaim(sourceClaim)) {
                return;
            }

            if (sourceClaim.parent != null && sourceClaim.getData().doesInheritParent()) {
                accumulator.add(sourceClaim.parent.getSpongeContext());
            } else {
                accumulator.add(sourceClaim.getSpongeContext());
            }
            accumulator.add(new Context("server", GriefDefenderPlugin.getInstance().getPermissionProvider().getServerName()));
        }
    }

}