No chat to show

This project's files are all here to explore.

Files
bukkit
bungee
core
universal
velocity
pom.xml
README.md
Code
24 1,063
bukkit/src/main/java/com/kodari/maxplayer/bukkit/MaxPlayerPlugin.java
package com.kodari.maxplayer.bukkit;

import com.kodari.maxplayer.core.FullServerMessage;
import com.kodari.maxplayer.core.FullServerMessageConfig;
import com.kodari.maxplayer.core.MaxPlayerStorage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class MaxPlayerPlugin extends JavaPlugin implements Listener {
    private int limit;
    private FullServerMessage fullServerMessage;

    @Override
    public void onEnable() {
        MaxPlayerStorage storage = new ServerPropertiesStorage(getServer().getWorldContainer().toPath());
        limit = storage.load(getServer().getMaxPlayers());
        getServer().setMaxPlayers(limit);
        fullServerMessage = new FullServerMessageConfig(
                getDataFolder().toPath().resolve("maxplayer-bukkit.properties")).load();

        MaxPlayerCommand command = new MaxPlayerCommand(this, storage);
        PluginCommand pluginCommand = getCommand("maxplayer");
        if (pluginCommand == null) {
            getLogger().severe("The maxplayer command is missing from plugin.yml");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
        pluginCommand.setExecutor(command);
        pluginCommand.setTabCompleter(command);
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerLogin(PlayerLoginEvent event) {
        if (getServer().getOnlinePlayers().size() < limit) {
            return;
        }
        if (event.getResult() == PlayerLoginEvent.Result.ALLOWED
                || event.getResult() == PlayerLoginEvent.Result.KICK_FULL) {
            event.disallow(PlayerLoginEvent.Result.KICK_FULL, getFullServerMessageText());
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
        getServer().setMaxPlayers(limit);
    }

    public String getFullServerMessageText() {
        return fullServerMessage.isEnabled()
                ? getConfiguredFullServerMessageText()
                : "";
    }

    public String getConfiguredFullServerMessageText() {
        return LegacyComponentSerializer.legacySection().serialize(fullServerMessage.asComponent());
    }
}