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);
}
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));
}
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();
}
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;
}
}
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;
}
Aggregations