TotalFreedomMod/commons/src/main/java/me/totalfreedom/totalfreedommod/ServerPing.java

71 lines
2.0 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod;
2022-03-25 20:45:32 +00:00
import com.google.gson.Gson;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FUtil;
2023-03-27 03:59:17 +00:00
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.server.ServerListPingEvent;
2022-03-25 20:45:32 +00:00
import java.io.InputStreamReader;
public class ServerPing extends FreedomService
{
2022-03-25 20:45:32 +00:00
private final Gson gson = new Gson();
private final VersionMeta meta = gson.fromJson(new InputStreamReader(Bukkit.class.getClassLoader().getResourceAsStream("version.json")),VersionMeta.class);
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
{
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop()
{
}
@EventHandler(priority = EventPriority.HIGH)
public void onServerPing(ServerListPingEvent event)
{
final String ip = event.getAddress().getHostAddress().trim();
if (plugin.bm.isIpBanned(ip))
{
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_BAN_MOTD.getString()));
return;
}
if (ConfigEntry.ADMIN_ONLY_MODE.getBoolean())
{
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_ADMINMODE_MOTD.getString()));
return;
}
2018-03-18 08:32:50 +00:00
if (LoginProcess.isLockdownEnabled())
{
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_LOCKDOWN_MOTD.getString()));
2018-03-18 08:32:50 +00:00
return;
}
if (Bukkit.hasWhitelist())
{
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_WHITELIST_MOTD.getString()));
return;
}
if (Bukkit.getOnlinePlayers().size() >= Bukkit.getMaxPlayers())
{
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_FULL_MOTD.getString()));
return;
}
2023-03-27 03:59:17 +00:00
event.motd(FUtil.miniMessage(ConfigEntry.SERVER_MOTD.getString(), Placeholder.unparsed("version", meta.id)));
}
2022-03-25 20:45:32 +00:00
private static class VersionMeta
{
private String id;
}
}