Search in sources :

Example 1 with Resource

use of org.spongepowered.api.resource.Resource in project SpongeCommon by SpongePowered.

the class PackTest method onRegisterCommand.

@Listener
public void onRegisterCommand(final RegisterCommandEvent<Command.Parameterized> event) {
    final Parameter.Value<PluginContainer> pluginContainerParameter = Parameter.plugin().key("plugin").build();
    final Parameter.Value<String> nameParameter = Parameter.string().key("name").build();
    final Parameter.Value<String> pathParameter = Parameter.string().key("path").build();
    event.register(this.plugin, Command.builder().addParameter(Parameter.firstOf(pluginContainerParameter, nameParameter)).addParameter(pathParameter).executor(exec -> {
        final Pack pack;
        final String path = exec.requireOne(pathParameter);
        if (exec.hasAny(pluginContainerParameter)) {
            pack = Sponge.server().packRepository().pack(exec.requireOne(pluginContainerParameter));
        } else {
            final String packName = exec.requireOne(nameParameter);
            pack = Sponge.server().packRepository().pack(packName).orElse(null);
            if (pack == null) {
                return CommandResult.error(Component.text("Pack " + packName + " does not exist."));
            }
        }
        try (final Resource resource = pack.contents().requireResource(PackType.server(), ResourcePath.of(pack.id(), path))) {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(resource.inputStream()));
            reader.lines().map(x -> Component.text(x.substring(0, Math.min(100, x.length())))).forEach(x -> exec.sendMessage(Identity.nil(), x));
        } catch (final Exception e) {
            e.printStackTrace();
            return CommandResult.error(Component.text("Could not locate: " + e.getMessage()));
        }
        return CommandResult.success();
    }).build(), "packtest");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) PackType(org.spongepowered.api.resource.pack.PackType) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Pack(org.spongepowered.api.resource.pack.Pack) Command(org.spongepowered.api.command.Command) Identity(net.kyori.adventure.identity.Identity) Inject(com.google.inject.Inject) Sponge(org.spongepowered.api.Sponge) InputStreamReader(java.io.InputStreamReader) ResourcePath(org.spongepowered.api.resource.ResourcePath) Logger(org.apache.logging.log4j.Logger) PluginContainer(org.spongepowered.plugin.PluginContainer) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) Engine(org.spongepowered.api.Engine) Resource(org.spongepowered.api.resource.Resource) StartedEngineEvent(org.spongepowered.api.event.lifecycle.StartedEngineEvent) BufferedReader(java.io.BufferedReader) Listener(org.spongepowered.api.event.Listener) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) PluginContainer(org.spongepowered.plugin.PluginContainer) InputStreamReader(java.io.InputStreamReader) Resource(org.spongepowered.api.resource.Resource) BufferedReader(java.io.BufferedReader) Parameter(org.spongepowered.api.command.parameter.Parameter) Pack(org.spongepowered.api.resource.pack.Pack) Listener(org.spongepowered.api.event.Listener)

Aggregations

Inject (com.google.inject.Inject)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Identity (net.kyori.adventure.identity.Identity)1 Component (net.kyori.adventure.text.Component)1 Logger (org.apache.logging.log4j.Logger)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Engine (org.spongepowered.api.Engine)1 Sponge (org.spongepowered.api.Sponge)1 Command (org.spongepowered.api.command.Command)1 CommandResult (org.spongepowered.api.command.CommandResult)1 Parameter (org.spongepowered.api.command.parameter.Parameter)1 Listener (org.spongepowered.api.event.Listener)1 RegisterCommandEvent (org.spongepowered.api.event.lifecycle.RegisterCommandEvent)1 StartedEngineEvent (org.spongepowered.api.event.lifecycle.StartedEngineEvent)1 Resource (org.spongepowered.api.resource.Resource)1 ResourcePath (org.spongepowered.api.resource.ResourcePath)1 Pack (org.spongepowered.api.resource.pack.Pack)1 PackType (org.spongepowered.api.resource.pack.PackType)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1