Gitignore
This commit is contained in:
parent
c075ef890a
commit
5d08552a7c
108 changed files with 68 additions and 32686 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
*.pydevproject
|
||||
.project
|
||||
.metadata
|
||||
.metadata/
|
||||
bin/
|
||||
tmp/
|
||||
*.tmp
|
||||
|
|
|
@ -1,425 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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;
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
}
|
||||
}
|
|
@ -1,436 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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;
|
||||
}
|
||||
}
|
|
@ -1,274 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin...");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,266 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§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);
|
||||
break;
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,430 +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)");
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,440 +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)");
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,265 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already ignored this request.§r");
|
||||
break;
|
||||
}
|
||||
/*case "reload": //2015.07.20.
|
||||
DoReload(player);
|
||||
break;*/
|
||||
case "admin": //2015.08.09.
|
||||
DoAdmin(player, args);
|
||||
break;
|
||||
case "nonpresser": //2015.08.09.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
player.sendMessage("§cYou need to accept the flair first.§r");
|
||||
break;
|
||||
}
|
||||
if(p.FlairDecided)
|
||||
{
|
||||
player.sendMessage("§cYou have already set the flair type.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§7(non-pr.)§r";
|
||||
SetPlayerFlair(player, p, "§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";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
//args[0] is "admin"
|
||||
switch(args[1].toLowerCase())
|
||||
{
|
||||
case "reload":
|
||||
ReloadPlayer=player; //2015.08.09.
|
||||
SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r");
|
||||
break;
|
||||
case "playerinfo":
|
||||
DoPlayerInfo(player, args);
|
||||
break;
|
||||
case "getlasterror":
|
||||
DoGetLastError(player, args);
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,403 +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)");
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -1,265 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already ignored this request.§r");
|
||||
break;
|
||||
}
|
||||
/*case "reload": //2015.07.20.
|
||||
DoReload(player);
|
||||
break;*/
|
||||
case "admin": //2015.08.09.
|
||||
DoAdmin(player, args);
|
||||
break;
|
||||
case "nonpresser": //2015.08.09.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
player.sendMessage("§cYou need to accept the flair first.§r");
|
||||
break;
|
||||
}
|
||||
if(p.FlairDecided)
|
||||
{
|
||||
player.sendMessage("§cYou have already set the flair type.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§7(non-pr.)§r";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
//args[0] is "admin"
|
||||
switch(args[1].toLowerCase())
|
||||
{
|
||||
case "reload":
|
||||
ReloadPlayer=player; //2015.08.09.
|
||||
SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r");
|
||||
break;
|
||||
case "playerinfo":
|
||||
DoPlayerInfo(player, args);
|
||||
break;
|
||||
case "getlasterror":
|
||||
DoGetLastError(player, args);
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,408 +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)");
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -1,277 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.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.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{ /2015.08.10.
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
String URL="https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar";
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
}
|
||||
}
|
|
@ -1,275 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin...");
|
||||
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,273 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Checking for updates...");
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import de.inventivegames.TellRawAutoMessage.Reflection;
|
||||
|
||||
public class PlayerListener implements Listener
|
||||
{ //2015.07.16.
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
Player p=event.getPlayer();
|
||||
//PluginMain.Players.add(p);
|
||||
//event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p));
|
||||
//if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20.
|
||||
if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
|
||||
{
|
||||
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName()));
|
||||
PluginMain.AppendPlayerDisplayFlair(player, p, flair)
|
||||
}
|
||||
else
|
||||
{ //2015.07.20.
|
||||
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
|
||||
sendRawMessage(p, json);
|
||||
}
|
||||
//System.out.println("Added player "+p.getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerLeave(PlayerQuitEvent event)
|
||||
{
|
||||
//for(Player player : PluginMain.Players)
|
||||
/*for(int i=0; i<PluginMain.Players.size();)
|
||||
{
|
||||
Player player=PluginMain.Players.get(i);
|
||||
if(player.getName().equals(event.getPlayer().getName()))
|
||||
{
|
||||
PluginMain.Players.remove(player);
|
||||
//System.out.println("Removed player "+event.getPlayer().getName());
|
||||
}
|
||||
else
|
||||
i++; //If the player is removed, the next item will be on the same index
|
||||
}*/
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event)
|
||||
{
|
||||
//event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+"(test)> "+event.getMessage()); //2015.08.08.
|
||||
String name;
|
||||
event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08.
|
||||
}
|
||||
|
||||
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
|
||||
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
|
||||
public static void sendRawMessage(Player player, String message)
|
||||
{
|
||||
try {
|
||||
System.out.println("1");
|
||||
Object handle = Reflection.getHandle(player);
|
||||
System.out.println("2");
|
||||
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
|
||||
System.out.println("3");
|
||||
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
|
||||
System.out.println("4");
|
||||
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
|
||||
System.out.println("5");
|
||||
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
|
||||
System.out.println("6");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,277 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.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.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
String URL="https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar";
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
}
|
||||
}
|
|
@ -1,436 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,268 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,276 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -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.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");
|
||||
if()
|
||||
try {
|
||||
BufferedWriter bw;
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
bw.write(player.getName()+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,425 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -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.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())
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,274 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin...");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,425 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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;
|
||||
}
|
||||
}
|
|
@ -1,428 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
if(start==-1+"IGN:".length()) //
|
||||
continue; //2015.08.09.
|
||||
int end = ign.indexOf(' ', start);
|
||||
if (end == -1 || end == start)
|
||||
end=ign.indexOf('\n', start); //2015.07.15.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
System.out.println("Trimmed IGN: "+ign);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,426 +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)");
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
TownColors.put(s[0], s[1]);
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
file=new File("customflairs.txt"); //2015.08.09.
|
||||
if(file.exists())
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
while((line=br.readLine())!=null)
|
||||
{
|
||||
String[] s=line.split(" ");
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09.
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
//throw new IOException("Test"); //2015.08.09.
|
||||
System.out.println("Auto-flair plugin loaded files!");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static void SaveFiles() //<-- 2015.08.09.
|
||||
{
|
||||
try
|
||||
{
|
||||
FileWriter fw;
|
||||
fw = new FileWriter("flairsaccepted.txt");
|
||||
fw.close();
|
||||
fw = new FileWriter("flairsignored.txt");
|
||||
fw.close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
try {
|
||||
File file=new File("flairsaccepted.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
file=new File("flairsignored.txt");
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.IgnoredFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,273 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,408 +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)");
|
||||
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("autoflairdecisions.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values())
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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.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 = 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;
|
||||
}
|
||||
}
|
|
@ -1,427 +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)");
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
boolean successful = tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,411 +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)");
|
||||
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
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09., String flair)
|
||||
{ //2015.08.09.
|
||||
flair=flair.replace('&', '§');
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair;
|
||||
File file=new File("customflairs.txt");
|
||||
BufferedWriter bw;
|
||||
try {
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
bw.write(player.getName()+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,427 +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)");
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
if(start==-1+"IGN:".length()) //+length: 2015.08.10.
|
||||
continue; //2015.08.09.
|
||||
int end = ign.indexOf(' ', start);
|
||||
if (end == -1 || end == start)
|
||||
end=ign.indexOf('\n', start); //2015.07.15.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
System.out.println("Trimmed IGN: "+ign);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,275 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
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.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
System.out.println("Trimmed IGN: "+ign);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,440 +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)");
|
||||
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"))
|
||||
{
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,403 +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)");
|
||||
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.getName()+"\n");
|
||||
bw.close();
|
||||
}
|
||||
}
|
|
@ -1,266 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -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.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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,268 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,436 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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;
|
||||
}
|
||||
}
|
|
@ -1,427 +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)");
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09.
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
//throw new IOException("Test"); //2015.08.09.
|
||||
System.out.println("Auto-flair plugin loaded files!");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static void SaveFiles() //<-- 2015.08.09.
|
||||
{
|
||||
try
|
||||
{
|
||||
FileWriter fw;
|
||||
fw = new FileWriter("flairsaccepted.txt");
|
||||
fw.close();
|
||||
fw = new FileWriter("flairsignored.txt");
|
||||
fw.close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
try {
|
||||
File file=new File("flairsaccepted.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
file=new File("flairsignored.txt");
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.IgnoredFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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();
|
||||
tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
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.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
System.out.println("Trimmed IGN: "+ign);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import de.inventivegames.TellRawAutoMessage.Reflection;
|
||||
|
||||
public class PlayerListener implements Listener
|
||||
{ //2015.07.16.
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
Player p=event.getPlayer();
|
||||
//PluginMain.Players.add(p);
|
||||
//event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p));
|
||||
//if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20.
|
||||
if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
|
||||
{
|
||||
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
|
||||
PluginMain.AppendPlayerDisplayFlair(p, mp.UserName, mp.Flair);
|
||||
}
|
||||
else
|
||||
{ //2015.07.20.
|
||||
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
|
||||
sendRawMessage(p, json);
|
||||
}
|
||||
//System.out.println("Added player "+p.getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerLeave(PlayerQuitEvent event)
|
||||
{
|
||||
//for(Player player : PluginMain.Players)
|
||||
/*for(int i=0; i<PluginMain.Players.size();)
|
||||
{
|
||||
Player player=PluginMain.Players.get(i);
|
||||
if(player.getName().equals(event.getPlayer().getName()))
|
||||
{
|
||||
PluginMain.Players.remove(player);
|
||||
//System.out.println("Removed player "+event.getPlayer().getName());
|
||||
}
|
||||
else
|
||||
i++; //If the player is removed, the next item will be on the same index
|
||||
}*/
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event)
|
||||
{
|
||||
//event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+"(test)> "+event.getMessage()); //2015.08.08.
|
||||
String name;
|
||||
event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08.
|
||||
}
|
||||
|
||||
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
|
||||
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
|
||||
public static void sendRawMessage(Player player, String message)
|
||||
{
|
||||
try {
|
||||
System.out.println("1");
|
||||
Object handle = Reflection.getHandle(player);
|
||||
System.out.println("2");
|
||||
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
|
||||
System.out.println("3");
|
||||
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
|
||||
System.out.println("4");
|
||||
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
|
||||
System.out.println("5");
|
||||
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
|
||||
System.out.println("6");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,429 +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)");
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,277 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.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.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,409 +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)");
|
||||
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) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
System.out.println("Start: "+start);
|
||||
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.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
System.out.println("Trimmed IGN: "+start);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,269 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,274 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,408 +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)");
|
||||
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)
|
||||
{ //2015.08.09.
|
||||
flair=flair.replace('&', '§');
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair;
|
||||
File file=new File("autoflairdecisions.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values())
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
}
|
||||
}
|
|
@ -1,436 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,425 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,430 +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)");
|
||||
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(file);
|
||||
File tempFile = new File("myTempFile.txt");
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
boolean successful = tempFile.renameTo(inputFile);
|
||||
}
|
||||
}
|
|
@ -1,440 +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)");
|
||||
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.SendMessage(player, "")
|
||||
}
|
||||
File file=new File("customflairs.txt");
|
||||
try {
|
||||
BufferedWriter bw;
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
bw.write(player.getName()+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,425 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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; //2015.08.09.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,268 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,264 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already ignored this request.§r");
|
||||
break;
|
||||
}
|
||||
/*case "reload": //2015.07.20.
|
||||
DoReload(player);
|
||||
break;*/
|
||||
case "admin": //2015.08.09.
|
||||
DoAdmin(player, args);
|
||||
break;
|
||||
case "nonpresser": //2015.08.09.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
player.sendMessage("§cYou need to accept the flair first.§r");
|
||||
break;
|
||||
}
|
||||
if(p.FlairDecided)
|
||||
{
|
||||
player.sendMessage("§cYou have already set the flair type.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§7(non-pr.)§r";
|
||||
SetPlayerFlair(player, p, "§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|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
//args[0] is "admin"
|
||||
switch(args[1].toLowerCase())
|
||||
{
|
||||
case "reload":
|
||||
ReloadPlayer=player; //2015.08.09.
|
||||
SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r");
|
||||
break;
|
||||
case "playerinfo":
|
||||
DoPlayerInfo(player, args);
|
||||
break;
|
||||
case "getlasterror":
|
||||
DoGetLastError(player, args);
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.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 bool RemoveLineFromFile(String file)
|
||||
{
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
boolean successful = tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,277 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.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.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
String URL="https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar",
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import de.inventivegames.TellRawAutoMessage.Reflection;
|
||||
|
||||
public class PlayerListener implements Listener
|
||||
{ //2015.07.16.
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
Player p=event.getPlayer();
|
||||
//PluginMain.Players.add(p);
|
||||
//event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p));
|
||||
//if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20.
|
||||
if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
|
||||
{
|
||||
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
|
||||
PluginMain.AppendPlayerDisplayFlair(p, mp.UserName, mp.Flair);
|
||||
}
|
||||
else
|
||||
{ //2015.07.20.
|
||||
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
|
||||
sendRawMessage(p, json);
|
||||
}
|
||||
//System.out.println("Added player "+p.getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerLeave(PlayerQuitEvent event)
|
||||
{
|
||||
//for(Player player : PluginMain.Players)
|
||||
/*for(int i=0; i<PluginMain.Players.size();)
|
||||
{
|
||||
Player player=PluginMain.Players.get(i);
|
||||
if(player.getName().equals(event.getPlayer().getName()))
|
||||
{
|
||||
PluginMain.Players.remove(player);
|
||||
//System.out.println("Removed player "+event.getPlayer().getName());
|
||||
}
|
||||
else
|
||||
i++; //If the player is removed, the next item will be on the same index
|
||||
}*/
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event)
|
||||
{
|
||||
//event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+"(test)> "+event.getMessage()); //2015.08.08.
|
||||
String flair=MaybeOfflinePlayer.AllPlayers.get(event.getPlayer().getName()).Flair;
|
||||
event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08.
|
||||
}
|
||||
|
||||
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
|
||||
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
|
||||
public static void sendRawMessage(Player player, String message)
|
||||
{
|
||||
try {
|
||||
System.out.println("1");
|
||||
Object handle = Reflection.getHandle(player);
|
||||
System.out.println("2");
|
||||
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
|
||||
System.out.println("3");
|
||||
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
|
||||
System.out.println("4");
|
||||
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
|
||||
System.out.println("5");
|
||||
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
|
||||
System.out.println("6");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,431 +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)");
|
||||
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();
|
||||
//System.out.println("Start: "+start);
|
||||
if(start==-1+"IGN:".length()) //+length: 2015.08.10.
|
||||
continue; //2015.08.09.
|
||||
int end = ign.indexOf(' ', start);
|
||||
if (end == -1 || end == start)
|
||||
end=ign.indexOf('\n', start); //2015.07.15.
|
||||
System.out.println("End: "+end);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
//System.out.println("IGN: "+ign);
|
||||
ign = ign.trim();
|
||||
//System.out.println("Trimmed IGN: "+ign);
|
||||
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(" ");
|
||||
if(s.length>=2) //<-- 2015.08.10.
|
||||
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(" ");
|
||||
if(s.length>=2) //2015.08.10.
|
||||
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+" "+player.Flair+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,274 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin...");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,265 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
//args[0] is "admin"
|
||||
switch(args[1].toLowerCase())
|
||||
{
|
||||
case "reload":
|
||||
ReloadPlayer=player; //2015.08.09.
|
||||
SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r");
|
||||
break;
|
||||
case "playerinfo":
|
||||
DoPlayerInfo(player, args);
|
||||
break;
|
||||
case "getlasterror":
|
||||
DoGetLastError(player, args);
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,408 +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)");
|
||||
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)
|
||||
{ //2015.08.09.
|
||||
flair=flair.replace('&', '§');
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(player.getName()).Flair=flair;
|
||||
File file=new File("flairsaccepted.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values())
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
}
|
||||
}
|
|
@ -1,426 +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)");
|
||||
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(" ");
|
||||
if(s.length>=2)
|
||||
TownColors.put(s[0], s[1]);
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
file=new File("customflairs.txt"); //2015.08.09.
|
||||
if(file.exists())
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
while((line=br.readLine())!=null)
|
||||
{
|
||||
String[] s=line.split(" ");
|
||||
MaybeOfflinePlayer.AddPlayerIfNeeded(s[0]).Flair=s[1]; //2015.08.09.
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
//throw new IOException("Test"); //2015.08.09.
|
||||
System.out.println("Auto-flair plugin loaded files!");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static void SaveFiles() //<-- 2015.08.09.
|
||||
{
|
||||
try
|
||||
{
|
||||
FileWriter fw;
|
||||
fw = new FileWriter("flairsaccepted.txt");
|
||||
fw.close();
|
||||
fw = new FileWriter("flairsignored.txt");
|
||||
fw.close();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
try {
|
||||
File file=new File("flairsaccepted.txt");
|
||||
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.AcceptedFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
file=new File("flairsignored.txt");
|
||||
bw = new BufferedWriter(new FileWriter(file, true));
|
||||
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
|
||||
{
|
||||
if(!player.IgnoredFlair)
|
||||
continue; //2015.08.08.
|
||||
bw.write(player.PlayerName+"\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
}
|
||||
public static boolean RemoveLineFromFile(String file, String line)
|
||||
{ //2015.08.09.
|
||||
File inputFile = new File(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
if(!inputFile.exists())
|
||||
return true; //2015.08.10.
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = line;
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
//if(trimmedLine.equals(lineToRemove)) continue;
|
||||
if(trimmedLine.contains(lineToRemove)) continue; //2015.08.09.
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
return tempFile.renameTo(inputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
LastException=e; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import de.inventivegames.TellRawAutoMessage.Reflection;
|
||||
|
||||
public class PlayerListener implements Listener
|
||||
{ //2015.07.16.
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
Player p=event.getPlayer();
|
||||
//PluginMain.Players.add(p);
|
||||
//event.getPlayer().setDisplayName(p.getDisplayName()+PluginMain.GetFlair(p));
|
||||
//if(PluginMain.PlayerUserNames.containsKey(p.getName())) //<-- 2015.07.20.
|
||||
if(MaybeOfflinePlayer.AllPlayers.containsKey(p.getName())) //<-- 2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
|
||||
{
|
||||
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
|
||||
PluginMain.AppendPlayerDisplayFlair(player, p, flair)
|
||||
}
|
||||
else
|
||||
{ //2015.07.20.
|
||||
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
|
||||
sendRawMessage(p, json);
|
||||
}
|
||||
//System.out.println("Added player "+p.getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerLeave(PlayerQuitEvent event)
|
||||
{
|
||||
//for(Player player : PluginMain.Players)
|
||||
/*for(int i=0; i<PluginMain.Players.size();)
|
||||
{
|
||||
Player player=PluginMain.Players.get(i);
|
||||
if(player.getName().equals(event.getPlayer().getName()))
|
||||
{
|
||||
PluginMain.Players.remove(player);
|
||||
//System.out.println("Removed player "+event.getPlayer().getName());
|
||||
}
|
||||
else
|
||||
i++; //If the player is removed, the next item will be on the same index
|
||||
}*/
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event)
|
||||
{
|
||||
//event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+"(test)> "+event.getMessage()); //2015.08.08.
|
||||
String name;
|
||||
event.setFormat(event.getFormat().substring(0, event.getFormat().indexOf(">"))+flair+"> "+event.getMessage()); //2015.08.08.
|
||||
}
|
||||
|
||||
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
|
||||
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
|
||||
public static void sendRawMessage(Player player, String message)
|
||||
{
|
||||
try {
|
||||
System.out.println("1");
|
||||
Object handle = Reflection.getHandle(player);
|
||||
System.out.println("2");
|
||||
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
|
||||
System.out.println("3");
|
||||
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
|
||||
System.out.println("4");
|
||||
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
|
||||
System.out.println("5");
|
||||
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
|
||||
System.out.println("6");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,409 +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)");
|
||||
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.
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,430 +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)");
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,430 +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)");
|
||||
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(file);
|
||||
File tempFile = new File("_temp.txt");
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
|
||||
String lineToRemove = "bbb";
|
||||
String currentLine;
|
||||
|
||||
while((currentLine = reader.readLine()) != null) {
|
||||
// trim newline when comparing with lineToRemove
|
||||
String trimmedLine = currentLine.trim();
|
||||
if(trimmedLine.equals(lineToRemove)) continue;
|
||||
writer.write(currentLine + System.getProperty("line.separator"));
|
||||
}
|
||||
writer.close();
|
||||
reader.close();
|
||||
boolean successful = tempFile.renameTo(inputFile);
|
||||
}
|
||||
}
|
|
@ -1,277 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.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.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-Flair plugin...");
|
||||
System.out.println("Forced updating of Auto-Flair plugin...");
|
||||
https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
FileUtils.copyURLToFile(URL, File)
|
||||
}
|
||||
}
|
|
@ -1,269 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -1,265 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already ignored this request.§r");
|
||||
break;
|
||||
}
|
||||
/*case "reload": //2015.07.20.
|
||||
DoReload(player);
|
||||
break;*/
|
||||
case "admin": //2015.08.09.
|
||||
DoAdmin(player, args);
|
||||
break;
|
||||
case "nonpresser": //2015.08.09.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
player.sendMessage("§cYou need to accept the flair first.§r");
|
||||
break;
|
||||
}
|
||||
if(p.FlairDecided)
|
||||
{
|
||||
player.sendMessage("§cYou have already set the flair type.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§7(non-pr.)§r";
|
||||
SetPlayerFlair(player, p, "§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";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
//args[0] is "admin"
|
||||
switch(args[1].toLowerCase())
|
||||
{
|
||||
case "reload":
|
||||
ReloadPlayer=player; //2015.08.09.
|
||||
SendMessage(player, "§6Make sure to save the current settings before you modify and reload them! Type /u admin confirm when done.§r");
|
||||
break;
|
||||
case "playerinfo":
|
||||
DoPlayerInfo(player, args);
|
||||
break;
|
||||
case "getlasterror":
|
||||
DoGetLastError(player, args);
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair§r";
|
||||
SendMessage(player, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou need to be OP to use this command.§r");
|
||||
}
|
||||
private static void DoPlayerInfo(Player player, String[] args)
|
||||
{ //2015.08.09.
|
||||
//args[0] is "admin" - args[1] is "playerinfo"
|
||||
if(args.length==2)
|
||||
{
|
||||
String message="§cUsage: /u admin playerinfo <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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
}
|
|
@ -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.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()
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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.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;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,274 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Checking for updates...");
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
|
@ -1,275 +0,0 @@
|
|||
package tk.sznp.thebuttonautoflair;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Commands implements CommandExecutor {
|
||||
// This method is called, when somebody uses our command
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if(args.length<1)
|
||||
return false;
|
||||
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
|
||||
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
|
||||
if(p.Flair==null)
|
||||
{
|
||||
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
|
||||
return true;
|
||||
}
|
||||
switch(args[0].toLowerCase()) //toLowerCase: 2015.08.09.
|
||||
{
|
||||
case "accept":
|
||||
{
|
||||
if(p.IgnoredFlair)
|
||||
p.IgnoredFlair=false; //2015.08.08.
|
||||
if(!p.AcceptedFlair)
|
||||
{
|
||||
String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
p.AcceptedFlair=true; //2015.08.08.
|
||||
PluginMain.AppendPlayerDisplayFlair(p, player);
|
||||
player.sendMessage("§6Your flair has been set:§r "+flair);
|
||||
}
|
||||
else
|
||||
player.sendMessage("§cYou already have this user's flair.§r");
|
||||
break;
|
||||
}
|
||||
case "ignore":
|
||||
{
|
||||
if(p.AcceptedFlair)
|
||||
p.AcceptedFlair=false; //2015.08.08.
|
||||
if(!p.IgnoredFlair)
|
||||
{
|
||||
p.IgnoredFlair=true;
|
||||
//String flair=p.Flair; //2015.08.08.
|
||||
//PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
|
||||
player.sendMessage("§6You have ignored this request. You can still 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";
|
||||
SetPlayerFlair(player, p, "§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 or your flair type is known.§r");
|
||||
break;
|
||||
}
|
||||
//p.Flair="§r(can't press)§r";
|
||||
SetPlayerFlair(player, p, "§r(can't pr.)§r");
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*if(args[0].toLowerCase()=="reload")
|
||||
DoReload(null); //2015.07.20.*/
|
||||
else if(args.length>0 && args[0].toLowerCase().equals("admin")) //2015.08.09.
|
||||
{
|
||||
DoAdmin(null, args); //2015.08.09.
|
||||
return true; //2015.08.09.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static void DoReload(Player player)
|
||||
{ //2015.07.20.
|
||||
//if(player==null || player.isOp() || player.getName()=="NorbiPeti")
|
||||
//{
|
||||
try
|
||||
{
|
||||
File file=new File("autoflairconfig.txt");
|
||||
if(file.exists())
|
||||
{
|
||||
PluginMain.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|getlasterror|save|setflair|updateplugin§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);
|
||||
break; //<-- 2015.08.10.
|
||||
case "confirm":
|
||||
if(ReloadPlayer==player)
|
||||
DoReload(player); //2015.08.09.
|
||||
else
|
||||
SendMessage(player, "§cYou need to do /u admin reload first.§r");
|
||||
break;
|
||||
case "save":
|
||||
PluginMain.SaveFiles(); //2015.08.09.
|
||||
SendMessage(player, "§6Saved files. Now you can edit them and reload if you want.§r");
|
||||
break;
|
||||
case "setflair":
|
||||
DoSetFlair(player, args);
|
||||
break;
|
||||
case "updateplugin": //2015.08.10.
|
||||
DoUpdatePlugin(player);
|
||||
break;
|
||||
default:
|
||||
String message="§cUsage: /u admin reload|playerinfo|getlasterror|save|setflair|updateplugin§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.PlayerName+"\n");
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error!\n"+e);
|
||||
PluginMain.LastException=e; //2015.08.09.
|
||||
}
|
||||
SendMessage(player, "§6The flair has been set. Player: "+targetplayer.PlayerName+" Flair: "+flair+"§r");
|
||||
}
|
||||
private static void DoSetFlair(Player player, String[] args)
|
||||
{
|
||||
//args[0] is "admin" - args[1] is "setflair"
|
||||
if(args.length<4)
|
||||
{
|
||||
SendMessage(player, "§cUsage: /u admin setflair <playername> <flair>");
|
||||
return;
|
||||
}
|
||||
SetPlayerFlair(player, MaybeOfflinePlayer.AddPlayerIfNeeded(args[2]), args[3]);
|
||||
}
|
||||
private static void DoUpdatePlugin(Player player)
|
||||
{
|
||||
SendMessage(player, "Updating Auto-flair plugin...");
|
||||
|
||||
//https://github.com/NorbiPeti/thebuttonautoflairmc/raw/master/TheButtonAutoFlair.jar
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue