Search in sources :

Example 31 with ServerPlayer

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");
}
Also used : ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Parameter(org.spongepowered.api.command.parameter.Parameter) Listener(org.spongepowered.api.event.Listener)

Example 32 with ServerPlayer

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");
}
Also used : Identified(net.kyori.adventure.identity.Identified) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) Listener(org.spongepowered.api.event.Listener)

Aggregations

ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)32 Component (net.kyori.adventure.text.Component)13 Cause (org.spongepowered.api.event.Cause)9 Listener (org.spongepowered.api.event.Listener)8 Sponge (org.spongepowered.api.Sponge)7 Inject (org.spongepowered.asm.mixin.injection.Inject)7 TypeToken (io.leangen.geantyref.TypeToken)6 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)6 Inject (com.google.inject.Inject)5 UUID (java.util.UUID)5 Identity (net.kyori.adventure.identity.Identity)5 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)5 BlockPos (net.minecraft.core.BlockPos)5 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)5 CommandResult (org.spongepowered.api.command.CommandResult)5 CommandException (org.spongepowered.api.command.exception.CommandException)5 CommandContext (org.spongepowered.api.command.parameter.CommandContext)5 Map (java.util.Map)4 Command (org.spongepowered.api.command.Command)4 Parameter (org.spongepowered.api.command.parameter.Parameter)4