Towny event broadcasting to Discord and masked links #96
27 changed files with 1046 additions and 1100 deletions
|
@ -1,25 +0,0 @@
|
|||
package buttondevteam.chat;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class AnnouncerThread implements Runnable {
|
||||
private static int AnnounceMessageIndex = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!PluginMain.Instance.stop) {
|
||||
try {
|
||||
Thread.sleep(PluginMain.AnnounceTime);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (Bukkit.getOnlinePlayers().size() == 0) continue; //Don't post to Discord if nobody is on
|
||||
if (PluginMain.AnnounceMessages.size() > AnnounceMessageIndex) {
|
||||
Bukkit.broadcastMessage(PluginMain.AnnounceMessages.get(AnnounceMessageIndex));
|
||||
AnnounceMessageIndex++;
|
||||
if (AnnounceMessageIndex == PluginMain.AnnounceMessages.size())
|
||||
AnnounceMessageIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package buttondevteam.chat;
|
||||
|
||||
import buttondevteam.chat.components.flair.FlairStates;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.player.EnumPlayerData;
|
||||
import buttondevteam.lib.player.PlayerClass;
|
||||
import buttondevteam.lib.player.PlayerData;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -46,14 +46,11 @@ public class ChatPlayer extends TBMCPlayerBase {
|
|||
return data(null);
|
||||
}
|
||||
|
||||
public Location SavedLocation;
|
||||
public boolean Working;
|
||||
// public int Tables = 10;
|
||||
public boolean SendingLink = false;
|
||||
public boolean RainbowPresserColorMode = false;
|
||||
public Color OtherColorMode = null;
|
||||
public boolean ChatOnly = false;
|
||||
public int LoginWarningCount = 0;
|
||||
|
||||
public static final int FlairTimeNonPresser = -1;
|
||||
public static final int FlairTimeCantPress = -2;
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
package buttondevteam.chat;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class JarUtils {
|
||||
|
||||
public static boolean extractFromJar(final String fileName,
|
||||
final String dest) throws IOException {
|
||||
if (getRunningJar() == null) {
|
||||
return false;
|
||||
}
|
||||
final File file = new File(dest);
|
||||
if (file.isDirectory()) {
|
||||
file.mkdir();
|
||||
return false;
|
||||
}
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
final JarFile jar = getRunningJar();
|
||||
final Enumeration<JarEntry> e = jar.entries();
|
||||
while (e.hasMoreElements()) {
|
||||
final JarEntry je = e.nextElement();
|
||||
if (!je.getName().contains(fileName)) {
|
||||
continue;
|
||||
}
|
||||
final InputStream in = new BufferedInputStream(
|
||||
jar.getInputStream(je));
|
||||
final OutputStream out = new BufferedOutputStream(
|
||||
new FileOutputStream(file));
|
||||
copyInputStream(in, out);
|
||||
jar.close();
|
||||
return true;
|
||||
}
|
||||
jar.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
private final static void copyInputStream(final InputStream in,
|
||||
final OutputStream out) throws IOException {
|
||||
try {
|
||||
final byte[] buff = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buff)) > 0) {
|
||||
out.write(buff, 0, n);
|
||||
}
|
||||
} finally {
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static URL getJarUrl(final File file) throws IOException {
|
||||
return new URL("jar:" + file.toURI().toURL().toExternalForm() + "!/");
|
||||
}
|
||||
|
||||
public static JarFile getRunningJar() throws IOException {
|
||||
if (!RUNNING_FROM_JAR) {
|
||||
return null; // null if not running from jar
|
||||
}
|
||||
String path = new File(JarUtils.class.getProtectionDomain()
|
||||
.getCodeSource().getLocation().getPath()).getAbsolutePath();
|
||||
path = URLDecoder.decode(path, "UTF-8");
|
||||
return new JarFile(path);
|
||||
}
|
||||
|
||||
private static boolean RUNNING_FROM_JAR = false;
|
||||
|
||||
static {
|
||||
final URL resource = JarUtils.class.getClassLoader().getResource(
|
||||
"plugin.yml");
|
||||
if (resource != null) {
|
||||
RUNNING_FROM_JAR = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -3,52 +3,36 @@ package buttondevteam.chat;
|
|||
import buttondevteam.chat.commands.YeehawCommand;
|
||||
import buttondevteam.chat.commands.ucmds.TownColorCommand;
|
||||
import buttondevteam.chat.components.TownColorComponent;
|
||||
import buttondevteam.chat.components.TownyComponent;
|
||||
import buttondevteam.chat.components.announce.AnnouncerComponent;
|
||||
import buttondevteam.chat.components.flair.FlairComponent;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
||||
import buttondevteam.chat.listener.PlayerListener;
|
||||
import buttondevteam.chat.listener.TownyListener;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.component.channel.Channel.RecipientTestResult;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.palmergames.bukkit.towny.Towny;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.TownyUniverse;
|
||||
import lombok.val;
|
||||
import net.milkbowl.vault.chat.Chat;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.dynmap.towny.DTBridge;
|
||||
import org.dynmap.towny.DynmapTownyPlugin;
|
||||
import org.htmlcleaner.HtmlCleaner;
|
||||
import org.htmlcleaner.TagNode;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -57,26 +41,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
// https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
|
||||
public static PluginMain Instance;
|
||||
public static ConsoleCommandSender Console;
|
||||
private final static String FlairThreadURL = "https://www.reddit.com/r/Chromagamers/comments/51ys94/flair_thread_for_the_mc_server/";
|
||||
|
||||
public static Scoreboard SB;
|
||||
public static TownyUniverse TU;
|
||||
private static ArrayList<String> Towns;
|
||||
private static ArrayList<String> Nations;
|
||||
|
||||
public static Channel TownChat;
|
||||
public static Channel NationChat;
|
||||
private static Channel RPChannel;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This variable is used as a cache for flair state checking when reading the flair thread.
|
||||
* </p>
|
||||
* <p>
|
||||
* It's used because normally it has to load all associated player files every time to read the flair state
|
||||
* </p>
|
||||
*/
|
||||
private Set<String> PlayersWithFlairs = new HashSet<>();
|
||||
|
||||
// Fired when plugin is first enabled
|
||||
@Override
|
||||
|
@ -92,18 +61,12 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
LoadFiles();
|
||||
|
||||
SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...]
|
||||
TU = ((Towny) Bukkit.getPluginManager().getPlugin("Towny")).getTownyUniverse();
|
||||
Towns = TU.getTownsMap().values().stream().map(Town::getName).collect(Collectors.toCollection(ArrayList::new)); // Creates a snapshot of towns, new towns will be added when needed
|
||||
Nations = TU.getNationsMap().values().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations
|
||||
|
||||
TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns
|
||||
NationColor.keySet().removeIf(n -> !TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations
|
||||
Component.registerComponent(this, new TownyComponent());
|
||||
TownColors.keySet().removeIf(t -> !TownyComponent.TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns
|
||||
NationColor.keySet().removeIf(n -> !TownyComponent.TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations
|
||||
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true)));
|
||||
TBMCChatAPI.RegisterChatChannel(RPChannel = new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global
|
||||
TBMCChatAPI.RegisterChatChannel(new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global
|
||||
|
||||
Bukkit.getScheduler().runTask(this, () -> {
|
||||
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
||||
|
@ -116,11 +79,9 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
if (!setupEconomy() || !setupPermissions())
|
||||
TBMCCoreAPI.SendException("We're in trouble", new Exception("Failed to set up economy or permissions!"));
|
||||
|
||||
new Thread(this::FlairGetterThreadMethod).start();
|
||||
new Thread(new AnnouncerThread()).start();
|
||||
|
||||
Component.registerComponent(this, new TownyComponent());
|
||||
Component.registerComponent(this, new TownColorComponent());
|
||||
Component.registerComponent(this, new FlairComponent()); //The original purpose of this plugin
|
||||
Component.registerComponent(this, new AnnouncerComponent());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,152 +101,14 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
}
|
||||
}
|
||||
|
||||
public Boolean stop = false;
|
||||
public static Essentials essentials = null;
|
||||
|
||||
// Fired when plugin is disabled
|
||||
@Override
|
||||
public void onDisable() {
|
||||
SaveFiles();
|
||||
stop = true;
|
||||
}
|
||||
|
||||
private void FlairGetterThreadMethod() {
|
||||
int errorcount = 0;
|
||||
while (!stop) {
|
||||
try {
|
||||
String body = TBMCCoreAPI.DownloadString(FlairThreadURL + ".json?limit=1000");
|
||||
JsonArray json = new JsonParser().parse(body).getAsJsonArray().get(1).getAsJsonObject().get("data")
|
||||
.getAsJsonObject().get("children").getAsJsonArray();
|
||||
for (Object obj : json) {
|
||||
JsonObject item = (JsonObject) obj;
|
||||
String author = item.get("data").getAsJsonObject().get("author").getAsString();
|
||||
String ign = item.get("data").getAsJsonObject().get("body").getAsString();
|
||||
int start = ign.indexOf("IGN:") + "IGN:".length();
|
||||
if (start == -1 + "IGN:".length())
|
||||
continue;
|
||||
int end = ign.indexOf(' ', start);
|
||||
if (end == -1 || end == start)
|
||||
end = ign.indexOf('\n', start);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
ign = ign.trim();
|
||||
if (PlayersWithFlairs.contains(ign))
|
||||
continue;
|
||||
try (ChatPlayer mp = TBMCPlayerBase.getFromName(ign, ChatPlayer.class)) { // Loads player file
|
||||
if (mp == null)
|
||||
continue;
|
||||
/*
|
||||
* if (!JoinedBefore(mp, 2015, 6, 5)) continue;
|
||||
*/
|
||||
if (!mp.UserNames().contains(author))
|
||||
mp.UserNames().add(author);
|
||||
if (mp.FlairState().get().equals(FlairStates.NoComment)) {
|
||||
mp.FlairState().set(FlairStates.Commented);
|
||||
ConfirmUserMessage(mp);
|
||||
}
|
||||
PlayersWithFlairs.add(ign); // Don't redownload even if flair isn't accepted
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorcount++;
|
||||
if (errorcount >= 10) {
|
||||
errorcount = 0;
|
||||
if (!e.getMessage().contains("Server returned HTTP response code")
|
||||
&& !(e instanceof UnknownHostException))
|
||||
TBMCCoreAPI.SendException("Error while getting flairs from Reddit!", e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DownloadFlair(ChatPlayer mp) throws IOException {
|
||||
String[] flairdata = TBMCCoreAPI
|
||||
.DownloadString("http://karmadecay.com/thebutton-data.php?users=" + mp.UserName().get())
|
||||
.replace("\"", "").split(":");
|
||||
String flair;
|
||||
if (flairdata.length > 1)
|
||||
flair = flairdata[1];
|
||||
else
|
||||
flair = "";
|
||||
String flairclass;
|
||||
if (flairdata.length > 2)
|
||||
flairclass = flairdata[2];
|
||||
else
|
||||
flairclass = "unknown";
|
||||
SetFlair(mp, flair, flairclass, mp.UserName().get());
|
||||
}
|
||||
|
||||
private void SetFlair(ChatPlayer p, String text, String flairclass, String username) {
|
||||
p.UserName().set(username);
|
||||
p.FlairState().set(FlairStates.Recognised);
|
||||
switch (flairclass) {
|
||||
case "cheater":
|
||||
p.SetFlair(Short.parseShort(text), true);
|
||||
return;
|
||||
case "unknown":
|
||||
try {
|
||||
if (CheckForJoinDate(p)) {
|
||||
if (text.equals("-1")) // If true, only non-presser/can't press; if false, any flair (but we can still detect can't press)
|
||||
p.SetFlair(ChatPlayer.FlairTimeNonPresser);
|
||||
else
|
||||
p.SetFlair(ChatPlayer.FlairTimeNone); // Flair unknown
|
||||
} else {
|
||||
p.SetFlair(ChatPlayer.FlairTimeCantPress);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
p.FlairState().set(FlairStates.Commented); // Flair unknown
|
||||
p.SetFlair(ChatPlayer.FlairTimeNone);
|
||||
TBMCCoreAPI.SendException("Error while checking join date for player " + p.PlayerName() + "!", e);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
p.SetFlair(Short.parseShort(text));
|
||||
}
|
||||
|
||||
private static boolean CheckForJoinDate(ChatPlayer mp) throws Exception {
|
||||
return JoinedBefore(mp, 2015, 4, 1);
|
||||
}
|
||||
|
||||
private static boolean JoinedBefore(ChatPlayer mp, int year, int month, int day) throws Exception {
|
||||
URL url = new URL("https://www.reddit.com/u/" + mp.UserName());
|
||||
URLConnection con = url.openConnection();
|
||||
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
|
||||
InputStream in = con.getInputStream();
|
||||
HtmlCleaner cleaner = new HtmlCleaner();
|
||||
TagNode node = cleaner.clean(in);
|
||||
|
||||
node = node.getElementsByAttValue("class", "age", true, true)[0];
|
||||
node = node.getElementsByName("time", false)[0];
|
||||
String joindate = node.getAttributeByName("datetime");
|
||||
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd");
|
||||
joindate = joindate.split("T")[0];
|
||||
Date date = parserSDF.parse(joindate);
|
||||
return date.before(new Calendar.Builder().setTimeZone(TimeZone.getTimeZone("UTC")).setDate(year, month, day)
|
||||
.build().getTime());
|
||||
}
|
||||
|
||||
public static void ConfirmUserMessage(ChatPlayer mp) {
|
||||
Player p = Bukkit.getPlayer(mp.getUUID());
|
||||
if (mp.FlairState().get().equals(FlairStates.Commented) && p != null)
|
||||
if (mp.UserNames().size() > 1)
|
||||
p.sendMessage(
|
||||
"§9Multiple Reddit users commented your name. You can select with /u accept.§r §6Type /u accept or /u ignore§r");
|
||||
else
|
||||
p.sendMessage("§9A Reddit user commented your name. Is that you?§r §6Type /u accept or /u ignore§r");
|
||||
}
|
||||
|
||||
public static ArrayList<String> AnnounceMessages = new ArrayList<>();
|
||||
public static int AnnounceTime = 15 * 60 * 1000;
|
||||
/**
|
||||
* Names lowercased
|
||||
*/
|
||||
|
@ -305,9 +128,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
yc.load(file);
|
||||
PlayerListener.NotificationSound = yc.getString("notificationsound");
|
||||
PlayerListener.NotificationPitch = yc.getDouble("notificationpitch");
|
||||
AnnounceTime = yc.getInt("announcetime", 15 * 60 * 1000);
|
||||
AnnounceMessages.addAll(yc.getStringList("announcements"));
|
||||
PlayerListener.AlphaDeaths = yc.getInt("alphadeaths");
|
||||
val cs = yc.getConfigurationSection("towncolors");
|
||||
if (cs != null)
|
||||
TownColors.putAll(cs.getValues(true).entrySet().stream()
|
||||
|
@ -333,9 +153,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
YamlConfiguration yc = new YamlConfiguration();
|
||||
yc.set("notificationsound", PlayerListener.NotificationSound);
|
||||
yc.set("notificationpitch", PlayerListener.NotificationPitch);
|
||||
yc.set("announcetime", AnnounceTime);
|
||||
yc.set("announcements", AnnounceMessages);
|
||||
yc.set("alphadeaths", PlayerListener.AlphaDeaths);
|
||||
yc.createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
|
||||
v -> Arrays.stream(v.getValue()).map(Enum::toString).toArray(String[]::new))));
|
||||
yc.set("towncolorcount", TownColorCommand.ColorCount);
|
||||
|
@ -361,16 +178,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
return (permission != null);
|
||||
}
|
||||
|
||||
private boolean setupChat() {
|
||||
RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager()
|
||||
.getRegistration(net.milkbowl.vault.chat.Chat.class);
|
||||
if (chatProvider != null) {
|
||||
chat = chatProvider.getProvider();
|
||||
}
|
||||
|
||||
return (chat != null);
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager()
|
||||
.getRegistration(net.milkbowl.vault.economy.Economy.class);
|
||||
|
@ -381,53 +188,4 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|||
return (economy != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the error message for the message sender if they can't send it and the score
|
||||
*/
|
||||
private static RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) {
|
||||
if (!(sender instanceof Player))
|
||||
return new RecipientTestResult("§cYou are not a player!");
|
||||
Resident resident = PluginMain.TU.getResidentMap().get(sender.getName().toLowerCase());
|
||||
RecipientTestResult result = checkTownNationChatInternal(sender, nationchat, resident);
|
||||
if (result.errormessage != null && resident != null && resident.getModes().contains("spy")) // Only use spy if they wouldn't see it
|
||||
result = new RecipientTestResult(1000, "allspies"); // There won't be more than a thousand towns/nations probably
|
||||
return result;
|
||||
}
|
||||
|
||||
private static RecipientTestResult checkTownNationChatInternal(CommandSender sender, boolean nationchat,
|
||||
Resident resident) {
|
||||
try {
|
||||
/*
|
||||
* p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message));
|
||||
*/
|
||||
Town town = null;
|
||||
if (resident != null && resident.hasTown())
|
||||
town = resident.getTown();
|
||||
if (town == null)
|
||||
return new RecipientTestResult("You aren't in a town.");
|
||||
Nation nation = null;
|
||||
int index;
|
||||
if (nationchat) {
|
||||
if (town.hasNation())
|
||||
nation = town.getNation();
|
||||
if (nation == null)
|
||||
return new RecipientTestResult("Your town isn't in a nation.");
|
||||
index = getTownNationIndex(nation.getName(), true);
|
||||
} else
|
||||
index = getTownNationIndex(town.getName(), false);
|
||||
return new RecipientTestResult(index, nationchat ? nation.getName() : town.getName());
|
||||
} catch (NotRegisteredException e) {
|
||||
return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
|
||||
}
|
||||
}
|
||||
|
||||
public static int getTownNationIndex(String name, boolean nation) {
|
||||
val list = nation ? Nations : Towns;
|
||||
int index = list.indexOf(name);
|
||||
if (index < 0) {
|
||||
list.add(name);
|
||||
index = list.size() - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,9 +39,9 @@ public class HistoryCommand extends UCommandBase {
|
|||
sender.sendMessage("§6---- Chat History ----");
|
||||
Stream<Channel> stream;
|
||||
if (args.length == 0) {
|
||||
stream = Channel.getChannels().stream();
|
||||
stream = Channel.getChannels();
|
||||
} else {
|
||||
Optional<Channel> och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
|
||||
Optional<Channel> och = Channel.getChannels().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
|
||||
if (!och.isPresent()) {
|
||||
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
|
||||
return true;
|
||||
|
|
|
@ -2,6 +2,7 @@ package buttondevteam.chat.commands.ucmds;
|
|||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
|
@ -34,7 +35,7 @@ public class NColorCommand extends UCommandBase {
|
|||
Resident res;
|
||||
Town town;
|
||||
try {
|
||||
if ((res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())) == null || !res.hasTown()
|
||||
if ((res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())) == null || !res.hasTown()
|
||||
|| (town = res.getTown()) == null) {
|
||||
player.sendMessage("§cYou need to be in a town.");
|
||||
return true;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.chat.commands.ucmds;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||
|
@ -26,8 +26,8 @@ public class NationColorCommand extends UCommandBase {
|
|||
@Override
|
||||
public boolean OnCommand(Player player, String alias, String[] args) {
|
||||
Resident res;
|
||||
if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||
&& (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) {
|
||||
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||
&& (res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) {
|
||||
player.sendMessage("§cYou need to be the king of a nation to set it's colors.");
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.chat.commands.ucmds;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||
|
@ -31,8 +31,8 @@ public class TownColorCommand extends UCommandBase {
|
|||
@Override
|
||||
public boolean OnCommand(Player player, String alias, String[] args) {
|
||||
Resident res;
|
||||
if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||
&& (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) {
|
||||
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||
&& (res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) {
|
||||
player.sendMessage("§cYou need to be the mayor of a town to set it's colors.");
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.admin;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.chat.listener.TownyListener;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
|
@ -31,7 +32,7 @@ public class NationColorCommand extends AdminCommandBase {
|
|||
sender.sendMessage("§cYou can only use one color as a nation color.");
|
||||
return true;
|
||||
}
|
||||
final Nation nation = PluginMain.TU.getNationsMap().get(args[0].toLowerCase());
|
||||
final Nation nation = TownyComponent.TU.getNationsMap().get(args[0].toLowerCase());
|
||||
if (nation == null) {
|
||||
sender.sendMessage("§cThe nation '" + args[0] + "' cannot be found.");
|
||||
return true;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.admin;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.chat.listener.TownyListener;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
|
@ -35,12 +36,12 @@ public class TownColorCommand extends AdminCommandBase {
|
|||
public static boolean SetTownColor(CommandSender sender, String alias, String[] args) {
|
||||
if (args.length < 2)
|
||||
return false;
|
||||
if (!PluginMain.TU.getTownsMap().containsKey(args[0].toLowerCase())) {
|
||||
if (!TownyComponent.TU.getTownsMap().containsKey(args[0].toLowerCase())) {
|
||||
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
|
||||
return true;
|
||||
}
|
||||
Color[] clrs = new Color[args.length - 1];
|
||||
Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase());
|
||||
Town targetTown = TownyComponent.TU.getTownsMap().get(args[0].toLowerCase());
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
val c = getColorOrSendError(args[i], sender);
|
||||
if (!c.isPresent())
|
||||
|
@ -50,7 +51,7 @@ public class TownColorCommand extends AdminCommandBase {
|
|||
for (Map.Entry<String, Color[]> other : PluginMain.TownColors.entrySet()) {
|
||||
Color nc, tnc;
|
||||
try {
|
||||
nc = PluginMain.NationColor.get(PluginMain.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase());
|
||||
nc = PluginMain.NationColor.get(TownyComponent.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase());
|
||||
} catch (Exception e) { //Too lazy for lots of null-checks and it may throw exceptions anyways
|
||||
nc = null;
|
||||
}
|
||||
|
@ -103,6 +104,6 @@ public class TownColorCommand extends AdminCommandBase {
|
|||
}
|
||||
|
||||
public static String getTownNameCased(String name) {
|
||||
return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName();
|
||||
return TownyComponent.TU.getTownsMap().get(name.toLowerCase()).getName();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package buttondevteam.chat.components;
|
||||
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
|
||||
public class TownyComponent extends Component {
|
||||
@Override
|
||||
protected void enable() {
|
||||
TownyAnnouncer.setup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
TownyAnnouncer.setdown();
|
||||
}
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
||||
public class AddCommand extends AnnounceCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -29,7 +27,8 @@ public class AddCommand extends AnnounceCommandBase {
|
|||
sb.append(" ");
|
||||
}
|
||||
String finalmessage = sb.toString().replace('&', '§');
|
||||
PluginMain.AnnounceMessages.add(finalmessage);
|
||||
AnnouncerComponent component = (AnnouncerComponent) getComponent();
|
||||
component.AnnounceMessages().get().add(finalmessage);
|
||||
sender.sendMessage("§bAnnouncement added. - Plase avoid using this command if possible, see /u announce add without args.§r");
|
||||
return true;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
|
@ -0,0 +1,50 @@
|
|||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AnnouncerComponent extends Component implements Runnable {
|
||||
public ConfigData<ArrayList<String>> AnnounceMessages() {
|
||||
return getConfig().getData("announceMessages", new ArrayList<>(0));
|
||||
}
|
||||
|
||||
public ConfigData<Integer> AnnounceTime() {
|
||||
return getConfig().getData("announceTime", 15 * 60 * 1000);
|
||||
}
|
||||
private static int AnnounceMessageIndex = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isEnabled()) {
|
||||
try {
|
||||
Thread.sleep(AnnounceTime().get());
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (Bukkit.getOnlinePlayers().size() == 0) continue; //Don't post to Discord if nobody is on
|
||||
if (AnnounceMessages().get().size() > AnnounceMessageIndex) {
|
||||
Bukkit.broadcastMessage(AnnounceMessages().get().get(AnnounceMessageIndex));
|
||||
AnnounceMessageIndex++;
|
||||
if (AnnounceMessageIndex == AnnounceMessages().get().size())
|
||||
AnnounceMessageIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void enable() {
|
||||
registerCommand(new AddCommand());
|
||||
registerCommand(new EditCommand());
|
||||
registerCommand(new ListCommand());
|
||||
registerCommand(new RemoveCommand());
|
||||
registerCommand(new SetTimeCommand());
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
}
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
||||
public class EditCommand extends AnnounceCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -34,9 +32,10 @@ public class EditCommand extends AnnounceCommandBase {
|
|||
int index = Integer.parseInt(args[0]);
|
||||
if (index > 100)
|
||||
return false;
|
||||
while (PluginMain.AnnounceMessages.size() <= index)
|
||||
PluginMain.AnnounceMessages.add("");
|
||||
PluginMain.AnnounceMessages.set(Integer.parseInt(args[0]),
|
||||
AnnouncerComponent component = (AnnouncerComponent) getComponent();
|
||||
while (component.AnnounceMessages().get().size() <= index)
|
||||
component.AnnounceMessages().get().add("");
|
||||
component.AnnounceMessages().get().set(Integer.parseInt(args[0]),
|
||||
finalmessage1);
|
||||
sender.sendMessage("Announcement edited.");
|
||||
return true;
|
|
@ -1,9 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
||||
public class ListCommand extends AnnounceCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -18,10 +16,11 @@ public class ListCommand extends AnnounceCommandBase {
|
|||
sender.sendMessage("§bList of announce messages:§r");
|
||||
sender.sendMessage("§bFormat: [index] message§r");
|
||||
int i = 0;
|
||||
for (String message : PluginMain.AnnounceMessages)
|
||||
AnnouncerComponent component = (AnnouncerComponent) getComponent();
|
||||
for (String message : component.AnnounceMessages().get())
|
||||
sender.sendMessage("[" + i++ + "] " + message);
|
||||
sender.sendMessage("§bCurrent wait time between announcements: "
|
||||
+ PluginMain.AnnounceTime / 60 / 1000 + " minute(s)§r");
|
||||
+ component.AnnounceTime().get() / 60 / 1000 + " minute(s)§r");
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
||||
public class RemoveCommand extends AnnounceCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -23,7 +21,7 @@ public class RemoveCommand extends AnnounceCommandBase {
|
|||
sender.sendMessage("§cUsage: /u announce remove <index>");
|
||||
return true;
|
||||
}
|
||||
PluginMain.AnnounceMessages.remove(Integer.parseInt(args[0]));
|
||||
((AnnouncerComponent) getComponent()).AnnounceMessages().get().remove(Integer.parseInt(args[0]));
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
package buttondevteam.chat.commands.ucmds.announce;
|
||||
package buttondevteam.chat.components.announce;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
||||
public class SetTimeCommand extends AnnounceCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -20,7 +18,7 @@ public class SetTimeCommand extends AnnounceCommandBase {
|
|||
return false;
|
||||
}
|
||||
try {
|
||||
PluginMain.AnnounceTime = Integer.parseInt(args[0]) * 60 * 1000;
|
||||
((AnnouncerComponent) getComponent()).AnnounceTime().set(Integer.parseInt(args[0]) * 60 * 1000);
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage("§cMinutes argument must be a number. Got: "
|
||||
+ args[0]);
|
|
@ -1,18 +1,16 @@
|
|||
package buttondevteam.chat.commands.ucmds;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
package buttondevteam.chat.components.flair;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.FlairStates;
|
||||
import buttondevteam.chat.PlayerJoinTimerTask;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
@CommandClass(modOnly = false)
|
||||
@OptionallyPlayerCommandClass(playerOnly = true)
|
||||
|
@ -68,7 +66,7 @@ public class AcceptCommand extends UCommandBase {
|
|||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
PluginMain.Instance.DownloadFlair(mp);
|
||||
FlairComponent.DownloadFlair(mp);
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException(
|
||||
"An error occured while downloading flair for " + player.getCustomName() + "!", e);
|
||||
|
@ -86,7 +84,7 @@ public class AcceptCommand extends UCommandBase {
|
|||
}
|
||||
String flair = mp.GetFormattedFlair();
|
||||
mp.FlairState().set(FlairStates.Accepted);
|
||||
PluginMain.ConfirmUserMessage(mp);
|
||||
FlairComponent.ConfirmUserMessage(mp);
|
||||
player.sendMessage("§bYour flair has been set:§r " + flair);
|
||||
mp.Working = false;
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
package buttondevteam.chat.components.flair;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.htmlcleaner.HtmlCleaner;
|
||||
import org.htmlcleaner.TagNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class FlairComponent extends Component {
|
||||
private ConfigData<String> FlairThreadURL() {
|
||||
return getConfig().getData("flairThreadURL", "https://www.reddit.com/r/Chromagamers/comments/51ys94/flair_thread_for_the_mc_server/");
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This variable is used as a cache for flair state checking when reading the flair thread.
|
||||
* </p>
|
||||
* <p>
|
||||
* It's used because normally it has to load all associated player files every time to read the flair state
|
||||
* </p>
|
||||
*/
|
||||
private Set<String> PlayersWithFlairs = new HashSet<>();
|
||||
|
||||
@Override
|
||||
protected void enable() {
|
||||
registerCommand(new AcceptCommand());
|
||||
registerCommand(new IgnoreCommand());
|
||||
registerCommand(new SetFlairCommand());
|
||||
new Thread(this::FlairGetterThreadMethod).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
|
||||
}
|
||||
|
||||
private void FlairGetterThreadMethod() {
|
||||
int errorcount = 0;
|
||||
while (isEnabled()) {
|
||||
try {
|
||||
String body = TBMCCoreAPI.DownloadString(FlairThreadURL().get() + ".json?limit=1000");
|
||||
JsonArray json = new JsonParser().parse(body).getAsJsonArray().get(1).getAsJsonObject().get("data")
|
||||
.getAsJsonObject().get("children").getAsJsonArray();
|
||||
for (Object obj : json) {
|
||||
JsonObject item = (JsonObject) obj;
|
||||
String author = item.get("data").getAsJsonObject().get("author").getAsString();
|
||||
String ign = item.get("data").getAsJsonObject().get("body").getAsString();
|
||||
int start = ign.indexOf("IGN:") + "IGN:".length();
|
||||
if (start == -1 + "IGN:".length())
|
||||
continue;
|
||||
int end = ign.indexOf(' ', start);
|
||||
if (end == -1 || end == start)
|
||||
end = ign.indexOf('\n', start);
|
||||
if (end == -1 || end == start)
|
||||
ign = ign.substring(start);
|
||||
else
|
||||
ign = ign.substring(start, end);
|
||||
ign = ign.trim();
|
||||
if (PlayersWithFlairs.contains(ign))
|
||||
continue;
|
||||
try (ChatPlayer mp = TBMCPlayerBase.getFromName(ign, ChatPlayer.class)) { // Loads player file
|
||||
if (mp == null)
|
||||
continue;
|
||||
/*
|
||||
* if (!JoinedBefore(mp, 2015, 6, 5)) continue;
|
||||
*/
|
||||
if (!mp.UserNames().contains(author))
|
||||
mp.UserNames().add(author);
|
||||
if (mp.FlairState().get().equals(FlairStates.NoComment)) {
|
||||
mp.FlairState().set(FlairStates.Commented);
|
||||
ConfirmUserMessage(mp);
|
||||
}
|
||||
PlayersWithFlairs.add(ign); // Don't redownload even if flair isn't accepted
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorcount++;
|
||||
if (errorcount >= 10) {
|
||||
errorcount = 0;
|
||||
if (!e.getMessage().contains("Server returned HTTP response code")
|
||||
&& !(e instanceof UnknownHostException))
|
||||
TBMCCoreAPI.SendException("Error while getting flairs from Reddit!", e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void DownloadFlair(ChatPlayer mp) throws IOException {
|
||||
String[] flairdata = TBMCCoreAPI
|
||||
.DownloadString("http://karmadecay.com/thebutton-data.php?users=" + mp.UserName().get())
|
||||
.replace("\"", "").split(":");
|
||||
String flair;
|
||||
if (flairdata.length > 1)
|
||||
flair = flairdata[1];
|
||||
else
|
||||
flair = "";
|
||||
String flairclass;
|
||||
if (flairdata.length > 2)
|
||||
flairclass = flairdata[2];
|
||||
else
|
||||
flairclass = "unknown";
|
||||
SetFlair(mp, flair, flairclass, mp.UserName().get());
|
||||
}
|
||||
|
||||
private static void SetFlair(ChatPlayer p, String text, String flairclass, String username) {
|
||||
p.UserName().set(username);
|
||||
p.FlairState().set(FlairStates.Recognised);
|
||||
switch (flairclass) {
|
||||
case "cheater":
|
||||
p.SetFlair(Short.parseShort(text), true);
|
||||
return;
|
||||
case "unknown":
|
||||
try {
|
||||
if (CheckForJoinDate(p)) {
|
||||
if (text.equals("-1")) // If true, only non-presser/can't press; if false, any flair (but we can still detect can't press)
|
||||
p.SetFlair(ChatPlayer.FlairTimeNonPresser);
|
||||
else
|
||||
p.SetFlair(ChatPlayer.FlairTimeNone); // Flair unknown
|
||||
} else {
|
||||
p.SetFlair(ChatPlayer.FlairTimeCantPress);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
p.FlairState().set(FlairStates.Commented); // Flair unknown
|
||||
p.SetFlair(ChatPlayer.FlairTimeNone);
|
||||
TBMCCoreAPI.SendException("Error while checking join date for player " + p.PlayerName() + "!", e);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
p.SetFlair(Short.parseShort(text));
|
||||
}
|
||||
|
||||
private static boolean CheckForJoinDate(ChatPlayer mp) throws Exception {
|
||||
return JoinedBefore(mp, 2015, 4, 1);
|
||||
}
|
||||
|
||||
private static boolean JoinedBefore(ChatPlayer mp, int year, int month, int day) throws Exception {
|
||||
URL url = new URL("https://www.reddit.com/u/" + mp.UserName());
|
||||
URLConnection con = url.openConnection();
|
||||
con.setRequestProperty("User-Agent", "TheButtonAutoFlair");
|
||||
InputStream in = con.getInputStream();
|
||||
HtmlCleaner cleaner = new HtmlCleaner();
|
||||
TagNode node = cleaner.clean(in);
|
||||
|
||||
node = node.getElementsByAttValue("class", "age", true, true)[0];
|
||||
node = node.getElementsByName("time", false)[0];
|
||||
String joindate = node.getAttributeByName("datetime");
|
||||
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd");
|
||||
joindate = joindate.split("T")[0];
|
||||
Date date = parserSDF.parse(joindate);
|
||||
return date.before(new Calendar.Builder().setTimeZone(TimeZone.getTimeZone("UTC")).setDate(year, month, day)
|
||||
.build().getTime());
|
||||
}
|
||||
|
||||
public static void ConfirmUserMessage(ChatPlayer mp) {
|
||||
Player p = Bukkit.getPlayer(mp.getUUID());
|
||||
if (mp.FlairState().get().equals(FlairStates.Commented) && p != null)
|
||||
if (mp.UserNames().size() > 1)
|
||||
p.sendMessage(
|
||||
"§9Multiple Reddit users commented your name. You can select with /u accept.§r §6Type /u accept or /u ignore§r");
|
||||
else
|
||||
p.sendMessage("§9A Reddit user commented your name. Is that you?§r §6Type /u accept or /u ignore§r");
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package buttondevteam.chat;
|
||||
package buttondevteam.chat.components.flair;
|
||||
|
||||
public enum FlairStates {
|
||||
Accepted, Ignored, Recognised, Commented, NoComment
|
|
@ -1,13 +1,12 @@
|
|||
package buttondevteam.chat.commands.ucmds;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
package buttondevteam.chat.components.flair;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.FlairStates;
|
||||
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandClass(modOnly = false)
|
||||
@OptionallyPlayerCommandClass(playerOnly = true)
|
|
@ -1,13 +1,12 @@
|
|||
package buttondevteam.chat.commands.ucmds.admin;
|
||||
package buttondevteam.chat.components.flair;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.FlairStates;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
|
||||
public class SetFlairCommand extends AdminCommandBase {
|
||||
|
||||
@Override
|
||||
|
@ -44,14 +43,14 @@ public class SetFlairCommand extends AdminCommandBase {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
boolean cheater = false;
|
||||
boolean cheater;
|
||||
if (args[2].equalsIgnoreCase("true"))
|
||||
cheater = true;
|
||||
else if (args[2].equalsIgnoreCase("false"))
|
||||
cheater = false;
|
||||
else {
|
||||
sender.sendMessage("§cUnknown value for cheater parameter. Run without args to see usage.");
|
||||
return true;
|
||||
sender.sendMessage("§cUnknown value for cheater parameter.");
|
||||
return false;
|
||||
}
|
||||
ChatPlayer mp = TBMCPlayerBase.getPlayer(p.getUniqueId(), ChatPlayer.class);
|
||||
mp.SetFlair(flairtime, cheater);
|
|
@ -1,4 +1,4 @@
|
|||
package buttondevteam.chat.components;
|
||||
package buttondevteam.chat.components.towny;
|
||||
|
||||
import buttondevteam.chat.ChatProcessing;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
|
@ -23,12 +23,12 @@ public class TownyAnnouncer {
|
|||
switch (String.valueOf(m.group(1))) { //valueOf: Handles null
|
||||
case "Town":
|
||||
TBMCChatAPI.SendSystemMessage(PluginMain.TownChat,
|
||||
new Channel.RecipientTestResult(PluginMain.getTownNationIndex(groupID, false), groupID),
|
||||
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, false), groupID),
|
||||
logRecord.getMessage(), ChatProcessing.MCORIGIN);
|
||||
break;
|
||||
case "Nation":
|
||||
TBMCChatAPI.SendSystemMessage(PluginMain.NationChat,
|
||||
new Channel.RecipientTestResult(PluginMain.getTownNationIndex(groupID, true), groupID),
|
||||
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, true), groupID),
|
||||
logRecord.getMessage(), ChatProcessing.MCORIGIN);
|
||||
break;
|
||||
case "Global":
|
|
@ -0,0 +1,94 @@
|
|||
package buttondevteam.chat.components.towny;
|
||||
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import com.palmergames.bukkit.towny.Towny;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import com.palmergames.bukkit.towny.object.TownyUniverse;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TownyComponent extends Component {
|
||||
public static TownyUniverse TU;
|
||||
private static ArrayList<String> Towns;
|
||||
private static ArrayList<String> Nations;
|
||||
|
||||
@Override
|
||||
protected void enable() {
|
||||
TU = ((Towny) Bukkit.getPluginManager().getPlugin("Towny")).getTownyUniverse();
|
||||
Towns = TU.getTownsMap().values().stream().map(Town::getName).collect(Collectors.toCollection(ArrayList::new)); // Creates a snapshot of towns, new towns will be added when needed
|
||||
Nations = TU.getNationsMap().values().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
PluginMain.TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
PluginMain.NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true)));
|
||||
TownyAnnouncer.setup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
TownyAnnouncer.setdown();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the error message for the message sender if they can't send it and the score
|
||||
*/
|
||||
private static Channel.RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) {
|
||||
if (!(sender instanceof Player))
|
||||
return new Channel.RecipientTestResult("§cYou are not a player!");
|
||||
Resident resident = TU.getResidentMap().get(sender.getName().toLowerCase());
|
||||
Channel.RecipientTestResult result = checkTownNationChatInternal(sender, nationchat, resident);
|
||||
if (result.errormessage != null && resident != null && resident.getModes().contains("spy")) // Only use spy if they wouldn't see it
|
||||
result = new Channel.RecipientTestResult(1000, "allspies"); // There won't be more than a thousand towns/nations probably
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Channel.RecipientTestResult checkTownNationChatInternal(CommandSender sender, boolean nationchat,
|
||||
Resident resident) {
|
||||
try {
|
||||
/*
|
||||
* p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message));
|
||||
*/
|
||||
Town town = null;
|
||||
if (resident != null && resident.hasTown())
|
||||
town = resident.getTown();
|
||||
if (town == null)
|
||||
return new Channel.RecipientTestResult("You aren't in a town.");
|
||||
Nation nation = null;
|
||||
int index;
|
||||
if (nationchat) {
|
||||
if (town.hasNation())
|
||||
nation = town.getNation();
|
||||
if (nation == null)
|
||||
return new Channel.RecipientTestResult("Your town isn't in a nation.");
|
||||
index = getTownNationIndex(nation.getName(), true);
|
||||
} else
|
||||
index = getTownNationIndex(town.getName(), false);
|
||||
return new Channel.RecipientTestResult(index, nationchat ? nation.getName() : town.getName());
|
||||
} catch (NotRegisteredException e) {
|
||||
return new Channel.RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
|
||||
}
|
||||
}
|
||||
|
||||
public static int getTownNationIndex(String name, boolean nation) {
|
||||
val list = nation ? Nations : Towns;
|
||||
int index = list.indexOf(name);
|
||||
if (index < 0) {
|
||||
list.add(name);
|
||||
index = list.size() - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,14 @@
|
|||
package buttondevteam.chat.listener;
|
||||
|
||||
import buttondevteam.chat.ChatPlayer;
|
||||
import buttondevteam.chat.FlairStates;
|
||||
import buttondevteam.chat.PlayerJoinTimerTask;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.commands.UnlolCommand;
|
||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||
import buttondevteam.chat.components.flair.FlairComponent;
|
||||
import buttondevteam.chat.components.flair.FlairStates;
|
||||
import buttondevteam.chat.components.towny.TownyComponent;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
||||
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
|
||||
|
@ -40,18 +43,20 @@ public class PlayerJoinLeaveListener implements Listener {
|
|||
ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class);
|
||||
Player p = Bukkit.getPlayer(cp.getUUID());
|
||||
|
||||
if (!cp.FlairState().get().equals(FlairStates.NoComment)) {
|
||||
PluginMain.ConfirmUserMessage(cp);
|
||||
Timer timer = new Timer();
|
||||
PlayerJoinTimerTask tt = new PlayerJoinTimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
mp.FlairUpdate();
|
||||
}
|
||||
};
|
||||
tt.mp = cp;
|
||||
timer.schedule(tt, 1000);
|
||||
} //TODO: Better Reddit integration (OAuth)
|
||||
if (ComponentManager.isEnabled(FlairComponent.class)) {
|
||||
if (!cp.FlairState().get().equals(FlairStates.NoComment)) {
|
||||
FlairComponent.ConfirmUserMessage(cp);
|
||||
Timer timer = new Timer();
|
||||
PlayerJoinTimerTask tt = new PlayerJoinTimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
mp.FlairUpdate();
|
||||
}
|
||||
};
|
||||
tt.mp = cp;
|
||||
timer.schedule(tt, 1000);
|
||||
} //TODO: Better Reddit integration (OAuth)
|
||||
}
|
||||
|
||||
String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname();
|
||||
|
||||
|
@ -90,7 +95,7 @@ public class PlayerJoinLeaveListener implements Listener {
|
|||
if (nickname.contains("~")) //StartsWith doesn't work because of color codes
|
||||
nickname = nickname.replace("~", ""); //It gets stacked otherwise
|
||||
String name = ChatColor.stripColor(nickname); //Enforce "town colors" on non-members
|
||||
val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase());
|
||||
val res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase());
|
||||
if (res == null || !res.hasTown())
|
||||
return name;
|
||||
try {
|
||||
|
|
|
@ -4,9 +4,11 @@ import buttondevteam.chat.ChatPlayer;
|
|||
import buttondevteam.chat.ChatProcessing;
|
||||
import buttondevteam.chat.PluginMain;
|
||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||
import buttondevteam.chat.components.flair.FlairComponent;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.component.channel.ChatChannelRegisterEvent;
|
||||
import buttondevteam.component.channel.ChatRoom;
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.chat.ChatMessage;
|
||||
|
@ -50,15 +52,9 @@ public class PlayerListener implements Listener {
|
|||
*/
|
||||
public static BiMap<String, UUID> nicknames = HashBiMap.create();
|
||||
|
||||
public static boolean Enable = false;
|
||||
|
||||
public static int LoginWarningCountTotal = 5;
|
||||
|
||||
public static String NotificationSound;
|
||||
public static double NotificationPitch;
|
||||
|
||||
public static boolean ShowRPTag = false;
|
||||
|
||||
public final static String[] LaughStrings = new String[]{"xd", "lel", "lawl", "kek", "lmao", "hue", "hah", "rofl"};
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
|
@ -88,7 +84,7 @@ public class PlayerListener implements Listener {
|
|||
return false;
|
||||
// ^^ We can only store player or console channels - Directly sending to channels would still work if they had an event
|
||||
cmd = sender instanceof ConsoleCommandSender ? message : message.substring(1);
|
||||
for (Channel channel : Channel.getChannels()) {
|
||||
for (Channel channel : ((Iterable<Channel>) Channel.getChannels()::iterator)) { //Using Stream.forEach would be too easy
|
||||
if (checkchid.test(channel, cmd)) {
|
||||
Channel oldch = mp.channel().get();
|
||||
if (oldch instanceof ChatRoom)
|
||||
|
@ -130,7 +126,7 @@ public class PlayerListener implements Listener {
|
|||
return true;
|
||||
}
|
||||
} else
|
||||
for (Channel channel : Channel.getChannels()) {
|
||||
for (Channel channel : (Iterable<Channel>) Channel.getChannels()::iterator) {
|
||||
if (checkchid.test(channel, cmd)) { //Apparently method references don't require final variables
|
||||
TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, mp, message.substring(index + 1)).build(), channel);
|
||||
return true;
|
||||
|
@ -173,13 +169,10 @@ public class PlayerListener implements Listener {
|
|||
public static boolean ActiveF = false;
|
||||
public static ChatPlayer FPlayer = null;
|
||||
public static BukkitTask Ftask = null;
|
||||
public static int AlphaDeaths;
|
||||
public static ArrayList<CommandSender> Fs = new ArrayList<>();
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDeath(PlayerDeathEvent e) {
|
||||
if (e.getEntity().getName().equals("Alpha_Bacca44"))
|
||||
AlphaDeaths++;
|
||||
// MinigamePlayer mgp = Minigames.plugin.pdata.getMinigamePlayer(e.getEntity());
|
||||
if (/* (mgp != null && !mgp.isInMinigame()) && */ new Random().nextBoolean()) { // Don't store Fs for NPCs
|
||||
Runnable tt = () -> {
|
||||
|
@ -249,9 +242,11 @@ public class PlayerListener implements Listener {
|
|||
e.addInfo("Minecraft name: " + cp.PlayerName().get());
|
||||
if (cp.UserName().get() != null && cp.UserName().get().length() > 0)
|
||||
e.addInfo("Reddit name: " + cp.UserName().get());
|
||||
final String flair = cp.GetFormattedFlair(e.getTarget() != InfoTarget.MCCommand);
|
||||
if (flair.length() > 0)
|
||||
e.addInfo("/r/TheButton flair: " + flair);
|
||||
if (ComponentManager.isEnabled(FlairComponent.class)) {
|
||||
final String flair = cp.GetFormattedFlair(e.getTarget() != InfoTarget.MCCommand);
|
||||
if (flair.length() > 0)
|
||||
e.addInfo("/r/TheButton flair: " + flair);
|
||||
}
|
||||
e.addInfo(String.format("Respect: %.2f", cp.getF()));
|
||||
} catch (Exception ex) {
|
||||
TBMCCoreAPI.SendException("Error while providing chat info for player " + e.getPlayer().getFileName(), ex);
|
||||
|
|
Loading…
Reference in a new issue