Search in sources :

Example 1 with MapCanvas

use of org.spongepowered.api.map.MapCanvas in project SpongeCommon by SpongePowered.

the class MapTest method testMapShades.

private CommandResult testMapShades(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final Collection<RegistryEntry<MapShade>> mapShades = Sponge.game().registry(RegistryTypes.MAP_SHADE).streamEntries().collect(Collectors.toList());
    for (final RegistryEntry<MapShade> entry : mapShades) {
        final MapColor mapColor = MapColor.of(MapColorTypes.COLOR_GREEN.get(), entry.value());
        final MapCanvas mapCanvas = MapCanvas.builder().paintAll(mapColor).build();
        final MapInfo mapInfo = Sponge.server().mapStorage().createNewMapInfo().orElseThrow(() -> new CommandException(Component.text("Unable to create new map!")));
        mapInfo.offer(Keys.MAP_LOCKED, true);
        mapInfo.offer(Keys.MAP_CANVAS, mapCanvas);
        final ItemStack itemStack = ItemStack.of(ItemTypes.FILLED_MAP);
        itemStack.offer(Keys.MAP_INFO, mapInfo);
        itemStack.offer(Keys.CUSTOM_NAME, Component.text(entry.key().formatted()));
        player.inventory().primary().offer(itemStack);
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapCanvas(org.spongepowered.api.map.MapCanvas) MapShade(org.spongepowered.api.map.color.MapShade) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) RegistryEntry(org.spongepowered.api.registry.RegistryEntry) MapColor(org.spongepowered.api.map.color.MapColor)

Example 2 with MapCanvas

use of org.spongepowered.api.map.MapCanvas in project SpongeCommon by SpongePowered.

the class MapTest method savePallete.

private CommandResult savePallete(final CommandContext ctx) {
    final File file = new File("pallete.png");
    try {
        if (!file.isFile()) {
            file.createNewFile();
        }
        final MapCanvas.Builder builder = MapCanvas.builder();
        final List<MapColor[]> mapColors = new ArrayList<>();
        for (final MapColorType mapColorType : Sponge.game().registry(RegistryTypes.MAP_COLOR_TYPE).stream().collect(Collectors.toList())) {
            final MapColor[] colors = new MapColor[] { MapColor.of(mapColorType), MapColor.builder().baseColor(mapColorType).darkest().build(), MapColor.builder().baseColor(mapColorType).base().build(), MapColor.builder().baseColor(mapColorType).darkest().build() };
            mapColors.add(colors);
        }
        int x = 0;
        for (final MapColor[] colors : mapColors) {
            int y = 0;
            builder.paint(x, y, x, y, colors[0]);
            y++;
            builder.paint(x, y, x, y, colors[1]);
            y++;
            builder.paint(x, y, x, y, colors[2]);
            y++;
            builder.paint(x, y, x, y, colors[3]);
            x++;
        }
        final MapCanvas canvas = builder.build();
        final Color color = new Color(0, 0, 0, 0);
        ImageIO.write((BufferedImage) canvas.toImage(color), "png", file);
    } catch (final IOException e) {
        Sponge.server().sendMessage(Component.text("IOException"));
    }
    return CommandResult.success();
}
Also used : MapCanvas(org.spongepowered.api.map.MapCanvas) Color(java.awt.Color) MapColor(org.spongepowered.api.map.color.MapColor) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) MapColorType(org.spongepowered.api.map.color.MapColorType) MapColor(org.spongepowered.api.map.color.MapColor)

Example 3 with MapCanvas

use of org.spongepowered.api.map.MapCanvas in project SpongeCommon by SpongePowered.

the class MapTest method loadMapFromFile.

private CommandResult loadMapFromFile(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 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();
        }
    }
    final BufferedImage image;
    try {
        image = ImageIO.read(file);
    } catch (final IOException e) {
        throw new CommandException(Component.text(e.getMessage()), e);
    }
    final MapCanvas canvas = MapCanvas.builder().fromImage(image).build();
    final MapInfo mapInfo = map.require(Keys.MAP_INFO);
    mapInfo.offer(Keys.MAP_TRACKS_PLAYERS, false);
    mapInfo.offer(Keys.MAP_DECORATIONS, Collections.emptySet());
    mapInfo.offer(Keys.MAP_LOCKED, true);
    mapInfo.offer(Keys.MAP_CANVAS, canvas);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapCanvas(org.spongepowered.api.map.MapCanvas) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) IOException(java.io.IOException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 4 with MapCanvas

use of org.spongepowered.api.map.MapCanvas in project SpongeCommon by SpongePowered.

the class MapTest method create.

private CommandResult create(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final MapInfo mapInfo = Sponge.server().mapStorage().createNewMapInfo().orElseThrow(() -> new CommandException(Component.text("Map creation was cancelled!")));
    final ItemStack itemStack = ItemStack.of(ItemTypes.FILLED_MAP, 1);
    final MapCanvas canvas = MapCanvas.builder().paintAll(MapColor.of(MapColorTypes.COLOR_RED)).build();
    mapInfo.offer(Keys.MAP_CANVAS, canvas);
    mapInfo.offer(Keys.MAP_LOCKED, true);
    mapInfo.offer(Keys.MAP_LOCATION, player.position().toInt().toVector2(true));
    itemStack.offer(Keys.MAP_INFO, mapInfo);
    player.inventory().offer(itemStack);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapCanvas(org.spongepowered.api.map.MapCanvas) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

MapCanvas (org.spongepowered.api.map.MapCanvas)4 CommandException (org.spongepowered.api.command.exception.CommandException)3 Player (org.spongepowered.api.entity.living.player.Player)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 MapInfo (org.spongepowered.api.map.MapInfo)3 File (java.io.File)2 IOException (java.io.IOException)2 MapColor (org.spongepowered.api.map.color.MapColor)2 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 MapColorType (org.spongepowered.api.map.color.MapColorType)1 MapShade (org.spongepowered.api.map.color.MapShade)1 RegistryEntry (org.spongepowered.api.registry.RegistryEntry)1