Search in sources :

Example 1 with Context

use of org.spongepowered.api.service.context.Context in project LanternServer by LanternPowered.

the class LanternContextCalculator method buildAddressCache.

private LoadingCache<RemoteSource, Set<Context>> buildAddressCache(final String contextKey, final Function<RemoteSource, InetAddress> function) {
    return Caffeine.newBuilder().weakKeys().build(key -> {
        final ImmutableSet.Builder<Context> builder = ImmutableSet.builder();
        final InetAddress address = checkNotNull(function.apply(key), "address");
        builder.add(new Context(contextKey, address.getHostAddress()));
        for (String set : Maps.filterValues(Lantern.getGame().getGlobalConfig().getIpSets(), input -> input.test(address)).keySet()) {
            builder.add(new Context(contextKey, set));
        }
        return builder.build();
    });
}
Also used : Context(org.spongepowered.api.service.context.Context) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) ImmutableSet(com.google.common.collect.ImmutableSet) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Maps(com.google.common.collect.Maps) Function(java.util.function.Function) RemoteSource(org.spongepowered.api.command.source.RemoteSource) InetAddress(java.net.InetAddress) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Locatable(org.spongepowered.api.world.Locatable) Lantern(org.lanternpowered.server.game.Lantern) World(org.spongepowered.api.world.World) Optional(java.util.Optional) Subject(org.spongepowered.api.service.permission.Subject) ImmutableSet(com.google.common.collect.ImmutableSet) InetAddress(java.net.InetAddress)

Example 2 with Context

use of org.spongepowered.api.service.context.Context in project LanternServer by LanternPowered.

the class LanternContextCalculator method accumulateContexts.

@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator) {
    final Optional<CommandSource> subjSource = subject.getCommandSource();
    if (subjSource.isPresent()) {
        final CommandSource source = subjSource.get();
        if (source instanceof Locatable) {
            final World currentExt = ((Locatable) source).getWorld();
            accumulator.add(currentExt.getContext());
            accumulator.add((currentExt.getDimension().getContext()));
        }
        if (source instanceof RemoteSource) {
            final RemoteSource rem = (RemoteSource) source;
            accumulator.addAll(this.remoteIpCache.get(rem));
            accumulator.addAll(this.localIpCache.get(rem));
            accumulator.add(new Context(Context.LOCAL_PORT_KEY, String.valueOf(rem.getConnection().getVirtualHost().getPort())));
            accumulator.add(new Context(Context.LOCAL_HOST_KEY, rem.getConnection().getVirtualHost().getHostName()));
        }
    }
}
Also used : Context(org.spongepowered.api.service.context.Context) RemoteSource(org.spongepowered.api.command.source.RemoteSource) CommandSource(org.spongepowered.api.command.CommandSource) World(org.spongepowered.api.world.World) Locatable(org.spongepowered.api.world.Locatable)

Example 3 with Context

use of org.spongepowered.api.service.context.Context in project Nucleus by NucleusPowered.

the class EnforceGamemodeListener method enforce.

private void enforce(Player player, World world) {
    if (world.getProperties().getGameMode() == GameModes.NOT_SET) {
        return;
    }
    Set<Context> contextSet = Sets.newHashSet(player.getActiveContexts());
    contextSet.removeIf(x -> x.getKey().equals(Context.WORLD_KEY));
    contextSet.add(new Context(Context.WORLD_KEY, world.getName()));
    if (!player.hasPermission(contextSet, this.perm)) {
        // set their gamemode accordingly.
        player.offer(Keys.GAME_MODE, world.getProperties().getGameMode());
    }
}
Also used : Context(org.spongepowered.api.service.context.Context)

Example 4 with Context

use of org.spongepowered.api.service.context.Context in project Nucleus by NucleusPowered.

the class SetupPermissionsCommand method setupPerms.

private void setupPerms(CommandSource src, Subject group, SuggestedLevel level, boolean reset, boolean inherit) throws Exception {
    if (inherit && level.getLowerLevel() != null) {
        setupPerms(src, group, level.getLowerLevel(), reset, inherit);
    }
    Set<Context> globalContext = Sets.newHashSet();
    SubjectData data = group.getSubjectData();
    Set<String> definedPermissions = data.getPermissions(ImmutableSet.of()).keySet();
    Logger logger = Nucleus.getNucleus().getLogger();
    MessageProvider messageProvider = Nucleus.getNucleus().getMessageProvider();
    // Register all the permissions, but only those that have yet to be assigned.
    permissionRegistry.getPermissions().entrySet().stream().filter(x -> x.getValue().level == level).filter(x -> reset || !definedPermissions.contains(x.getKey())).forEach(x -> {
        logger.info(messageProvider.getMessageWithFormat("command.nucleus.permission.added", x.getKey(), group.getIdentifier()));
        data.setPermission(globalContext, x.getKey(), Tristate.TRUE);
    });
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.permission.complete", level.toString().toLowerCase(), group.getIdentifier()));
}
Also used : CommandContext(org.spongepowered.api.command.args.CommandContext) Context(org.spongepowered.api.service.context.Context) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) GenericArguments(org.spongepowered.api.command.args.GenericArguments) PermissionService(org.spongepowered.api.service.permission.PermissionService) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) Subject(org.spongepowered.api.service.permission.Subject) Nullable(javax.annotation.Nullable) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) SubjectData(org.spongepowered.api.service.permission.SubjectData) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Tristate(org.spongepowered.api.util.Tristate) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Optional(java.util.Optional) Collections(java.util.Collections) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) SubjectData(org.spongepowered.api.service.permission.SubjectData) Logger(org.slf4j.Logger)

Example 5 with Context

use of org.spongepowered.api.service.context.Context in project SpongeCommon by SpongePowered.

the class MixinDimensionType method onConstruct.

@Inject(method = "<init>", at = @At("RETURN"))
public void onConstruct(String enumName, int ordinal, int idIn, String nameIn, String suffixIn, Class<? extends WorldProvider> clazzIn, CallbackInfo ci) {
    String dimName = enumName.toLowerCase().replace(" ", "_").replaceAll("[^A-Za-z0-9_]", "");
    this.enumName = dimName;
    this.modId = SpongeImplHooks.getModIdFromClass(clazzIn);
    this.configPath = SpongeImpl.getSpongeConfigDir().resolve("worlds").resolve(this.modId).resolve(this.enumName);
    this.config = new SpongeConfig<>(SpongeConfig.Type.DIMENSION, this.configPath.resolve("dimension.conf"), SpongeImpl.ECOSYSTEM_ID);
    this.generateSpawnOnLoad = idIn == 0;
    this.loadSpawn = this.generateSpawnOnLoad;
    this.config.getConfig().getWorld().setGenerateSpawnOnLoad(this.generateSpawnOnLoad);
    this.sanitizedId = this.modId + ":" + dimName;
    String contextId = this.sanitizedId.replace(":", ".");
    this.context = new Context(Context.DIMENSION_KEY, contextId);
    if (!WorldManager.isDimensionRegistered(idIn)) {
        DimensionTypeRegistryModule.getInstance().registerAdditionalCatalog((org.spongepowered.api.world.DimensionType) this);
    }
}
Also used : Context(org.spongepowered.api.service.context.Context) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Context (org.spongepowered.api.service.context.Context)13 World (org.spongepowered.api.world.World)7 Optional (java.util.Optional)6 Set (java.util.Set)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 UUID (java.util.UUID)5 Sponge (org.spongepowered.api.Sponge)5 User (org.spongepowered.api.entity.living.player.User)5 Vector3d (com.flowpowered.math.vector.Vector3d)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 Sets (com.google.common.collect.Sets)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 Nullable (javax.annotation.Nullable)4 Player (org.spongepowered.api.entity.living.player.Player)4 Cause (org.spongepowered.api.event.cause.Cause)4