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");
}
Aggregations