use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.
the class HumanTest method onRegisterCommand.
@Listener
public void onRegisterCommand(final RegisterCommandEvent<Command.Parameterized> event) {
final Parameter.Value<String> nameParameter = Parameter.string().key("name").optional().build();
final Parameter.Value<String> mimicParameter = Parameter.string().key("mimic_username").optional().build();
event.register(this.plugin, Command.builder().addParameter(nameParameter).addParameter(mimicParameter).permission(this.plugin.metadata().id() + ".command.human.create").executor(context -> {
final ServerPlayer player = context.cause().first(ServerPlayer.class).get();
final Optional<String> optName = context.one(nameParameter);
final String mimicUsername = context.one(mimicParameter).orElse(optName.orElse(player.name()));
final boolean result = this.spawnHuman(player.serverLocation(), optName.orElse("Human " + player.name()), mimicUsername);
return result ? CommandResult.success() : CommandResult.error(Component.text("Failed to spawn the human!"));
}).build(), "ch", "createhuman");
}
use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.
the class ChatTest method registerCommands.
@Listener
public void registerCommands(final RegisterCommandEvent<Command.Parameterized> event) {
// /togglebossbar
event.register(this.container, Command.builder().permission("chattest.togglebossbar").executor(ctx -> {
if (this.barVisible) {
this.game.server().hideBossBar(ChatTest.INFO_BAR);
} else {
this.game.server().showBossBar(ChatTest.INFO_BAR);
}
this.barVisible = !this.barVisible;
return CommandResult.success();
}).build(), "togglebossbar");
event.register(this.container, Command.builder().permission("chattest.sendbook").executor(ctx -> {
ctx.cause().audience().openBook(Book.builder().title(Component.text("A story")).author(Component.text("You")).pages(Component.translatable("chattest.book.1"), Component.translatable("chattest.book.2")));
return CommandResult.success();
}).build(), "sendbook");
final Parameter.Value<ServerPlayer> targetArg = Parameter.player().key("target").build();
final Parameter.Value<Component> messageArg = Parameter.jsonText().key("message").build();
event.register(this.container, Command.builder().permission("chatttest.tell-resolve").addParameters(targetArg, messageArg).executor(ctx -> {
final ServerPlayer target = ctx.requireOne(targetArg);
final Component message = ctx.requireOne(messageArg);
final Component resolvedMessage = SpongeComponents.resolve(message, ctx.cause(), target, ResolveOperations.CONTEXTUAL_COMPONENTS);
target.sendMessage(ctx.cause().first(Identified.class).map(Identified::identity).orElse(Identity.nil()), resolvedMessage);
return CommandResult.success();
}).build(), "tellresolve");
}
Aggregations