From 52fa187d916c861f650eecf89531be78ecbac3ae Mon Sep 17 00:00:00 2001 From: Trever Fischer Date: Sat, 25 Feb 2012 13:59:54 -0500 Subject: [PATCH] Allow the URL to be configured via plugins/Caminus/config.yml --- src/main/java/us/camin/JoinListener.java | 30 ++++++++++++++---------- src/main/java/us/camin/Plugin.java | 10 ++++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/java/us/camin/JoinListener.java b/src/main/java/us/camin/JoinListener.java index dd41f84..85d3622 100644 --- a/src/main/java/us/camin/JoinListener.java +++ b/src/main/java/us/camin/JoinListener.java @@ -22,16 +22,24 @@ import java.io.InputStreamReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; +import java.net.HttpURLConnection; +import java.util.logging.Logger; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerLoginEvent; public class JoinListener extends PlayerListener { + Logger log = Logger.getLogger("Caminus.Join"); + private String m_url; + public JoinListener() { } + public void setURL(String url) { + m_url = url; + } + public static void main(String[] args) throws IOException, MalformedURLException { JoinListener listener = new JoinListener(); if (listener.isUserAuthed(args[0])) @@ -43,8 +51,8 @@ public class JoinListener extends PlayerListener { public void onPlayerLogin(PlayerLoginEvent event) { Player p = event.getPlayer(); try { - if (!isUserAuthed(p.getName())) - event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required."); + if (!isUserAuthed(p.getName())) + event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required."); } catch (MalformedURLException e) { event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!"); } catch (IOException e) { @@ -53,14 +61,12 @@ public class JoinListener extends PlayerListener { } public boolean isUserAuthed(String user) throws IOException, MalformedURLException { - URL authServer = new URL("http://dev.camin.us/api/validate/"+user); - URLConnection conn = authServer.openConnection(); - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - boolean ret; - if (in.readLine().equals("false")) - ret = false; - ret = true; - in.close(); - return ret; + URL authServer = new URL(m_url+user); + log.info("Authing "+user+" against "+authServer); + HttpURLConnection conn = (HttpURLConnection)authServer.openConnection(); + int code = conn.getResponseCode(); + if (code >= 200 && code <= 300) + return true; + return false; } } diff --git a/src/main/java/us/camin/Plugin.java b/src/main/java/us/camin/Plugin.java index 65e47e0..f6e03b0 100644 --- a/src/main/java/us/camin/Plugin.java +++ b/src/main/java/us/camin/Plugin.java @@ -17,7 +17,7 @@ package us.camin; along with Caminus. If not, see . */ -import org.bukkit.Location; +import org.bukkit.configuration.Configuration; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.plugin.PluginManager; @@ -28,6 +28,7 @@ import java.util.logging.Logger; public class Plugin extends JavaPlugin { Logger log = Logger.getLogger("Caminus");//Define your logger + private JoinListener m_listener; public void onDisable() { log.info("[Caminus] Plugin disabled"); @@ -37,6 +38,11 @@ public class Plugin extends JavaPlugin { log.info("[Caminus] Plugin enabled"); PluginManager pm = this.getServer().getPluginManager(); - pm.registerEvent(Event.Type.PLAYER_LOGIN, new JoinListener(), Event.Priority.Normal, this); + m_listener = new JoinListener(); + Configuration conf = getConfig(); + conf.addDefault("url", "http://camin.us/api/validate/"); + String url = conf.getString("url"); + m_listener.setURL(url); + pm.registerEvent(Event.Type.PLAYER_LOGIN, m_listener, Event.Priority.Normal, this); } }