Last active
August 29, 2015 14:20
-
-
Save Cewlt/b2f42c06545522ac16f8 to your computer and use it in GitHub Desktop.
dirty code, POC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.colt.TestPlugin; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import net.md_5.bungee.api.ChatColor; | |
import net.md_5.bungee.api.chat.ClickEvent; | |
import net.md_5.bungee.api.chat.TextComponent; | |
import org.bukkit.Bukkit; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.AsyncPlayerChatEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class TestPlugin extends JavaPlugin implements Listener { | |
public void onEnable() { | |
Bukkit.getServer().getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler | |
public void chat(AsyncPlayerChatEvent e) { | |
String pattern = "@\\w{1,15}"; | |
String msg = e.getMessage(); | |
if(msg.contains(pattern) && isValidTwitter(msg) == true) { | |
TextComponent ee = new TextComponent(msg); | |
ee.setColor(ChatColor.RED); | |
ee.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.twitter.com/" + msg.replace("@", ""))); | |
e.getPlayer().spigot().sendMessage(ee); | |
e.setCancelled(true); | |
} | |
} | |
public static boolean isValidTwitter(String username) { | |
try { | |
URL u = new URL("https://www.twitter.com/" + username.replace("@", "")); | |
HttpURLConnection huc = (HttpURLConnection)u.openConnection(); | |
huc.setRequestMethod("HEAD"); | |
huc.connect(); | |
if(huc.getResponseCode() == 404) { | |
return false; | |
} else if(huc.getResponseCode() == 200) { | |
return true; | |
} | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (ProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment