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