Search in sources :

Example 1 with ProjectileSource

use of org.spongepowered.api.projectile.source.ProjectileSource 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

Inject (com.google.inject.Inject)1 TypeToken (io.leangen.geantyref.TypeToken)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Identity (net.kyori.adventure.identity.Identity)1 Component (net.kyori.adventure.text.Component)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Sponge (org.spongepowered.api.Sponge)1 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)1 Dispenser (org.spongepowered.api.block.entity.carrier.Dispenser)1 Command (org.spongepowered.api.command.Command)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandException (org.spongepowered.api.command.exception.CommandException)1 CommandContext (org.spongepowered.api.command.parameter.CommandContext)1 Parameter (org.spongepowered.api.command.parameter.Parameter)1 Keys (org.spongepowered.api.data.Keys)1 Entity (org.spongepowered.api.entity.Entity)1 EntityType (org.spongepowered.api.entity.EntityType)1