Search in sources :

Example 11 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeTriggerBuilder method build.

@Override
public Trigger<C> build() {
    checkState(this.id != null, "The id must be set");
    checkState(this.configType != null, "The configType must be set");
    final PluginContainer plugin = Sponge.getCauseStackManager().getCurrentCause().first(PluginContainer.class).get();
    final String name = StringUtils.isNotEmpty(this.name) ? this.name : this.id;
    return (Trigger<C>) new SpongeTrigger((Class) this.configType, (Function) this.constructor, new ResourceLocation(plugin.getId(), id), (Consumer) this.eventHandler, name);
}
Also used : Function(java.util.function.Function) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Trigger(org.spongepowered.api.advancement.criteria.trigger.Trigger) Consumer(java.util.function.Consumer) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 12 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeAssetManager method getAsset.

@Override
public Optional<Asset> getAsset(Object instance, String name) {
    checkNotNull(instance, "plugin instance");
    checkNotNull(name, "name");
    checkArgument(!name.isEmpty(), "name cannot be empty");
    PluginContainer plugin = Sponge.getPluginManager().fromInstance(instance).get();
    URL url = CLASS_LOADER.getResource(DEFAULT_ASSET_DIR + plugin.getId() + '/' + name);
    if (url == null) {
        return Optional.empty();
    }
    return Optional.of(new SpongeAsset(plugin, url));
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) URL(java.net.URL)

Example 13 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeCommandFactory method createSpongePluginsCommand.

private static CommandSpec createSpongePluginsCommand() {
    return CommandSpec.builder().description(Text.of("List currently installed plugins")).permission("sponge.command.plugins").arguments(optionalWeak(literal(Text.of("reload"), "reload")), optional(plugin(Text.of("plugin")))).executor((src, args) -> {
        if (args.hasAny("reload") && src.hasPermission("sponge.command.plugins.reload")) {
            src.sendMessage(Text.of("Sending reload event to all plugins. Please wait."));
            Sponge.getCauseStackManager().pushCause(src);
            SpongeImpl.postEvent(SpongeEventFactory.createGameReloadEvent(Sponge.getCauseStackManager().getCurrentCause()));
            Sponge.getCauseStackManager().popCause();
            src.sendMessage(Text.of("Reload complete!"));
        } else if (args.hasAny("plugin")) {
            sendContainerMeta(src, args, "plugin");
        } else {
            final Collection<PluginContainer> containers = SpongeImpl.getGame().getPluginManager().getPlugins();
            final List<PluginContainer> sortedContainers = new ArrayList<>();
            // Add static listings first
            CONTAINER_LIST_STATICS.forEach(containerId -> containers.stream().filter(container -> container.getId().equalsIgnoreCase(containerId)).findFirst().ifPresent(sortedContainers::add));
            containers.stream().filter(SpongeImplHooks.getPluginFilterPredicate()).sorted(Comparator.comparing(PluginContainer::getName)).forEachOrdered(sortedContainers::add);
            if (src instanceof Player) {
                final List<Text> containerList = new ArrayList<>();
                final PaginationList.Builder builder = PaginationList.builder();
                builder.title(Text.of(TextColors.YELLOW, "Plugins", TextColors.WHITE, " (", sortedContainers.size(), ")")).padding(Text.of(TextColors.DARK_GREEN, "="));
                for (PluginContainer container : sortedContainers) {
                    final Text.Builder containerBuilder = Text.builder().append(Text.of(TextColors.RESET, " - ", TextColors.GREEN, container.getName())).onClick(TextActions.runCommand("/sponge:sponge plugins " + container.getId())).onHover(TextActions.showText(Text.of(TextColors.RESET, "ID: ", container.getId(), Text.NEW_LINE, "Version: ", container.getVersion().orElse("Unknown"))));
                    containerList.add(containerBuilder.build());
                }
                builder.contents(containerList).build().sendTo(src);
            } else {
                final Text.Builder builder = Text.builder();
                builder.append(Text.of(TextColors.YELLOW, "Plugins", TextColors.WHITE, " (", sortedContainers.size(), "): "));
                boolean first = true;
                for (PluginContainer container : sortedContainers) {
                    if (!first) {
                        builder.append(SEPARATOR_TEXT);
                    }
                    first = false;
                    builder.append(Text.of(TextColors.GREEN, container.getName()));
                }
                src.sendMessage(builder.build());
            }
        }
        return CommandResult.success();
    }).build();
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) ClickAction(org.spongepowered.api.text.action.ClickAction) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) DimensionType(org.spongepowered.api.world.DimensionType) URL(java.net.URL) CommandCallable(org.spongepowered.api.command.CommandCallable) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) GenericArguments(org.spongepowered.api.command.args.GenericArguments) GenericArguments.choices(org.spongepowered.api.command.args.GenericArguments.choices) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) GenericArguments.plugin(org.spongepowered.api.command.args.GenericArguments.plugin) WorldConfig(org.spongepowered.common.config.type.WorldConfig) SpongeTimingsFactory(co.aikar.timings.SpongeTimingsFactory) TextActions(org.spongepowered.api.text.action.TextActions) User(org.spongepowered.api.entity.living.player.User) GenericArguments.dimension(org.spongepowered.api.command.args.GenericArguments.dimension) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) MixinEnvironment(org.spongepowered.asm.mixin.MixinEnvironment) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) SpongeApiTranslationHelper(org.spongepowered.api.util.SpongeApiTranslationHelper) World(org.spongepowered.api.world.World) DimensionConfig(org.spongepowered.common.config.type.DimensionConfig) CommandManager(org.spongepowered.api.command.CommandManager) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) SpongeConfig(org.spongepowered.common.config.SpongeConfig) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) SpongeImpl(org.spongepowered.common.SpongeImpl) Timings(co.aikar.timings.Timings) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) LocalDateTime(java.time.LocalDateTime) CommandMapping(org.spongepowered.api.command.CommandMapping) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Function(java.util.function.Function) TreeSet(java.util.TreeSet) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) RayTraceResult(net.minecraft.util.math.RayTraceResult) GenericArguments.seq(org.spongepowered.api.command.args.GenericArguments.seq) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Chunk(net.minecraft.world.chunk.Chunk) WorldServer(net.minecraft.world.WorldServer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) GenericArguments.literal(org.spongepowered.api.command.args.GenericArguments.literal) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) GenericArguments.world(org.spongepowered.api.command.args.GenericArguments.world) Entity(net.minecraft.entity.Entity) CommandResult(org.spongepowered.api.command.CommandResult) MalformedURLException(java.net.MalformedURLException) SpongeHooks(org.spongepowered.common.util.SpongeHooks) WorldManager(org.spongepowered.common.world.WorldManager) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) DecimalFormat(java.text.DecimalFormat) BlockUtil(org.spongepowered.common.block.BlockUtil) GeneralConfigBase(org.spongepowered.common.config.type.GeneralConfigBase) File(java.io.File) CommandException(org.spongepowered.api.command.CommandException) IBlockState(net.minecraft.block.state.IBlockState) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) DateTimeFormatter(java.time.format.DateTimeFormatter) GenericArguments.flags(org.spongepowered.api.command.args.GenericArguments.flags) GenericArguments.optionalWeak(org.spongepowered.api.command.args.GenericArguments.optionalWeak) GlobalConfig(org.spongepowered.common.config.type.GlobalConfig) Comparator(java.util.Comparator) Player(org.spongepowered.api.entity.living.player.Player) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) PaginationList(org.spongepowered.api.service.pagination.PaginationList)

Example 14 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeCommandManager method register.

@Override
public Optional<CommandMapping> register(Object plugin, CommandCallable callable, List<String> aliases, Function<List<String>, List<String>> callback) {
    checkNotNull(plugin, "plugin");
    Optional<PluginContainer> containerOptional = Sponge.getGame().getPluginManager().fromInstance(plugin);
    if (!containerOptional.isPresent()) {
        throw new IllegalArgumentException("The provided plugin object does not have an associated plugin container " + "(in other words, is 'plugin' actually your plugin object?");
    }
    PluginContainer container = containerOptional.get();
    synchronized (this.lock) {
        // <namespace>:<alias> for all commands
        List<String> aliasesWithPrefix = new ArrayList<>(aliases.size() * 3);
        for (final String originalAlias : aliases) {
            final String alias = this.fixAlias(container, originalAlias);
            if (aliasesWithPrefix.contains(alias)) {
                this.logger.debug("Plugin '{}' is attempting to register duplicate alias '{}'", container.getId(), alias);
                continue;
            }
            final Collection<CommandMapping> ownedCommands = this.owners.get(container);
            for (CommandMapping mapping : this.dispatcher.getAll(alias)) {
                if (ownedCommands.contains(mapping)) {
                    boolean isWrapper = callable instanceof MinecraftCommandWrapper;
                    if (!(isWrapper && ((MinecraftCommandWrapper) callable).suppressDuplicateAlias(alias))) {
                        throw new IllegalArgumentException("A plugin may not register multiple commands for the same alias ('" + alias + "')!");
                    }
                }
            }
            aliasesWithPrefix.add(alias);
            aliasesWithPrefix.add(container.getId() + ':' + alias);
        }
        Optional<CommandMapping> mapping = this.dispatcher.register(callable, aliasesWithPrefix, callback);
        if (mapping.isPresent()) {
            this.owners.put(container, mapping.get());
            this.reverseOwners.put(mapping.get(), container);
        }
        return mapping;
    }
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) CommandMapping(org.spongepowered.api.command.CommandMapping) ArrayList(java.util.ArrayList)

Example 15 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeManipulatorRegistry method bake.

void bake() {
    checkState(this.tempRegistry != null);
    // ValueProcessors
    this.tempRegistry.valueProcessorMap.forEach((key, value) -> {
        ImmutableList.Builder<ValueProcessor<?, ?>> valueListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.VALUE_PROCESSOR_COMPARATOR);
        valueListBuilder.addAll(value);
        final ValueProcessorDelegate<?, ?> delegate = new ValueProcessorDelegate(key, valueListBuilder.build());
        this.valueDelegates.put(key, delegate);
    });
    // DataProcessors
    this.tempRegistry.processorMap.forEach((key, value) -> {
        ImmutableList.Builder<DataProcessor<?, ?>> dataListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.DATA_PROCESSOR_COMPARATOR);
        dataListBuilder.addAll(value);
        final DataProcessorDelegate<?, ?> delegate = new DataProcessorDelegate(dataListBuilder.build());
        this.dataProcessorDelegates.put(key, delegate);
    });
    SpongeDataManager manager = SpongeDataManager.getInstance();
    // DataManipulatorBuilders part 2 (Have to register them back for serialization stuff
    this.dataProcessorDelegates.forEach((key, value) -> {
        if (!Modifier.isInterface(key.getModifiers()) && !Modifier.isAbstract(key.getModifiers())) {
            DataFunction<DataContainer, DataManipulator, Optional<? extends DataManipulator<?, ?>>> function = ((DataProcessor) value)::fill;
            SpongeDataManipulatorBuilder builder = new SpongeDataManipulatorBuilder(value, key, function);
            manager.builderMap.put(key, checkNotNull(builder));
            manager.registerBuilder(key, builder);
        } else {
            final Class<? extends DataManipulator<?, ?>> clazz = this.interfaceToImplDataManipulatorClasses.get(key);
            DataFunction<DataContainer, DataManipulator, Optional<? extends DataManipulator<?, ?>>> function = ((DataProcessor) value)::fill;
            SpongeDataManipulatorBuilder builder = new SpongeDataManipulatorBuilder(value, clazz, function);
            manager.builderMap.put(key, checkNotNull(builder));
            manager.registerBuilder(key, builder);
        }
    });
    // Immutable DataProcessors
    this.tempRegistry.immutableProcessorMap.forEach((key, value) -> {
        ImmutableList.Builder<DataProcessor<?, ?>> dataListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.DATA_PROCESSOR_COMPARATOR);
        dataListBuilder.addAll(value);
        final DataProcessorDelegate<?, ?> delegate = new DataProcessorDelegate(dataListBuilder.build());
        this.immutableDataProcessorDelegates.put(key, delegate);
    });
    // NBT processors
    ImmutableTable.Builder<Class<? extends DataManipulator<?, ?>>, NbtDataType, NbtDataProcessor<?, ?>> builder = ImmutableTable.builder();
    this.tempRegistry.nbtProcessorMap.forEach((key, value) -> {
        final HashMultimap<NbtDataType, NbtDataProcessor<?, ?>> processorMultimap = HashMultimap.create();
        for (NbtDataProcessor<?, ?> nbtDataProcessor : value) {
            processorMultimap.put(nbtDataProcessor.getTargetType(), nbtDataProcessor);
        }
        for (Map.Entry<NbtDataType, Collection<NbtDataProcessor<?, ?>>> nbtDataTypeCollectionEntry : processorMultimap.asMap().entrySet()) {
            ImmutableList.Builder<NbtDataProcessor<?, ?>> processorBuilder = ImmutableList.builder();
            processorBuilder.addAll(nbtDataTypeCollectionEntry.getValue());
            final NbtDataType dataType = nbtDataTypeCollectionEntry.getKey();
            builder.put(key, dataType, new SpongeNbtProcessorDelegate(processorBuilder.build(), dataType));
        }
    });
    this.nbtProcessorTable = builder.build();
    ImmutableSet.Builder<DataRegistration<?, ?>> registrationBuilder = ImmutableSet.builder();
    ImmutableMap.Builder<Class<? extends DataManipulator<?, ?>>, DataRegistration<?, ?>> manipulatorBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Class<? extends ImmutableDataManipulator<?, ?>>, DataRegistration<?, ?>> immutableBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<String, DataRegistration<?, ?>> idBuilder = ImmutableMap.builder();
    ImmutableMultimap.Builder<PluginContainer, DataRegistration<?, ?>> pluginBuilder = ImmutableMultimap.builder();
    this.tempRegistry.registrations.forEach(registration -> {
        registrationBuilder.add(registration);
        manipulatorBuilder.put(registration.getManipulatorClass(), registration);
        if (!registration.getImplementationClass().equals(registration.getManipulatorClass())) {
            manipulatorBuilder.put(registration.getImplementationClass(), registration);
        }
        immutableBuilder.put(registration.getImmutableManipulatorClass(), registration);
        if (!registration.getImmutableImplementationClass().equals(registration.getImmutableManipulatorClass())) {
            immutableBuilder.put(registration.getImmutableImplementationClass(), registration);
        }
        idBuilder.put(registration.getId(), registration);
        pluginBuilder.put(registration.getPluginContainer(), registration);
    });
    this.registrations = registrationBuilder.build();
    this.manipulatorRegistrationMap = manipulatorBuilder.build();
    this.immutableRegistrationMap = immutableBuilder.build();
    this.registrationMap = idBuilder.build();
    this.pluginBasedRegistrations = pluginBuilder.build();
    final SpongeConfig<CustomDataConfig> dataConfig = SpongeImpl.getDataConfig();
    dataConfig.reload();
    dataConfig.save();
    final CustomDataRegistrationCategory config = dataConfig.getConfig().getDataRegistrationConfig();
    config.populateRegistrations(this.registrations);
    // Save the list of registered id's, this way the config can be re-understood.
    dataConfig.save();
    // Finalizes the registration by setting the temporary object to null
    this.tempRegistry = null;
}
Also used : CustomDataRegistrationCategory(org.spongepowered.common.config.category.CustomDataRegistrationCategory) ImmutableList(com.google.common.collect.ImmutableList) NbtValueProcessor(org.spongepowered.common.data.nbt.value.NbtValueProcessor) CustomDataConfig(org.spongepowered.common.config.type.CustomDataConfig) NbtDataType(org.spongepowered.common.data.nbt.NbtDataType) DataProcessorDelegate(org.spongepowered.common.data.util.DataProcessorDelegate) DataContainer(org.spongepowered.api.data.DataContainer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) SpongeDataManipulatorBuilder(org.spongepowered.common.data.builder.manipulator.SpongeDataManipulatorBuilder) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) ValueProcessorDelegate(org.spongepowered.common.data.util.ValueProcessorDelegate) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NbtDataProcessor(org.spongepowered.common.data.nbt.data.NbtDataProcessor) ImmutableTable(com.google.common.collect.ImmutableTable) ImmutableMap(com.google.common.collect.ImmutableMap) DataRegistration(org.spongepowered.api.data.DataRegistration) Collection(java.util.Collection) NbtDataProcessor(org.spongepowered.common.data.nbt.data.NbtDataProcessor) SpongeNbtProcessorDelegate(org.spongepowered.common.data.nbt.SpongeNbtProcessorDelegate) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

PluginContainer (org.spongepowered.api.plugin.PluginContainer)75 Text (org.spongepowered.api.text.Text)15 CommandResult (org.spongepowered.api.command.CommandResult)13 GenericArguments (org.spongepowered.api.command.args.GenericArguments)13 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)13 List (java.util.List)12 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)11 CommandException (org.spongepowered.api.command.CommandException)11 ArrayList (java.util.ArrayList)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)9 Sponge (org.spongepowered.api.Sponge)9 Player (org.spongepowered.api.entity.living.player.Player)8 Collection (java.util.Collection)7 Map (java.util.Map)7 Nullable (javax.annotation.Nullable)7 CommandSource (org.spongepowered.api.command.CommandSource)7 GenericArguments2 (org.lanternpowered.server.command.element.GenericArguments2)6 CommandMapping (org.spongepowered.api.command.CommandMapping)6 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)6