PingStatus module

This commit is contained in:
2018-10-07 03:06:34 +02:00
parent c4279130f7
commit 2628105be4
8 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package cz.marwland.mc.features;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.server.ServerListPingEvent;
import cz.marwland.mc.core.features.Feature;
public class PingStatus extends Feature {
FileConfiguration cfg;
List<String> line1 = new ArrayList<>();
List<String> line2 = new ArrayList<>();
List<String> common = new ArrayList<>();
public PingStatus() {
super();
this.registerConfig("pingstatus.yml");
}
@Override
public void onEnable() {
super.onEnable();
cfg = getConfig("pingstatus.yml");
List<String> line1cfg = cfg.getStringList("line1");
if (line1cfg != null)
line1 = line1cfg;
List<String> line2cfg = cfg.getStringList("line2");
if (line2cfg != null)
line2 = line2cfg;
List<String> commoncfg = cfg.getStringList("common");
if (commoncfg != null)
common = commoncfg;
}
@EventHandler
public void onPingServer(ServerListPingEvent event) {
String motd = null;
if (common.size() > 0) {
motd = getRandomEntry(this.common);
} else if (line1.size() > 0 && line2.size() > 0) {
motd = getRandomEntry(this.line1) + "\n" + getRandomEntry(this.line2);
}
if (motd != null)
event.setMotd(motd);
}
public static <T> T getRandomEntry(List<T> list) {
return list.get((int) Math.floor((Math.random() * list.size())));
}
}