Search in sources :

Example 16 with Audience

use of net.kyori.adventure.audience.Audience 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 17 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class MapTest method enable.

@Override
public void enable(final CommandContext ctx) {
    final Audience audience = ctx.cause().audience();
    if (!this.isEnabled) {
        this.isEnabled = true;
        Sponge.eventManager().registerListeners(this.container, this.listeners);
        audience.sendMessage(Component.text("Map listeners are enabled. Created maps will now start blue.", NamedTextColor.GREEN));
    } else {
        audience.sendMessage(Component.text("Map listeners are already enabled.", NamedTextColor.YELLOW));
    }
}
Also used : Audience(net.kyori.adventure.audience.Audience)

Example 18 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class MapTest method testMapSerialization.

private CommandResult testMapSerialization(final CommandContext ctx) {
    MapInfo mapInfo = null;
    final Audience audience = ctx.cause().audience();
    if (audience instanceof Player) {
        final Player player = (Player) audience;
        mapInfo = player.itemInHand(HandTypes.MAIN_HAND).get(Keys.MAP_INFO).orElse(null);
    }
    final MapStorage mapStorage = Sponge.server().mapStorage();
    if (mapInfo == null) {
        mapInfo = mapStorage.allMapInfos().stream().findAny().orElse(mapStorage.createNewMapInfo().get());
    }
    final DataView mapInfoView = blankMapDecorationIds(mapInfo.toContainer());
    mapInfoView.set(DataQuery.of("MapData", "MapLocked"), true).set(DataQuery.of("UnsafeMapId"), 10);
    return CommandResult.success();
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Player(org.spongepowered.api.entity.living.player.Player) Audience(net.kyori.adventure.audience.Audience) MapInfo(org.spongepowered.api.map.MapInfo) MapStorage(org.spongepowered.api.map.MapStorage)

Example 19 with Audience

use of net.kyori.adventure.audience.Audience in project SpongeCommon by SpongePowered.

the class MapTest method checkSerialization.

private void checkSerialization(final CommandContext ctx, final String testName, final String expected, final String after) {
    final Audience audience = ctx.cause().audience();
    final boolean success = expected.equals(after);
    final Component text = Component.text("Test of ").append(Component.text(testName, NamedTextColor.BLUE)).append(success ? Component.text(" SUCCEEDED", NamedTextColor.GREEN) : Component.text(" FAILED", NamedTextColor.RED));
    audience.sendMessage(text);
    if (!success) {
        logger.info(testName + " Expected: " + expected);
        logger.info(testName + " Real: " + after);
    }
}
Also used : Audience(net.kyori.adventure.audience.Audience) Component(net.kyori.adventure.text.Component)

Aggregations

Audience (net.kyori.adventure.audience.Audience)19 Component (net.kyori.adventure.text.Component)9 CommandException (org.spongepowered.api.command.exception.CommandException)6 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)5 Optional (java.util.Optional)4 Player (org.spongepowered.api.entity.living.player.Player)4 Collection (java.util.Collection)3 List (java.util.List)3 UUID (java.util.UUID)3 Identity (net.kyori.adventure.identity.Identity)3 TextComponent (net.kyori.adventure.text.TextComponent)3 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)3 Inject (com.google.inject.Inject)2 Instant (java.time.Instant)2 Collections (java.util.Collections)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)2 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)2 QueryResult (me.botsko.prism.actionlibs.QueryResult)2