r_ndom0306

I want to create a plugin that will run the /tick freeze command when there is one player online and will run the /tick unfreeze command when there is more than one player online

Kodari AI
KodariMay 6 at 11:11 am 6,202 (v2)
AutoTickFreeze
Automatically manages server tick rate based on player count. Runs /tick freeze when only one player is online to save resources, and runs /tick unfreeze when more than one player joins. Listens to PlayerJoinEvent and PlayerQuitEvent to check the current online player count and dispatch the appropriate tick command via the console. Lightweight and zero-configuration.

On PlayerQuitEvent, the quitting player is still in the online players collection, so the count is decremented by 1 to get the accurate post-quit count before dispatching the freeze/unfreeze command.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
4 71
src/main/java/com/autotickfreeze/AutoTickFreeze.java
package com.autotickfreeze;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class AutoTickFreeze extends JavaPlugin {

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
    }

    public void updateTickState(int onlineCount) {
        String command = onlineCount <= 1 ? "tick freeze" : "tick unfreeze";
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
    }
}