Search in sources :

Example 6 with TypeToken

use of io.leangen.geantyref.TypeToken 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

TypeToken (io.leangen.geantyref.TypeToken)6 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)4 Optional (java.util.Optional)3 Sponge (org.spongepowered.api.Sponge)3 Cause (org.spongepowered.api.event.Cause)3 Inject (com.google.inject.Inject)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Identity (net.kyori.adventure.identity.Identity)2 Component (net.kyori.adventure.text.Component)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 ResourceKey (org.spongepowered.api.ResourceKey)2 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)2 FilteredTriggerConfiguration (org.spongepowered.api.advancement.criteria.trigger.FilteredTriggerConfiguration)2 Command (org.spongepowered.api.command.Command)2 GenericTypeReflector (io.leangen.geantyref.GenericTypeReflector)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1