package net.simpvp.Misc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;

import net.md_5.bungee.api.ChatColor;


public class ServerListMessage implements Listener {
	
	List<?> listOfMOTDS;
	Method method = null;
	
	@EventHandler
	public void serverListPingEvent(ServerListPingEvent event) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		if (method != null) {
			String eventName = (String) method.invoke(null);
			if (eventName != null) {
				event.setMotd(ChatColor.LIGHT_PURPLE + "Currently playing:" + eventName);
				return;
			}
		}
		if (listOfMOTDS != null && !listOfMOTDS.isEmpty()) {
			String motd = (String) listOfMOTDS.get(ThreadLocalRandom.current().nextInt(listOfMOTDS.size()));
			event.setMotd(ChatColor.translateAlternateColorCodes('&', motd));
		}
	}
	
	/**
	 * Use reflection to get access to the event plugin. Thanks kutekats!
	 */
	public ServerListMessage(Misc plugin) {
		Class<?> eventsClass = null;
		listOfMOTDS = plugin.getConfig().getList("serverListMessages");
		try {
		  eventsClass = Class.forName("net.simpvp.Events.Event");
		  method = eventsClass.getMethod("getEventName");
		} catch (ClassNotFoundException | SecurityException | IllegalArgumentException | NoSuchMethodException e) {
			Misc.instance.getLogger().info("Event plugin not found");
		}
	}
	
}


@@ -17,19 +17,17 @@
 vp.Misc;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -70,17 +70,16 @@
 ception;
-%0D
 %0Aimport 
@@ -103,17 +103,16 @@
 .Method;
-%0D
 %0Aimport 
@@ -126,17 +126,16 @@
 il.List;
-%0D
 %0Aimport 
@@ -173,19 +173,17 @@
 lRandom;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -212,17 +212,16 @@
 Handler;
-%0D
 %0Aimport 
@@ -246,17 +246,16 @@
 istener;
-%0D
 %0Aimport 
@@ -298,19 +298,17 @@
 ngEvent;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -341,13 +341,10 @@
 lor;
-%0D%0A%0D%0A%0D
+%0A%0A
 %0Apub
@@ -392,20 +392,17 @@
 stener %7B
-%0D%0A%09%0D
+%0A
 %0A%09List%3C?
@@ -415,17 +415,16 @@
 OfMOTDS;
-%0D
 %0A%09Method
@@ -438,20 +438,17 @@
  = null;
-%0D%0A%09%0D
+%0A
 %0A%09@Event
@@ -454,17 +454,16 @@
 tHandler
-%0D
 %0A%09public
@@ -599,17 +599,16 @@
 eption %7B
-%0D
 %0A%09%09if (m
@@ -619,25 +619,24 @@
 d != null) %7B
-%0D
 %0A%09%09%09String e
@@ -675,17 +675,16 @@
 e(null);
-%0D
 %0A%09%09%09if (
@@ -703,17 +703,16 @@
  null) %7B
-%0D
 %0A%09%09%09%09eve
@@ -781,17 +781,16 @@
 ntName);
-%0D
 %0A%09%09%09%09ret
@@ -797,20 +797,17 @@
 urn;
-%0D
 %0A%09%09%09%7D
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%09i
@@ -857,17 +857,16 @@
 pty()) %7B
-%0D
 %0A%09%09%09Stri
@@ -957,17 +957,16 @@
 ize()));
-%0D
 %0A%09%09%09even
@@ -1030,27 +1030,21 @@
 d));
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%7D
-%0D%0A%09%0D
+%0A
 %0A%09/**
-%0D
 %0A%09 *
@@ -1114,15 +1114,13 @@
 ats!
-%0D
 %0A%09 */
-%0D
 %0A%09pu
@@ -1156,17 +1156,16 @@
 lugin) %7B
-%0D
 %0A%09%09Class
@@ -1187,17 +1187,16 @@
  = null;
-%0D
 %0A%09%09listO
@@ -1257,17 +1257,16 @@
 s%22);
-%0D
 %0A%09%09try %7B
 %0D%0A%09%09
@@ -1261,17 +1261,16 @@
 %0A%09%09try %7B
-%0D
 %0A%09%09  eve
@@ -1321,17 +1321,16 @@
 Event%22);
-%0D
 %0A%09%09  met
@@ -1373,17 +1373,16 @@
 tName%22);
-%0D
 %0A%09%09%7D cat
@@ -1483,17 +1483,16 @@
 ion e) %7B
-%0D
 %0A%09%09%09Misc
@@ -1548,23 +1548,16 @@
 d%22);
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%7D
-%0D%0A%09%0D%0A%7D%0D%0A%0D
+%0A%0A%7D%0A
 %0A

Convert to unix line endings