Lot of admin commands, bug fixes, and custom flairs

- Added many admin commands (see /u admin)
- Probably fixed the flairs for some players
- Added support for custom flairs, also used when deciding unknown flair
types
This commit is contained in:
Norbi Peti 2015-08-10 00:28:15 +02:00
parent 03248b3855
commit 68325bdd5f
467 changed files with 82050 additions and 26885 deletions

View file

@ -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. !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 !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. !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.

Binary file not shown.

Binary file not shown.

View file

@ -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 <player>§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.");
}
}

View file

@ -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"
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}*/
public static Collection<? extends Player> 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;
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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 <player>§r";
if(player==null)
System.out.println(message);
else
player.sendMessage(message);
}
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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 <player>§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");
}
}

View file

@ -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");
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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;
}
}

View file

@ -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 <player>§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 <playername> <flair>");
return;
}
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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;
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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;
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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");
}
}

View file

@ -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 <player>§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.
}
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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);
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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;
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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<String, MaybeOfflinePlayer> 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
}*/
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
{
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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"
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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"
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
public static void LoadFiles(boolean reload)
{
}
}

View file

@ -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 <player>§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.
}
}
}

View file

@ -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 <player>§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
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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();
}
}

View file

@ -21,8 +21,9 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import tk.sznp.thebuttonautoflair.MaybeOfflinePlayer.FlairClassType;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.WorldCoord; import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin public class PluginMain extends JavaPlugin
@ -48,7 +49,7 @@ public class PluginMain extends JavaPlugin
while ((line = br.readLine()) != null) while ((line = br.readLine()) != null)
{ {
String name=line.replace("\n", ""); String name=line.replace("\n", "");
System.out.println("Name: " + name); //System.out.println("Name: " + name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08. MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
} }
br.close(); br.close();
@ -77,8 +78,10 @@ public class PluginMain extends JavaPlugin
} }
br.close(); br.close();
} }
//throw new IOException("Test"); //2015.08.09.
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error!\n"+e); System.out.println("Error!\n"+e);
LastException=e; //2015.08.09.
} }
Runnable r=new Runnable(){public void run(){ThreadMethod();}}; Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r); Thread t=new Thread(r);
@ -100,33 +103,31 @@ public class PluginMain extends JavaPlugin
catch(Exception e) catch(Exception e)
{ {
System.out.println("Error!\n"+e); System.out.println("Error!\n"+e);
LastException=e; //2015.08.09.
} }
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. try {
{ File file=new File("flairsaccepted.txt");
if(!player.AcceptedFlair) BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
continue; //2015.08.08. for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
File file=new File("flairsaccepted.txt"); {
try { if(!player.AcceptedFlair)
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true)); continue; //2015.08.08.
bw.write(player.PlayerName+"\n"); bw.write(player.PlayerName+"\n");
bw.close(); }
} catch (IOException e) { bw.close();
System.out.println("Error!\n"+e); file=new File("flairsignored.txt");
} bw = new BufferedWriter(new FileWriter(file, true));
} for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08. {
{ if(!player.IgnoredFlair)
if(!player.IgnoredFlair) continue; //2015.08.08.
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.write(player.PlayerName+"\n");
bw.close(); }
} catch (IOException e) { bw.close();
System.out.println("Error!\n"+e); } catch (IOException e) {
} System.out.println("Error!\n"+e);
} LastException=e; //2015.08.09.
}
stop=true; stop=true;
} }
@ -181,10 +182,13 @@ public class PluginMain extends JavaPlugin
catch(Exception e) catch(Exception e)
{ {
System.out.println("Error!\n"+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 public String DownloadString(String urlstr) throws MalformedURLException, IOException
{ {
URL url = new URL(urlstr); URL url = new URL(urlstr);
@ -198,37 +202,18 @@ public class PluginMain extends JavaPlugin
return body; return body;
} }
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20. public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername) 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. MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08. return p.Flair!=null; //2015.08.08.
} }
public void SetFlair(String playername, String text, String flairclass, String username) public void SetFlair(String playername, String text, String flairclass, String username)
{ {
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
String finalflair; String finalflair;
p.FlairDecided=true;
switch(flairclass) switch(flairclass)
{ {
case "press-1": case "press-1":
@ -258,38 +243,24 @@ public class PluginMain extends JavaPlugin
case "cant-press": //2015.08.08. case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r"; finalflair="§r(can't press)§r";
break; break;
case "undecided": //2015.08.09.
p.FlairDecided=false;
finalflair="";
break;
default: default:
finalflair=""; finalflair="";
break; break;
} }
if(finalflair.length()==0) //<-- 2015.07.20. if(finalflair.length()==0) //<-- 2015.07.20.
return; return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08. p.Flair=finalflair; //2015.08.08.
p.UserName=username; //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); System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08. 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)) if(player.getName().equals(playername))
{ {
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair); AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break; break;
} }
} }
@ -297,43 +268,36 @@ public class PluginMain extends JavaPlugin
public static String GetFlair(Player player) public static String GetFlair(Player player)
{ //2015.07.16. { //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08. String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair; 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(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return; return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair) if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair); {
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20. AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
}
else else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r"); player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
} }
public static void AppendPlayerDisplayFlairFinal(Player player, String flair) public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20. { //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§"); String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08. String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>(); ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) { for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) { if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i); Positions.add(i);
} }
} }
//System.out.println("C: Positions.size(): "+Positions.size()); String finalname=""; //TODO
String finalname="";
if(Positions.size()>=colors.length) if(Positions.size()>=colors.length)
{ {
//System.out.println("D");
int x=0; int x=0;
for(int i=0; i<Positions.size(); i++) for(int i=0; i<Positions.size(); i++)
{ {
@ -343,17 +307,12 @@ public class PluginMain extends JavaPlugin
nextpos=Positions.get(i+1); nextpos=Positions.get(i+1);
else else
nextpos=displayname.length(); nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r"; String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr; finalname+=substr;
} }
//System.out.println("F");
} }
else else
{ {
//System.out.println("E");
Positions.clear(); Positions.clear();
int unit=displayname.length()/colors.length; int unit=displayname.length()/colors.length;
int x=0; int x=0;
@ -365,16 +324,10 @@ public class PluginMain extends JavaPlugin
nextpos=i+unit; nextpos=i+unit;
else else
nextpos=displayname.length(); nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r"; String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr; finalname+=substr;
} }
//System.out.println("G");
} }
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
} }
@ -387,7 +340,6 @@ public class PluginMain extends JavaPlugin
public static String GetPlayerTown(Player player) public static String GetPlayerTown(Player player)
{ //2015.07.20. { //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try { try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO
return town.getName(); return town.getName();
@ -398,10 +350,6 @@ public class PluginMain extends JavaPlugin
public static void RemovePlayerDisplayFlairFinal(Player player, String flair) public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20. { //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. MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
} }

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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");
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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);
}
}

View file

@ -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 <player>§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"
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§r";
if(player==null)
System.out.println(message);
else
player.sendMessage(message);
return;
}
}
}

View file

@ -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 <player>§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.
}
}
}

View file

@ -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 <player>§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");
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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.
}
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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();
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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 <player>§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;
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
{
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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 <player>§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 <playername> <flair>");
return;
}
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()))
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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)
{
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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 <player>§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;
}
}

View file

@ -33,7 +33,8 @@ public class Commands implements CommandExecutor {
if(!p.AcceptedFlair) if(!p.AcceptedFlair)
{ {
String flair=p.Flair; //2015.08.08. 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. p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair); player.sendMessage("§6Your flair has been set:§r "+flair);
} }
@ -62,16 +63,20 @@ public class Commands implements CommandExecutor {
case "admin": //2015.08.09. case "admin": //2015.08.09.
DoAdmin(player, args); DoAdmin(player, args);
break; break;
case "nonpresser": //2015.08.09.
break;
default: default:
return false; return false;
} }
return true; return true;
} }
/*if(args[0].toLowerCase()=="reload") /*if(args[0].toLowerCase()=="reload")
DoReload(null); //2015.07.20.*/ 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. DoAdmin(null, args); //2015.08.09.
return true; //2015.08.09.
}
return false; return false;
} }
private static void DoReload(Player player) private static void DoReload(Player player)
@ -111,6 +116,7 @@ public class Commands implements CommandExecutor {
System.out.println("Error!\n"+e); System.out.println("Error!\n"+e);
if(player!=null) if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r"); player.sendMessage("§cAn error occured. See console for details.§r");
PluginMain.LastException=e; //2015.08.09.
} }
//} //}
//else //else
@ -120,6 +126,7 @@ public class Commands implements CommandExecutor {
{ //2015.08.09. { //2015.08.09.
if(player==null || player.isOp() || player.getName()=="NorbiPeti") if(player==null || player.isOp() || player.getName()=="NorbiPeti")
{ {
//System.out.println("Args length: " + args.length);
if(args.length==1) if(args.length==1)
{ {
String message="§cUsage: /u admin reload|playerinfo§r"; String message="§cUsage: /u admin reload|playerinfo§r";
@ -135,6 +142,12 @@ public class Commands implements CommandExecutor {
case "playerinfo": case "playerinfo":
DoPlayerInfo(player, args); DoPlayerInfo(player, args);
break; break;
case "getlasterror":
DoGetLastError(player, args);
default:
String message="§cUsage: /u admin reload|playerinfo§r";
SendMessage(player, message);
return;
} }
} }
else else
@ -147,11 +160,13 @@ public class Commands implements CommandExecutor {
{ {
String message="§cUsage: /u admin playerinfo <player>§r"; String message="§cUsage: /u admin playerinfo <player>§r";
SendMessage(player, message); SendMessage(player, message);
return;
} }
if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2])) if(!MaybeOfflinePlayer.AllPlayers.containsKey(args[2]))
{ {
String message="§cPlayer not found: "+args[2]+"§r"; String message="§cPlayer not found: "+args[2]+"§r";
SendMessage(player, message); SendMessage(player, message);
return;
} }
MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]); MaybeOfflinePlayer p = MaybeOfflinePlayer.AllPlayers.get(args[2]);
SendMessage(player, "Player name: "+p.PlayerName); SendMessage(player, "Player name: "+p.PlayerName);
@ -159,7 +174,6 @@ public class Commands implements CommandExecutor {
SendMessage(player, "Username: "+p.UserName); SendMessage(player, "Username: "+p.UserName);
SendMessage(player, "Flair accepted: "+p.AcceptedFlair); SendMessage(player, "Flair accepted: "+p.AcceptedFlair);
SendMessage(player, "Flair ignored: "+p.IgnoredFlair); SendMessage(player, "Flair ignored: "+p.IgnoredFlair);
return;
} }
private static void SendMessage(Player player, String message) private static void SendMessage(Player player, String message)
{ //2015.08.09. { //2015.08.09.
@ -167,6 +181,17 @@ public class Commands implements CommandExecutor {
System.out.println(message); System.out.println(message);
else else
player.sendMessage(message); 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.");
} }
} }

View file

@ -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");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> 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<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -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 <player>§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");
}
}

View file

@ -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");
}
}

View file

@ -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<String, MaybeOfflinePlayer> 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);
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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 <player>§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.");
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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;
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

View file

@ -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");
}
}

View file

@ -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 <player>§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;
}
}

View file

@ -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 <player>§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.
}
}
}

View file

@ -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");
}
}

View file

@ -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<String, String> TownColors=new HashMap<String, String>(); //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<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname=""; //TODO
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
}
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
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.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> 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.
}
}
}

Some files were not shown because too many files have changed in this diff Show more