Added can't press flair, OO, compatible with Essentials

- Added can't press flair
- Made an object, instead of many Maps
- Using chat formatting instead of display names
This commit is contained in:
Norbi Peti 2015-08-08 22:58:45 +02:00
parent ecf55a86e2
commit 6eddb90154
822 changed files with 38114 additions and 123388 deletions

View file

@ -276,3 +276,180 @@ org.eclipse.core.runtime.AssertionFailedException: assertion failed:
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438) at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
!SESSION 2015-07-27 17:30:41.381 -----------------------------------------------
eclipse.buildId=M20130204-1200
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2015-07-27 17:31:56.400
!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.egit.ui 2 0 2015-07-27 17:31:56.421
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!SESSION 2015-08-08 18:30:00.907 -----------------------------------------------
eclipse.buildId=M20130204-1200
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU
Framework arguments: -product org.eclipse.epp.package.jee.product -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product -data D:\Z - Norbi cucca\0 Projektek\TheButtonMCAutoFlairProto\Spigot plugin -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2015-08-08 18:30:23.469
!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.egit.ui 2 0 2015-08-08 18:30:23.581
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE Keybinding conflicts occurred. They may interfere with normal accelerator operation.
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for ALT+SHIFT+ARROW_DOWN:
Binding(ALT+SHIFT+ARROW_DOWN,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.java.select.last,Restore Last Selection,
Restore last selection,
Category(org.eclipse.ui.category.edit,Edit,null,true),
org.eclipse.ui.internal.MakeHandlersGo@7ce87f9d,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.jdt.ui.javaEditorScope,,,system)
Binding(ALT+SHIFT+ARROW_DOWN,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.markTaskReadGoToNextUnread,Mark Task Read and Go To Next Unread Task,
,
Category(org.eclipse.mylyn.tasks.ui.commands,Task Repositories,null,true),
org.eclipse.ui.internal.MakeHandlersGo@e944541,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for ALT+ARROW_DOWN:
Binding(ALT+ARROW_DOWN,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.goToNextUnread,Go To Next Unread Task,
,
Category(org.eclipse.ui.category.navigate,Navigate,null,true),
org.eclipse.ui.internal.MakeHandlersGo@6198e9b5,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
Binding(ALT+ARROW_DOWN,
ParameterizedCommand(Command(org.eclipse.ui.edit.text.moveLineDown,Move Lines Down,
Moves the selected lines down,
Category(org.eclipse.ui.category.textEditor,Text Editing,Text Editing Commands,true),
org.eclipse.ui.internal.MakeHandlersGo@1c89cec5,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.textEditorScope,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for ALT+SHIFT+ARROW_UP:
Binding(ALT+SHIFT+ARROW_UP,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.java.select.enclosing,Select Enclosing Element,
Expand selection to include enclosing element,
Category(org.eclipse.ui.category.edit,Edit,null,true),
org.eclipse.ui.internal.MakeHandlersGo@1447bef2,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.jdt.ui.javaEditorScope,,,system)
Binding(ALT+SHIFT+ARROW_UP,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.markTaskReadGoToPreviousUnread,Mark Task Read and Go To Previous Unread Task,
,
Category(org.eclipse.mylyn.tasks.ui.commands,Task Repositories,null,true),
org.eclipse.ui.internal.MakeHandlersGo@2b6c3f70,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for ALT+SHIFT+U:
Binding(ALT+SHIFT+U,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.remove.occurrence.annotations,Remove Occurrence Annotations,
Removes the occurrence annotations from the current editor,
Category(org.eclipse.jdt.ui.category.source,Source,Java Source Actions,true),
org.eclipse.ui.internal.MakeHandlersGo@77ec911,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.jdt.ui.javaEditorScope,,,system)
Binding(ALT+SHIFT+U,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.markTaskUnread,Mark Task Unread,
,
Category(org.eclipse.mylyn.tasks.ui.commands,Task Repositories,null,true),
org.eclipse.ui.internal.MakeHandlersGo@55d3c0dd,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for INSERT:
Binding(INSERT,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.new.local.task,New Local Task,
,
Category(org.eclipse.mylyn.tasks.ui.commands,Task Repositories,null,true),
org.eclipse.ui.internal.MakeHandlersGo@7c4a44bb,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
Binding(INSERT,
ParameterizedCommand(Command(org.eclipse.ui.edit.text.toggleOverwrite,Toggle Overwrite,
Toggle overwrite mode,
Category(org.eclipse.ui.category.textEditor,Text Editing,Text Editing Commands,true),
org.eclipse.ui.internal.MakeHandlersGo@369773fd,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.textEditorScope,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2015-08-08 20:21:36.761
!MESSAGE A conflict occurred for ALT+ARROW_UP:
Binding(ALT+ARROW_UP,
ParameterizedCommand(Command(org.eclipse.mylyn.tasks.ui.command.goToPreviousUnread,Go To Previous Unread Task,
,
Category(org.eclipse.ui.category.navigate,Navigate,null,true),
org.eclipse.ui.internal.MakeHandlersGo@5d7399f9,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.mylyn.tasks.ui.views.tasks,,,system)
Binding(ALT+ARROW_UP,
ParameterizedCommand(Command(org.eclipse.ui.edit.text.moveLineUp,Move Lines Up,
Moves the selected lines up,
Category(org.eclipse.ui.category.textEditor,Text Editing,Text Editing Commands,true),
org.eclipse.ui.internal.MakeHandlersGo@47d0127f,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.textEditorScope,,,system)
!SESSION 2015-08-08 22:18:57.768 -----------------------------------------------
eclipse.buildId=M20130204-1200
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=hu_HU
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2015-08-08 22:19:31.422
!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.egit.ui 2 0 2015-08-08 22:19:31.528
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Norbi'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.

Binary file not shown.

Binary file not shown.

View file

@ -1,343 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i));
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,68 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
public static void sendRawMessage(Player player, String message) {
try {
System.out.prinom
Object handle = Reflection.getHandle(player);
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -1,48 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.add(player.getName());
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
player.sendMessage("§6Your flair has been set:§r "+flair);
break;
case "ignore":
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,7 @@
package tk.sznp.thebuttonautoflair;
public class MaybeOfflinePlayer {
String PlayerName;
String UserName;
String Flair;
}

View file

@ -1,307 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String[] line=br.readLine().split(" ");
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,431 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
//System.out.println("Name: "+name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -0,0 +1,125 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(p.Flair==null)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
{
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
{
String flair=PluginMain.PlayerFlairs.get(p.getName());
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -0,0 +1,434 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
System.out.println("Name: " + name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -1,73 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("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.prinln("3");
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
System.out.prinln("4");
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
System.out.prinln("5");
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
System.out.prinln("6");
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,25 @@
package tk.sznp.thebuttonautoflair;
import java.util.HashMap;
public class MaybeOfflinePlayer
{ //2015.08.08.
public String PlayerName;
public String UserName;
public String Flair;
public boolean AcceptedFlair;
public boolean IgnoredFlair;
public String DisplayName;
public static HashMap<String, MaybeOfflinePlayer> AllPlayers=new HashMap<>(); //2015.08.08.
public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername)
{
if(!AllPlayers.containsKey(playername))
{
MaybeOfflinePlayer player=new MaybeOfflinePlayer();
player.PlayerName=playername;
AllPlayers.put(playername, player);
return player;
}
return AllPlayers.get(playername);
}
}

View file

@ -0,0 +1,74 @@
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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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().replaceFirst(event.getMessage(), "")+"(test)"+event.getMessage());
}
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();
}
}
}

View file

@ -1,383 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
System.out.println("pos: "+pos+" nextpos: "+nextpos);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,312 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PlayerTowns.put(s[0], s[1])
}
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -0,0 +1,5 @@
package tk.sznp.thebuttonautoflair;
public class MaybeOfflinePlayer {
}

View file

@ -0,0 +1,418 @@
package tk.sznp.thebuttonautoflair;
import 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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.s
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
}

View file

@ -1,67 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
public static void sendRawMessage(Player player, String message) {
try {
Object handle = Reflection.getHandle(player);
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -1,371 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,286 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
if(flairdata.length>1)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
}

View file

@ -0,0 +1,126 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(p.Flair==null)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
for(Player p : PluginMain.GetPlayers())
{
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
if(mp.HasFlair)
{
String flair=mp.Flair;
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -1,73 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedWriter;
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;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("flairsaccepted.txt", true));
bw.write(player.getName()+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,382 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,319 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String name)
{
}
}

View file

@ -1,49 +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;
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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{
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\"}]}}}]";
}
//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)
{
}
}

View file

@ -1,27 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerListener implements Listener
{ //2015.07.16.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
PluginMain.Players.add(event.getPlayer());
}
@EventHandler
public void onPlayerLeave(PlayerLeaveEvent event)
{
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event)
{
}
}

View file

@ -1,382 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos-1, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,92 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
try
{
File 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(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,64 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,337 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,37 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
PluginMain.AcceptedPlayers.add(player.getName());
player.setDisplayName(player.getDisplayName()+PluginMain.PlayerFlairs.get(player.getName()));
break;
case "ignore":
PluginMain.IgnoredPlayers.add(player.getName());
break;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,336 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,126 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(p.Flair==null)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
for(Player p : PluginMain.GetPlayers())
{
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
if(p.Flair!=null)
{
String flair=p.Flair;
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -1,41 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
PluginMain.AcceptedPlayers.add(player.getName());
player.setDisplayName(player.getDisplayName()+PluginMain.PlayerFlairs.get(player.getName()));
break;
case "ignore":
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
PluginMain.IgnoredPlayers.add(player.getName());
break;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,294 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForClass(flairclass);
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -1,61 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("$cYou already have this flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,16 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.util.function.Predicate;
public class MyPredicate<T> implements Predicate<T>
{
public MyPredicate(T first)
{
}
@Override
public boolean test(T second) {
return false;
}
}

View file

@ -1,63 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("$cYou already have this flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r";)
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,24 @@
package tk.sznp.thebuttonautoflair;
import java.util.ArrayList;
import java.util.HashMap;
public class MaybeOfflinePlayer {
public String PlayerName;
public String UserName;
public String Flair;
public boolean AcceptedFlair;
public boolean IgnoredFlair;
public String TownColors;
public String DisplayName;
public static HashMap<String, MaybeOfflinePlayer> AllPlayers=new HashMap<>(); //2015.08.08.
public static void AddPlayer(String playername)
{
if(!AllPlayers.contains(name))
{
MaybeOfflinePlayer player=new MaybeOfflinePlayer();
player.PlayerName=playername;
AllPlayers.put(playername, player);
}
}
}

View file

@ -0,0 +1,74 @@
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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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().replaceFirst(event.getMessage(), "")+"(test)"+event.getMessage());
}
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();
}
}
}

View file

@ -1,292 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -1,333 +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.HashMap;
import java.util.List;
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.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.TownyWorld;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (NotRegisteredException e) {
return "";
}
}
}

View file

@ -1,343 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,354 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr=displayname.substring(pos, nextpos-pos);
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,382 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
String substr="§"+colors[x++]+displayname.substring(pos-1, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos-1)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,69 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
public static void sendRawMessage(Player player, String message) {
try {
System.out.prinln("1");
Object handle = Reflection.getHandle(player);
System.out.prinln("2");
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,421 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
PluginManager manager = getServer().getPluginManager();
manager.registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
}

View file

@ -1,307 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.txt");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line=br.readLine();
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -1,292 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//player.setDisplayName(player.getDisplayName()+flair);
}
}

View file

@ -0,0 +1,74 @@
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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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().replaceFirst(event.getMessage(), "")+"(test)"+event.getMessage());
}
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();
}
}
}

View file

@ -1,63 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("$cYou already have this flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,384 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length()-1;
System.out.println("pos: "+pos+" nextpos: "+nextpos);
System.out.println("nextpos-pos: "+nextpos-pos);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
//int nextpos=i+unit;
String substr="§"+colors[x++]+displayname.substring(pos, unit)+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,126 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(!p.HasFlair)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
for(Player p : PluginMain.GetPlayers())
{
MaybeOfflinePlayer mp = MaybeOfflinePlayer.AllPlayers.get(p.getName());
if(mp.Flair!=null)
{
String flair=mp.Flair;
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -0,0 +1,431 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
System.out.println("Name: "+name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -1,361 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -0,0 +1,437 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
System.out.println("Name: " + name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
System.out.println("PlayerName: "+player.getName());
System.out.println(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair);
System.out.println(MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair);
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -1,21 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerListener implements Listener
{
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
PluginMain.Players.add(event.getPlayer());
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event)
{
}
}

View file

@ -1,244 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
br=new BufferedReader(new FileReader("flairsignored.txt"));
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
if(flairdata.length>1)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
}

View file

@ -1,341 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
int x=0>
while((x=displayname.indexOf("§", x)))
Positions.add(x)
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,343 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=displayname.length()-1; i>=0; i--) {
if(Character.isUpperCase(str.charAt(i))) {
return i;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,254 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
if(flairdata.length>1)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
{
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("flairsaccepted.txt", true));
bw.write(player.getName()+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
}

View file

@ -1,85 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
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(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,307 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -1,291 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{
}
}

View file

@ -1,50 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.add(player.getName());
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
player.sendMessage("§6Your flair has been set:§r "+flair);
break;
case "ignore":
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
player.setDisplayName(.substring(0, dname));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,285 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch
{
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
if(flairdata.length>1)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
player.setDisplayName(player.getDisplayName()+flair);
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
}

View file

@ -1,94 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
player.sendMessage(")
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,391 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(!(Positions.size()>=colors.length)) //TODO
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,92 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
if(player.isOp())
{
try
{
File 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(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,43 +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;
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));
System.out.println("Added player "+event.getPlayer().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()==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)
{
}
}

View file

@ -1,307 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
File file=new File("autoflairconfig.ini");
try {
BufferedReader br=new BufferedReader(new FileReader(file));
br.readLine();
br.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
return "";
}
}

View file

@ -1,67 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload":
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,397 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,312 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
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(" ");
PlayerTowns.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{
return "";
}
}

View file

@ -1,335 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color), dname.indexOf(flair)));
}
}

View file

@ -1,389 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(!(Positions.size()>=colors.length)) //TODO
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
String substr="§"+colors[x++]+displayname.substring(pos, nextpos+"§r";
finalname+=substr;
}
System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,292 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -0,0 +1,419 @@
package tk.sznp.thebuttonautoflair;
import 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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
}

View file

@ -1,31 +0,0 @@
package tk.sznp.thebuttonautoflair;
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;
public class PlayerListener implements Listener
{ //2015.07.16.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
PluginMain.Players.add(event.getPlayer());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
for(Player player : PluginMain.Players)
{
}
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event)
{
}
}

View file

@ -1,41 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
PluginMain.AcceptedPlayers.add(player.getName());
player.setDisplayName(player.getDisplayName()+PluginMain.PlayerFlairs.get(player.getName()));
break;
case "ignore":
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
PluginMain.AcceptedPlayers.add(player.getName());
break;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -1,355 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr=displayname.substring(pos, nextpos-pos);
finalname+=substr;
}
}
player.setDisplayName(color+displayname+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,67 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{
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\"}]}}}]";
p.sendTitle(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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
public static void sendRawMessage(Player player, String message) {
try {
Object handle = Reflection.getHandle(player);
Object connection = Reflection.getField(handle.getClass(), "playerConnection").get(handle);
Object serialized = Reflection.getMethod(nmsChatSerializer, "a", String.class).invoke(null, message);
Object packet = nmsPacketPlayOutChat.getConstructor(Reflection.getNMSClass("IChatBaseComponent")).newInstance(serialized);
Reflection.getMethod(connection.getClass(), "sendPacket").invoke(connection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -1,50 +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;
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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{
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\"}]}}}]";
p.sendTitle(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)
{
}
}

View file

@ -1,370 +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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()>0) //<-- 2015.07.20.
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player)); //TODO: Multiple colors put on first capital letters
String[] colors = color.split("§");
String displayname=player.getDisplayName();
ArrayList<Integer> Positions=new ArrayList<>();
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
String finalname="";
if(Positions.size()>=colors.length)
{
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+1);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
else
{
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length(); i+=unit)
{
int pos=Positions.get(i);
int nextpos=Positions.get(i+unit);
String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
finalname+=substr;
}
}
//player.setDisplayName(color+displayname+"§r"+flair);
player.setDisplayName(finalname+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
}
}

View file

@ -1,78 +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.
PluginMain.AppendPlayerDisplayFlair(p, PluginMain.PlayerUserNames.get(p.getName()), PluginMain.GetFlair(p));
else
{ //2015.07.20.
String json="[\"\",{\"text\":\"§6Hi! If you'd like your flair displayed ingame, write your Minecraft name to \"},{\"text\":\"[this thread.]\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread§r\"}]}}}]";
sendRawMessage(p, json);
}
//System.out.println("Added player "+p.getName());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
//for(Player player : PluginMain.Players)
for(int i=0; i<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)
{
}
private static Class<?> nmsChatSerializer = Reflection.getNMSClass("ChatSerializer");
private static Class<?> nmsPacketPlayOutChat = Reflection.getNMSClass("PacketPlayOutChat");
public static void sendRawMessage(Player player, String message) {
if (majorVersion < 1.8 || (majorVersion == 1.8 && lesserVersion == 1)) {
chatSerializerClazz = Reflection.getNMSClass("ChatSerializer");
} else {
chatSerializerClazz = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
}
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();
}
}
}

View file

@ -0,0 +1,125 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(p.Flair==null)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values())
{
if(p.Flair!=null)
{
String flair=PluginMain.PlayerFlairs.get(p.getName());
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -0,0 +1,26 @@
package tk.sznp.thebuttonautoflair;
import java.util.HashMap;
public class MaybeOfflinePlayer
{ //2015.08.08.
public String PlayerName;
public String UserName;
public String Flair;
public boolean AcceptedFlair;
public boolean IgnoredFlair;
public String TownColors;
public String DisplayName;
public static HashMap<String, MaybeOfflinePlayer> AllPlayers=new HashMap<>(); //2015.08.08.
public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername)
{
if(!AllPlayers.containsKey(playername))
{
MaybeOfflinePlayer player=new MaybeOfflinePlayer();
player.PlayerName=playername;
AllPlayers.put(playername, player);
return player;
}
return AllPlayers.get(playername);
}
}

View file

@ -0,0 +1,122 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName());
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName());
if(!p.IgnoredFlair)
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for(Player p : PluginMain.Players)
{
if(PluginMain.PlayerFlairs.containsKey(p.getName()))
{
String flair=PluginMain.PlayerFlairs.get(p.getName());
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -0,0 +1,25 @@
package tk.sznp.thebuttonautoflair;
import java.util.HashMap;
public class MaybeOfflinePlayer
{ //2015.08.08.
public String PlayerName;
public String UserName;
public String Flair;
public boolean AcceptedFlair;
public boolean IgnoredFlair;
public String DisplayName;
public static HashMap<String, MaybeOfflinePlayer> AllPlayers=new HashMap<>(); //2015.08.08.
public static MaybeOfflinePlayer AddPlayerIfNeeded(String playername)
{
if(!AllPlayers.containsKey(playername))
{
MaybeOfflinePlayer player=new MaybeOfflinePlayer();
player.PlayerName=playername;
AllPlayers.put(playername, player);
return player;
}
return AllPlayers.get(playername);
}
}

View file

@ -0,0 +1,125 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
MaybeOfflinePlayer p=MaybeOfflinePlayer.AllPlayers.get(player.getName()); //2015.08.08.
//if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
if(p.Flair==null)
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
/*if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());*/
if(p.IgnoredFlair)
p.IgnoredFlair=false; //2015.08.08.
//if(!PluginMain.AcceptedPlayers.contains(player.getName()))
if(!p.AcceptedFlair)
{
//String flair=PluginMain.PlayerFlairs.get(player.getName());
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
//PluginMain.AcceptedPlayers.add(player.getName());
p.AcceptedFlair=true; //2015.08.08.
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(p.AcceptedFlair)
p.AcceptedFlair=false; //2015.08.08.
if(!p.IgnoredFlair)
{
p.IgnoredFlair=true;
//String dname=player.getDisplayName();
String flair=p.Flair; //2015.08.08.
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
DoReload(player);
break;
default:
return false;
}
return true;
}
if(args[0]=="reload")
DoReload(null); //2015.07.20.
return false;
}
private static void DoReload(Player player)
{ //2015.07.20.
if(player==null || player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
//for(Player p : PluginMain.Players)
for(MaybeOfflinePlayer p : MaybeOfflinePlayer.AllPlayers.values())
{
if(p.Flair!=null)
{
String flair=p.Flair;
PluginMain.RemovePlayerDisplayFlairFinal(p, flair);
PluginMain.AppendPlayerDisplayFlairFinal(p, flair);
}
}
String msg="§6Reloaded config file.§r";
if(player!=null)
player.sendMessage(msg);
else
System.out.println(msg);
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
if(player!=null)
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
}
}

View file

@ -1,47 +0,0 @@
package tk.sznp.thebuttonautoflair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.add(player.getName());
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(player.getDisplayName()+flair);
player.sendMessage("§6Your flair has been set:§r "+flair);
break;
case "ignore":
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.add(player.getName());
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,434 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
System.out.println("Name: " + name);
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown(); //TODO
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
//MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -1,322 +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.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.Towny;
import com.palmergames.bukkit.towny.object.TownyWorld;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(String playername)
{
Towny.getTownyInstance()
}
}

View file

@ -1,294 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForClass(flairclass);
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

View file

@ -1,93 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,420 @@
package tk.sznp.thebuttonautoflair;
import 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.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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
PluginManager manager = getServer().getPluginManager();
manager.registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(GetPlayerTown(player));
String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
}

View file

@ -1,99 +0,0 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if(args.length<1)
return false;
if(!PluginMain.PlayerFlairs.containsKey(player.getName()))
{
player.sendMessage("Error: You need to write your username to the reddit thread at /r/TheButtonMinecraft");
return true;
}
switch(args[0])
{
case "accept":
{
if(PluginMain.IgnoredPlayers.contains(player.getName()))
PluginMain.IgnoredPlayers.remove(player.getName());
if(!PluginMain.AcceptedPlayers.contains(player.getName()))
{
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(player.getDisplayName()+flair);
PluginMain.AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
PluginMain.AcceptedPlayers.add(player.getName());
player.sendMessage("§6Your flair has been set:§r "+flair);
}
else
player.sendMessage("§cYou already have this user's flair.§r");
break;
}
case "ignore":
{
if(PluginMain.AcceptedPlayers.contains(player.getName()))
PluginMain.AcceptedPlayers.remove(player.getName());
if(!PluginMain.IgnoredPlayers.contains(player.getName()))
{
PluginMain.IgnoredPlayers.add(player.getName());
//String dname=player.getDisplayName();
String flair=PluginMain.PlayerFlairs.get(player.getName());
//player.setDisplayName(dname.substring(0, dname.indexOf(flair)));
PluginMain.RemovePlayerDisplayFlairFinal(player, flair); //2015.07.20.
player.sendMessage("§6You have ignored this request. You can still use /u accept though.§r");
}
else
player.sendMessage("§cYou already ignored this request.§r");
break;
}
case "reload": //2015.07.20.
if(player.isOp())
{
try
{
File file=new File("autoflairconfig.txt");
if(file.exists())
{
PluginMain.TownColors.clear();
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
PluginMain.TownColors.put(s[0], s[1]);
}
br.close();
for()
player.sendMessage("§6Reloaded config file.§r");
}
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
player.sendMessage("§cAn error occured. See console for details.§r");
}
}
else
player.sendMessage("§cYou need to be OP to use this command.§r");
break;
default:
return false;
}
return true;
}
// If the player (or console) uses our command correct, we can return true
return false;
}
}

View file

@ -0,0 +1,431 @@
package tk.sznp.thebuttonautoflair;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
public class PluginMain extends JavaPlugin
{ //Translated to Java: 2015.07.15.
//A user, which flair isn't obtainable:
//https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
private static PluginMain Instance;
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
Instance=this; //2015.08.08.
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
//AcceptedPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
System.out.println("Name: "+name);s
MaybeOfflinePlayer.AddPlayerIfNeeded(name).AcceptedFlair=true; //2015.08.08.
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
//IgnoredPlayers.add(line.replace("\n", ""));
String name=line.replace("\n", "");
MaybeOfflinePlayer.AddPlayerIfNeeded(name).IgnoredFlair=true; //2015.08.08.
}
br.close();
}
file=new File("autoflairconfig.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null)
{
String[] s=line.split(" ");
TownColors.put(s[0], s[1]);
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
//for(String player : AcceptedPlayers)
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.AcceptedFlair)
continue; //2015.08.08.
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(MaybeOfflinePlayer player : MaybeOfflinePlayer.AllPlayers.values()) //<-- 2015.08.08.
{
if(!player.IgnoredFlair)
continue; //2015.08.08.
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
/*System.out.println("Sleeping for 5 seconds..."); //2015.07.20.
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} //2015.07.20.*/
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
/*public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
//public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.*/
public static Map<String, String> TownColors=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
//return PlayerFlairs.containsKey(playername);
//return MaybeOfflinePlayer.AllPlayers.containsKey(playername);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
return p.Flair!=null; //2015.08.08.
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
case "cant-press": //2015.08.08.
finalflair="§r(can't press)§r";
break;
default:
finalflair="";
break;
}
if(finalflair.length()==0) //<-- 2015.07.20.
return;
//PlayerFlairs.put(playername, finalflair);
//PlayerUserNames.put(playername, username);
MaybeOfflinePlayer p=MaybeOfflinePlayer.AddPlayerIfNeeded(playername); //2015.08.08.
p.Flair=finalflair; //2015.08.08.
p.UserName=username; //2015.08.08.
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
//for(Player player : Players)
for(Player player : getServer().getOnlinePlayers()) //<-- 2015.08.08.
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
//String flair=PlayerFlairs.get(player.getName());
String flair=MaybeOfflinePlayer.AllPlayers.get(player.getName()).Flair; //2015.08.08.
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
//if(IgnoredPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).IgnoredFlair)
return;
//if(AcceptedPlayers.contains(player.getName()))
if(MaybeOfflinePlayer.AllPlayers.get(player.getName()).AcceptedFlair)
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//System.out.println("A");
String color = GetColorForTown(GetPlayerTown(player)); //TO!DO: Multiple colors put on first capital letters
String[] colors = color.substring(1).split("§");
//String displayname=player.getDisplayName();
String displayname=player.getName(); //2015.08.08.
ArrayList<Integer> Positions=new ArrayList<>();
//System.out.println("B");
for(int i=0; i<displayname.length(); i++) {
if(Character.isUpperCase(displayname.charAt(i))) {
Positions.add(i);
}
}
//System.out.println("C: Positions.size(): "+Positions.size());
String finalname="";
if(Positions.size()>=colors.length)
{
//System.out.println("D");
int x=0;
for(int i=0; i<Positions.size(); i++)
{
int pos=Positions.get(i);
int nextpos;
if(i!=Positions.size()-1)
nextpos=Positions.get(i+1);
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
//String substr="§"+colors[x++]+displayname.substring(pos, nextpos-pos)+"§r";
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("F");
}
else
{
//System.out.println("E");
Positions.clear();
int unit=displayname.length()/colors.length;
int x=0;
for(int i=0; i<displayname.length()-unit; i+=unit)
{
int pos=i;
int nextpos;
if(i<displayname.length()-unit-unit)
nextpos=i+unit;
else
nextpos=displayname.length();
//System.out.println("pos: "+pos+" nextpos: "+nextpos);
//System.out.println("nextpos-pos: "+(nextpos-pos));
String substr="§"+colors[x++]+displayname.substring(pos, nextpos)+"§r";
finalname+=substr;
}
//System.out.println("G");
}
//player.setDisplayName(color+displayname+"§r"+flair);
//player.setDisplayName(finalname+flair);
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=finalname+flair; //2015.08.08.
}
public static String GetColorForTown(String townname)
{ //2015.07.20.
if(TownColors.containsKey(townname))
return TownColors.get(townname);
return "";
}
public static String GetPlayerTown(Player player)
{ //2015.07.20.
//List<Town> towns = TownyUniverse.getDataSource().getTowns();
try {
Town town = WorldCoord.parseWorldCoord(player).getTownBlock().getTown();
return town.getName();
} catch (Exception e) {
return "";
}
}
public static void RemovePlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
//String color = GetColorForTown(GetPlayerTown(player));
//String dname=player.getDisplayName();
//player.setDisplayName(dname.substring(dname.indexOf(color)+3, dname.indexOf(flair)));
MaybeOfflinePlayer.AllPlayers.get(player.getName()).DisplayName=null; //2015.08.08.
}
public static Collection<? extends Player> GetPlayers()
{
return Instance.getServer().getOnlinePlayers();
}
}

View file

@ -1,294 +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.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;
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/
// Fired when plugin is first enabled
@Override
public void onEnable()
{
System.out.println("The Button Auto-flair Plugin by NorbiPeti (:P)");
//System.out.println("Original C# version: http://pastebin.com/tX8xCPbp");
//System.out.println("The Java version is... Also made by the same person.");
//System.out.println("With the help of StackOverflow and similar.");
/*catch(MalformedURLException e)
{
}
catch(IOException e)
{
}*/
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
//System.out.println("Registering commands...");
this.getCommand("u").setExecutor(new Commands());
this.getCommand("u").setUsage(this.getCommand("u").getUsage().replace('&', '§'));
try {
File file=new File("flairsaccepted.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsaccepted.txt"));
String line;
while ((line = br.readLine()) != null)
{
AcceptedPlayers.add(line.replace("\n", ""));
}
br.close();
}
file=new File("flairsignored.txt");
if(file.exists())
{
BufferedReader br=new BufferedReader(new FileReader("flairsignored.txt"));
String line;
while ((line = br.readLine()) != null)
{
IgnoredPlayers.add(line.replace("\n", ""));
}
br.close();
}
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
//System.out.println("Registering done.");
Runnable r=new Runnable(){public void run(){ThreadMethod();}};
Thread t=new Thread(r);
t.start();
}
Boolean stop=false;
// Fired when plugin is disabled
@Override
public void onDisable()
{
try
{
FileWriter fw;
fw = new FileWriter("flairsaccepted.txt");
fw.close();
fw = new FileWriter("flairsignored.txt");
fw.close();
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
for(String player : AcceptedPlayers)
{
File file=new File("flairsaccepted.txt");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
for(String player : IgnoredPlayers)
{
File file=new File("flairsignored.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(player+"\n");
bw.close();
} catch (IOException e) {
System.out.println("Error!\n"+e);
}
}
stop=true;
}
public void ThreadMethod() //<-- 2015.07.16.
{
while(!stop)
{
try
{
String body=DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
JSONArray json=new JSONArray(body).getJSONObject(1).getJSONObject("data").getJSONArray("children");
for(Object obj : json)
{
JSONObject item = (JSONObject)obj;
String author=item.getJSONObject("data").getString("author");
String ign=item.getJSONObject("data").getString("body");
int start = ign.indexOf("IGN:") + "IGN:".length();
int end = ign.indexOf(' ', start);
if (end == -1 || end == start)
end=ign.indexOf('\n', start); //2015.07.15.
if (end == -1 || end == start)
ign = ign.substring(start);
else
ign = ign.substring(start, end);
ign = ign.trim();
if(HasIGFlair(ign))
continue;
//System.out.println("Author: "+author);
try {
Thread.sleep(10);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] flairdata = DownloadString("http://karmadecay.com/thebutton-data.php?users=" + author).replace("\"", "").split(":");
String flair;
if(flairdata.length > 1) //2015.07.15.
flair = flairdata[1];
else
flair="";
if (flair != "-1")
flair = flair + "s";
else
flair = "non-presser";
String flairclass;
//System.out.println("flairdata.length:"+flairdata.length);
if(flairdata.length>2)
flairclass = flairdata[2];
else
flairclass="unknown";
SetFlair(ign, flair, flairclass, author);
}
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println("Error!\n"+e);
}
}
}
public String DownloadString(String urlstr) throws MalformedURLException, IOException
{
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
//It has to store offline player flairs too, therefore it can't use Player object
public static Map<String, String> PlayerFlairs=new HashMap<String, String>();
public static Map<String, String> PlayerUserNames=new HashMap<String, String>();
//public Map<Player, String> PlayerFlairs=new HashMap<Player, String>();
public static ArrayList<Player> Players=new ArrayList<Player>();
public static ArrayList<String> AcceptedPlayers=new ArrayList<String>(); //2015.07.16.
public static ArrayList<String> IgnoredPlayers=new ArrayList<String>(); //2015.07.16.
public static Map<String, String> PlayerTowns=new HashMap<String, String>(); //2015.07.20.
public Boolean HasIGFlair(String playername)
{
/*Player player=null;
for(Player p : Players)
{
if(p.getName()==playername)
{
player=p;
break;
}
}
if(player==null)
return false;*/
return PlayerFlairs.containsKey(playername);
}
public void SetFlair(String playername, String text, String flairclass, String username)
{
String finalflair;
switch(flairclass)
{
case "press-1":
finalflair="§c("+text+")§r";
break;
case "press-2":
finalflair="§6("+text+")§r";
break;
case "press-3":
finalflair="§e("+text+")§r";
break;
case "press-4":
finalflair="§a("+text+")§r";
break;
case "press-5":
finalflair="§9("+text+")§r";
break;
case "press-6":
finalflair="§5("+text+")§r";
break;
case "no-press":
finalflair="§7(non-pr.)§r";
break;
case "cheater":
finalflair="§5("+text+")§r";
break;
default:
finalflair="";
break;
}
PlayerFlairs.put(playername, finalflair);
PlayerUserNames.put(playername, username);
/*for(Player player : Players)
{
if(player.getName()==playername)
{
PlayerFlairs.put(player, finalflair);
break;
}
}*/
//System.out.println("SetFlair - playername: "+playername+" text: "+text+" flairclass: "+flairclass);
System.out.println("Added new flair to "+playername+": "+finalflair);
for(Player player : Players)
{
//System.out.println("Online player: "+player.getName());
//System.out.println("player.getName ("+player.getName()+") == playername ("+playername+"): "+(player.getName()==playername));
if(player.getName().equals(playername))
{
//System.out.println("DisplayName: "+player.getDisplayName());
//player.setDisplayName(player.getDisplayName()+finalflair);
AppendPlayerDisplayFlair(player, username, finalflair);
//System.out.println("DisplayName: "+player.getDisplayName());
break;
}
}
}
public static String GetFlair(Player player)
{ //2015.07.16.
String flair=PlayerFlairs.get(player.getName());
return flair==null ? "" : flair;
}
public static void AppendPlayerDisplayFlair(Player player, String username, String flair)
{
if(IgnoredPlayers.contains(player.getName()))
return;
if(AcceptedPlayers.contains(player.getName()))
//player.setDisplayName(player.getDisplayName()+flair);
AppendPlayerDisplayFlairFinal(player, flair); //2015.07.20.
else
player.sendMessage("§9Are you Reddit user "+username+"?§r §6Type /u accept or /u ignore§r");
}
public static void AppendPlayerDisplayFlairFinal(Player player, String flair)
{ //2015.07.20.
String color = GetColorForTown(PlayerTowns.get(player.getName()));
player.setDisplayName(color+player.getDisplayName()+"§r"+flair);
}
}

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