All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.adventure.audience.IterableAudienceProvider Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.adventure.audience;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.ConsoleSender;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;
import java.util.stream.StreamSupport;

/**
 * A provider of iterable audiences.
 */
class IterableAudienceProvider implements AudienceProvider> {
    private final List console = List.of(MinecraftServer.getCommandManager().getConsoleSender());
    private final AudienceRegistry registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);

    protected IterableAudienceProvider() {
    }

    @Override
    public @NotNull Iterable all() {
        List all = new ArrayList<>();
        this.players().forEach(all::add);
        this.console().forEach(all::add);
        this.customs().forEach(all::add);
        return all;
    }

    @Override
    public @NotNull Iterable players() {
        return MinecraftServer.getConnectionManager().getOnlinePlayers();
    }

    @Override
    public @NotNull Iterable players(@NotNull Predicate filter) {
        return MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).toList();
    }

    @Override
    public @NotNull Iterable console() {
        return this.console;
    }

    @Override
    public @NotNull Iterable server() {
        List all = new ArrayList<>();
        this.players().forEach(all::add);
        this.console().forEach(all::add);
        return all;
    }

    @Override
    public @NotNull Iterable customs() {
        return this.registry.all();
    }

    @Override
    public @NotNull Iterable custom(@NotNull Key key) {
        return this.registry.of(key);
    }

    @Override
    public @NotNull Iterable custom(@NotNull Key key, Predicate filter) {
        return StreamSupport.stream(this.registry.of(key).spliterator(), false).filter(filter).toList();
    }

    @Override
    public @NotNull Iterable customs(@NotNull Predicate filter) {
        return this.registry.of(filter);
    }

    @Override
    public @NotNull Iterable all(@NotNull Predicate filter) {
        return StreamSupport.stream(this.all().spliterator(), false).filter(filter).toList();
    }

    @Override
    public @NotNull AudienceRegistry registry() {
        return this.registry;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy