Search in sources :

Example 26 with CommandException

use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.

the class ProjectileTest method registerCommand.

@Listener
public void registerCommand(final RegisterCommandEvent<Command.Parameterized> event) {
    final Parameter.Value<EntityType<@NonNull ?>> entityTypeParameter = Parameter.registryElement(new TypeToken<EntityType<?>>() {
    }, (ctx) -> Sponge.game(), RegistryTypes.ENTITY_TYPE, "minecraft", "sponge").key("type").build();
    final Parameter.Value<Boolean> targetParameter = Parameter.bool().key("target").optional().build();
    final Command.Parameterized launchCommand = Command.builder().addParameters(entityTypeParameter, targetParameter).executor(context -> {
        final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
        final EntityType<?> entityType = context.requireOne(entityTypeParameter);
        final Optional<Projectile> launched;
        if (context.one(targetParameter).orElse(false)) {
            final Collection<? extends Entity> nearbyEntities = player.nearbyEntities(10, entity -> entity instanceof Living && entity != player);
            if (nearbyEntities.isEmpty()) {
                return CommandResult.error(Component.text("No entity to target nearby"));
            }
            final Entity target = nearbyEntities.iterator().next();
            launched = player.launchProjectileTo((EntityType<Projectile>) entityType, target);
            if (launched.isPresent()) {
                player.sendMessage(Identity.nil(), Component.text("Launched projectile to " + RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), target.type()).asString()));
                return CommandResult.success();
            }
        } else {
            launched = player.launchProjectile((EntityType<Projectile>) entityType);
            if (launched.isPresent()) {
                player.sendMessage(Identity.nil(), Component.text("Launched projectile"));
                return CommandResult.success();
            }
        }
        throw new CommandException(Component.text("Could not launch projectile"));
    }).build();
    event.register(this.plugin, launchCommand, "launch");
    final Command.Parameterized launchToMeCommand = Command.builder().addParameter(entityTypeParameter).executor(context -> {
        final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
        final Collection<? extends ProjectileSource> nearbyProjectileSources = (Collection<? extends ProjectileSource>) player.nearbyEntities(10, entity -> entity instanceof ProjectileSource);
        if (nearbyProjectileSources.isEmpty()) {
            return CommandResult.error(Component.text("No projectile source nearby"));
        }
        final ProjectileSource projectileSource = nearbyProjectileSources.iterator().next();
        final EntityType<?> entityType = context.requireOne(entityTypeParameter);
        final Optional<? extends Projectile> launched = projectileSource.launchProjectileTo((EntityType<Projectile>) entityType, player);
        final EntityType<?> type = ((Entity) projectileSource).type();
        if (launched.isPresent()) {
            final EntityType<?> launchedType = launched.get().type();
            player.sendMessage(Identity.nil(), Component.text().append(Component.text("You made a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), type).asString())).append(Component.text(" shoot a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), launchedType).asString())).append(Component.text(" at you")).build());
            return CommandResult.success();
        }
        throw new CommandException(Component.text().append(Component.text("Could not launch a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), type).asString())).append(Component.text(" from a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), entityType).asString())).append(Component.text(" at you")).build());
    }).build();
    event.register(this.plugin, launchToMeCommand, "launchtome");
    final Parameter.Value<ServerLocation> dispenserParameter = Parameter.location().key("dispenser").build();
    final Command.Parameterized triggerDispenserCommand = Command.builder().addParameters(dispenserParameter, entityTypeParameter).executor(context -> {
        final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
        final BlockEntity dispenser = context.requireOne(dispenserParameter).blockEntity().orElse(null);
        if (dispenser == null) {
            return CommandResult.error(Component.text("Could not find dispenser"));
        }
        final EntityType<?> entityType = context.requireOne(entityTypeParameter);
        final Optional<? extends Projectile> launched = ((Dispenser) dispenser).launchProjectile((EntityType<Projectile>) entityType);
        if (launched.isPresent()) {
            launched.get().offer(Keys.SHOOTER, player);
            player.sendMessage(Identity.nil(), Component.text().append(Component.text("The dispenser launched a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), launched.get().type()).asString())).build());
            return CommandResult.success();
        }
        return CommandResult.error(Component.text().append(Component.text("Could not make the dispenser launch a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), entityType).asString())).build());
    }).build();
    event.register(this.plugin, triggerDispenserCommand, "triggerdispenser");
}
Also used : BlockEntity(org.spongepowered.api.block.entity.BlockEntity) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Command(org.spongepowered.api.command.Command) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Inject(com.google.inject.Inject) EntityTypes(org.spongepowered.api.entity.EntityTypes) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) LinkedList(java.util.LinkedList) Living(org.spongepowered.api.entity.living.Living) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) CommandResult(org.spongepowered.api.command.CommandResult) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) LoadableModule(org.spongepowered.test.LoadableModule) Identity(net.kyori.adventure.identity.Identity) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) TypeToken(io.leangen.geantyref.TypeToken) Entity(org.spongepowered.api.entity.Entity) ProjectileSource(org.spongepowered.api.projectile.source.ProjectileSource) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Dispenser(org.spongepowered.api.block.entity.carrier.Dispenser) Keys(org.spongepowered.api.data.Keys) Vector3d(org.spongepowered.math.vector.Vector3d) PluginContainer(org.spongepowered.plugin.PluginContainer) CommandContext(org.spongepowered.api.command.parameter.CommandContext) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Projectile(org.spongepowered.api.entity.projectile.Projectile) Queue(java.util.Queue) Listener(org.spongepowered.api.event.Listener) CommandException(org.spongepowered.api.command.exception.CommandException) First(org.spongepowered.api.event.filter.cause.First) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ServerLocation(org.spongepowered.api.world.server.ServerLocation) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Living(org.spongepowered.api.entity.living.Living) CommandException(org.spongepowered.api.command.exception.CommandException) Projectile(org.spongepowered.api.entity.projectile.Projectile) EntityType(org.spongepowered.api.entity.EntityType) Command(org.spongepowered.api.command.Command) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Parameter(org.spongepowered.api.command.parameter.Parameter) Collection(java.util.Collection) ProjectileSource(org.spongepowered.api.projectile.source.ProjectileSource) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Listener(org.spongepowered.api.event.Listener)

Aggregations

CommandException (org.spongepowered.api.command.exception.CommandException)26 Player (org.spongepowered.api.entity.living.player.Player)19 ItemStack (org.spongepowered.api.item.inventory.ItemStack)17 MapInfo (org.spongepowered.api.map.MapInfo)10 Component (net.kyori.adventure.text.Component)8 Audience (net.kyori.adventure.audience.Audience)6 CommandResult (org.spongepowered.api.command.CommandResult)5 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)5 Inject (com.google.inject.Inject)4 UUID (java.util.UUID)4 Identity (net.kyori.adventure.identity.Identity)4 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)4 Sponge (org.spongepowered.api.Sponge)4 Command (org.spongepowered.api.command.Command)4 Listener (org.spongepowered.api.event.Listener)4 RegisterCommandEvent (org.spongepowered.api.event.lifecycle.RegisterCommandEvent)4 PluginContainer (org.spongepowered.plugin.PluginContainer)4 Plugin (org.spongepowered.plugin.builtin.jvm.Plugin)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3