Search in sources :

Example 21 with CommandException

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");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) Command(org.spongepowered.api.command.Command) Inject(com.google.inject.Inject) Collection(java.util.Collection) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) AABB(org.spongepowered.api.util.AABB) Entity(org.spongepowered.api.entity.Entity) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) Logger(org.apache.logging.log4j.Logger) Locatable(org.spongepowered.api.world.Locatable) PluginContainer(org.spongepowered.plugin.PluginContainer) Audience(net.kyori.adventure.audience.Audience) Component(net.kyori.adventure.text.Component) Listener(org.spongepowered.api.event.Listener) CommandException(org.spongepowered.api.command.exception.CommandException) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Entity(org.spongepowered.api.entity.Entity) Command(org.spongepowered.api.command.Command) Audience(net.kyori.adventure.audience.Audience) ServerLocation(org.spongepowered.api.world.server.ServerLocation) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Collection(java.util.Collection) CommandException(org.spongepowered.api.command.exception.CommandException) Locatable(org.spongepowered.api.world.Locatable) Listener(org.spongepowered.api.event.Listener)

Example 22 with CommandException

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();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 23 with CommandException

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();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) UUID(java.util.UUID) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 24 with CommandException

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();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MapColor(org.spongepowered.api.map.color.MapColor)

Example 25 with CommandException

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

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