Search in sources :

Example 21 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class CommandTime method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    final Map<String, Integer> presets = new HashMap<>();
    presets.put("day", 1000);
    presets.put("night", 13000);
    specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none())).child(CommandSpec.builder().arguments(new CommandElement(Text.of("value")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            final String input = args.next().toLowerCase();
            // Try to use one of the presets first
            if (presets.containsKey(input)) {
                return presets.get(input);
            }
            try {
                return Integer.parseInt(input);
            } catch (NumberFormatException ex) {
                throw args.createError(t("Expected an integer or a valid preset, but input '%s' was not", input));
            }
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            final String prefix = args.nextIfPresent().orElse("");
            return presets.keySet().stream().filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
        }
    }).executor((src, args) -> {
        WorldProperties world = CommandHelper.getWorldProperties(src, args);
        int time = args.<Integer>getOne("value").get();
        world.setWorldTime(time);
        src.sendMessage(t("commands.time.set", time));
        return CommandResult.success();
    }).build(), "set").child(CommandSpec.builder().arguments(GenericArguments.integer(Text.of("value"))).executor((src, args) -> {
        WorldProperties world = CommandHelper.getWorldProperties(src, args);
        int time = args.<Integer>getOne("value").get();
        world.setWorldTime(world.getWorldTime() + time);
        src.sendMessage(t("commands.time.added", time));
        return CommandResult.success();
    }).build(), "add").child(CommandSpec.builder().arguments(GenericArguments2.enumValue(Text.of("value"), QueryType.class)).executor((src, args) -> {
        WorldProperties world = CommandHelper.getWorldProperties(src, args);
        QueryType queryType = args.<QueryType>getOne("value").get();
        int result;
        switch(queryType) {
            case DAYTIME:
                result = (int) (world.getWorldTime() % Integer.MAX_VALUE);
                break;
            case GAMETIME:
                result = (int) (world.getTotalTime() % Integer.MAX_VALUE);
                break;
            case DAY:
                result = (int) (world.getTotalTime() / 24000);
                break;
            default:
                throw new IllegalStateException("Unknown query type: " + queryType);
        }
        src.sendMessage(t("commands.time.query", result));
        return CommandResult.builder().successCount(1).queryResult(result).build();
    }).build(), "query");
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandResult(org.spongepowered.api.command.CommandResult) CommandHelper.getWorld(org.lanternpowered.server.command.CommandHelper.getWorld) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) HashMap(java.util.HashMap) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) List(java.util.List) 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) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) Map(java.util.Map) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommandContext(org.spongepowered.api.command.args.CommandContext) HashMap(java.util.HashMap) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandSource(org.spongepowered.api.command.CommandSource) CommandElement(org.spongepowered.api.command.args.CommandElement) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 22 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class CommandTitle method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.playerOrSource(Text.of("player"))).child(CommandSpec.builder().executor((src, args) -> {
        args.<Player>getOne("player").get().clearTitle();
        src.sendMessage(t("commands.title.success"));
        return CommandResult.success();
    }).build(), "clear").child(CommandSpec.builder().arguments(GenericArguments2.remainingString(Text.of("title"))).executor((src, args) -> {
        Text title;
        try {
            title = TextSerializers.JSON.deserialize(args.<String>getOne("title").get());
        } catch (TextParseException e) {
            throw new CommandException(t("commands.tellraw.jsonException", e.getMessage()));
        }
        args.<Player>getOne("player").get().sendTitle(Title.builder().title(title).build());
        src.sendMessage(t("commands.title.success"));
        return CommandResult.success();
    }).build(), "title").child(CommandSpec.builder().arguments(GenericArguments2.remainingString(Text.of("title"))).executor((src, args) -> {
        Text title;
        try {
            title = TextSerializers.JSON.deserialize(args.<String>getOne("title").get());
        } catch (TextParseException e) {
            throw new CommandException(t("commands.tellraw.jsonException", e.getMessage()));
        }
        args.<Player>getOne("player").get().sendTitle(Title.builder().subtitle(title).build());
        src.sendMessage(t("commands.title.success"));
        return CommandResult.success();
    }).build(), "subtitle").child(CommandSpec.builder().executor((src, args) -> {
        args.<Player>getOne("player").get().resetTitle();
        src.sendMessage(t("commands.title.success"));
        return CommandResult.success();
    }).build(), "reset").child(CommandSpec.builder().arguments(GenericArguments.integer(Text.of("fadeIn")), GenericArguments.integer(Text.of("stay")), GenericArguments.integer(Text.of("fadeOut"))).executor((src, args) -> {
        args.<Player>getOne("player").get().sendTitle(Title.builder().fadeIn(args.<Integer>getOne("fadeIn").get()).stay(args.<Integer>getOne("stay").get()).fadeOut(args.<Integer>getOne("fadeOut").get()).build());
        src.sendMessage(t("commands.title.success"));
        return CommandResult.success();
    }).build(), "times");
}
Also used : CommandException(org.spongepowered.api.command.CommandException) CommandResult(org.spongepowered.api.command.CommandResult) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) Text(org.spongepowered.api.text.Text) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) Title(org.spongepowered.api.text.title.Title) Player(org.spongepowered.api.entity.living.player.Player) TextParseException(org.spongepowered.api.text.serializer.TextParseException) GenericArguments(org.spongepowered.api.command.args.GenericArguments) PluginContainer(org.spongepowered.api.plugin.PluginContainer) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TextParseException(org.spongepowered.api.text.serializer.TextParseException)

Example 23 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class CommandVersion method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.description(t("commands.version.description")).executor((src, args) -> {
        final Platform platform = Lantern.getGame().getPlatform();
        PluginContainer plugin = platform.getContainer(Platform.Component.GAME);
        src.sendMessage(t("commands.version.minecraft", plugin.getVersion().orElse(UNKNOWN), LanternMinecraftVersion.CURRENT.getProtocol()));
        plugin = platform.getContainer(Platform.Component.IMPLEMENTATION);
        src.sendMessage(t("commands.version.implementation", plugin.getName(), plugin.getVersion().orElse(UNKNOWN)));
        plugin = platform.getContainer(Platform.Component.API);
        src.sendMessage(t("commands.version.api", plugin.getName(), plugin.getVersion().orElse(UNKNOWN)));
        return CommandResult.success();
    });
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) Platform(org.spongepowered.api.Platform)

Example 24 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class LanternEventManager method unregisterPluginListeners.

@Override
public void unregisterPluginListeners(Object pluginObj) {
    final PluginContainer plugin = checkPlugin(pluginObj, "plugin");
    unregister(handler -> plugin.equals(handler.getPlugin()));
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer)

Example 25 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class LanternEventManager method registerListeners.

@SuppressWarnings("unchecked")
@Override
public void registerListeners(Object plugin, Object listener) {
    final PluginContainer pluginContainer = checkPlugin(plugin, "plugin");
    checkNotNull(listener, "listener");
    registerListenerInstance(pluginContainer, listener);
    final List<RegisteredListener<?>> handlers = new ArrayList<>();
    final Map<Method, String> methodErrors = new HashMap<>();
    final Class<?> handle = listener.getClass();
    for (Method method : handle.getMethods()) {
        final Listener subscribe = method.getAnnotation(Listener.class);
        if (subscribe != null) {
            final String error = getHandlerErrorOrNull(method);
            if (error == null) {
                final TypeToken eventType = TypeToken.of(method.getGenericParameterTypes()[0]);
                final AnnotatedEventListener handler;
                try {
                    handler = this.listenerFactory.create(listener, method);
                } catch (Exception e) {
                    this.logger.error("Failed to create listener for {} on {}", method, handle, e);
                    continue;
                }
                handlers.add(createRegistration(pluginContainer, eventType, subscribe.order(), handler));
            } else {
                methodErrors.put(method, error);
            }
        }
    }
    // about those.
    for (Class<?> handleParent = handle; handleParent != Object.class; handleParent = handleParent.getSuperclass()) {
        for (Method method : handleParent.getDeclaredMethods()) {
            if (method.getAnnotation(Listener.class) != null && !methodErrors.containsKey(method)) {
                final String error = getHandlerErrorOrNull(method);
                if (error != null) {
                    methodErrors.put(method, error);
                }
            }
        }
    }
    for (Map.Entry<Method, String> method : methodErrors.entrySet()) {
        this.logger.warn("Invalid listener method {} in {}: {}", method.getKey(), method.getKey().getDeclaringClass().getName(), method.getValue());
    }
    register(handlers);
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) EventListener(org.spongepowered.api.event.EventListener) KeyEventListener(org.lanternpowered.server.data.key.KeyEventListener) Listener(org.spongepowered.api.event.Listener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeToken(com.google.common.reflect.TypeToken) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

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