Search in sources :

Example 1 with MiniMOTDConfig

use of xyz.jpenilla.minimotd.common.config.MiniMOTDConfig in project MiniMOTD by jpenilla.

the class ClientPingServerEventListener method handle.

@Override
public void handle(@NonNull final ClientPingServerEvent event) {
    final ClientPingServerEvent.Response response = event.response();
    final ClientPingServerEvent.Response.Players players;
    final ClientPingServerEvent.Response.Players players0 = response.players().orElse(null);
    if (players0 != null) {
        players = players0;
    } else {
        response.setHidePlayers(false);
        players = response.players().orElse(null);
        if (players == null) {
            this.miniMOTD.logger().warn(String.format("Failed to handle ClientPingServerEvent: '%s', response.players() was null.", event));
            return;
        }
    }
    final MiniMOTDConfig config = this.miniMOTD.configManager().mainConfig();
    final PingResponse<Favicon> mini = this.miniMOTD.createMOTD(config, players.online(), players.max());
    mini.playerCount().applyCount(players::setOnline, players::setMax);
    mini.motd(motd -> {
        if (this.legacy(event.client().version())) {
            response.setDescription(ComponentColorDownsampler.downsampler().downsample(motd));
        } else {
            response.setDescription(motd);
        }
    });
    mini.icon(response::setFavicon);
    if (mini.disablePlayerListHover()) {
        players.profiles().clear();
    }
    if (mini.hidePlayerCount()) {
        response.setHidePlayers(true);
    }
}
Also used : PingResponse(xyz.jpenilla.minimotd.common.PingResponse) MiniMOTDConfig(xyz.jpenilla.minimotd.common.config.MiniMOTDConfig) ClientPingServerEvent(org.spongepowered.api.event.server.ClientPingServerEvent) Favicon(org.spongepowered.api.network.status.Favicon)

Example 2 with MiniMOTDConfig

use of xyz.jpenilla.minimotd.common.config.MiniMOTDConfig in project MiniMOTD by jpenilla.

the class PaperPingListener method handlePing.

@EventHandler
public void handlePing(@NonNull final PaperServerListPingEvent event) {
    final MiniMOTDConfig cfg = this.miniMOTD.configManager().mainConfig();
    final PingResponse<CachedServerIcon> response = this.miniMOTD.createMOTD(cfg, event.getNumPlayers(), event.getMaxPlayers());
    response.playerCount().applyCount(event::setNumPlayers, event::setMaxPlayers);
    response.motd(motd -> {
        if (event.getClient().getProtocolVersion() < Constants.MINECRAFT_1_16_PROTOCOL_VERSION || PaperLib.getMinecraftVersion() < 16) {
            event.setMotd(LegacyComponentSerializer.legacySection().serialize(motd));
        } else {
            event.setMotd(this.unusualHexSerializer.serialize(motd));
        }
    });
    response.icon(event::setServerIcon);
    if (response.disablePlayerListHover()) {
        event.getPlayerSample().clear();
    }
    if (response.hidePlayerCount()) {
        event.setHidePlayers(true);
    }
}
Also used : MiniMOTDConfig(xyz.jpenilla.minimotd.common.config.MiniMOTDConfig) CachedServerIcon(org.bukkit.util.CachedServerIcon) EventHandler(org.bukkit.event.EventHandler)

Example 3 with MiniMOTDConfig

use of xyz.jpenilla.minimotd.common.config.MiniMOTDConfig in project MiniMOTD by jpenilla.

the class PingListener method handlePing.

@EventHandler
public void handlePing(@NonNull final ServerListPingEvent event) {
    final MiniMOTDConfig cfg = this.miniMOTD.configManager().mainConfig();
    final PingResponse<CachedServerIcon> response = this.miniMOTD.createMOTD(cfg, event.getNumPlayers(), event.getMaxPlayers());
    event.setMaxPlayers(response.playerCount().maxPlayers());
    response.motd(motd -> {
        if (PaperLib.getMinecraftVersion() > 15) {
            event.setMotd(this.unusualHexSerializer.serialize(motd));
        } else {
            event.setMotd(LegacyComponentSerializer.legacySection().serialize(motd));
        }
    });
    response.icon(event::setServerIcon);
}
Also used : MiniMOTDConfig(xyz.jpenilla.minimotd.common.config.MiniMOTDConfig) CachedServerIcon(org.bukkit.util.CachedServerIcon) EventHandler(org.bukkit.event.EventHandler)

Example 4 with MiniMOTDConfig

use of xyz.jpenilla.minimotd.common.config.MiniMOTDConfig in project MiniMOTD by jpenilla.

the class ServerStatusPacketListenerImplMixin method injectHandleStatusRequest.

@Redirect(method = "handleStatusRequest", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;getStatus()Lnet/minecraft/network/protocol/status/ServerStatus;"))
public ServerStatus injectHandleStatusRequest(final MinecraftServer minecraftServer) {
    final ServerStatus vanillaStatus = minecraftServer.getStatus();
    final ServerStatus modifiedStatus = new ServerStatus();
    modifiedStatus.setDescription(vanillaStatus.getDescription());
    modifiedStatus.setFavicon(vanillaStatus.getFavicon());
    modifiedStatus.setVersion(vanillaStatus.getVersion());
    final MiniMOTDFabric miniMOTDFabric = MiniMOTDFabric.get();
    final MiniMOTD<String> miniMOTD = miniMOTDFabric.miniMOTD();
    final MiniMOTDConfig config = miniMOTD.configManager().mainConfig();
    final PingResponse<String> response = miniMOTD.createMOTD(config, minecraftServer.getPlayerCount(), vanillaStatus.getPlayers().getMaxPlayers());
    response.motd(motd -> {
        if (((ConnectionAccess) this.connection).protocolVersion() >= Constants.MINECRAFT_1_16_PROTOCOL_VERSION) {
            modifiedStatus.setDescription(miniMOTDFabric.audiences().toNative(motd));
        } else {
            modifiedStatus.setDescription(miniMOTDFabric.audiences().toNative(ComponentColorDownsampler.downsampler().downsample(motd)));
        }
    });
    response.icon(modifiedStatus::setFavicon);
    if (!response.hidePlayerCount()) {
        final GameProfile[] oldSample = vanillaStatus.getPlayers().getSample();
        final ServerStatus.Players newPlayers = new ServerStatus.Players(response.playerCount().maxPlayers(), response.playerCount().onlinePlayers());
        if (response.disablePlayerListHover()) {
            newPlayers.setSample(new GameProfile[] {});
        } else {
            newPlayers.setSample(oldSample);
        }
        modifiedStatus.setPlayers(newPlayers);
    }
    return modifiedStatus;
}
Also used : MiniMOTDConfig(xyz.jpenilla.minimotd.common.config.MiniMOTDConfig) MiniMOTDFabric(xyz.jpenilla.minimotd.fabric.MiniMOTDFabric) GameProfile(com.mojang.authlib.GameProfile) ServerStatus(net.minecraft.network.protocol.status.ServerStatus) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with MiniMOTDConfig

use of xyz.jpenilla.minimotd.common.config.MiniMOTDConfig in project MiniMOTD by jpenilla.

the class ClientPingServerEventListener method handle.

@Override
public void handle(@NonNull final ClientPingServerEvent event) {
    final ClientPingServerEvent.Response response = event.getResponse();
    final ClientPingServerEvent.Response.Players players;
    final ClientPingServerEvent.Response.Players players0 = response.getPlayers().orElse(null);
    if (players0 != null) {
        players = players0;
    } else {
        response.setHidePlayers(false);
        players = response.getPlayers().orElse(null);
        if (players == null) {
            this.miniMOTD.logger().warn(String.format("Failed to handle ClientPingServerEvent: '%s', response.getPlayers() was null.", event));
            return;
        }
    }
    final MiniMOTDConfig config = this.miniMOTD.configManager().mainConfig();
    final PingResponse<Favicon> mini = this.miniMOTD.createMOTD(config, players.getOnline(), players.getMax());
    mini.playerCount().applyCount(players::setOnline, players::setMax);
    mini.motd(motd -> response.setDescription(SpongeComponentSerializer.get().serialize(motd)));
    mini.icon(response::setFavicon);
    if (mini.disablePlayerListHover()) {
        players.getProfiles().clear();
    }
    if (mini.hidePlayerCount()) {
        response.setHidePlayers(true);
    }
}
Also used : PingResponse(xyz.jpenilla.minimotd.common.PingResponse) MiniMOTDConfig(xyz.jpenilla.minimotd.common.config.MiniMOTDConfig) ClientPingServerEvent(org.spongepowered.api.event.server.ClientPingServerEvent) Favicon(org.spongepowered.api.network.status.Favicon)

Aggregations

MiniMOTDConfig (xyz.jpenilla.minimotd.common.config.MiniMOTDConfig)8 EventHandler (org.bukkit.event.EventHandler)2 CachedServerIcon (org.bukkit.util.CachedServerIcon)2 ClientPingServerEvent (org.spongepowered.api.event.server.ClientPingServerEvent)2 Favicon (org.spongepowered.api.network.status.Favicon)2 PingResponse (xyz.jpenilla.minimotd.common.PingResponse)2 GameProfile (com.mojang.authlib.GameProfile)1 ServerPing (com.velocitypowered.api.proxy.server.ServerPing)1 Favicon (com.velocitypowered.api.util.Favicon)1 Component (net.kyori.adventure.text.Component)1 Favicon (net.md_5.bungee.api.Favicon)1 ServerPing (net.md_5.bungee.api.ServerPing)1 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)1 TextComponent (net.md_5.bungee.api.chat.TextComponent)1 EventHandler (net.md_5.bungee.event.EventHandler)1 ServerStatus (net.minecraft.network.protocol.status.ServerStatus)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 MiniMOTDFabric (xyz.jpenilla.minimotd.fabric.MiniMOTDFabric)1