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