Search in sources :

Example 6 with CommandException

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

the class MapTest method getMapUUID.

private CommandResult getMapUUID(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack map = player.itemInHand(HandTypes.MAIN_HAND);
    if (map.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand!"));
    }
    final Component uuid = Component.text(map.require(Keys.MAP_INFO).uniqueId().toString());
    player.sendMessage(uuid);
    this.logger.info("map uuid: " + uuid);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Component(net.kyori.adventure.text.Component)

Example 7 with CommandException

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

the class MapTest method setMapNether.

private CommandResult setMapNether(final CommandContext ctx) throws CommandException {
    final Audience audience = ctx.cause().audience();
    final Player player = this.requirePlayer(ctx);
    final ItemStack map = player.itemInHand(HandTypes.MAIN_HAND);
    if (map.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    final ResourceKey netherKey = ResourceKey.minecraft("the_nether");
    final Optional<ServerWorld> nether = Sponge.server().worldManager().world(netherKey);
    if (!nether.isPresent()) {
        final CompletableFuture<ServerWorld> loadedNether = Sponge.server().worldManager().loadWorld(netherKey);
        loadedNether.whenComplete((v, e) -> {
            if (e != null) {
                audience.sendMessage(Component.text("Failed to load nether world!", NamedTextColor.GREEN));
                logger.error("Error loading nether world!", e);
            } else {
                audience.sendMessage(Component.text("Loaded nether world (dim-1)", NamedTextColor.GREEN));
            }
        });
        throw new CommandException(Component.text("No nether loaded, trying to load now, please wait"));
    }
    map.require(Keys.MAP_INFO).offer(Keys.MAP_WORLD, nether.get().key());
    return CommandResult.success();
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Player(org.spongepowered.api.entity.living.player.Player) Audience(net.kyori.adventure.audience.Audience) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ResourceKey(org.spongepowered.api.ResourceKey)

Example 8 with CommandException

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

the class MapTest method addNamedDecoration.

private CommandResult addNamedDecoration(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack heldMap = player.itemInHand(HandTypes.MAIN_HAND);
    if (heldMap.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    final MapDecoration decoration = MapDecoration.builder().type(MapDecorationTypes.BANNER_BLUE).customName(Component.text("I AM A ").color(NamedTextColor.BLUE).append(BlockTypes.BLUE_BANNER.get())).rotation(MapDecorationOrientations.NORTH).build();
    heldMap.require(Keys.MAP_INFO).offer(Keys.MAP_DECORATIONS, Sets.newHashSet(decoration));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapDecoration(org.spongepowered.api.map.decoration.MapDecoration) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 9 with CommandException

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

the class MapTest method orientDecorationsDown.

private CommandResult orientDecorationsDown(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack heldMap = player.itemInHand(HandTypes.MAIN_HAND);
    if (heldMap.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    heldMap.require(Keys.MAP_INFO).require(Keys.MAP_DECORATIONS).forEach(decoration -> decoration.setRotation(MapDecorationOrientations.SOUTH));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 10 with CommandException

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

the class MapTest method saveToFile.

private CommandResult saveToFile(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack map = player.itemInHand(HandTypes.MAIN_HAND);
    if (map.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    final Image image = map.require(Keys.MAP_INFO).require(Keys.MAP_CANVAS).toImage();
    final File file = new File("map.png");
    if (!file.isFile()) {
        try {
            if (!file.createNewFile()) {
                throw new IOException("failed to create new file :(");
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    try {
        ImageIO.write((BufferedImage) image, "png", file);
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) CommandException(org.spongepowered.api.command.exception.CommandException) IOException(java.io.IOException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) File(java.io.File)

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