diff --git a/.metadata/.log b/.metadata/.log index 9e28562..bda18fc 100644 --- a/.metadata/.log +++ b/.metadata/.log @@ -745,3 +745,50 @@ Contains: Could not delete: D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairP !MESSAGE Could not delete: D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairProto\Spigot plugin\TheButtonAutoFlair\bin\tk\sznp. !SUBENTRY 5 org.eclipse.core.filesystem 4 273 2015-08-09 16:10:11.010 !MESSAGE Could not delete: D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairProto\Spigot plugin\TheButtonAutoFlair\bin\tk. +!SESSION 2015-08-09 20:56:55.150 ----------------------------------------------- +eclipse.buildId=M20130204-1200 +java.version=1.8.0_45 +java.vendor=Oracle Corporation +BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU +Framework arguments: -product org.eclipse.epp.package.jee.product +Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product + +!ENTRY org.eclipse.egit.ui 2 0 2015-08-09 20:58:12.223 +!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level +Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory. +The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in +this system level configuration. The Git installation location can be configured on the +Team > Git > Configuration preference page's 'System Settings' tab. +This warning can be switched off on the Team > Git > Confirmations and Warnings preference page. + +!ENTRY org.eclipse.egit.ui 2 0 2015-08-09 20:58:12.224 +!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git +user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is +not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and +EGit might behave differently since they see different configuration options. +This warning can be switched off on the Team > Git > Confirmations and Warnings preference page. +!SESSION 2015-08-09 22:48:23.020 ----------------------------------------------- +eclipse.buildId=M20130204-1200 +java.version=1.8.0_45 +java.vendor=Oracle Corporation +BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU +Framework arguments: -product org.eclipse.epp.package.jee.product +Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product + +!ENTRY org.eclipse.core.resources 2 10035 2015-08-09 22:49:53.071 +!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes. + +!ENTRY org.eclipse.egit.ui 2 0 2015-08-09 22:53:15.430 +!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level +Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory. +The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in +this system level configuration. The Git installation location can be configured on the +Team > Git > Configuration preference page's 'System Settings' tab. +This warning can be switched off on the Team > Git > Confirmations and Warnings preference page. + +!ENTRY org.eclipse.egit.ui 2 0 2015-08-09 22:53:15.431 +!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git +user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is +not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and +EGit might behave differently since they see different configuration options. +This warning can be switched off on the Team > Git > Confirmations and Warnings preference page. diff --git a/.metadata/.mylyn/.tasks.xml.zip b/.metadata/.mylyn/.tasks.xml.zip index 6b15865..0bf1e7e 100644 Binary files a/.metadata/.mylyn/.tasks.xml.zip and b/.metadata/.mylyn/.tasks.xml.zip differ diff --git a/.metadata/.mylyn/tasks.xml.zip b/.metadata/.mylyn/tasks.xml.zip index 1fcc9a7..aaeae30 100644 Binary files a/.metadata/.mylyn/tasks.xml.zip and b/.metadata/.mylyn/tasks.xml.zip differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/3027b10cd93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/0/3027b10cd93e001517108d149b613203 new file mode 100644 index 0000000..8131160 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/3027b10cd93e001517108d149b613203 @@ -0,0 +1,226 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/601f8274873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/601f8274873e00151ce8ff6d4f74dabd deleted file mode 100644 index aaa641d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/601f8274873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,144 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/90ea77eee43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/1/90ea77eee43e001517108d149b613203 new file mode 100644 index 0000000..67c0651 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/90ea77eee43e001517108d149b613203 @@ -0,0 +1,425 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if(!inputFile.exists()) + return true; + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/a0191b15d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1/a0191b15d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..85f16ce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/a0191b15d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,374 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { + System.out.println("Auto-flair plugin cleanup for reloading..."); //2015.08.09. + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/d071b8d5873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/d071b8d5873e00151ce8ff6d4f74dabd deleted file mode 100644 index aab975f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/d071b8d5873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,152 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==args[2]) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/d0997b1b023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/1/d0997b1b023e00151c31fb33fff4337f deleted file mode 100644 index 2650c6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/d0997b1b023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/f03cfc260d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/1/f03cfc260d3e00151089cca86c5b1a99 deleted file mode 100644 index ee35a22..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1/f03cfc260d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/80fad266df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/10/80fad266df3e001517108d149b613203 new file mode 100644 index 0000000..2a1ad93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/10/80fad266df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/d069a424873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/10/d069a424873e00151ce8ff6d4f74dabd deleted file mode 100644 index f0c8ce6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/10/d069a424873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,136 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02f8f8d083e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02f8f8d083e00151c31fb33fff4337f deleted file mode 100644 index 4b994f9..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02f8f8d083e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.HasFlair) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/10177772dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/12/10177772dd3e001517108d149b613203 new file mode 100644 index 0000000..9bd5a8e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/10177772dd3e001517108d149b613203 @@ -0,0 +1,409 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close();, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/900059d3e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/12/900059d3e03e001517108d149b613203 new file mode 100644 index 0000000..6ad1625 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/900059d3e03e001517108d149b613203 @@ -0,0 +1,264 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/90d09f90df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/12/90d09f90df3e001517108d149b613203 new file mode 100644 index 0000000..423d0b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/90d09f90df3e001517108d149b613203 @@ -0,0 +1,421 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/b09714c2cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/12/b09714c2cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..206b53a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/b09714c2cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/903ca4bcde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/13/903ca4bcde3e001517108d149b613203 new file mode 100644 index 0000000..697a10d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/903ca4bcde3e001517108d149b613203 @@ -0,0 +1,436 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/20b7f4c2d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20b7f4c2d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5cf5c6e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20b7f4c2d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true; + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/a0689143d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/15/a0689143d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..b72f226 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/a0689143d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,221 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05be66e043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05be66e043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/15/d05be66e043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/90523966df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/16/90523966df3e001517108d149b613203 new file mode 100644 index 0000000..2a1ad93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/90523966df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/101a2f29df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/17/101a2f29df3e001517108d149b613203 new file mode 100644 index 0000000..6b55eb2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/101a2f29df3e001517108d149b613203 @@ -0,0 +1,251 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/c0e42302ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/19/c0e42302ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..84912b7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/c0e42302ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,354 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": + finalflair=""; //2015.08.09. + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/101bc97ade3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/101bc97ade3e001517108d149b613203 new file mode 100644 index 0000000..1398adf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/101bc97ade3e001517108d149b613203 @@ -0,0 +1,430 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File("myFile.txt"); + File tempFile = new File("myTempFile.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/308e38e1de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/308e38e1de3e001517108d149b613203 new file mode 100644 index 0000000..9e4d66d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/308e38e1de3e001517108d149b613203 @@ -0,0 +1,440 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!RemoveLineFromFile("customflairs.txt")) + { + Commands + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f0c4a095083e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f0c4a095083e00151c31fb33fff4337f deleted file mode 100644 index f174b9f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f0c4a095083e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(!p.HasFlair) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50b35f3bd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50b35f3bd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f15dd12 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50b35f3bd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,376 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/609377ad0d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/609377ad0d3e00151089cca86c5b1a99 deleted file mode 100644 index 752fdf8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/609377ad0d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,437 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - System.out.println("PlayerName: "+player.getName()); - System.out.println(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair); - System.out.println(MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair); - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/a0ecf93ad23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/a0ecf93ad23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f15dd12 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/a0ecf93ad23e001518fa86d7ae2a1cf8 @@ -0,0 +1,376 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20ce3447d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20ce3447d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..869953e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20ce3447d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,224 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/a0477e13d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/a0477e13d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f26d723 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/a0477e13d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,220 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/b057e861ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/b057e861ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..4343f31 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/b057e861ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public FlairClassType FlairType; //2015.08.09. + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + /*public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + }*/ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/203f7772d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/203f7772d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..efbed4c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/203f7772d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,380 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/604b1a15873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/604b1a15873e00151ce8ff6d4f74dabd deleted file mode 100644 index 2b6fbe4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/604b1a15873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,135 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case " - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/00863467e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2/00863467e03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/00863467e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/a06adb06d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/2/a06adb06d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2ccf0cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/a06adb06d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,219 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/b08b3094cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/20/b08b3094cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..50ac8ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/b08b3094cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,364 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/509f004a873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/509f004a873e00151ce8ff6d4f74dabd deleted file mode 100644 index 7947b97..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/21/509f004a873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,138 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - case "playerinfo": - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/a0775b0ee03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/21/a0775b0ee03e001517108d149b613203 new file mode 100644 index 0000000..e6e7dea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/a0775b0ee03e001517108d149b613203 @@ -0,0 +1,259 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/50fe465a863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/22/50fe465a863e00151ce8ff6d4f74dabd deleted file mode 100644 index 4e09c08..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/22/50fe465a863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/8052bd83df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/22/8052bd83df3e001517108d149b613203 new file mode 100644 index 0000000..2bbf620 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/22/8052bd83df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/a091cfc4d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/22/a091cfc4d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5d7321f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/22/a091cfc4d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/a0c7c72cd33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/23/a0c7c72cd33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f17161d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/a0c7c72cd33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + //SendMessage(player, msg); //2015.08.09. + + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0c95a0cd93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0c95a0cd93e001517108d149b613203 new file mode 100644 index 0000000..b9d957a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0c95a0cd93e001517108d149b613203 @@ -0,0 +1,224 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/3067ea8bd13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/25/3067ea8bd13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c1c6d6a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/3067ea8bd13e001518fa86d7ae2a1cf8 @@ -0,0 +1,326 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + Load(false); + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/902f77f1de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/25/902f77f1de3e001517108d149b613203 new file mode 100644 index 0000000..74f21a1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/902f77f1de3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!RemoveLineFromFile("customflairs.txt")) + { + Commands.SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/00298c12e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/26/00298c12e03e001517108d149b613203 new file mode 100644 index 0000000..2b242de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/00298c12e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + s + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/108127addd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/26/108127addd3e001517108d149b613203 new file mode 100644 index 0000000..3da8073 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/108127addd3e001517108d149b613203 @@ -0,0 +1,403 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + bw.write(player.PlayerName+"\n"); + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/e04b0add303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/26/20fee89fce3e001518fa86d7ae2a1cf8 similarity index 70% rename from .metadata/.plugins/org.eclipse.core.resources/.history/f8/e04b0add303e00151cbdba8b5f0a340e rename to .metadata/.plugins/org.eclipse.core.resources/.history/26/20fee89fce3e001518fa86d7ae2a1cf8 index cf8ca2c..ab06769 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/e04b0add303e00151cbdba8b5f0a340e +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/20fee89fce3e001518fa86d7ae2a1cf8 @@ -21,8 +21,9 @@ import org.bukkit.plugin.java.JavaPlugin; import org.json.JSONArray; import org.json.JSONObject; +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; import com.palmergames.bukkit.towny.object.WorldCoord; public class PluginMain extends JavaPlugin @@ -48,7 +49,7 @@ public class PluginMain extends JavaPlugin while ((line = br.readLine()) != null) { String name=line.replace("\n", ""); - System.out.println("Name: " + name); + //System.out.println("Name: " + name); MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. } br.close(); @@ -77,8 +78,10 @@ public class PluginMain extends JavaPlugin } br.close(); } + //throw new IOException("Test"); //2015.08.09. } catch (IOException e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Thread t=new Thread(r); @@ -100,33 +103,31 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } stop=true; } @@ -181,10 +182,13 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } } } + public static Exception LastException; //2015.08.09. + public String DownloadString(String urlstr) throws MalformedURLException, IOException { URL url = new URL(urlstr); @@ -198,37 +202,18 @@ public class PluginMain extends JavaPlugin return body; } - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ public static Map TownColors=new HashMap(); //2015.07.20. public Boolean HasIGFlair(String playername) { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. return p.Flair!=null; //2015.08.08. } public void SetFlair(String playername, String text, String flairclass, String username) { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. String finalflair; + p.FlairDecided=true; switch(flairclass) { case "press-1": @@ -258,38 +243,24 @@ public class PluginMain extends JavaPlugin case "cant-press": //2015.08.08. finalflair="§r(can't press)§r"; break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; default: finalflair=""; break; } if(finalflair.length()==0) //<-- 2015.07.20. return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. p.Flair=finalflair; //2015.08.08. p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); if(player.getName().equals(playername)) { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); break; } } @@ -297,43 +268,36 @@ public class PluginMain extends JavaPlugin public static String GetFlair(Player player) { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. return flair==null ? "" : flair; } public static void AppendPlayerDisplayFlair(Player player, String username, String flair) { - //if(IgnoredPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) return; - //if(AcceptedPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); + { AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } else player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); } public static void AppendPlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //System.out.println("A"); String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); String displayname=player.getName(); //2015.08.08. ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); for(int i=0; i=colors.length) { - //System.out.println("D"); int x=0; for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); try { Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO return town.getName(); @@ -398,10 +350,6 @@ public class PluginMain extends JavaPlugin public static void RemovePlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/a0111a40d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/26/a0111a40d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..61ec61c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/a0111a40d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/9011edbed23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/27/9011edbed23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..717cff8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/9011edbed23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0c61635023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0c61635023e00151c31fb33fff4337f deleted file mode 100644 index e00eae6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/27/d0c61635023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values()) - { - if(p.Flair!=null) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/f08a8910cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/27/f08a8910cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..999a5ae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/f08a8910cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/309e6352d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/28/309e6352d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..d073151 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/309e6352d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/4085cc81fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/29/4085cc81fe3d00151c31fb33fff4337f deleted file mode 100644 index 77b05cb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/4085cc81fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - PluginMain.IgnoredPlayers.add(player.getName()); - //String dname=player.getDisplayName(); - String flair=PluginMain.PlayerFlairs.get(player.getName()); - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/50dd5657043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/29/50dd5657043e00151c31fb33fff4337f deleted file mode 100644 index 772f7ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/29/50dd5657043e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values()) - { - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/001db465df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/001db465df3e001517108d149b613203 new file mode 100644 index 0000000..df11079 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/001db465df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/00a6389de03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/00a6389de03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/00a6389de03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20868515d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20868515d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ccaedeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20868515d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,363 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20a4e0af0d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20a4e0af0d3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/20a4e0af0d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/50ddd938863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/50ddd938863e00151ce8ff6d4f74dabd deleted file mode 100644 index 8cba558..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/50ddd938863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoShowErrors(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0123229883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0123229883e00151ce8ff6d4f74dabd deleted file mode 100644 index 2fca4b8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0123229883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,163 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/10b26929df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/10b26929df3e001517108d149b613203 new file mode 100644 index 0000000..5f009d8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/10b26929df3e001517108d149b613203 @@ -0,0 +1,251 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + return + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/00c8dce1df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/00c8dce1df3e001517108d149b613203 new file mode 100644 index 0000000..ac8d077 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/00c8dce1df3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void DoSetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c0b10f0c023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c0b10f0c023e00151c31fb33fff4337f deleted file mode 100644 index 1174627..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/c0b10f0c023e00151c31fb33fff4337f +++ /dev/null @@ -1,124 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/003fe9fade3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/003fe9fade3e001517108d149b613203 new file mode 100644 index 0000000..99a2079 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/003fe9fade3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + Commands.SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/807b35a8dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/807b35a8dd3e001517108d149b613203 new file mode 100644 index 0000000..9eedbdb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/807b35a8dd3e001517108d149b613203 @@ -0,0 +1,408 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/90fc2ebfd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/90fc2ebfd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..717cff8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/90fc2ebfd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/1089a9abdc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/1089a9abdc3e001517108d149b613203 new file mode 100644 index 0000000..2c1a551 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/1089a9abdc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/6035f831893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/6035f831893e00151ce8ff6d4f74dabd deleted file mode 100644 index e2107b1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/6035f831893e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,169 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); - SendMessage(player, "Player name: "+p.PlayerName); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/10d5c0cc303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/30/10d5c0cc303e00151cbdba8b5f0a340e deleted file mode 100644 index 8f610fd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/10d5c0cc303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,424 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/20a0c374d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/30/20a0c374d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..641be01 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/20a0c374d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,345 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/706b3ad0303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/30/706b3ad0303e00151cbdba8b5f0a340e deleted file mode 100644 index 206493e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/30/706b3ad0303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,422 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/808be1e8e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/31/808be1e8e03e001517108d149b613203 new file mode 100644 index 0000000..42ec92d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/808be1e8e03e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName())) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/c0e1dbcdcf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/31/c0e1dbcdcf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..276d1df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/c0e1dbcdcf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,363 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(player, p); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/608c284a083e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/33/608c284a083e00151c31fb33fff4337f deleted file mode 100644 index 1cd84d0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/33/608c284a083e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/60a1e0d1303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/33/60a1e0d1303e00151cbdba8b5f0a340e deleted file mode 100644 index f5d4fd5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/33/60a1e0d1303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,421 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/90a26f4bdd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/33/90a26f4bdd3e001517108d149b613203 new file mode 100644 index 0000000..45d12d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/90a26f4bdd3e001517108d149b613203 @@ -0,0 +1,398 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/b097b71bce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/34/b097b71bce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..1f5948c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/b097b71bce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,354 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/407f1825013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/36/407f1825013e00151c31fb33fff4337f deleted file mode 100644 index d5af4e4..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/36/407f1825013e00151c31fb33fff4337f +++ /dev/null @@ -1,124 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/20e61fc4d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/37/20e61fc4d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..39f0d07 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/20e61fc4d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,370 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/5064f5c0883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/5064f5c0883e00151ce8ff6d4f74dabd deleted file mode 100644 index ac79356..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/37/5064f5c0883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,173 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/d0544e5b893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/a0f3a0e9cf3e001518fa86d7ae2a1cf8 similarity index 83% rename from .metadata/.plugins/org.eclipse.core.resources/.history/14/d0544e5b893e00151ce8ff6d4f74dabd rename to .metadata/.plugins/org.eclipse.core.resources/.history/38/a0f3a0e9cf3e001518fa86d7ae2a1cf8 index e8f0509..327e9f3 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/14/d0544e5b893e00151ce8ff6d4f74dabd +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/a0f3a0e9cf3e001518fa86d7ae2a1cf8 @@ -33,7 +33,8 @@ public class Commands implements CommandExecutor { if(!p.AcceptedFlair) { String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(player, p); p.AcceptedFlair=true; //2015.08.08. player.sendMessage("§6Your flair has been set:§r "+flair); } @@ -62,16 +63,20 @@ public class Commands implements CommandExecutor { case "admin": //2015.08.09. DoAdmin(player, args); break; + case "nonpresser": //2015.08.09. + break; default: return false; } return true; } - /*if(args[0].toLowerCase()=="reload") DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } return false; } private static void DoReload(Player player) @@ -111,6 +116,7 @@ public class Commands implements CommandExecutor { System.out.println("Error!\n"+e); if(player!=null) player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. } //} //else @@ -120,6 +126,7 @@ public class Commands implements CommandExecutor { { //2015.08.09. if(player==null || player.isOp() || player.getName()=="NorbiPeti") { + //System.out.println("Args length: " + args.length); if(args.length==1) { String message="§cUsage: /u admin reload|playerinfo§r"; @@ -135,6 +142,12 @@ public class Commands implements CommandExecutor { case "playerinfo": DoPlayerInfo(player, args); break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; } } else @@ -147,11 +160,13 @@ public class Commands implements CommandExecutor { { String message="§cUsage: /u admin playerinfo §r"; SendMessage(player, message); + return; } if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) { String message="§cPlayer not found: "+args[2]+"§r"; SendMessage(player, message); + return; } MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); SendMessage(player, "Player name: "+p.PlayerName); @@ -159,7 +174,6 @@ public class Commands implements CommandExecutor { SendMessage(player, "Username: "+p.UserName); SendMessage(player, "Flair accepted: "+p.AcceptedFlair); SendMessage(player, "Flair ignored: "+p.IgnoredFlair); - return; } private static void SendMessage(Player player, String message) { //2015.08.09. @@ -167,6 +181,17 @@ public class Commands implements CommandExecutor { System.out.println(message); else player.sendMessage(message); - return; + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/e086e0cc843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/e086e0cc843e00151ce8ff6d4f74dabd deleted file mode 100644 index a8e5994..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/38/e086e0cc843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/2006034ad03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/39/2006034ad03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c6af7c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/2006034ad03e001518fa86d7ae2a1cf8 @@ -0,0 +1,201 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/705720220d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/705720220d3e00151089cca86c5b1a99 deleted file mode 100644 index ad07236..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/705720220d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,433 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/909349e2df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/909349e2df3e001517108d149b613203 new file mode 100644 index 0000000..403b6bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/909349e2df3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void DoSetrFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/d0b0a0be863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/d0b0a0be863e00151ce8ff6d4f74dabd deleted file mode 100644 index cacc30e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/d0b0a0be863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,132 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/30bdb825cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/30bdb825cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..a9fdf9d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/30bdb825cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,25 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/a0a30729d93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/a0a30729d93e001517108d149b613203 new file mode 100644 index 0000000..46801eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/a0a30729d93e001517108d149b613203 @@ -0,0 +1,227 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/205637bed23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/205637bed23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..19e20e0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/205637bed23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true) + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/60d8cd51873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/60d8cd51873e00151ce8ff6d4f74dabd deleted file mode 100644 index 01037e0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/60d8cd51873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,139 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/007663b2df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/007663b2df3e001517108d149b613203 new file mode 100644 index 0000000..f08a770 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/007663b2df3e001517108d149b613203 @@ -0,0 +1,421 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00f95890de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00f95890de3e001517108d149b613203 new file mode 100644 index 0000000..60a390b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00f95890de3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/6051d217853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/6051d217853e00151ce8ff6d4f74dabd deleted file mode 100644 index e292615..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/6051d217853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,121 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/c0b328b0883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/c0b328b0883e00151ce8ff6d4f74dabd deleted file mode 100644 index fade14e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/c0b328b0883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,176 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/00fa721adf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/00fa721adf3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/00fa721adf3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/509fad42873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/509fad42873e00151ce8ff6d4f74dabd deleted file mode 100644 index c25a794..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/509fad42873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,136 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/90e3e69bde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/90e3e69bde3e001517108d149b613203 new file mode 100644 index 0000000..9c0f686 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/90e3e69bde3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/e0d118c0863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/e0d118c0863e00151ce8ff6d4f74dabd deleted file mode 100644 index a8ddfb3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/e0d118c0863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,135 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - swithc(args[1].toLowerCase()) - { - - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/70140e5a0c3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/70140e5a0c3e00151089cca86c5b1a99 deleted file mode 100644 index 090b35d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/70140e5a0c3e00151089cca86c5b1a99 +++ /dev/null @@ -1,432 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/a0709f19cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/a0709f19cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..9476659 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/a0709f19cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/d0bd9472043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/d0bd9472043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/d0bd9472043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/b030d637cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/4/b030d637cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..587e2b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/b030d637cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,29 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + //public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + public enum FlairClassType + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/607238ae0d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/40/607238ae0d3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/40/607238ae0d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/900a4bdce03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/40/900a4bdce03e001517108d149b613203 new file mode 100644 index 0000000..848fea5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/40/900a4bdce03e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, ) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/50a33590fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/41/50a33590fe3d00151c31fb33fff4337f deleted file mode 100644 index e66301f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/41/50a33590fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,123 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/90a95fb5e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/43/90a95fb5e03e001517108d149b613203 new file mode 100644 index 0000000..af05fcf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/90a95fb5e03e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair ") + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0b51c31023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0b51c31023e00151c31fb33fff4337f deleted file mode 100644 index 73c56cf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0b51c31023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values()) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/3078a0bed23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/44/3078a0bed23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..717cff8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/3078a0bed23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/c0bce577873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/c0bce577873e00151ce8ff6d4f74dabd deleted file mode 100644 index 40b9c4f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/44/c0bce577873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,145 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - s - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/a01312cb303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/46/a01312cb303e00151cbdba8b5f0a340e deleted file mode 100644 index b75c29b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/46/a01312cb303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,425 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/a0e24f1ad43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/46/a0e24f1ad43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..678f6a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/a0e24f1ad43e001518fa86d7ae2a1cf8 @@ -0,0 +1,221 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/209354bad13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/47/209354bad13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..1a55983 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/209354bad13e001518fa86d7ae2a1cf8 @@ -0,0 +1,370 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + if(reload) + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/20a74d8ad13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/47/20a74d8ad13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c0e1cef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/20a74d8ad13e001518fa86d7ae2a1cf8 @@ -0,0 +1,368 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/00204eb0de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/48/00204eb0de3e001517108d149b613203 new file mode 100644 index 0000000..8f9b87a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/00204eb0de3e001517108d149b613203 @@ -0,0 +1,436 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/10d650b8e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/48/10d650b8e03e001517108d149b613203 new file mode 100644 index 0000000..6ad1625 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/10d650b8e03e001517108d149b613203 @@ -0,0 +1,264 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/60516d96843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/60516d96843e00151ce8ff6d4f74dabd deleted file mode 100644 index 55d1b29..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/48/60516d96843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,117 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/203c0b4ad33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/49/203c0b4ad33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..1cec033 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/203c0b4ad33e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/a04d8dc3d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/49/a04d8dc3d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5d7321f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/a04d8dc3d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/b0556e29ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/49/b0556e29ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..26a60f9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/b0556e29ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,355 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/202fd432d93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/202fd432d93e001517108d149b613203 new file mode 100644 index 0000000..a463049 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/202fd432d93e001517108d149b613203 @@ -0,0 +1,228 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/20662b05d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/20662b05d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..63c2d54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/20662b05d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/50f7fce0873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/50f7fce0873e00151ce8ff6d4f74dabd deleted file mode 100644 index a0eb8d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/50f7fce0873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,152 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/70e5ccd7303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/203dd097ce3e001518fa86d7ae2a1cf8 similarity index 70% rename from .metadata/.plugins/org.eclipse.core.resources/.history/4/70e5ccd7303e00151cbdba8b5f0a340e rename to .metadata/.plugins/org.eclipse.core.resources/.history/4b/203dd097ce3e001518fa86d7ae2a1cf8 index 06839a7..4e85b74 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4/70e5ccd7303e00151cbdba8b5f0a340e +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/203dd097ce3e001518fa86d7ae2a1cf8 @@ -21,8 +21,9 @@ import org.bukkit.plugin.java.JavaPlugin; import org.json.JSONArray; import org.json.JSONObject; +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; import com.palmergames.bukkit.towny.object.WorldCoord; public class PluginMain extends JavaPlugin @@ -48,7 +49,7 @@ public class PluginMain extends JavaPlugin while ((line = br.readLine()) != null) { String name=line.replace("\n", ""); - System.out.println("Name: " + name); + //System.out.println("Name: " + name); MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. } br.close(); @@ -77,8 +78,10 @@ public class PluginMain extends JavaPlugin } br.close(); } + //throw new IOException("Test"); //2015.08.09. } catch (IOException e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Thread t=new Thread(r); @@ -100,33 +103,31 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } stop=true; } @@ -154,7 +155,6 @@ public class PluginMain extends JavaPlugin ign = ign.trim(); if(HasIGFlair(ign)) continue; - //System.out.println("Author: "+author); try { Thread.sleep(10); } catch(InterruptedException ex) { @@ -171,7 +171,6 @@ public class PluginMain extends JavaPlugin else flair = "non-presser"; String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); if(flairdata.length>2) flairclass = flairdata[2]; else @@ -183,10 +182,13 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } } } + public static Exception LastException; //2015.08.09. + public String DownloadString(String urlstr) throws MalformedURLException, IOException { URL url = new URL(urlstr); @@ -200,37 +202,18 @@ public class PluginMain extends JavaPlugin return body; } - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ public static Map TownColors=new HashMap(); //2015.07.20. public Boolean HasIGFlair(String playername) { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. return p.Flair!=null; //2015.08.08. } public void SetFlair(String playername, String text, String flairclass, String username) { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. String finalflair; + p.FlairDecided=true; switch(flairclass) { case "press-1": @@ -260,38 +243,24 @@ public class PluginMain extends JavaPlugin case "cant-press": //2015.08.08. finalflair="§r(can't press)§r"; break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; default: finalflair=""; break; } if(finalflair.length()==0) //<-- 2015.07.20. return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. p.Flair=finalflair; //2015.08.08. p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); if(player.getName().equals(playername)) { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); break; } } @@ -299,19 +268,15 @@ public class PluginMain extends JavaPlugin public static String GetFlair(Player player) { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. return flair==null ? "" : flair; } public static void AppendPlayerDisplayFlair(Player player, String username, String flair) { - //if(IgnoredPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) return; - //if(AcceptedPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. else player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); @@ -319,23 +284,18 @@ public class PluginMain extends JavaPlugin public static void AppendPlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //System.out.println("A"); String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); String displayname=player.getName(); //2015.08.08. ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); for(int i=0; i=colors.length) { - //System.out.println("D"); int x=0; for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); try { Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO return town.getName(); @@ -400,10 +348,6 @@ public class PluginMain extends JavaPlugin public static void RemovePlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d0831972873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d0831972873e00151ce8ff6d4f74dabd deleted file mode 100644 index 568b3d5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d0831972873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,144 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/80b8c135e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/80b8c135e13e001517108d149b613203 new file mode 100644 index 0000000..29ccded --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/80b8c135e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/a0ee15dcd13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/a0ee15dcd13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..fb29d4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/a0ee15dcd13e001518fa86d7ae2a1cf8 @@ -0,0 +1,373 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + if(reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/e0491f86883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/e0491f86883e00151ce8ff6d4f74dabd deleted file mode 100644 index 010c5a2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/e0491f86883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,173 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50e0ec42893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50e0ec42893e00151ce8ff6d4f74dabd deleted file mode 100644 index 4e90cb1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50e0ec42893e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,171 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); - SendMessage(player, "Player name: "+p.PlayerName); - SendMessage(player, "Username: "+p.UserName); - SendMessage(player, "Flair accepted: "+p.AcceptedFlair); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/9071ec23e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5/9071ec23e13e001517108d149b613203 new file mode 100644 index 0000000..29ccded --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/9071ec23e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/908d0453dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5/908d0453dd3e001517108d149b613203 new file mode 100644 index 0000000..b2f66e5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/908d0453dd3e001517108d149b613203 @@ -0,0 +1,398 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/506a31a2883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/506a31a2883e00151ce8ff6d4f74dabd deleted file mode 100644 index f163736..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/50/506a31a2883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,173 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/50cc60cf883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/50cc60cf883e00151ce8ff6d4f74dabd deleted file mode 100644 index b8cf010..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/51/50cc60cf883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,170 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/d0e8e8e2873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/d0e8e8e2873e00151ce8ff6d4f74dabd deleted file mode 100644 index 2de7e2d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/53/d0e8e8e2873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,156 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/c0fc6b25cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/55/c0fc6b25cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..6549d0e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/55/c0fc6b25cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,24 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/10a840c1de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/56/10a840c1de3e001517108d149b613203 new file mode 100644 index 0000000..587bd04 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/10a840c1de3e001517108d149b613203 @@ -0,0 +1,437 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + if() + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/205a1cf9d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/56/205a1cf9d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..d7286f9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/205a1cf9d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,374 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { + System.out.println("Reloading files for auto-flair plugin..."); //2015.08.09. + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/d0b1ba48873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/d0b1ba48873e00151ce8ff6d4f74dabd deleted file mode 100644 index 2c6aa0e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/56/d0b1ba48873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,137 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - case " - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/107b898edc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/58/107b898edc3e001517108d149b613203 new file mode 100644 index 0000000..3a0c2b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/58/107b898edc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/90935a89dc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/59/90935a89dc3e001517108d149b613203 new file mode 100644 index 0000000..abf639b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/90935a89dc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/d0ec3c3e893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/d0ec3c3e893e00151ce8ff6d4f74dabd deleted file mode 100644 index f7f1828..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/59/d0ec3c3e893e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,170 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); - SendMessage(player, "Player name: "+p.PlayerName); - SendMessage(player, "Username: "+p.UserName); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/0031cee5e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/0031cee5e03e001517108d149b613203 new file mode 100644 index 0000000..b3a3e05 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/0031cee5e03e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName())) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/109a5eedde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/109a5eedde3e001517108d149b613203 new file mode 100644 index 0000000..fdf80ca --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/109a5eedde3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!RemoveLineFromFile("customflairs.txt")) + { + Commands.SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/80e572e0e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/80e572e0e43e001517108d149b613203 new file mode 100644 index 0000000..f7e15aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/80e572e0e43e001517108d149b613203 @@ -0,0 +1,424 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if(!inputFile.exists()) + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d014d921873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d014d921873e00151ce8ff6d4f74dabd deleted file mode 100644 index dc43ad6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d014d921873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,135 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "" - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/004c7803e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/004c7803e03e001517108d149b613203 new file mode 100644 index 0000000..490906a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/004c7803e03e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/a03b3ea1d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/a03b3ea1d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..3d0defb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/a03b3ea1d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,219 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(); + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0a6c2d0d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0a6c2d0d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5d7321f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0a6c2d0d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0befd81d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0befd81d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..3318744 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a0befd81d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/2061341bd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/2061341bd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ff39b38 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/2061341bd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,375 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/60d7f94a0e3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/60d7f94a0e3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/60d7f94a0e3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0841a72043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0841a72043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0841a72043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0b28923ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0b28923ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..26a60f9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/c0b28923ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,355 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/2029114dd33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6/2029114dd33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..14132d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/2029114dd33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + p.sendMessage(msg); + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/208a8252ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6/208a8252ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..7477f60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/208a8252ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public FlairClassType FlairType; //2015.08.09. + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/a032431fd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6/a032431fd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..b21f1ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/a032431fd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,375 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/104f566fdd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/60/104f566fdd3e001517108d149b613203 new file mode 100644 index 0000000..d462046 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/104f566fdd3e001517108d149b613203 @@ -0,0 +1,400 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/808a5063df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/60/808a5063df3e001517108d149b613203 new file mode 100644 index 0000000..df11079 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/808a5063df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/0023220fe03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/61/0023220fe03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/0023220fe03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/20e39728df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/61/20e39728df3e001517108d149b613203 new file mode 100644 index 0000000..6b55eb2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/20e39728df3e001517108d149b613203 @@ -0,0 +1,251 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/b0779e98d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/61/b0779e98d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f7dee03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/b0779e98d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,205 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/00fd4080df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/62/00fd4080df3e001517108d149b613203 new file mode 100644 index 0000000..a2babdc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/00fd4080df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/502ee671043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/62/502ee671043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/502ee671043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/8065dabd843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/8065dabd843e00151ce8ff6d4f74dabd deleted file mode 100644 index 1b10b37..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/62/8065dabd843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/f0d392d5303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/63/206c2677ce3e001518fa86d7ae2a1cf8 similarity index 69% rename from .metadata/.plugins/org.eclipse.core.resources/.history/97/f0d392d5303e00151cbdba8b5f0a340e rename to .metadata/.plugins/org.eclipse.core.resources/.history/63/206c2677ce3e001518fa86d7ae2a1cf8 index e143040..c13bb09 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/97/f0d392d5303e00151cbdba8b5f0a340e +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/206c2677ce3e001518fa86d7ae2a1cf8 @@ -21,8 +21,9 @@ import org.bukkit.plugin.java.JavaPlugin; import org.json.JSONArray; import org.json.JSONObject; +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; import com.palmergames.bukkit.towny.object.WorldCoord; public class PluginMain extends JavaPlugin @@ -48,7 +49,7 @@ public class PluginMain extends JavaPlugin while ((line = br.readLine()) != null) { String name=line.replace("\n", ""); - System.out.println("Name: " + name); + //System.out.println("Name: " + name); MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. } br.close(); @@ -77,8 +78,10 @@ public class PluginMain extends JavaPlugin } br.close(); } + //throw new IOException("Test"); //2015.08.09. } catch (IOException e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Thread t=new Thread(r); @@ -100,44 +103,36 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } stop=true; } public void ThreadMethod() //<-- 2015.07.16. { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ while(!stop) { try @@ -160,7 +155,6 @@ public class PluginMain extends JavaPlugin ign = ign.trim(); if(HasIGFlair(ign)) continue; - //System.out.println("Author: "+author); try { Thread.sleep(10); } catch(InterruptedException ex) { @@ -177,7 +171,6 @@ public class PluginMain extends JavaPlugin else flair = "non-presser"; String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); if(flairdata.length>2) flairclass = flairdata[2]; else @@ -189,10 +182,13 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } } } + public static Exception LastException; //2015.08.09. + public String DownloadString(String urlstr) throws MalformedURLException, IOException { URL url = new URL(urlstr); @@ -206,37 +202,18 @@ public class PluginMain extends JavaPlugin return body; } - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ public static Map TownColors=new HashMap(); //2015.07.20. public Boolean HasIGFlair(String playername) { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. return p.Flair!=null; //2015.08.08. } public void SetFlair(String playername, String text, String flairclass, String username) { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. String finalflair; + p.FlairDecided=true; switch(flairclass) { case "press-1": @@ -266,38 +243,24 @@ public class PluginMain extends JavaPlugin case "cant-press": //2015.08.08. finalflair="§r(can't press)§r"; break; + case "undecided": //2015.08.09. + p.FlairType=FlairClassType.Undecided; + finalflair=""; + break; default: finalflair=""; break; } if(finalflair.length()==0) //<-- 2015.07.20. return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. p.Flair=finalflair; //2015.08.08. p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); if(player.getName().equals(playername)) { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); break; } } @@ -305,19 +268,15 @@ public class PluginMain extends JavaPlugin public static String GetFlair(Player player) { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. return flair==null ? "" : flair; } public static void AppendPlayerDisplayFlair(Player player, String username, String flair) { - //if(IgnoredPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) return; - //if(AcceptedPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. else player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); @@ -325,23 +284,18 @@ public class PluginMain extends JavaPlugin public static void AppendPlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //System.out.println("A"); String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); String displayname=player.getName(); //2015.08.08. ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); for(int i=0; i=colors.length) { - //System.out.println("D"); int x=0; for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); try { Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO return town.getName(); @@ -406,10 +348,6 @@ public class PluginMain extends JavaPlugin public static void RemovePlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/202fdd3acd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/64/202fdd3acd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..d2a8475 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/202fdd3acd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + //public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + public enum FlairClassType + { + NonPresser, + CantPress, + Undecided + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/20a74791d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/64/20a74791d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5a05c55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/20a74791d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,205 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + return; + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/50d16a24893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/50d16a24893e00151ce8ff6d4f74dabd deleted file mode 100644 index 80c391c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/64/50d16a24893e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,167 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/8096a0dee43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/64/8096a0dee43e001517108d149b613203 new file mode 100644 index 0000000..67c0651 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/8096a0dee43e001517108d149b613203 @@ -0,0 +1,425 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if(!inputFile.exists()) + return true; + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/50973663863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/50973663863e00151ce8ff6d4f74dabd deleted file mode 100644 index 1cce5d8..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/65/50973663863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/d0650077873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/d0650077873e00151ce8ff6d4f74dabd deleted file mode 100644 index 1e3d1d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/66/d0650077873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,145 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/90750be4e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/69/90750be4e43e001517108d149b613203 new file mode 100644 index 0000000..45eac34 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/90750be4e43e001517108d149b613203 @@ -0,0 +1,422 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/20a2e34ad03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/20a2e34ad03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..330b73a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/20a2e34ad03e001518fa86d7ae2a1cf8 @@ -0,0 +1,201 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + s + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/20b08dced03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/20b08dced03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..0384150 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/20b08dced03e001518fa86d7ae2a1cf8 @@ -0,0 +1,211 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/500072fecd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/500072fecd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5740654 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/500072fecd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,351 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6d/30dfaa0fd93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/30dfaa0fd93e001517108d149b613203 new file mode 100644 index 0000000..c2af90a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6d/30dfaa0fd93e001517108d149b613203 @@ -0,0 +1,227 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/a01e1adad13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/a01e1adad13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..fb29d4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/a01e1adad13e001518fa86d7ae2a1cf8 @@ -0,0 +1,373 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + if(reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/b0802cf3de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/b0802cf3de3e001517108d149b613203 new file mode 100644 index 0000000..e679de8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/b0802cf3de3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + Commands.SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/e0394523853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/e0394523853e00151ce8ff6d4f74dabd deleted file mode 100644 index bf1b322..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/e0394523853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/004f0f0ae03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/004f0f0ae03e001517108d149b613203 new file mode 100644 index 0000000..b0c7144 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/004f0f0ae03e001517108d149b613203 @@ -0,0 +1,259 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "playerinfo" + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/308fe70ed93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/308fe70ed93e001517108d149b613203 new file mode 100644 index 0000000..c2af90a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/308fe70ed93e001517108d149b613203 @@ -0,0 +1,227 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7/d0a7c5c8ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/7/d0a7c5c8ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..752a73d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7/d0a7c5c8ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,361 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/90557930e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/70/90557930e13e001517108d149b613203 new file mode 100644 index 0000000..e7f90ce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/90557930e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": //2015.08.09. + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": //2015.08.09. + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": //2015.08.09. + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/b09765e7d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/71/b09765e7d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..738511a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/71/b09765e7d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,373 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/c0147a73043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/71/c0147a73043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/71/c0147a73043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/60069e3b863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/60069e3b863e00151ce8ff6d4f74dabd deleted file mode 100644 index 2b45c26..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/60069e3b863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/601fd2f0013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/72/601fd2f0013e00151c31fb33fff4337f deleted file mode 100644 index 0f9b45b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/601fd2f0013e00151c31fb33fff4337f +++ /dev/null @@ -1,124 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/6080d12b893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/6080d12b893e00151ce8ff6d4f74dabd deleted file mode 100644 index 9d9d757..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/72/6080d12b893e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,168 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/005a2e08e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/73/005a2e08e03e001517108d149b613203 new file mode 100644 index 0000000..49c6680 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/73/005a2e08e03e001517108d149b613203 @@ -0,0 +1,259 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/40821671043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/73/40821671043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/73/40821671043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/50eccfd4e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/73/50eccfd4e43e001517108d149b613203 new file mode 100644 index 0000000..188ed19 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/73/50eccfd4e43e001517108d149b613203 @@ -0,0 +1,421 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/809dfb8ddd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/73/809dfb8ddd3e001517108d149b613203 new file mode 100644 index 0000000..7349211 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/73/809dfb8ddd3e001517108d149b613203 @@ -0,0 +1,408 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("autoflairdecisions.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/a095babcdc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/73/a095babcdc3e001517108d149b613203 new file mode 100644 index 0000000..9c88529 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/73/a095babcdc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair= + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/008b13dedf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/74/008b13dedf3e001517108d149b613203 new file mode 100644 index 0000000..65911d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/008b13dedf3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/10419116e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/74/10419116e13e001517108d149b613203 new file mode 100644 index 0000000..6e1da0e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/10419116e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/8080bcd3e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/74/8080bcd3e43e001517108d149b613203 new file mode 100644 index 0000000..eee5b38 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/8080bcd3e43e001517108d149b613203 @@ -0,0 +1,422 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/200e61c5d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/75/200e61c5d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..03f8e7b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/200e61c5d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,209 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/20bc5301d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/75/20bc5301d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..7e0571a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/20bc5301d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,222 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/a0acffa2d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/75/a0acffa2d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2b15b31 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/a0acffa2d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,368 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/00ad7695df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/77/00ad7695df3e001517108d149b613203 new file mode 100644 index 0000000..e36fb60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/00ad7695df3e001517108d149b613203 @@ -0,0 +1,421 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/10dec4cbce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/77/10dec4cbce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5908a1d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/10dec4cbce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,360 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/50df6a0f313e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/77/50df6a0f313e00151cbdba8b5f0a340e deleted file mode 100644 index 1cd84d0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/77/50df6a0f313e00151cbdba8b5f0a340e +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/40118f77873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/40118f77873e00151ce8ff6d4f74dabd deleted file mode 100644 index 68ec621..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/78/40118f77873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,145 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - w - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/804750c0843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/804750c0843e00151ce8ff6d4f74dabd deleted file mode 100644 index cf9d482..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/78/804750c0843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.08. - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/a0991cb7d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/78/a0991cb7d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2a4b609 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/a0991cb7d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,370 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + if(reload) + MaybeOfflinePlayer.AllPlayers.clear(); + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/106a6cf8dc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/79/106a6cf8dc3e001517108d149b613203 new file mode 100644 index 0000000..56c16c3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/106a6cf8dc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/4046a21a013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/4046a21a013e00151c31fb33fff4337f deleted file mode 100644 index c604fd3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/4046a21a013e00151c31fb33fff4337f +++ /dev/null @@ -1,123 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c081221f023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c081221f023e00151c31fb33fff4337f deleted file mode 100644 index 7a8d627..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/c081221f023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer : MaybeOfflinePlayer.AllPlayers.values()) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/3014764dcd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/3014764dcd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c8a5a26 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/3014764dcd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + //public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/a0934f14cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/a0934f14cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ad8a270 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/a0934f14cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10c43d1bdf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10c43d1bdf3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/10c43d1bdf3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/a005617fd13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/a005617fd13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..dbcf375 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/a005617fd13e001518fa86d7ae2a1cf8 @@ -0,0 +1,363 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/001a1675df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/001a1675df3e001517108d149b613203 new file mode 100644 index 0000000..2a1ad93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/001a1675df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2093d427df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2093d427df3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2093d427df3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/9025b8a1de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/9025b8a1de3e001517108d149b613203 new file mode 100644 index 0000000..f0d5af2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/9025b8a1de3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/e0936b2f853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/e0936b2f853e00151ce8ff6d4f74dabd deleted file mode 100644 index 4fc0380..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8/e0936b2f853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,129 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoShowErrors(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/00713b62df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/80/00713b62df3e001517108d149b613203 new file mode 100644 index 0000000..bfee7cd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/00713b62df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "") + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/4075d399d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/80/4075d399d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..8abed8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/4075d399d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,206 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/50a96102873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/50a96102873e00151ce8ff6d4f74dabd deleted file mode 100644 index 2290b1a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/80/50a96102873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,135 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/b0e3d39bd13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/81/b0e3d39bd13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..81f3985 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/b0e3d39bd13e001518fa86d7ae2a1cf8 @@ -0,0 +1,326 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/00f1d2ebde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/82/00f1d2ebde3e001517108d149b613203 new file mode 100644 index 0000000..f007b97 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/00f1d2ebde3e001517108d149b613203 @@ -0,0 +1,228 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/201f90d5d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/82/201f90d5d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..0384150 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/201f90d5d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,211 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/80cc011bdf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/82/80cc011bdf3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/80cc011bdf3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/20a1e315cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/83/20a1e315cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..72f8cf3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/20a1e315cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/90fff2badd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/83/90fff2badd3e001517108d149b613203 new file mode 100644 index 0000000..777c16f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/90fff2badd3e001517108d149b613203 @@ -0,0 +1,411 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09., String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/a0c00f13cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/84/a0c00f13cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..999a5ae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/a0c00f13cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/f037c8f6843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/f037c8f6843e00151ce8ff6d4f74dabd deleted file mode 100644 index 9f42545..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/85/f037c8f6843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/e024da2a853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/e024da2a853e00151ce8ff6d4f74dabd deleted file mode 100644 index 2e97c39..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/86/e024da2a853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,129 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoShowErrors(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/101b2028df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/88/101b2028df3e001517108d149b613203 new file mode 100644 index 0000000..5f009d8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/101b2028df3e001517108d149b613203 @@ -0,0 +1,251 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + return + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/e001984d0e3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/88/e001984d0e3e00151089cca86c5b1a99 deleted file mode 100644 index e7381d7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/88/e001984d0e3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/80137592e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/89/80137592e43e001517108d149b613203 new file mode 100644 index 0000000..69b0f0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/80137592e43e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + //String flair=p.Flair; //2015.08.08. + //PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + //String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/00d17baede3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/00d17baede3e001517108d149b613203 new file mode 100644 index 0000000..fe096bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/00d17baede3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60d47714883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60d47714883e00151ce8ff6d4f74dabd deleted file mode 100644 index 31f7f4b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60d47714883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,162 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a0ea662dcf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a0ea662dcf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..1213563 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a0ea662dcf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/d09f3274873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/d09f3274873e00151ce8ff6d4f74dabd deleted file mode 100644 index 1e3d1d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/d09f3274873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,145 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00c5f1e2e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00c5f1e2e03e001517108d149b613203 new file mode 100644 index 0000000..897aaab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/00c5f1e2e03e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName())) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/20aae3d4de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/20aae3d4de3e001517108d149b613203 new file mode 100644 index 0000000..4d9e4b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/20aae3d4de3e001517108d149b613203 @@ -0,0 +1,440 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!RemoveLineFromFile("customflairs.txt")) + { + + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8048b1b4dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8048b1b4dd3e001517108d149b613203 new file mode 100644 index 0000000..8e05cb9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8048b1b4dd3e001517108d149b613203 @@ -0,0 +1,403 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/90fd528ee33e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/90fd528ee33e001517108d149b613203 new file mode 100644 index 0000000..2a14b51 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/90fd528ee33e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/d0631404023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/d0631404023e00151c31fb33fff4337f deleted file mode 100644 index ab9966d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/d0631404023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/9034fdd8df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/9034fdd8df3e001517108d149b613203 new file mode 100644 index 0000000..a3f007b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/9034fdd8df3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + SetFlair() + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/10ab8866df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/9/10ab8866df3e001517108d149b613203 new file mode 100644 index 0000000..2a1ad93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/10ab8866df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/0009f78bdf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/91/0009f78bdf3e001517108d149b613203 new file mode 100644 index 0000000..c9f322f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/0009f78bdf3e001517108d149b613203 @@ -0,0 +1,421 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/d0078548033e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/91/c01c0d9ecf3e001518fa86d7ae2a1cf8 similarity index 67% rename from .metadata/.plugins/org.eclipse.core.resources/.history/3b/d0078548033e00151c31fb33fff4337f rename to .metadata/.plugins/org.eclipse.core.resources/.history/91/c01c0d9ecf3e001518fa86d7ae2a1cf8 index e51b439..a085586 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/d0078548033e00151c31fb33fff4337f +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/c01c0d9ecf3e001518fa86d7ae2a1cf8 @@ -15,43 +15,35 @@ public class PlayerListener implements Listener public void onPlayerJoin(PlayerJoinEvent event) { Player p=event.getPlayer(); - //PluginMain.Players.add(p); - //event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p)); - //if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20. - //PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p)); - if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08. - + MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); //2015.08.08. + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); //2015.08.08. + if(mp.Flair!=null) + PluginMain.AppendPlayerDisplayFlair(p, mp.UserName, mp.Flair); else { //2015.07.20. String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]"; sendRawMessage(p, json); } - //System.out.println("Added player "+p.getName()); } @EventHandler public void onPlayerLeave(PlayerQuitEvent event) { - //for(Player player : PluginMain.Players) - /*for(int i=0; i"))+"(test)> "+event.getMessage()); //2015.08.08. - String name; - event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08. + MaybeOfflinePlayer player = MaybeOfflinePlayer.AllPlayers.get(event.getPlayer().getName()); + String flair=player.Flair; //2015.08.08. + if(flair==null || !player.AcceptedFlair) + flair=""; //2015.08.08. + String message=event.getMessage(); //2015.08.08. + /*for(Player p : PluginMain.GetPlayers()) + { //2015.08.08. + message.replaceAll(p.getName(), ) + }*/ + event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+message); //2015.08.08. } private static Class nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer"); @@ -72,6 +64,7 @@ public class PlayerListener implements Listener System.out.println("6"); } catch (Exception e) { e.printStackTrace(); + PluginMain.LastException=e; //2015.08.09. } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/d0efd7c1fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/92/d0efd7c1fe3d00151c31fb33fff4337f deleted file mode 100644 index 36d406a..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/92/d0efd7c1fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/10f3fc0acf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/93/10f3fc0acf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..a62c891 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/10f3fc0acf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,361 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f07864d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f07864d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f15dd12 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f07864d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,376 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/2033b6d6d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/94/2033b6d6d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..6ac02e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/2033b6d6d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,373 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + System.out.println("Loading files for auto-flair plugin..."); + if(reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/602d1cd1873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/602d1cd1873e00151ce8ff6d4f74dabd deleted file mode 100644 index 1ecfa95..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/94/602d1cd1873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,152 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==args[2]) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/605f63960d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/95/605f63960d3e00151089cca86c5b1a99 deleted file mode 100644 index 9aaee42..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/605f63960d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,436 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - System.out.println("PlayerName: "+player.getName()); - System.out.println(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair); - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/70d8d329853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/70d8d329853e00151ce8ff6d4f74dabd deleted file mode 100644 index b0f40f6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/95/70d8d329853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,127 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoShowErrors(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/20ba5e09d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/96/20ba5e09d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..6a40e1a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/20ba5e09d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,219 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/90d42e62e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/96/90d42e62e43e001517108d149b613203 new file mode 100644 index 0000000..e7b6525 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/90d42e62e43e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + //PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/20ad60e0d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/97/20ad60e0d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..4c2bf02 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/20ad60e0d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,211 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="" + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/8035b5d5df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/97/8035b5d5df3e001517108d149b613203 new file mode 100644 index 0000000..2377b00 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/8035b5d5df3e001517108d149b613203 @@ -0,0 +1,253 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case " + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0aae0bdd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0aae0bdd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..229de7d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0aae0bdd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,223 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/c0e2ad6fce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/97/c0e2ad6fce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..28be51a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/c0e2ad6fce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,357 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairType=FlairClassType.Undecided; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/201af098d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/98/201af098d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f7dee03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/201af098d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,205 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/308353a4d93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/99/308353a4d93e001517108d149b613203 new file mode 100644 index 0000000..4ad7ffc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/308353a4d93e001517108d149b613203 @@ -0,0 +1,228 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/802cbb44dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/802cbb44dd3e001517108d149b613203 new file mode 100644 index 0000000..2d5bf44 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/802cbb44dd3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/90dce6d5ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/90dce6d5ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f48f1b4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/90dce6d5ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,361 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0aebdacd03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0aebdacd03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..8abed8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0aebdacd03e001518fa86d7ae2a1cf8 @@ -0,0 +1,206 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/0068cab7e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/0068cab7e03e001517108d149b613203 new file mode 100644 index 0000000..1a68137 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/0068cab7e03e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/30bb8899d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/30bb8899d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2e695ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/30bb8899d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,326 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + Load(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/50e1b4b6883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/50e1b4b6883e00151ce8ff6d4f74dabd deleted file mode 100644 index 6c54b93..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/50e1b4b6883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,173 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; -SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/705c51af0d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/a/705c51af0d3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/705c51af0d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/c0191a3f023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/a/c0191a3f023e00151c31fb33fff4337f deleted file mode 100644 index 772f7ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a/c0191a3f023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values()) - { - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f08cfd11853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f08cfd11853e00151ce8ff6d4f74dabd deleted file mode 100644 index abbb5cd..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/f08cfd11853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,120 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/009afe94e33e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/009afe94e33e001517108d149b613203 new file mode 100644 index 0000000..c4b45ca --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/009afe94e33e001517108d149b613203 @@ -0,0 +1,422 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1035b42cd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1035b42cd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..3051444 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1035b42cd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,376 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1076adccde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1076adccde3e001517108d149b613203 new file mode 100644 index 0000000..697a10d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/1076adccde3e001517108d149b613203 @@ -0,0 +1,436 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/a05e6526d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/a05e6526d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..7d30f89 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/a05e6526d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,375 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/206c0ba2d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/206c0ba2d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..23364ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/206c0ba2d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,222 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/20ecb1f0d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/20ecb1f0d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..41fefc9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/20ecb1f0d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,211 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/900097abde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/900097abde3e001517108d149b613203 new file mode 100644 index 0000000..e1431c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/900097abde3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/00bc291adf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/00bc291adf3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/00bc291adf3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/e09f3eb6883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/e09f3eb6883e00151ce8ff6d4f74dabd deleted file mode 100644 index f48b09f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/e09f3eb6883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,177 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/901a3493e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/901a3493e03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/901a3493e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a0f428a3d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a0f428a3d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..72b38bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a0f428a3d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,219 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/80c576a3de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/80c576a3de3e001517108d149b613203 new file mode 100644 index 0000000..fe096bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/80c576a3de3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/20f25379dc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/20f25379dc3e001517108d149b613203 new file mode 100644 index 0000000..4d40cf6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/20f25379dc3e001517108d149b613203 @@ -0,0 +1,382 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/006bbef3df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/006bbef3df3e001517108d149b613203 new file mode 100644 index 0000000..88ceb0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/006bbef3df3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void DoSetFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10465292e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10465292e03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/10465292e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/a053b075de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/a053b075de3e001517108d149b613203 new file mode 100644 index 0000000..592cb5a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/a053b075de3e001517108d149b613203 @@ -0,0 +1,429 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + {File inputFile = new File("myFile.txt"); + File tempFile = new File("myTempFile.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/40e5b0ce303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/40e5b0ce303e00151cbdba8b5f0a340e deleted file mode 100644 index abca588..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/40e5b0ce303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,423 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60ef3b14313e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60ef3b14313e00151cbdba8b5f0a340e deleted file mode 100644 index d63a548..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/60ef3b14313e00151cbdba8b5f0a340e +++ /dev/null @@ -1,118 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40db9018d93e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40db9018d93e001517108d149b613203 new file mode 100644 index 0000000..c2af90a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40db9018d93e001517108d149b613203 @@ -0,0 +1,227 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer!=null && ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a076f09ad33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a076f09ad33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..cb3725d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a076f09ad33e001518fa86d7ae2a1cf8 @@ -0,0 +1,218 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/5065ec8ffe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/5065ec8ffe3d00151c31fb33fff4337f deleted file mode 100644 index 51a39bf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/5065ec8ffe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f0586fbb843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f0586fbb843e00151ce8ff6d4f74dabd deleted file mode 100644 index 4138abb..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/f0586fbb843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,117 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/00f387b8dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/00f387b8dd3e001517108d149b613203 new file mode 100644 index 0000000..2f2582c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/00f387b8dd3e001517108d149b613203 @@ -0,0 +1,409 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/60b39408d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/60b39408d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..05f66ff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/60b39408d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,220 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done."); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + DoReload(player); //2015.08.09. + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/602947d9303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/c0954ebcce3e001518fa86d7ae2a1cf8 similarity index 70% rename from .metadata/.plugins/org.eclipse.core.resources/.history/13/602947d9303e00151cbdba8b5f0a340e rename to .metadata/.plugins/org.eclipse.core.resources/.history/ad/c0954ebcce3e001518fa86d7ae2a1cf8 index 29ec32a..2284d68 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/13/602947d9303e00151cbdba8b5f0a340e +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/c0954ebcce3e001518fa86d7ae2a1cf8 @@ -21,8 +21,9 @@ import org.bukkit.plugin.java.JavaPlugin; import org.json.JSONArray; import org.json.JSONObject; +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; import com.palmergames.bukkit.towny.object.WorldCoord; public class PluginMain extends JavaPlugin @@ -48,7 +49,7 @@ public class PluginMain extends JavaPlugin while ((line = br.readLine()) != null) { String name=line.replace("\n", ""); - System.out.println("Name: " + name); + //System.out.println("Name: " + name); MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. } br.close(); @@ -77,8 +78,10 @@ public class PluginMain extends JavaPlugin } br.close(); } + //throw new IOException("Test"); //2015.08.09. } catch (IOException e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Thread t=new Thread(r); @@ -100,33 +103,31 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } stop=true; } @@ -170,7 +171,6 @@ public class PluginMain extends JavaPlugin else flair = "non-presser"; String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); if(flairdata.length>2) flairclass = flairdata[2]; else @@ -182,10 +182,13 @@ public class PluginMain extends JavaPlugin catch(Exception e) { System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. } } } + public static Exception LastException; //2015.08.09. + public String DownloadString(String urlstr) throws MalformedURLException, IOException { URL url = new URL(urlstr); @@ -199,37 +202,18 @@ public class PluginMain extends JavaPlugin return body; } - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ public static Map TownColors=new HashMap(); //2015.07.20. public Boolean HasIGFlair(String playername) { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. return p.Flair!=null; //2015.08.08. } public void SetFlair(String playername, String text, String flairclass, String username) { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. String finalflair; + p.FlairDecided=true; switch(flairclass) { case "press-1": @@ -259,38 +243,24 @@ public class PluginMain extends JavaPlugin case "cant-press": //2015.08.08. finalflair="§r(can't press)§r"; break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; default: finalflair=""; break; } if(finalflair.length()==0) //<-- 2015.07.20. return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. p.Flair=finalflair; //2015.08.08. p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); if(player.getName().equals(playername)) { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); break; } } @@ -298,43 +268,37 @@ public class PluginMain extends JavaPlugin public static String GetFlair(Player player) { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. return flair==null ? "" : flair; } public static void AppendPlayerDisplayFlair(Player player, String username, String flair) { - //if(IgnoredPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) return; - //if(AcceptedPlayers.contains(player.getName())) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); + { AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + + } else player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); } public static void AppendPlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //System.out.println("A"); String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); String displayname=player.getName(); //2015.08.08. ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); for(int i=0; i=colors.length) { - //System.out.println("D"); int x=0; for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); try { Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO return town.getName(); @@ -399,10 +351,6 @@ public class PluginMain extends JavaPlugin public static void RemovePlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d05bdac3883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d05bdac3883e00151ce8ff6d4f74dabd deleted file mode 100644 index b1f2c2b..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d05bdac3883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,170 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/d0a0364f893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/1023a9f9cc3e001518fa86d7ae2a1cf8 similarity index 86% rename from .metadata/.plugins/org.eclipse.core.resources/.history/9e/d0a0364f893e00151ce8ff6d4f74dabd rename to .metadata/.plugins/org.eclipse.core.resources/.history/ae/1023a9f9cc3e001518fa86d7ae2a1cf8 index 95365e7..21abae7 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/d0a0364f893e00151ce8ff6d4f74dabd +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/1023a9f9cc3e001518fa86d7ae2a1cf8 @@ -67,11 +67,13 @@ public class Commands implements CommandExecutor { } return true; } - /*if(args[0].toLowerCase()=="reload") DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } return false; } private static void DoReload(Player player) @@ -111,6 +113,7 @@ public class Commands implements CommandExecutor { System.out.println("Error!\n"+e); if(player!=null) player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. } //} //else @@ -120,6 +123,7 @@ public class Commands implements CommandExecutor { { //2015.08.09. if(player==null || player.isOp() || player.getName()=="NorbiPeti") { + //System.out.println("Args length: " + args.length); if(args.length==1) { String message="§cUsage: /u admin reload|playerinfo§r"; @@ -135,6 +139,12 @@ public class Commands implements CommandExecutor { case "playerinfo": DoPlayerInfo(player, args); break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; } } else @@ -147,20 +157,20 @@ public class Commands implements CommandExecutor { { String message="§cUsage: /u admin playerinfo §r"; SendMessage(player, message); + return; } if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) { String message="§cPlayer not found: "+args[2]+"§r"; SendMessage(player, message); + return; } MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Username: "+p.UserName); SendMessage(player, "Flair accepted: "+p.AcceptedFlair); SendMessage(player, "Flair ignored: "+p.IgnoredFlair); - SendMessage(player, "Flair accepted: "+p.AcceptedFlair); - return; } private static void SendMessage(Player player, String message) { //2015.08.09. @@ -168,6 +178,17 @@ public class Commands implements CommandExecutor { System.out.println(message); else player.sendMessage(message); - return; + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/80773b20e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/80773b20e13e001517108d149b613203 new file mode 100644 index 0000000..c4f42b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/80773b20e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/80d63604df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/af/80d63604df3e001517108d149b613203 new file mode 100644 index 0000000..9e33acb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/80d63604df3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + public static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/301da326cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b/301da326cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..3d05e25 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/301da326cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,25 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/20248930cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/20248930cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..a73501d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/20248930cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,25 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + //public + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30440676d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30440676d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c6af7c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30440676d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,201 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/100a9853d43e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/100a9853d43e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..9a2577a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/100a9853d43e001518fa86d7ae2a1cf8 @@ -0,0 +1,224 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/c04f9e03ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/c04f9e03ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..68717ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/c04f9e03ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,354 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided":; //2015.08.09. + finalflair="" + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/90dba275dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/90dba275dd3e001517108d149b613203 new file mode 100644 index 0000000..88d98b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/90dba275dd3e001517108d149b613203 @@ -0,0 +1,408 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/202ce194d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/202ce194d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2e695ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/202ce194d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,326 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + Load(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/40ecd766ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/40ecd766ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..39e5134 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/40ecd766ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public boolean FlairSet; //2015.08.09. + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + /*public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + }*/ +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c0ca4f18013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c0ca4f18013e00151c31fb33fff4337f deleted file mode 100644 index 664f96d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c0ca4f18013e00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/703eba51893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20667af5cf3e001518fa86d7ae2a1cf8 similarity index 83% rename from .metadata/.plugins/org.eclipse.core.resources/.history/c5/703eba51893e00151ce8ff6d4f74dabd rename to .metadata/.plugins/org.eclipse.core.resources/.history/b6/20667af5cf3e001518fa86d7ae2a1cf8 index cbeaea2..eab3194 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/703eba51893e00151ce8ff6d4f74dabd +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20667af5cf3e001518fa86d7ae2a1cf8 @@ -33,7 +33,8 @@ public class Commands implements CommandExecutor { if(!p.AcceptedFlair) { String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); p.AcceptedFlair=true; //2015.08.08. player.sendMessage("§6Your flair has been set:§r "+flair); } @@ -62,16 +63,20 @@ public class Commands implements CommandExecutor { case "admin": //2015.08.09. DoAdmin(player, args); break; + case "nonpresser": //2015.08.09. + break; default: return false; } return true; } - /*if(args[0].toLowerCase()=="reload") DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } return false; } private static void DoReload(Player player) @@ -111,6 +116,7 @@ public class Commands implements CommandExecutor { System.out.println("Error!\n"+e); if(player!=null) player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. } //} //else @@ -120,6 +126,7 @@ public class Commands implements CommandExecutor { { //2015.08.09. if(player==null || player.isOp() || player.getName()=="NorbiPeti") { + //System.out.println("Args length: " + args.length); if(args.length==1) { String message="§cUsage: /u admin reload|playerinfo§r"; @@ -135,6 +142,12 @@ public class Commands implements CommandExecutor { case "playerinfo": DoPlayerInfo(player, args); break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; } } else @@ -147,19 +160,20 @@ public class Commands implements CommandExecutor { { String message="§cUsage: /u admin playerinfo §r"; SendMessage(player, message); + return; } if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) { String message="§cPlayer not found: "+args[2]+"§r"; SendMessage(player, message); + return; } MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Username: "+p.UserName); SendMessage(player, "Flair accepted: "+p.AcceptedFlair); SendMessage(player, "Flair ignored: "+p.IgnoredFlair); - return; } private static void SendMessage(Player player, String message) { //2015.08.09. @@ -167,6 +181,17 @@ public class Commands implements CommandExecutor { System.out.println(message); else player.sendMessage(message); - return; + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/504cf350863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/504cf350863e00151ce8ff6d4f74dabd deleted file mode 100644 index 677ad35..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/504cf350863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoAdmin(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/90a348520c3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/90a348520c3e00151089cca86c5b1a99 deleted file mode 100644 index 87c9d49..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/90a348520c3e00151089cca86c5b1a99 +++ /dev/null @@ -1,432 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/20f6378ed03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/20f6378ed03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..d6e50b3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/20f6378ed03e001518fa86d7ae2a1cf8 @@ -0,0 +1,204 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/b048b351cd3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/b048b351cd3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..4b9e4c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/b048b351cd3e001518fa86d7ae2a1cf8 @@ -0,0 +1,31 @@ +package tk.sznp.thebuttonautoflair; + +import java.util.HashMap; + +public class MaybeOfflinePlayer +{ //2015.08.08. + public String PlayerName; + public String UserName; + public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not + public boolean AcceptedFlair; + public boolean IgnoredFlair; + public FlairClassType FlairType; + public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. + public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) + { + if(!AllPlayers.containsKey(playername)) + { + MaybeOfflinePlayer player=new MaybeOfflinePlayer(); + player.PlayerName=playername; + AllPlayers.put(playername, player); + return player; + } + return AllPlayers.get(playername); + } + public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/5051ef26e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/5051ef26e13e001517108d149b613203 new file mode 100644 index 0000000..f0829ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/5051ef26e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": //2015.08.09. + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0096b9fee03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0096b9fee03e001517108d149b613203 new file mode 100644 index 0000000..8b30515 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0096b9fee03e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName())) + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/409c8390fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/409c8390fe3d00151c31fb33fff4337f deleted file mode 100644 index 51a39bf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/409c8390fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/50eb23fc013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/50eb23fc013e00151c31fb33fff4337f deleted file mode 100644 index 89e8084..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/b9/50eb23fc013e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.)) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/10207fd7df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/10207fd7df3e001517108d149b613203 new file mode 100644 index 0000000..2e8d713 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/10207fd7df3e001517108d149b613203 @@ -0,0 +1,254 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/70faf280883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/70faf280883e00151ce8ff6d4f74dabd deleted file mode 100644 index dc9734e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/70faf280883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,172 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/60b5d072043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/60b5d072043e00151c31fb33fff4337f deleted file mode 100644 index cd04256..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/60b5d072043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());; - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/30916f57cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/30916f57cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..262b029 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/30916f57cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,362 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a0204038ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a0204038ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..d838749 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a0204038ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,355 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a099c9afd13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a099c9afd13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..3926be9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/a099c9afd13e001518fa86d7ae2a1cf8 @@ -0,0 +1,368 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/907ef589dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/907ef589dd3e001517108d149b613203 new file mode 100644 index 0000000..6d2cbea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/907ef589dd3e001517108d149b613203 @@ -0,0 +1,408 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("autoflairdecisions.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d57cb4de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d57cb4de3e001517108d149b613203 new file mode 100644 index 0000000..9b5ff96 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d57cb4de3e001517108d149b613203 @@ -0,0 +1,436 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d70867df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d70867df3e001517108d149b613203 new file mode 100644 index 0000000..2a1ad93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/90d70867df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/d02b22e3883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/d02b22e3883e00151ce8ff6d4f74dabd deleted file mode 100644 index 80c391c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/d02b22e3883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,167 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - SendMessage(player, message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - SendMessage(player, message); - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - SendMessage(player, message); - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { //2015.08.09. - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/be/d0face78043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/be/d0face78043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/be/d0face78043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/60a98f74863e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/60a98f74863e00151ce8ff6d4f74dabd deleted file mode 100644 index a18a6c0..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/60a98f74863e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,131 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a056849fd33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a056849fd33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ef4123d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a056849fd33e001518fa86d7ae2a1cf8 @@ -0,0 +1,219 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a0d1b862cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a0d1b862cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..0fdd3aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/a0d1b862cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,364 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press?§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/00f451f0df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c/00f451f0df3e001517108d149b613203 new file mode 100644 index 0000000..6f03003 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/00f451f0df3e001517108d149b613203 @@ -0,0 +1,255 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void DoSetFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/b06ccd99ce3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/c/b06ccd99ce3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..4e85b74 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/b06ccd99ce3e001518fa86d7ae2a1cf8 @@ -0,0 +1,358 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + { + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) + AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/804c83d7e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/804c83d7e43e001517108d149b613203 new file mode 100644 index 0000000..45eac34 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/804c83d7e43e001517108d149b613203 @@ -0,0 +1,422 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/20d96d9ad33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/20d96d9ad33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..843f877 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/20d96d9ad33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/30656a8fdc3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/30656a8fdc3e001517108d149b613203 new file mode 100644 index 0000000..ee5ce1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/30656a8fdc3e001517108d149b613203 @@ -0,0 +1,394 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/103cea7ede3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/103cea7ede3e001517108d149b613203 new file mode 100644 index 0000000..e1f9c1a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/103cea7ede3e001517108d149b613203 @@ -0,0 +1,430 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("myTempFile.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50421721013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50421721013e00151c31fb33fff4337f deleted file mode 100644 index da1f3aa..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/50421721013e00151c31fb33fff4337f +++ /dev/null @@ -1,123 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if() - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0d4e9c9873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0d4e9c9873e00151ce8ff6d4f74dabd deleted file mode 100644 index 007de20..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0d4e9c9873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,148 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==args[2]) - { - String message="§cUsage: /u admin playerinfo §r"; - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/30315643d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/30315643d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..a8dea0e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/30315643d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,198 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/9087c2e9de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/9087c2e9de3e001517108d149b613203 new file mode 100644 index 0000000..9ee3630 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/9087c2e9de3e001517108d149b613203 @@ -0,0 +1,440 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!RemoveLineFromFile("customflairs.txt")) + { + Commands.SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/e0bc896e043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/e0bc896e043e00151c31fb33fff4337f deleted file mode 100644 index 5cd2e80..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c5/e0bc896e043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()) - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/50d2a147883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/50d2a147883e00151ce8ff6d4f74dabd deleted file mode 100644 index 9f90e23..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/50d2a147883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,172 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/c0dc5f8ffe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/c0dc5f8ffe3d00151c31fb33fff4337f deleted file mode 100644 index 02d5705..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/c0dc5f8ffe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/d022346e043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/d022346e043e00151c31fb33fff4337f deleted file mode 100644 index 8ca009c..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/d022346e043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player player : PluginMain.GetPlayers()) - { - - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/20873c35da3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/20873c35da3e001517108d149b613203 new file mode 100644 index 0000000..e279910 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/20873c35da3e001517108d149b613203 @@ -0,0 +1,380 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/30e525880d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/30e525880d3e00151089cca86c5b1a99 deleted file mode 100644 index 2c323a3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/30e525880d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,435 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - System.out.println("PlayerName: "+player.getName()); - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d05586b7fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d05586b7fe3d00151c31fb33fff4337f deleted file mode 100644 index 51a39bf..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d05586b7fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/e0273ad2843e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/e0273ad2843e00151ce8ff6d4f74dabd deleted file mode 100644 index 8f8c62d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/e0273ad2843e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/f00aad3f023e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/f00aad3f023e00151c31fb33fff4337f deleted file mode 100644 index 02b2d98..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/f00aad3f023e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values()) - { - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/00d17cf2e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/00d17cf2e43e001517108d149b613203 new file mode 100644 index 0000000..650e18a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/00d17cf2e43e001517108d149b613203 @@ -0,0 +1,425 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if(!inputFile.exists()) + return true; //2015.08.09. + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/005c8f6bdd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/005c8f6bdd3e001517108d149b613203 new file mode 100644 index 0000000..a826a4b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/005c8f6bdd3e001517108d149b613203 @@ -0,0 +1,399 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b1d9a5883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b1d9a5883e00151ce8ff6d4f74dabd deleted file mode 100644 index cb9cbd5..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b1d9a5883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,177 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - return; - } - private static void SendMessage(Player player, String message) - { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/00f60a63df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/00f60a63df3e001517108d149b613203 new file mode 100644 index 0000000..df11079 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/00f60a63df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d058f890873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d058f890873e00151ce8ff6d4f74dabd deleted file mode 100644 index 1e3d1d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/d058f890873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,145 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10d57c5c0d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10d57c5c0d3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10d57c5c0d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/b08607c4d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/b08607c4d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5d7321f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/b08607c4d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/804ab462df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/804ab462df3e001517108d149b613203 new file mode 100644 index 0000000..df11079 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/804ab462df3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+player.getName()+" Flair: "+flair); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/90837b3cd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/90837b3cd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..e95109b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/90837b3cd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,377 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/000b95b8e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/000b95b8e03e001517108d149b613203 new file mode 100644 index 0000000..6ad1625 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/000b95b8e03e001517108d149b613203 @@ -0,0 +1,264 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/d00a337efe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/d00a337efe3d00151c31fb33fff4337f deleted file mode 100644 index 5660588..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/d00a337efe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.IgnoredFlair) - { - PluginMain.IgnoredPlayers.add(player.getName()); - //String dname=player.getDisplayName(); - String flair=PluginMain.PlayerFlairs.get(player.getName()); - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/00fa3acadf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/00fa3acadf3e001517108d149b613203 new file mode 100644 index 0000000..60eeadd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/00fa3acadf3e001517108d149b613203 @@ -0,0 +1,252 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/10d8879fde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/10d8879fde3e001517108d149b613203 new file mode 100644 index 0000000..f0d5af2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/10d8879fde3e001517108d149b613203 @@ -0,0 +1,435 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static bool RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/509e01bffe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/509e01bffe3d00151c31fb33fff4337f deleted file mode 100644 index 6224020..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/509e01bffe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/204ad69bd03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/204ad69bd03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..331b6b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/204ad69bd03e001518fa86d7ae2a1cf8 @@ -0,0 +1,206 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + w + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/d0f664fd013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/d0f664fd013e00151c31fb33fff4337f deleted file mode 100644 index b72a527..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/d0f664fd013e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair=="") - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/00e70d14e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/00e70d14e03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/00e70d14e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/008e353cdf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/008e353cdf3e001517108d149b613203 new file mode 100644 index 0000000..6b55eb2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/008e353cdf3e001517108d149b613203 @@ -0,0 +1,251 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/50049d01883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/50049d01883e00151ce8ff6d4f74dabd deleted file mode 100644 index ef471ff..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/50049d01883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,162 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/9032a867e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/9032a867e43e001517108d149b613203 new file mode 100644 index 0000000..0ed8dd4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/9032a867e43e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + //PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0038bc1adf3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0038bc1adf3e001517108d149b613203 new file mode 100644 index 0000000..eac777b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0038bc1adf3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "Error removing previous custom flair!"); + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/80ae4e03e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/80ae4e03e13e001517108d149b613203 new file mode 100644 index 0000000..9e49651 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/80ae4e03e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/a08b7a0ed03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/a08b7a0ed03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5d70e40 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/a08b7a0ed03e001518fa86d7ae2a1cf8 @@ -0,0 +1,198 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/e0a5b112313e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/e0a5b112313e00151cbdba8b5f0a340e deleted file mode 100644 index afdf0ca..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/e0a5b112313e00151cbdba8b5f0a340e +++ /dev/null @@ -1,120 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/5064fb90fe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/5064fb90fe3d00151c31fb33fff4337f deleted file mode 100644 index e66301f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/5064fb90fe3d00151c31fb33fff4337f +++ /dev/null @@ -1,123 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/a0f61d9bd03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/a0f61d9bd03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..8abed8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/a0f61d9bd03e001518fa86d7ae2a1cf8 @@ -0,0 +1,206 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/c0293873043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/c0293873043e00151c31fb33fff4337f deleted file mode 100644 index cd04256..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/c0293873043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());; - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/e0a05c04853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/e0a05c04853e00151ce8ff6d4f74dabd deleted file mode 100644 index 9f42545..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/e0a05c04853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,119 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/f0762d6f043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/f0762d6f043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/f0762d6f043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/a0d614ced03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/da/a0d614ced03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..a8b8f80 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/a0d614ced03e001518fa86d7ae2a1cf8 @@ -0,0 +1,210 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/f088765c0c3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/da/f088765c0c3e00151089cca86c5b1a99 deleted file mode 100644 index f058cfe..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/da/f088765c0c3e00151089cca86c5b1a99 +++ /dev/null @@ -1,433 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/600bbc16013e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/db/600bbc16013e00151c31fb33fff4337f deleted file mode 100644 index b00988e..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/db/600bbc16013e00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80ebea64dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80ebea64dd3e001517108d149b613203 new file mode 100644 index 0000000..2441bac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/80ebea64dd3e001517108d149b613203 @@ -0,0 +1,398 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/9064eb98e33e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/df/9064eb98e33e001517108d149b613203 new file mode 100644 index 0000000..2b462b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/9064eb98e33e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/c06a7eb00d3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/df/c06a7eb00d3e00151089cca86c5b1a99 deleted file mode 100644 index efac5c3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/df/c06a7eb00d3e00151089cca86c5b1a99 +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/a0795133d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/e/a0795133d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..be2f453 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/a0795133d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,376 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/e0ffd477033e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/a07905a5cf3e001518fa86d7ae2a1cf8 similarity index 67% rename from .metadata/.plugins/org.eclipse.core.resources/.history/9d/e0ffd477033e00151c31fb33fff4337f rename to .metadata/.plugins/org.eclipse.core.resources/.history/e0/a07905a5cf3e001518fa86d7ae2a1cf8 index 41b903e..d0c7fdd 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/e0ffd477033e00151c31fb33fff4337f +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/a07905a5cf3e001518fa86d7ae2a1cf8 @@ -15,43 +15,35 @@ public class PlayerListener implements Listener public void onPlayerJoin(PlayerJoinEvent event) { Player p=event.getPlayer(); - //PluginMain.Players.add(p); - //event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p)); - //if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20. - if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08. - //PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p)); - + MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); //2015.08.08. + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); //2015.08.08. + if(mp.Flair!=null) + PluginMain.AppendPlayerDisplayFlair(mp, p); else { //2015.07.20. String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]"; sendRawMessage(p, json); } - //System.out.println("Added player "+p.getName()); } @EventHandler public void onPlayerLeave(PlayerQuitEvent event) { - //for(Player player : PluginMain.Players) - /*for(int i=0; i"))+"(test)> "+event.getMessage()); //2015.08.08. - String name; - event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08. + MaybeOfflinePlayer player = MaybeOfflinePlayer.AllPlayers.get(event.getPlayer().getName()); + String flair=player.Flair; //2015.08.08. + if(flair==null || !player.AcceptedFlair) + flair=""; //2015.08.08. + String message=event.getMessage(); //2015.08.08. + /*for(Player p : PluginMain.GetPlayers()) + { //2015.08.08. + message.replaceAll(p.getName(), ) + }*/ + event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+message); //2015.08.08. } private static Class nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer"); @@ -72,6 +64,7 @@ public class PlayerListener implements Listener System.out.println("6"); } catch (Exception e) { e.printStackTrace(); + PluginMain.LastException=e; //2015.08.09. } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/20517c59cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/20517c59cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..77a24f3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/20517c59cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,364 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press?§r"); + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/80cfda10e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/80cfda10e03e001517108d149b613203 new file mode 100644 index 0000000..0ccc23b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/80cfda10e03e001517108d149b613203 @@ -0,0 +1,260 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/905afa0fe03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/905afa0fe03e001517108d149b613203 new file mode 100644 index 0000000..e6e7dea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/905afa0fe03e001517108d149b613203 @@ -0,0 +1,259 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/3060bf8dcf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/3060bf8dcf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..620a063 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/3060bf8dcf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,364 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + AppendPlayerDisplayFlair(player, username, finalflair); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/a0c874c7303e00151cbdba8b5f0a340e b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/a0c874c7303e00151cbdba8b5f0a340e deleted file mode 100644 index ba283d2..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/a0c874c7303e00151cbdba8b5f0a340e +++ /dev/null @@ -1,434 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.TownyUniverse; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player.PlayerName+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - //MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c0b7626f043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c0b7626f043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c0b7626f043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/d0c6108f083e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/d0c6108f083e00151c31fb33fff4337f deleted file mode 100644 index f174b9f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/d0c6108f083e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(!p.HasFlair) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/90448e80dd3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/90448e80dd3e001517108d149b613203 new file mode 100644 index 0000000..3aeeb6d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/90448e80dd3e001517108d149b613203 @@ -0,0 +1,408 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("autoflairdecisions.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/40a8228bfe3d00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/40a8228bfe3d00151c31fb33fff4337f deleted file mode 100644 index fe7d591..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/40a8228bfe3d00151c31fb33fff4337f +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(PluginMain.AcceptedPlayers.contains(player.getName())) - PluginMain.AcceptedPlayers.remove(player.getName()); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=PluginMain.PlayerFlairs.get(player.getName()); - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.Players) - { - if(PluginMain.PlayerFlairs.containsKey(p.getName())) - { - String flair=PluginMain.PlayerFlairs.get(p.getName()); - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/3086890bd03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/3086890bd03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ce49e4e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/3086890bd03e001518fa86d7ae2a1cf8 @@ -0,0 +1,198 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(player, p); + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/803bcb6fde3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/803bcb6fde3e001517108d149b613203 new file mode 100644 index 0000000..ce1d794 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/803bcb6fde3e001517108d149b613203 @@ -0,0 +1,409 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/80c8e1dade3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/80c8e1dade3e001517108d149b613203 new file mode 100644 index 0000000..bf077b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/80c8e1dade3e001517108d149b613203 @@ -0,0 +1,228 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/e0406b19853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/e0406b19853e00151ce8ff6d4f74dabd deleted file mode 100644 index cf9e3b7..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/e0406b19853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,122 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0870f36883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0870f36883e00151ce8ff6d4f74dabd deleted file mode 100644 index 897b7ad..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0870f36883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,163 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0cc5971043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0cc5971043e00151c31fb33fff4337f deleted file mode 100644 index 87c0c13..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d0cc5971043e00151c31fb33fff4337f +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/d09b59ba873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/d09b59ba873e00151ce8ff6d4f74dabd deleted file mode 100644 index dce3338..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/d09b59ba873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,148 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==args[2]) - { - - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/500395f1873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/500395f1873e00151ce8ff6d4f74dabd deleted file mode 100644 index 007e8c1..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/500395f1873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,157 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/40594044033e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/206bbc9acf3e001518fa86d7ae2a1cf8 similarity index 67% rename from .metadata/.plugins/org.eclipse.core.resources/.history/c/40594044033e00151c31fb33fff4337f rename to .metadata/.plugins/org.eclipse.core.resources/.history/ef/206bbc9acf3e001518fa86d7ae2a1cf8 index 987dab3..a085586 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/c/40594044033e00151c31fb33fff4337f +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/206bbc9acf3e001518fa86d7ae2a1cf8 @@ -15,43 +15,35 @@ public class PlayerListener implements Listener public void onPlayerJoin(PlayerJoinEvent event) { Player p=event.getPlayer(); - //PluginMain.Players.add(p); - //event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p)); - //if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20. - // PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p)); - if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08. - + MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); //2015.08.08. + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); //2015.08.08. + if(mp.Flair!=null) + PluginMain.AppendPlayerDisplayFlair(p, mp.UserName, mp.Flair); else { //2015.07.20. String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]"; sendRawMessage(p, json); } - //System.out.println("Added player "+p.getName()); } @EventHandler public void onPlayerLeave(PlayerQuitEvent event) { - //for(Player player : PluginMain.Players) - /*for(int i=0; i"))+"(test)> "+event.getMessage()); //2015.08.08. - String name; - event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08. + MaybeOfflinePlayer player = MaybeOfflinePlayer.AllPlayers.get(event.getPlayer().getName()); + String flair=player.Flair; //2015.08.08. + if(flair==null || !player.AcceptedFlair) + flair=""; //2015.08.08. + String message=event.getMessage(); //2015.08.08. + /*for(Player p : PluginMain.GetPlayers()) + { //2015.08.08. + message.replaceAll(p.getName(), ) + }*/ + event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+message); //2015.08.08. } private static Class nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer"); @@ -72,6 +64,7 @@ public class PlayerListener implements Listener System.out.println("6"); } catch (Exception e) { e.printStackTrace(); + PluginMain.LastException=e; //2015.08.09. } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/50b12f8f883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/50b12f8f883e00151ce8ff6d4f74dabd deleted file mode 100644 index 07653a6..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/50b12f8f883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,177 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - String message="§cPlayer not found: "+args[2]+"§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/d0a65f54873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/d0a65f54873e00151ce8ff6d4f74dabd deleted file mode 100644 index b77e8ab..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/d0a65f54873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,140 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/90af968ede3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/f/90af968ede3e001517108d149b613203 new file mode 100644 index 0000000..f04e2b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/90af968ede3e001517108d149b613203 @@ -0,0 +1,430 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/a0d81438d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f/a0d81438d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..f17161d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/a0d81438d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,216 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + //SendMessage(player, msg); //2015.08.09. + + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/b08c4dd3d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f/b08c4dd3d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..fbfb948 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/b08c4dd3d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,372 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/e0df3528853e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/e0df3528853e00151ce8ff6d4f74dabd deleted file mode 100644 index 51121d3..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f/e0df3528853e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,126 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - case "showerrors": //2015.08.09. - DoShowErrors(player); - break; - default: - return false; - } - return true; - } - - if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20. - else if(args[0].toLowerCase()=="showerrors") - DoShowErrors(null); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoShowErrors(Player player) - { - - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f1/808b0319df3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/808b0319df3e001517108d149b613203 new file mode 100644 index 0000000..3b9f920 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/808b0319df3e001517108d149b613203 @@ -0,0 +1,250 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt")) + { + SendMessage(player, "") + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/205fa4c2d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/205fa4c2d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..5dcd8d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/205fa4c2d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,209 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + + } + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0380b49d03e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0380b49d03e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..c1a5059 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0380b49d03e001518fa86d7ae2a1cf8 @@ -0,0 +1,200 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + break; + case "cantpress": //2015.08.09. + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.TownColors.clear(); + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + PluginMain.TownColors.put(s[0], s[1]); + } + br.close(); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + String msg="§6Reloaded config file.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/203061bfd23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/203061bfd23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..717cff8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/203061bfd23e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Reloaded all files.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/50ae2f61873e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/50ae2f61873e00151ce8ff6d4f74dabd deleted file mode 100644 index c1f1c9d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/50ae2f61873e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,140 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/a09a2529d33e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/a09a2529d33e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..2a7e8d7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/a09a2529d33e001518fa86d7ae2a1cf8 @@ -0,0 +1,215 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + } + //String msg="§6Reloaded config file.§r"; + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; + SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + DoReload(player); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/206dc8d8d83e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/206dc8d8d83e001517108d149b613203 new file mode 100644 index 0000000..6d35231 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/206dc8d8d83e001517108d149b613203 @@ -0,0 +1,224 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer.equals(player)) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/20d00369d23e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/20d00369d23e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..7316b80 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/20d00369d23e001518fa86d7ae2a1cf8 @@ -0,0 +1,380 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() + { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/5072900e883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/5072900e883e00151ce8ff6d4f74dabd deleted file mode 100644 index e500f17..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/5072900e883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,162 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80e28d69e43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80e28d69e43e001517108d149b613203 new file mode 100644 index 0000000..12a63df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/80e28d69e43e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + //String flair=p.Flair; //2015.08.08. + //PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/9050bb87de3e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/9050bb87de3e001517108d149b613203 new file mode 100644 index 0000000..454cbb7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/9050bb87de3e001517108d149b613203 @@ -0,0 +1,430 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SetPlayerFlair(Player player, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + File file=new File("customflairs.txt"); + BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(player.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void RemoveLineFromFile(String file) + { + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = "bbb"; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + if(trimmedLine.equals(lineToRemove)) continue; + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + boolean successful = tempFile.renameTo(inputFile); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/108fca500c3e00151089cca86c5b1a99 b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/108fca500c3e00151089cca86c5b1a99 deleted file mode 100644 index 4697c6f..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/108fca500c3e00151089cca86c5b1a99 +++ /dev/null @@ -1,432 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.json.JSONArray; -import org.json.JSONObject; - -import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.towny.object.WorldCoord; - -public class PluginMain extends JavaPlugin -{ //Translated to Java: 2015.07.15. - //A user, which flair isn't obtainable: - //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ - private static PluginMain Instance; - // Fired when plugin is first enabled - @Override - public void onEnable() - { - System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); - //System.out.println("Original C# version: http://pastebin.com/tX8xCPbp"); - //System.out.println("The Java version is... Also made by the same person."); - //System.out.println("With the help of StackOverflow and similar."); - /*catch(MalformedURLException e) - { - } - catch(IOException e) - { - }*/ - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - //System.out.println("Registering commands..."); - this.getCommand("u").setExecutor(new Commands()); - this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); - Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - //AcceptedPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - //IgnoredPlayers.add(line.replace("\n", "")); - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - //System.out.println("Registering done."); - Runnable r=new Runnable(){public void run(){ThreadMethod();}}; - Thread t=new Thread(r); - t.start(); - } - Boolean stop=false; - // Fired when plugin is disabled - @Override - public void onDisable() - { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - //for(String player : AcceptedPlayers) - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - File file=new File("flairsaccepted.txt"); - try { - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - File file=new File("flairsignored.txt"); - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); - bw.write(player+"\n"); - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - } - } - stop=true; - } - - public void ThreadMethod() //<-- 2015.07.16. - { - /*System.out.println("Sleeping for 5 seconds..."); //2015.07.20. - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } //2015.07.20.*/ - while(!stop) - { - try - { - String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); - JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); - for(Object obj : json) - { - JSONObject item = (JSONObject)obj; - String author=item.getJSONObject("data").getString("author"); - String ign=item.getJSONObject("data").getString("body"); - int start = ign.indexOf("IGN:") + "IGN:".length(); - int end = ign.indexOf(' ', start); - if (end == -1 || end == start) - end=ign.indexOf('\n', start); //2015.07.15. - if (end == -1 || end == start) - ign = ign.substring(start); - else - ign = ign.substring(start, end); - ign = ign.trim(); - if(HasIGFlair(ign)) - continue; - //System.out.println("Author: "+author); - try { - Thread.sleep(10); - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } - String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); - String flair; - if(flairdata.length > 1) //2015.07.15. - flair = flairdata[1]; - else - flair=""; - if (flair != "-1") - flair = flair + "s"; - else - flair = "non-presser"; - String flairclass; - //System.out.println("flairdata.length:"+flairdata.length); - if(flairdata.length>2) - flairclass = flairdata[2]; - else - flairclass="unknown"; - SetFlair(ign, flair, flairclass, author); - } - Thread.sleep(10000); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - } - } - } - - public String DownloadString(String urlstr) throws MalformedURLException, IOException - { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } - - //It has to store offline player flairs too, therefore it can't use Player object - /*public static Map PlayerFlairs=new HashMap(); - public static Map PlayerUserNames=new HashMap(); - //public Map PlayerFlairs=new HashMap(); - public static ArrayList Players=new ArrayList(); - public static ArrayList AcceptedPlayers=new ArrayList(); //2015.07.16. - public static ArrayList IgnoredPlayers=new ArrayList(); //2015.07.16. - //public static Map PlayerTowns=new HashMap(); //2015.07.20.*/ - public static Map TownColors=new HashMap(); //2015.07.20. - public Boolean HasIGFlair(String playername) - { - /*Player player=null; - for(Player p : Players) - { - if(p.getName()==playername) - { - player=p; - break; - } - } - if(player==null) - return false;*/ - //return PlayerFlairs.containsKey(playername); - //return MaybeOfflinePlayer.AllPlayers.containsKey(playername); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - return p.Flair!=null; //2015.08.08. - } - - public void SetFlair(String playername, String text, String flairclass, String username) - { - String finalflair; - switch(flairclass) - { - case "press-1": - finalflair="§c("+text+")§r"; - break; - case "press-2": - finalflair="§6("+text+")§r"; - break; - case "press-3": - finalflair="§e("+text+")§r"; - break; - case "press-4": - finalflair="§a("+text+")§r"; - break; - case "press-5": - finalflair="§9("+text+")§r"; - break; - case "press-6": - finalflair="§5("+text+")§r"; - break; - case "no-press": - finalflair="§7(non-pr.)§r"; - break; - case "cheater": - finalflair="§5("+text+")§r"; - break; - case "cant-press": //2015.08.08. - finalflair="§r(can't press)§r"; - break; - default: - finalflair=""; - break; - } - if(finalflair.length()==0) //<-- 2015.07.20. - return; - //PlayerFlairs.put(playername, finalflair); - //PlayerUserNames.put(playername, username); - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. - p.Flair=finalflair; //2015.08.08. - p.UserName=username; //2015.08.08. - /*for(Player player : Players) - { - if(player.getName()==playername) - { - PlayerFlairs.put(player, finalflair); - break; - } - }*/ - //System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass); - System.out.println("Added new flair to "+playername+": "+finalflair); - //for(Player player : Players) - for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. - { - //System.out.println("Online player: "+player.getName()); - //System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername)); - if(player.getName().equals(playername)) - { - //System.out.println("DisplayName: "+player.getDisplayName()); - //player.setDisplayName(player.getDisplayName()+finalflair); - AppendPlayerDisplayFlair(player, username, finalflair); - //System.out.println("DisplayName: "+player.getDisplayName()); - break; - } - } - } - - public static String GetFlair(Player player) - { //2015.07.16. - //String flair=PlayerFlairs.get(player.getName()); - String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. - return flair==null ? "" : flair; - } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) - { - //if(IgnoredPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) - return; - //if(AcceptedPlayers.contains(player.getName())) - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - //player.setDisplayName(player.getDisplayName()+flair); - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); - } - - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //System.out.println("A"); - String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters - String[] colors = color.substring(1).split("§"); - //String displayname=player.getDisplayName(); - String displayname=player.getName(); //2015.08.08. - ArrayList Positions=new ArrayList<>(); - //System.out.println("B"); - for(int i=0; i=colors.length) - { - //System.out.println("D"); - int x=0; - for(int i=0; i towns = TownyUniverse.getDataSource().getTowns(); - try { - Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); - return town.getName(); - } catch (Exception e) { - return ""; - } - } - - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) - { //2015.07.20. - //String color = GetColorForTown(GetPlayerTown(player)); - //String dname=player.getDisplayName(); - //player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair))); - MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08. - } - - public static Collection GetPlayers() - { - return Instance.getServer().getOnlinePlayers(); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a01791b5d13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a01791b5d13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..6f7db8d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a01791b5d13e001518fa86d7ae2a1cf8 @@ -0,0 +1,369 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) + { + MaybeOfflinePlayer.AllPlayers.clear(); + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/90ef83b4e03e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/90ef83b4e03e001517108d149b613203 new file mode 100644 index 0000000..21d2086 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/90ef83b4e03e001517108d149b613203 @@ -0,0 +1,263 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, Player targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.getName())) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.getName()+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.getName()+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length()<4) + { + + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/307961d7cf3e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/307961d7cf3e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..ccaedeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/307961d7cf3e001518fa86d7ae2a1cf8 @@ -0,0 +1,363 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + player.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/503cf63d883e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/503cf63d883e00151ce8ff6d4f74dabd deleted file mode 100644 index aeba6ef..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/503cf63d883e00151ce8ff6d4f74dabd +++ /dev/null @@ -1,167 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. - { - case "accept": - { - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - if(!p.AcceptedFlair) - { - String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - /*case "reload": //2015.07.20. - DoReload(player); - break;*/ - case "admin": //2015.08.09. - DoAdmin(player, args); - break; - default: - return false; - } - return true; - } - - /*if(args[0].toLowerCase()=="reload") - DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") - DoAdmin(null, args); //2015.08.09. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - //if(player==null || player.isOp() || player.getName()=="NorbiPeti") - //{ - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - //} - //else - //player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoAdmin(Player player, String[] args) - { //2015.08.09. - if(player==null || player.isOp() || player.getName()=="NorbiPeti") - { - if(args.length==1) - { - String message="§cUsage: /u admin reload|playerinfo§r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - //args[0] is "admin" - switch(args[1].toLowerCase()) - { - case "reload": - DoReload(player); - break; - case "playerinfo": - DoPlayerInfo(player, args); - break; - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } - private static void DoPlayerInfo(Player player, String[] args) - { //2015.08.09. - //args[0] is "admin" - args[1] is "playerinfo" - if(args.length==2) - { - String message="§cUsage: /u admin playerinfo §r"; - if(player==null) - System.out.println(message); - else - player.sendMessage(message); - return; - } - if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) - { - - } - MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0072fbdce43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0072fbdce43e001517108d149b613203 new file mode 100644 index 0000000..dfc7f58 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0072fbdce43e001517108d149b613203 @@ -0,0 +1,424 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if() + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/50e6e65e043e00151c31fb33fff4337f b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/50e6e65e043e00151c31fb33fff4337f deleted file mode 100644 index fb4f19d..0000000 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/50e6e65e043e00151c31fb33fff4337f +++ /dev/null @@ -1,125 +0,0 @@ -package tk.sznp.thebuttonautoflair; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - // This method is called, when somebody uses our command - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - if(args.length<1) - return false; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. - //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) - if(p.Flair==null) - { - player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); - return true; - } - switch(args[0]) - { - case "accept": - { - /*if(PluginMain.IgnoredPlayers.contains(player.getName())) - PluginMain.IgnoredPlayers.remove(player.getName());*/ - if(p.IgnoredFlair) - p.IgnoredFlair=false; //2015.08.08. - //if(!PluginMain.AcceptedPlayers.contains(player.getName())) - if(!p.AcceptedFlair) - { - //String flair=PluginMain.PlayerFlairs.get(player.getName()); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(player.getDisplayName()+flair); - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. - //PluginMain.AcceptedPlayers.add(player.getName()); - p.AcceptedFlair=true; //2015.08.08. - player.sendMessage("§6Your flair has been set:§r "+flair); - } - else - player.sendMessage("§cYou already have this user's flair.§r"); - break; - } - case "ignore": - { - if(p.AcceptedFlair) - p.AcceptedFlair=false; //2015.08.08. - if(!p.IgnoredFlair) - { - p.IgnoredFlair=true; - //String dname=player.getDisplayName(); - String flair=p.Flair; //2015.08.08. - //player.setDisplayName(dname.substring(0, dname.indexOf(flair))); - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. - player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); - } - else - player.sendMessage("§cYou already ignored this request.§r"); - break; - } - case "reload": //2015.07.20. - DoReload(player); - break; - default: - return false; - } - return true; - } - - if(args[0]=="reload") - DoReload(null); //2015.07.20. - return false; - } - private static void DoReload(Player player) - { //2015.07.20. - if(player==null || player.isOp()) - { - try - { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - //for(Player p : PluginMain.Players) - for(Player p : PluginMain.GetPlayers()) - { - if(p.Flair!=null) - { - String flair=p.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - if(player!=null) - player.sendMessage(msg); - else - System.out.println(msg); - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - } - } - else - player.sendMessage("§cYou need to be OP to use this command.§r"); - } -} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/8023f1ece43e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/8023f1ece43e001517108d149b613203 new file mode 100644 index 0000000..45eac34 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/8023f1ece43e001517108d149b613203 @@ -0,0 +1,422 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + SaveFiles(); //2015.08.09. + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/80351029e13e001517108d149b613203 b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/80351029e13e001517108d149b613203 new file mode 100644 index 0000000..68a6552 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/80351029e13e001517108d149b613203 @@ -0,0 +1,265 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + // This method is called, when somebody uses our command + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if(args.length<1) + return false; + MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08. + //if(!PluginMain.PlayerFlairs.containsKey(player.getName())) + if(p.Flair==null) + { + player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft"); + return true; + } + switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09. + { + case "accept": + { + if(p.IgnoredFlair) + p.IgnoredFlair=false; //2015.08.08. + if(!p.AcceptedFlair) + { + String flair=p.Flair; //2015.08.08. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); + p.AcceptedFlair=true; //2015.08.08. + player.sendMessage("§6Your flair has been set:§r "+flair); + } + else + player.sendMessage("§cYou already have this user's flair.§r"); + break; + } + case "ignore": + { + if(p.AcceptedFlair) + p.AcceptedFlair=false; //2015.08.08. + if(!p.IgnoredFlair) + { + p.IgnoredFlair=true; + String flair=p.Flair; //2015.08.08. + PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); + } + else + player.sendMessage("§cYou already ignored this request.§r"); + break; + } + /*case "reload": //2015.07.20. + DoReload(player); + break;*/ + case "admin": //2015.08.09. + DoAdmin(player, args); + break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; + default: + return false; + } + return true; + } + /*if(args[0].toLowerCase()=="reload") + DoReload(null); //2015.07.20.*/ + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { + DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } + return false; + } + private static void DoReload(Player player) + { //2015.07.20. + //if(player==null || player.isOp() || player.getName()=="NorbiPeti") + //{ + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) + { + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + String flair=mp.Flair; + PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. + } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } + //} + //else + //player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static Player ReloadPlayer; //2015.08.09. + private static void DoAdmin(Player player, String[] args) + { //2015.08.09. + if(player==null || player.isOp() || player.getName()=="NorbiPeti") + { + //System.out.println("Args length: " + args.length); + if(args.length==1) + { + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; + } + //args[0] is "admin" + switch(args[1].toLowerCase()) + { + case "reload": + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); + break; + case "playerinfo": + DoPlayerInfo(player, args); + break; + case "getlasterror": + DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": //2015.08.09. + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": //2015.08.09. + DoSetFlair(player, args); + break; + default: + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save§r"; + SendMessage(player, message); + return; + } + } + else + player.sendMessage("§cYou need to be OP to use this command.§r"); + } + private static void DoPlayerInfo(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "playerinfo" + if(args.length==2) + { + String message="§cUsage: /u admin playerinfo §r"; + SendMessage(player, message); + return; + } + if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) + { + String message="§cPlayer not found: "+args[2]+"§r"; + SendMessage(player, message); + return; + } + MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); + SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); + SendMessage(player, "Username: "+p.UserName); + SendMessage(player, "Flair accepted: "+p.AcceptedFlair); + SendMessage(player, "Flair ignored: "+p.IgnoredFlair); + } + private static void SendMessage(Player player, String message) + { //2015.08.09. + if(player==null) + System.out.println(message); + else + player.sendMessage(message); + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); + } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/204112ead13e001518fa86d7ae2a1cf8 b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/204112ead13e001518fa86d7ae2a1cf8 new file mode 100644 index 0000000..318be3d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/204112ead13e001518fa86d7ae2a1cf8 @@ -0,0 +1,374 @@ +package tk.sznp.thebuttonautoflair; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.WorldCoord; + +public class PluginMain extends JavaPlugin +{ //Translated to Java: 2015.07.15. + //A user, which flair isn't obtainable: + //https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ + private static PluginMain Instance; + // Fired when plugin is first enabled + @Override + public void onEnable() + { + System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)"); + getServer().getPluginManager().registerEvents(new PlayerListener(), this); + this.getCommand("u").setExecutor(new Commands()); + this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); + Instance=this; //2015.08.08. + LoadFiles(false); //2015.08.09. + Runnable r=new Runnable(){public void run(){ThreadMethod();}}; + Thread t=new Thread(r); + t.start(); + } + Boolean stop=false; + // Fired when plugin is disabled + @Override + public void onDisable() + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + stop=true; + } + + public void ThreadMethod() //<-- 2015.07.16. + { + while(!stop) + { + try + { + String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000"); + JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children"); + for(Object obj : json) + { + JSONObject item = (JSONObject)obj; + String author=item.getJSONObject("data").getString("author"); + String ign=item.getJSONObject("data").getString("body"); + int start = ign.indexOf("IGN:") + "IGN:".length(); + int end = ign.indexOf(' ', start); + if (end == -1 || end == start) + end=ign.indexOf('\n', start); //2015.07.15. + if (end == -1 || end == start) + ign = ign.substring(start); + else + ign = ign.substring(start, end); + ign = ign.trim(); + if(HasIGFlair(ign)) + continue; + try { + Thread.sleep(10); + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":"); + String flair; + if(flairdata.length > 1) //2015.07.15. + flair = flairdata[1]; + else + flair=""; + if (flair != "-1") + flair = flair + "s"; + else + flair = "non-presser"; + String flairclass; + if(flairdata.length>2) + flairclass = flairdata[2]; + else + flairclass="unknown"; + SetFlair(ign, flair, flairclass, author); + } + Thread.sleep(10000); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + } + + public static Exception LastException; //2015.08.09. + + public String DownloadString(String urlstr) throws MalformedURLException, IOException + { + URL url = new URL(urlstr); + URLConnection con = url.openConnection(); + con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); + InputStream in = con.getInputStream(); + String encoding = con.getContentEncoding(); + encoding = encoding == null ? "UTF-8" : encoding; + String body = IOUtils.toString(in, encoding); + in.close(); + return body; + } + + public static Map TownColors=new HashMap(); //2015.07.20. + public Boolean HasIGFlair(String playername) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + return p.Flair!=null; //2015.08.08. + } + + public void SetFlair(String playername, String text, String flairclass, String username) + { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. + String finalflair; + p.FlairDecided=true; + switch(flairclass) + { + case "press-1": + finalflair="§c("+text+")§r"; + break; + case "press-2": + finalflair="§6("+text+")§r"; + break; + case "press-3": + finalflair="§e("+text+")§r"; + break; + case "press-4": + finalflair="§a("+text+")§r"; + break; + case "press-5": + finalflair="§9("+text+")§r"; + break; + case "press-6": + finalflair="§5("+text+")§r"; + break; + case "no-press": + finalflair="§7(non-pr.)§r"; + break; + case "cheater": + finalflair="§5("+text+")§r"; + break; + case "cant-press": //2015.08.08. + finalflair="§r(can't press)§r"; + break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; + default: + finalflair=""; + break; + } + if(finalflair.length()==0) //<-- 2015.07.20. + return; + p.Flair=finalflair; //2015.08.08. + p.UserName=username; //2015.08.08. + System.out.println("Added new flair to "+playername+": "+finalflair); + for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. + { + if(player.getName().equals(playername)) + { + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); + break; + } + } + } + + public static String GetFlair(Player player) + { //2015.07.16. + String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. + return flair==null ? "" : flair; + } + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. + { + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) + return; + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } + else + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); + } + + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) + { //2015.07.20. + String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters + String[] colors = color.substring(1).split("§"); + String displayname=player.getName(); //2015.08.08. + ArrayList Positions=new ArrayList<>(); + for(int i=0; i=colors.length) + { + int x=0; + for(int i=0; i GetPlayers() + { + return Instance.getServer().getOnlinePlayers(); + } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { + System.out.println("Reloading files for auto-flair plugin..."); + MaybeOfflinePlayer.AllPlayers.clear(); //2015.08.09. + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/d0bbb843893e00151ce8ff6d4f74dabd b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/a0790ee2cf3e001518fa86d7ae2a1cf8 similarity index 85% rename from .metadata/.plugins/org.eclipse.core.resources/.history/a0/d0bbb843893e00151ce8ff6d4f74dabd rename to .metadata/.plugins/org.eclipse.core.resources/.history/ff/a0790ee2cf3e001518fa86d7ae2a1cf8 index cbeaea2..38374ce 100644 --- a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/d0bbb843893e00151ce8ff6d4f74dabd +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/a0790ee2cf3e001518fa86d7ae2a1cf8 @@ -62,16 +62,20 @@ public class Commands implements CommandExecutor { case "admin": //2015.08.09. DoAdmin(player, args); break; + case "nonpresser": //2015.08.09. + break; default: return false; } return true; } - /*if(args[0].toLowerCase()=="reload") DoReload(null); //2015.07.20.*/ - else if(args[0].toLowerCase()=="admin") + else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09. + { DoAdmin(null, args); //2015.08.09. + return true; //2015.08.09. + } return false; } private static void DoReload(Player player) @@ -111,6 +115,7 @@ public class Commands implements CommandExecutor { System.out.println("Error!\n"+e); if(player!=null) player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. } //} //else @@ -120,6 +125,7 @@ public class Commands implements CommandExecutor { { //2015.08.09. if(player==null || player.isOp() || player.getName()=="NorbiPeti") { + //System.out.println("Args length: " + args.length); if(args.length==1) { String message="§cUsage: /u admin reload|playerinfo§r"; @@ -135,6 +141,12 @@ public class Commands implements CommandExecutor { case "playerinfo": DoPlayerInfo(player, args); break; + case "getlasterror": + DoGetLastError(player, args); + default: + String message="§cUsage: /u admin reload|playerinfo§r"; + SendMessage(player, message); + return; } } else @@ -147,19 +159,20 @@ public class Commands implements CommandExecutor { { String message="§cUsage: /u admin playerinfo §r"; SendMessage(player, message); + return; } if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) { String message="§cPlayer not found: "+args[2]+"§r"; SendMessage(player, message); + return; } MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); - SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Player name: "+p.PlayerName); + SendMessage(player, "User flair: "+p.Flair); SendMessage(player, "Username: "+p.UserName); SendMessage(player, "Flair accepted: "+p.AcceptedFlair); SendMessage(player, "Flair ignored: "+p.IgnoredFlair); - return; } private static void SendMessage(Player player, String message) { //2015.08.09. @@ -167,6 +180,17 @@ public class Commands implements CommandExecutor { System.out.println(message); else player.sendMessage(message); - return; + } + private static void DoGetLastError(Player player, String[] args) + { //2015.08.09. + //args[0] is "admin" - args[1] is "getlasterror" + if(PluginMain.LastException!=null) + { + SendMessage(player, "Last error:"); + SendMessage(player, PluginMain.LastException.toString()); + PluginMain.LastException=null; + } + else + SendMessage(player, "There were no exceptions."); } } diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.markers.snap index 837b604..ed1969b 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.syncinfo.snap index 837b604..ed1969b 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.syncinfo.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/RemoteSystemsTempFiles/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.indexes/e4/77/69/e/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.indexes/e4/77/69/e/history.index index 283cb9c..6993213 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.indexes/e4/77/69/e/history.index and b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.indexes/e4/77/69/e/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers index b420625..7797057 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers and b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers.snap index f1f6952..ed70d6d 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.syncinfo.snap index 837b604..ed1969b 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.syncinfo.snap and b/.metadata/.plugins/org.eclipse.core.resources/.projects/TheButtonAutoFlair/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap index 837b604..ed1969b 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap and b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/7.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/7.tree deleted file mode 100644 index 3611074..0000000 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.root/7.tree and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/8.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/8.tree new file mode 100644 index 0000000..b9e5136 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.root/8.tree differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources index 8f85b2a..2e532df 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources and b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.snap b/.metadata/.plugins/org.eclipse.core.resources/.snap index 3354738..52616b9 100644 Binary files a/.metadata/.plugins/org.eclipse.core.resources/.snap and b/.metadata/.plugins/org.eclipse.core.resources/.snap differ diff --git a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi index 86d712e..faba522 100644 --- a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi +++ b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi @@ -235,31 +235,31 @@ categoryTag:Help - + newtablook org.eclipse.e4.primaryDataStack EditorStack - + Editor org.eclipse.jdt.ui.CompilationUnitEditor removeOnHide - - Editor - org.eclipse.jdt.ui.CompilationUnitEditor - removeOnHide - - - + Editor org.eclipse.jdt.ui.CompilationUnitEditor removeOnHide activeOnClose + + + Editor + org.eclipse.jdt.ui.CompilationUnitEditor + removeOnHide + - + Editor org.eclipse.jdt.ui.CompilationUnitEditor removeOnHide @@ -317,8 +317,8 @@ View categoryTag:General - - + + View categoryTag:General @@ -420,18 +420,18 @@ Draggable - - - - - - - - - - - - + + + + + + + + + + + + toolbarSeparator @@ -439,38 +439,38 @@ Draggable - - - + + + Draggable - - + + Draggable - - + + Draggable - - + + Draggable - - - - + + + + Draggable - - - - + + + + Draggable @@ -480,20 +480,20 @@ Draggable - - - - + + + + Draggable - - - - - - - + + + + + + + Draggable @@ -511,15 +511,15 @@ Draggable - - - - - - - - - + + + + + + + + + toolbarSeparator @@ -531,8 +531,8 @@ Draggable - - + + stretch diff --git a/.metadata/.plugins/org.eclipse.jdt.core/1508644233.index b/.metadata/.plugins/org.eclipse.jdt.core/1508644233.index index 9b7966a..2abce78 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.core/1508644233.index and b/.metadata/.plugins/org.eclipse.jdt.core/1508644233.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt index 7741d94..08ab0e4 100644 --- a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt +++ b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt @@ -1,5 +1,5 @@ INDEX VERSION 1.126+D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairProto\Spigot plugin\.metadata\.plugins\org.eclipse.jdt.core -3045053200.index +1508644233.index 3089967703.index 2674037006.index 323251068.index @@ -22,5 +22,5 @@ INDEX VERSION 1.126+D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairProto\Spi 3954291735.index 994834796.index 3604769962.index -1508644233.index +3045053200.index 3656756579.index diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml b/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml index af0e569..a866bad 100644 --- a/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml +++ b/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml @@ -52,11 +52,11 @@ - + diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png index a95f608..9abe3c5 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png index 56554ec..c1396b1 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png index 9abe3c5..a930fe1 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png index a6abcd8..834bd74 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png index c00d1cb..56554ec 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png index a930fe1..a95f608 100644 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png deleted file mode 100644 index d198a93..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png deleted file mode 100644 index 834bd74..0000000 Binary files a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png and /dev/null differ diff --git a/.metadata/.plugins/org.eclipse.ui.workbench.texteditor/dialog_settings.xml b/.metadata/.plugins/org.eclipse.ui.workbench.texteditor/dialog_settings.xml new file mode 100644 index 0000000..6c805e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.ui.workbench.texteditor/dialog_settings.xml @@ -0,0 +1,23 @@ + +
+
+ + + + + + + + + + + +
+
+ + + + + +
+
diff --git a/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml b/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml index 975f072..1ad48d3 100644 --- a/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml +++ b/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml @@ -8,4 +8,6 @@ + + \ No newline at end of file diff --git a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/Commands.class b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/Commands.class index d69dec3..818baec 100644 Binary files a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/Commands.class and b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/Commands.class differ diff --git a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.class b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.class index 4d8f97d..2074d8d 100644 Binary files a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.class and b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.class differ diff --git a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PlayerListener.class b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PlayerListener.class index 37ef241..d36288a 100644 Binary files a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PlayerListener.class and b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PlayerListener.class differ diff --git a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain$1.class b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain$1.class index c70cdcc..7d254d2 100644 Binary files a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain$1.class and b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain$1.class differ diff --git a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain.class b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain.class index b00662b..61fcade 100644 Binary files a/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain.class and b/TheButtonAutoFlair/bin/tk/sznp/thebuttonautoflair/PluginMain.class differ diff --git a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/Commands.java b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/Commands.java index 21abae7..28d37b8 100644 --- a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/Commands.java +++ b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/Commands.java @@ -1,8 +1,9 @@ package tk.sznp.thebuttonautoflair; -import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; -import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -33,7 +34,8 @@ public class Commands implements CommandExecutor { if(!p.AcceptedFlair) { String flair=p.Flair; //2015.08.08. - PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + //PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + PluginMain.AppendPlayerDisplayFlair(p, player); p.AcceptedFlair=true; //2015.08.08. player.sendMessage("§6Your flair has been set:§r "+flair); } @@ -48,8 +50,8 @@ public class Commands implements CommandExecutor { if(!p.IgnoredFlair) { p.IgnoredFlair=true; - String flair=p.Flair; //2015.08.08. - PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. + //String flair=p.Flair; //2015.08.08. + //PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20. player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r"); } else @@ -62,6 +64,32 @@ public class Commands implements CommandExecutor { case "admin": //2015.08.09. DoAdmin(player, args); break; + case "nonpresser": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§7(non-pr.)§r"; + break; + case "cantpress": //2015.08.09. + if(!p.AcceptedFlair) + { + player.sendMessage("§cYou need to accept the flair first.§r"); + break; + } + if(p.FlairDecided) + { + player.sendMessage("§cYou have already set the flair type.§r"); + break; + } + p.Flair="§r(can't press)§r"; + break; default: return false; } @@ -80,45 +108,41 @@ public class Commands implements CommandExecutor { { //2015.07.20. //if(player==null || player.isOp() || player.getName()=="NorbiPeti") //{ - try + try + { + File file=new File("autoflairconfig.txt"); + if(file.exists()) { - File file=new File("autoflairconfig.txt"); - if(file.exists()) - { - PluginMain.TownColors.clear(); - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - PluginMain.TownColors.put(s[0], s[1]); - } - br.close(); - for(Player p : PluginMain.GetPlayers()) - { - MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); - if(mp.Flair!=null) - { - String flair=mp.Flair; - PluginMain.RemovePlayerDisplayFlairFinal(p, flair); - PluginMain.AppendPlayerDisplayFlairFinal(p, flair); - } - } - String msg="§6Reloaded config file.§r"; - SendMessage(player, msg); //2015.08.09. - } - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - if(player!=null) - player.sendMessage("§cAn error occured. See console for details.§r"); - PluginMain.LastException=e; //2015.08.09. + PluginMain.LoadFiles(true); //2015.08.09. + for(Player p : PluginMain.GetPlayers()) + { + MaybeOfflinePlayer mp = MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); + if(mp.Flair!=null) + { + //String flair=mp.Flair; + //PluginMain.RemovePlayerDisplayFlairFinal(p, flair); + //PluginMain.AppendPlayerDisplayFlairFinal(p, flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. + } + String msg="§6Note: The auto-flair plugin has been reloaded. You might need to wait 10s to have your flair.§r"; //2015.08.09. + p.sendMessage(msg); //2015.08.09. + } + //String msg="§6Reloaded config file.§r"; + //SendMessage(player, msg); //2015.08.09. } + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + if(player!=null) + player.sendMessage("§cAn error occured. See console for details.§r"); + PluginMain.LastException=e; //2015.08.09. + } //} //else //player.sendMessage("§cYou need to be OP to use this command.§r"); } + private static Player ReloadPlayer; //2015.08.09. private static void DoAdmin(Player player, String[] args) { //2015.08.09. if(player==null || player.isOp() || player.getName()=="NorbiPeti") @@ -126,7 +150,7 @@ public class Commands implements CommandExecutor { //System.out.println("Args length: " + args.length); if(args.length==1) { - String message="§cUsage: /u admin reload|playerinfo§r"; + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; SendMessage(player, message); return; } @@ -134,15 +158,29 @@ public class Commands implements CommandExecutor { switch(args[1].toLowerCase()) { case "reload": - DoReload(player); + ReloadPlayer=player; //2015.08.09. + SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r"); break; case "playerinfo": DoPlayerInfo(player, args); break; case "getlasterror": DoGetLastError(player, args); + case "confirm": + if(ReloadPlayer==player) + DoReload(player); //2015.08.09. + else + SendMessage(player, "§cYou need to do /u admin reload first.§r"); + break; + case "save": + PluginMain.SaveFiles(); //2015.08.09. + SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r"); + break; + case "setflair": + DoSetFlair(player, args); + break; default: - String message="§cUsage: /u admin reload|playerinfo§r"; + String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r"; SendMessage(player, message); return; } @@ -191,4 +229,35 @@ public class Commands implements CommandExecutor { else SendMessage(player, "There were no exceptions."); } + private static void SetPlayerFlair(Player player, MaybeOfflinePlayer targetplayer, String flair) + { //2015.08.09. + flair=flair.replace('&', '§'); + targetplayer.Flair=flair; + if(!PluginMain.RemoveLineFromFile("customflairs.txt", targetplayer.PlayerName)) + { + SendMessage(player, "§cError removing previous custom flair!§r"); + return; + } + File file=new File("customflairs.txt"); + try { + BufferedWriter bw; + bw = new BufferedWriter(new FileWriter(file, true)); + bw.write(targetplayer.PlayerName+"\n"); + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + PluginMain.LastException=e; //2015.08.09. + } + SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r"); + } + private static void DoSetFlair(Player player, String[] args) + { + //args[0] is "admin" - args[1] is "setflair" + if(args.length<4) + { + SendMessage(player, "§cUsage: /u admin setflair "); + return; + } + SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]); + } } diff --git a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.java b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.java index 6549d0e..28679fc 100644 --- a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.java +++ b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/MaybeOfflinePlayer.java @@ -9,6 +9,7 @@ public class MaybeOfflinePlayer public String Flair; //If the user comments their name, it gets set, it doesn't matter if they accepted it or not public boolean AcceptedFlair; public boolean IgnoredFlair; + public boolean FlairDecided; //2015.08.09. public static HashMap AllPlayers=new HashMap<>(); //2015.08.08. public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername) { @@ -21,4 +22,10 @@ public class MaybeOfflinePlayer } return AllPlayers.get(playername); } + /*public enum FlairClassType + { //2015.08.09. + NonPresser, + CantPress, + Undecided + }*/ } diff --git a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PlayerListener.java b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PlayerListener.java index a085586..dc0378b 100644 --- a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PlayerListener.java +++ b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PlayerListener.java @@ -18,7 +18,7 @@ public class PlayerListener implements Listener MaybeOfflinePlayer.AddPlayerIfNeeded(p.getName()); //2015.08.08. MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()); //2015.08.08. if(mp.Flair!=null) - PluginMain.AppendPlayerDisplayFlair(p, mp.UserName, mp.Flair); + PluginMain.AppendPlayerDisplayFlair(mp, p); //2015.08.09. else { //2015.07.20. String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]"; diff --git a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PluginMain.java b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PluginMain.java index 5740654..ceca4ee 100644 --- a/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PluginMain.java +++ b/TheButtonAutoFlair/src/tk/sznp/thebuttonautoflair/PluginMain.java @@ -38,49 +38,7 @@ public class PluginMain extends JavaPlugin this.getCommand("u").setExecutor(new Commands()); this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§')); Instance=this; //2015.08.08. - try { - File file=new File("flairsaccepted.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - //System.out.println("Name: " + name); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("flairsignored.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); - String line; - while ((line = br.readLine()) != null) - { - String name=line.replace("\n", ""); - MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. - } - br.close(); - } - file=new File("autoflairconfig.txt"); - if(file.exists()) - { - BufferedReader br=new BufferedReader(new FileReader(file)); - String line; - while((line=br.readLine())!=null) - { - String[] s=line.split(" "); - TownColors.put(s[0], s[1]); - } - br.close(); - } - //throw new IOException("Test"); //2015.08.09. - } catch (IOException e) { - System.out.println("Error!\n"+e); - LastException=e; //2015.08.09. - } + LoadFiles(false); //2015.08.09. Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Thread t=new Thread(r); t.start(); @@ -90,42 +48,7 @@ public class PluginMain extends JavaPlugin @Override public void onDisable() { - try - { - FileWriter fw; - fw = new FileWriter("flairsaccepted.txt"); - fw.close(); - fw = new FileWriter("flairsignored.txt"); - fw.close(); - } - catch(Exception e) - { - System.out.println("Error!\n"+e); - LastException=e; //2015.08.09. - } - try { - File file=new File("flairsaccepted.txt"); - BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.AcceptedFlair) - continue; //2015.08.08. - bw.write(player.PlayerName+"\n"); - } - bw.close(); - file=new File("flairsignored.txt"); - bw = new BufferedWriter(new FileWriter(file, true)); - for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. - { - if(!player.IgnoredFlair) - continue; //2015.08.08. - bw.write(player.PlayerName+"\n"); - } - bw.close(); - } catch (IOException e) { - System.out.println("Error!\n"+e); - LastException=e; //2015.08.09. - } + SaveFiles(); //2015.08.09. stop=true; } @@ -143,6 +66,8 @@ public class PluginMain extends JavaPlugin String author=item.getJSONObject("data").getString("author"); String ign=item.getJSONObject("data").getString("body"); int start = ign.indexOf("IGN:") + "IGN:".length(); + if(start==-1) + continue; //2015.08.09. int end = ign.indexOf(' ', start); if (end == -1 || end == start) end=ign.indexOf('\n', start); //2015.07.15. @@ -209,7 +134,9 @@ public class PluginMain extends JavaPlugin public void SetFlair(String playername, String text, String flairclass, String username) { + MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. String finalflair; + p.FlairDecided=true; switch(flairclass) { case "press-1": @@ -239,13 +166,16 @@ public class PluginMain extends JavaPlugin case "cant-press": //2015.08.08. finalflair="§r(can't press)§r"; break; + case "undecided": //2015.08.09. + p.FlairDecided=false; + finalflair=""; + break; default: finalflair=""; break; } if(finalflair.length()==0) //<-- 2015.07.20. return; - MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08. p.Flair=finalflair; //2015.08.08. p.UserName=username; //2015.08.08. System.out.println("Added new flair to "+playername+": "+finalflair); @@ -253,7 +183,8 @@ public class PluginMain extends JavaPlugin { if(player.getName().equals(playername)) { - AppendPlayerDisplayFlair(player, username, finalflair); + //AppendPlayerDisplayFlair(player, username, finalflair); + AppendPlayerDisplayFlair(p, player); break; } } @@ -264,18 +195,24 @@ public class PluginMain extends JavaPlugin String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. return flair==null ? "" : flair; } - - public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + + //public static void AppendPlayerDisplayFlair(Player player, String username, String flair) + public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player, Player p) //<-- 2015.08.09. { - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) + + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).IgnoredFlair) return; - if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) - AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. + if(MaybeOfflinePlayer.AllPlayers.get(p.getName()).AcceptedFlair) + { + AppendPlayerDisplayFlairFinal(p, player.Flair); //2015.07.20. + if(!player.FlairDecided) + p.sendMessage("§9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)§r"); //2015.08.09. + } else - player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); + p.sendMessage("§9Are you Reddit user "+player.UserName+"?§r §6Type /u accept or /u ignore§r"); } - public static void AppendPlayerDisplayFlairFinal(Player player, String flair) + private static void AppendPlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String[] colors = color.substring(1).split("§"); @@ -339,13 +276,150 @@ public class PluginMain extends JavaPlugin } } - public static void RemovePlayerDisplayFlairFinal(Player player, String flair) + /*public static void RemovePlayerDisplayFlairFinal(Player player, String flair) { //2015.07.20. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08. - } + }*/ public static Collection GetPlayers() { return Instance.getServer().getOnlinePlayers(); } + + public static void LoadFiles(boolean reload) //<-- 2015.08.09. + { + if(reload) + { //2015.08.09. + System.out.println("Auto-flair plugin cleanup for reloading..."); + MaybeOfflinePlayer.AllPlayers.clear(); + TownColors.clear(); + } + System.out.println("Loading files for auto-flair plugin..."); //2015.08.09. + try { + File file=new File("flairsaccepted.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + //System.out.println("Name: " + name); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("flairsignored.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt")); + String line; + while ((line = br.readLine()) != null) + { + String name=line.replace("\n", ""); + MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08. + } + br.close(); + } + file=new File("autoflairconfig.txt"); + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + TownColors.put(s[0], s[1]); + } + br.close(); + } + file=new File("customflairs.txt"); //2015.08.09. + if(file.exists()) + { + BufferedReader br=new BufferedReader(new FileReader(file)); + String line; + while((line=br.readLine())!=null) + { + String[] s=line.split(" "); + MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09. + } + br.close(); + } + //throw new IOException("Test"); //2015.08.09. + System.out.println("Auto-flair plugin loaded files!"); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static void SaveFiles() //<-- 2015.08.09. + { + try + { + FileWriter fw; + fw = new FileWriter("flairsaccepted.txt"); + fw.close(); + fw = new FileWriter("flairsignored.txt"); + fw.close(); + } + catch(Exception e) + { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + try { + File file=new File("flairsaccepted.txt"); + BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.AcceptedFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + file=new File("flairsignored.txt"); + bw = new BufferedWriter(new FileWriter(file, true)); + for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. + { + if(!player.IgnoredFlair) + continue; //2015.08.08. + bw.write(player.PlayerName+"\n"); + } + bw.close(); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + } + public static boolean RemoveLineFromFile(String file, String line) + { //2015.08.09. + File inputFile = new File(file); + File tempFile = new File("_temp.txt"); + + if(!inputFile.exists()) + return true; //2015.08.10. + + try { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + + String lineToRemove = line; + String currentLine; + + while((currentLine = reader.readLine()) != null) { + // trim newline when comparing with lineToRemove + String trimmedLine = currentLine.trim(); + //if(trimmedLine.equals(lineToRemove)) continue; + if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09. + writer.write(currentLine + System.getProperty("line.separator")); + } + writer.close(); + reader.close(); + return tempFile.renameTo(inputFile); + } catch (IOException e) { + System.out.println("Error!\n"+e); + LastException=e; //2015.08.09. + } + return false; + } }