2015-07-16 11:51:59 +00:00
|
|
|
|
package tk.sznp.thebuttonautoflair;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2015-10-17 00:06:32 +00:00
|
|
|
|
import org.bukkit.Bukkit;
|
2015-08-12 11:52:27 +00:00
|
|
|
|
import org.bukkit.command.ConsoleCommandSender;
|
2015-10-17 00:06:32 +00:00
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2015-07-16 11:51:59 +00:00
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
2015-08-18 23:09:16 +00:00
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.lang.String;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.net.URLConnection;
|
2015-08-19 19:30:27 +00:00
|
|
|
|
import java.util.ArrayList;
|
2015-08-18 23:09:16 +00:00
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2015-10-17 00:06:32 +00:00
|
|
|
|
import java.util.UUID;
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|
|
|
|
// A user, which flair isn't obtainable:
|
|
|
|
|
// https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
|
2015-08-19 19:30:27 +00:00
|
|
|
|
public static PluginMain Instance;
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static ConsoleCommandSender Console; // 2015.08.12.
|
2015-07-20 16:23:02 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
// Fired when plugin is first enabled
|
|
|
|
|
@Override
|
|
|
|
|
public void onEnable() {
|
2015-08-25 14:28:33 +00:00
|
|
|
|
System.out.println("The Button Minecraft server plugin");
|
2015-08-19 09:40:47 +00:00
|
|
|
|
getServer().getPluginManager().registerEvents(new PlayerListener(),
|
|
|
|
|
this);
|
2015-08-25 14:28:33 +00:00
|
|
|
|
Commands comm = new Commands();
|
|
|
|
|
this.getCommand("u").setExecutor(comm);
|
2015-08-19 09:40:47 +00:00
|
|
|
|
this.getCommand("u").setUsage(
|
|
|
|
|
this.getCommand("u").getUsage().replace('&', '<27>'));
|
2015-08-25 14:28:33 +00:00
|
|
|
|
this.getCommand("nrp").setExecutor(comm);
|
|
|
|
|
this.getCommand("nrp").setUsage(
|
|
|
|
|
this.getCommand("nrp").getUsage().replace('&', '<27>'));
|
2015-08-31 19:21:20 +00:00
|
|
|
|
this.getCommand("ooc").setExecutor(comm);
|
|
|
|
|
this.getCommand("ooc").setUsage(
|
|
|
|
|
this.getCommand("ooc").getUsage().replace('&', '<27>'));
|
2015-08-19 09:40:47 +00:00
|
|
|
|
Instance = this; // 2015.08.08.
|
|
|
|
|
Console = this.getServer().getConsoleSender(); // 2015.08.12.
|
|
|
|
|
LoadFiles(false); // 2015.08.09.
|
|
|
|
|
Runnable r = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ThreadMethod();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Thread t = new Thread(r);
|
|
|
|
|
t.start();
|
2015-08-19 19:30:27 +00:00
|
|
|
|
r = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
AnnouncerThread.Run();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
t = new Thread(r);
|
|
|
|
|
t.start();
|
2015-08-19 09:40:47 +00:00
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 19:30:27 +00:00
|
|
|
|
public Boolean stop = false;
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
// Fired when plugin is disabled
|
|
|
|
|
@Override
|
|
|
|
|
public void onDisable() {
|
|
|
|
|
SaveFiles(); // 2015.08.09.
|
|
|
|
|
stop = true;
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public void ThreadMethod() // <-- 2015.07.16.
|
|
|
|
|
{
|
|
|
|
|
while (!stop) {
|
|
|
|
|
try {
|
|
|
|
|
String body = DownloadString("https://www.reddit.com/r/TheButtonMinecraft/comments/3d25do/autoflair_system_comment_your_minecraft_name_and/.json?limit=1000");
|
|
|
|
|
JSONArray json = new JSONArray(body).getJSONObject(1)
|
|
|
|
|
.getJSONObject("data").getJSONArray("children");
|
|
|
|
|
for (Object obj : json) {
|
|
|
|
|
JSONObject item = (JSONObject) obj;
|
|
|
|
|
String author = item.getJSONObject("data").getString(
|
|
|
|
|
"author");
|
|
|
|
|
String ign = item.getJSONObject("data").getString("body");
|
|
|
|
|
int start = ign.indexOf("IGN:") + "IGN:".length();
|
|
|
|
|
if (start == -1 + "IGN:".length()) // +length: 2015.08.10.
|
|
|
|
|
continue; // 2015.08.09.
|
|
|
|
|
int end = ign.indexOf(' ', start);
|
|
|
|
|
if (end == -1 || end == start)
|
|
|
|
|
end = ign.indexOf('\n', start); // 2015.07.15.
|
|
|
|
|
if (end == -1 || end == start)
|
|
|
|
|
ign = ign.substring(start);
|
|
|
|
|
else
|
|
|
|
|
ign = ign.substring(start, end);
|
|
|
|
|
ign = ign.trim();
|
2015-10-17 00:06:32 +00:00
|
|
|
|
MaybeOfflinePlayer mp = MaybeOfflinePlayer.GetFromName(ign);
|
|
|
|
|
if(mp==null)
|
|
|
|
|
continue;
|
|
|
|
|
if (HasIGFlair(mp.UUID))
|
2015-08-19 09:40:47 +00:00
|
|
|
|
continue;
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(10);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
String[] flairdata = DownloadString(
|
|
|
|
|
"http://karmadecay.com/thebutton-data.php?users="
|
|
|
|
|
+ author).replace("\"", "").split(":");
|
|
|
|
|
String flair;
|
|
|
|
|
if (flairdata.length > 1) // 2015.07.15.
|
|
|
|
|
flair = flairdata[1];
|
|
|
|
|
else
|
|
|
|
|
flair = "";
|
|
|
|
|
if (flair != "-1")
|
|
|
|
|
flair = flair + "s";
|
|
|
|
|
String flairclass;
|
|
|
|
|
if (flairdata.length > 2)
|
|
|
|
|
flairclass = flairdata[2];
|
|
|
|
|
else
|
|
|
|
|
flairclass = "unknown";
|
2015-10-17 00:06:32 +00:00
|
|
|
|
SetFlair(mp.UUID, flair, flairclass, author);
|
2015-08-19 09:40:47 +00:00
|
|
|
|
}
|
2015-08-19 19:30:27 +00:00
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(10000);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
2015-08-19 09:40:47 +00:00
|
|
|
|
} catch (Exception e) {
|
2015-10-17 00:06:32 +00:00
|
|
|
|
// System.out.println("Error!\n" + e);
|
2015-08-19 09:40:47 +00:00
|
|
|
|
LastException = e; // 2015.08.09.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static Exception LastException; // 2015.08.09.
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public String DownloadString(String urlstr) throws MalformedURLException,
|
|
|
|
|
IOException {
|
|
|
|
|
URL url = new URL(urlstr);
|
|
|
|
|
URLConnection con = url.openConnection();
|
|
|
|
|
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
|
|
|
|
|
InputStream in = con.getInputStream();
|
|
|
|
|
String encoding = con.getContentEncoding();
|
|
|
|
|
encoding = encoding == null ? "UTF-8" : encoding;
|
|
|
|
|
String body = IOUtils.toString(in, encoding);
|
|
|
|
|
in.close();
|
|
|
|
|
return body;
|
|
|
|
|
}
|
2015-07-16 11:51:59 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static Map<String, String> TownColors = new HashMap<String, String>(); // 2015.07.20.
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-10-17 00:06:32 +00:00
|
|
|
|
public Boolean HasIGFlair(UUID uuid) {
|
|
|
|
|
MaybeOfflinePlayer p = MaybeOfflinePlayer.AddPlayerIfNeeded(uuid); // 2015.08.08.
|
2015-08-19 09:40:47 +00:00
|
|
|
|
return p.CommentedOnReddit; // 2015.08.10.
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-10-17 00:06:32 +00:00
|
|
|
|
public void SetFlair(UUID uuid, String text, String flairclass,
|
2015-08-19 09:40:47 +00:00
|
|
|
|
String username) {
|
2015-10-17 00:06:32 +00:00
|
|
|
|
MaybeOfflinePlayer p = MaybeOfflinePlayer.AddPlayerIfNeeded(uuid); // 2015.08.08.
|
2015-08-19 09:40:47 +00:00
|
|
|
|
String finalflair;
|
|
|
|
|
p.FlairDecided = true;
|
|
|
|
|
p.FlairRecognised = true;
|
|
|
|
|
switch (flairclass) {
|
|
|
|
|
case "press-1":
|
|
|
|
|
finalflair = "<EFBFBD>c(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "press-2":
|
|
|
|
|
finalflair = "<EFBFBD>6(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "press-3":
|
|
|
|
|
finalflair = "<EFBFBD>e(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "press-4":
|
|
|
|
|
finalflair = "<EFBFBD>a(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "press-5":
|
|
|
|
|
finalflair = "<EFBFBD>9(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "press-6":
|
|
|
|
|
finalflair = "<EFBFBD>5(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "no-press":
|
|
|
|
|
finalflair = "<EFBFBD>7(--s)<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "cheater":
|
|
|
|
|
finalflair = "<EFBFBD>5(" + text + ")<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "cant-press": // 2015.08.08.
|
|
|
|
|
finalflair = "<EFBFBD>r(??s)<29>r";
|
|
|
|
|
break;
|
|
|
|
|
case "unknown":
|
|
|
|
|
if (text.equals("-1")) // If true, only non-presser/can't press; if
|
|
|
|
|
// false, any flair
|
|
|
|
|
p.FlairDecided = false;
|
|
|
|
|
else
|
|
|
|
|
p.FlairRecognised = false;
|
|
|
|
|
finalflair = "";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
p.Flair = finalflair; // 2015.08.08.
|
|
|
|
|
p.CommentedOnReddit = true; // 2015.08.10.
|
|
|
|
|
p.UserName = username; // 2015.08.08.
|
2015-10-17 00:06:32 +00:00
|
|
|
|
System.out.println("Added flair for " + p.PlayerName);
|
|
|
|
|
AppendPlayerDisplayFlair(p, Bukkit.getPlayer(uuid));
|
2015-08-19 09:40:47 +00:00
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static String GetFlair(Player player) { // 2015.07.16.
|
2015-10-17 00:06:32 +00:00
|
|
|
|
String flair = MaybeOfflinePlayer.AllPlayers.get(player.getUniqueId()).Flair; // 2015.08.08.
|
2015-08-19 09:40:47 +00:00
|
|
|
|
return flair; // 2015.08.10.
|
|
|
|
|
}
|
2015-08-09 22:28:15 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static void AppendPlayerDisplayFlair(MaybeOfflinePlayer player,
|
|
|
|
|
Player p) // <-- 2015.08.09.
|
|
|
|
|
{
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-10-17 00:06:32 +00:00
|
|
|
|
if (MaybeOfflinePlayer.AllPlayers.get(p.getUniqueId()).IgnoredFlair)
|
2015-08-19 09:40:47 +00:00
|
|
|
|
return;
|
2015-10-17 00:06:32 +00:00
|
|
|
|
if (MaybeOfflinePlayer.AllPlayers.get(p.getUniqueId()).AcceptedFlair) {
|
2015-08-19 09:40:47 +00:00
|
|
|
|
if (!player.FlairDecided)
|
|
|
|
|
p.sendMessage("<EFBFBD>9Your flair type is unknown. Are you a non-presser or a can't press? (/u nonpresser or /u cantpress)<29>r"); // 2015.08.09.
|
|
|
|
|
} else
|
|
|
|
|
p.sendMessage("<EFBFBD>9Are you Reddit user " + player.UserName
|
|
|
|
|
+ "?<3F>r <20>6Type /u accept or /u ignore<72>r");
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static String GetColorForTown(String townname) { // 2015.07.20.
|
|
|
|
|
if (TownColors.containsKey(townname))
|
|
|
|
|
return TownColors.get(townname);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-10-17 00:06:32 +00:00
|
|
|
|
/*
|
|
|
|
|
* public static String GetPlayerTown(Player player) { // 2015.07.20. try {
|
|
|
|
|
* Town town = WorldCoord.parseWorldCoord(player).getTownBlock() .getTown();
|
|
|
|
|
* return town.getName(); } catch (Exception e) { return ""; } }
|
|
|
|
|
*/
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static Collection<? extends Player> GetPlayers() {
|
|
|
|
|
return Instance.getServer().getOnlinePlayers();
|
|
|
|
|
}
|
2015-08-18 23:09:16 +00:00
|
|
|
|
|
2015-08-19 19:30:27 +00:00
|
|
|
|
public static ArrayList<String> AnnounceMessages = new ArrayList<>();
|
|
|
|
|
public static int AnnounceTime = 15 * 60 * 1000;
|
|
|
|
|
|
2015-08-19 09:40:47 +00:00
|
|
|
|
public static void LoadFiles(boolean reload) // <-- 2015.08.09.
|
|
|
|
|
{
|
|
|
|
|
if (reload) { // 2015.08.09.
|
|
|
|
|
System.out
|
|
|
|
|
.println("The Button Minecraft plugin cleanup for reloading...");
|
|
|
|
|
MaybeOfflinePlayer.AllPlayers.clear();
|
|
|
|
|
TownColors.clear();
|
2015-08-19 19:30:27 +00:00
|
|
|
|
AnnounceMessages.clear();
|
2015-08-19 09:40:47 +00:00
|
|
|
|
}
|
|
|
|
|
System.out.println("Loading files for The Button Minecraft plugin..."); // 2015.08.09.
|
|
|
|
|
try {
|
2015-10-17 00:06:32 +00:00
|
|
|
|
File file = new File("announcemessages.txt");
|
|
|
|
|
if (file.exists())
|
|
|
|
|
file.delete();
|
|
|
|
|
file = new File("flairsaccepted.txt");
|
|
|
|
|
if (file.exists())
|
|
|
|
|
file.delete();
|
2015-08-19 09:40:47 +00:00
|
|
|
|
file = new File("flairsignored.txt");
|
2015-10-17 00:06:32 +00:00
|
|
|
|
if (file.exists())
|
|
|
|
|
file.delete();
|
|
|
|
|
file = new File("thebuttonmc.yml");
|
2015-08-19 09:40:47 +00:00
|
|
|
|
if (file.exists()) {
|
2015-10-17 00:06:32 +00:00
|
|
|
|
YamlConfiguration yc = new YamlConfiguration();
|
|
|
|
|
yc.load(file);
|
|
|
|
|
MaybeOfflinePlayer.Load(yc);
|
|
|
|
|
PlayerListener.NotificationSound = yc
|
|
|
|
|
.getString("notificationsound");
|
|
|
|
|
PlayerListener.NotificationPitch = yc
|
|
|
|
|
.getDouble("notificationpitch");
|
|
|
|
|
AnnounceTime = yc.getInt("announcetime");
|
|
|
|
|
AnnounceMessages.addAll(yc.getStringList("announcements"));
|
2015-08-19 19:30:27 +00:00
|
|
|
|
}
|
2015-08-19 09:40:47 +00:00
|
|
|
|
System.out.println("The Button Minecraft plugin loaded files!");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("Error!\n" + e);
|
|
|
|
|
LastException = e; // 2015.08.09.
|
2015-10-17 00:06:32 +00:00
|
|
|
|
} catch (InvalidConfigurationException e) {
|
2015-08-19 09:40:47 +00:00
|
|
|
|
System.out.println("Error!\n" + e);
|
|
|
|
|
LastException = e; // 2015.08.09.
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-19 19:30:27 +00:00
|
|
|
|
|
2015-10-17 00:06:32 +00:00
|
|
|
|
public static void SaveFiles() // <-- 2015.08.09.
|
|
|
|
|
{
|
|
|
|
|
System.out.println("Saving files for The Button Minecraft plugin..."); // 2015.08.09.
|
2015-08-19 19:30:27 +00:00
|
|
|
|
try {
|
2015-10-17 00:06:32 +00:00
|
|
|
|
File file = new File("thebuttonmc.yml");
|
|
|
|
|
YamlConfiguration yc = new YamlConfiguration();
|
|
|
|
|
MaybeOfflinePlayer.Save(yc);
|
|
|
|
|
yc.set("notificationsound", PlayerListener.NotificationSound);
|
|
|
|
|
yc.set("notificationpitch", PlayerListener.NotificationPitch);
|
|
|
|
|
yc.set("announcetime", AnnounceTime);
|
|
|
|
|
yc.set("announcements", AnnounceMessages);
|
|
|
|
|
yc.save(file);
|
|
|
|
|
System.out.println("The Button Minecraft plugin saved files!");
|
2015-08-19 19:30:27 +00:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("Error!\n" + e);
|
|
|
|
|
LastException = e; // 2015.08.09.
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-16 11:51:59 +00:00
|
|
|
|
}
|