use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.
the class EntityVolumeTest method onRegisterCommands.
@Listener
public void onRegisterCommands(final RegisterCommandEvent<Command.Parameterized> event) {
Command.Parameterized command = Command.builder().executor((ctx) -> {
final Object root = ctx.cause().root();
if (!(root instanceof Locatable)) {
throw new CommandException(Component.text("You must be locatable to use this command!"));
}
final Audience audience = ctx.cause().audience();
final ServerLocation serverLocation = ((Locatable) root).serverLocation();
final WorldChunk chunk = serverLocation.world().chunk(serverLocation.chunkPosition());
final Collection<? extends Entity> chunkEntities = chunk.entities();
final Collection<? extends Entity> worldEntities = serverLocation.world().entities();
final boolean worldContainsChunkEntities = serverLocation.world().entities().containsAll(chunkEntities);
audience.sendMessage(testResult("World contains chunk entities test", worldContainsChunkEntities));
final boolean worldContainsMoreEntitiesThanChunk = worldEntities.size() > chunkEntities.size();
audience.sendMessage(testResult("World contains more entities than chunk test", worldContainsMoreEntitiesThanChunk).append(Component.text(" (World " + worldEntities.size() + " vs Chunk " + chunkEntities.size() + ")")));
final boolean chunkEntitiesIsSameAsAABB = chunk.entities(AABB.of(chunk.min(), chunk.max())).equals(chunkEntities);
audience.sendMessage(testResult(".entities is the same as AABB of chunk", chunkEntitiesIsSameAsAABB));
audience.sendMessage(Component.text("See console for a list of all entities."));
this.logger.info(chunkEntities.size() + " entities in chunk " + chunk.chunkPosition() + ":\n" + chunkEntities);
this.logger.info("---------");
this.logger.info(worldEntities.size() + " entities in world " + serverLocation.world().properties().key() + ":\n" + worldEntities);
return CommandResult.success();
}).build();
event.register(this.plugin, command, "checkentitymethods");
}
use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.
the class MapTest method recenterMap.
private CommandResult recenterMap(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 MapInfo mapInfo = heldMap.require(Keys.MAP_INFO);
mapInfo.offer(Keys.MAP_LOCATION, player.location().blockPosition().toVector2(true));
player.sendMessage(Component.text("New center " + mapInfo.require(Keys.MAP_LOCATION)));
return CommandResult.success();
}
use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.
the class MapTest method getMapFromUUID.
private CommandResult getMapFromUUID(final Parameter.Value<UUID> uuidParameter, final CommandContext ctx) throws CommandException {
final Player player = this.requirePlayer(ctx);
final UUID uuid = ctx.one(uuidParameter).get();
final ItemStack itemStack = ItemStack.of(ItemTypes.FILLED_MAP, 1);
final MapInfo mapInfo = Sponge.server().mapStorage().mapInfo(uuid).orElseThrow(() -> new CommandException(Component.text("UUID " + uuid + " was not a valid map uuid!")));
itemStack.offer(Keys.MAP_INFO, mapInfo);
player.inventory().offer(itemStack);
return CommandResult.success();
}
use of org.spongepowered.api.command.exception.CommandException in project SpongeCommon by SpongePowered.
the class MapTest method setColorAndLocked.
private CommandResult setColorAndLocked(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"));
}
// map.offer(Keys.MAP_LOCATION, new Vector2i(10000,10000));
final MapColor color = MapColor.of(MapColorTypes.TERRACOTTA_BLACK);
final MapInfo mapInfo = map.require(Keys.MAP_INFO);
mapInfo.offer(Keys.MAP_LOCKED, true);
mapInfo.offer(Keys.MAP_CANVAS, MapCanvas.builder().paintAll(color).build());
player.sendMessage(Component.text(mapInfo.require(Keys.MAP_CANVAS).color(0, 0).toContainer().toString()));
return CommandResult.success();
}
use of org.spongepowered.api.command.exception.CommandException 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