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