Towny event broadcasting to Discord and masked links #96
38 changed files with 2462 additions and 2394 deletions
20
pom.xml
20
pom.xml
|
@ -183,7 +183,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.TBMCPlugins.ButtonCore</groupId>
|
<groupId>com.github.TBMCPlugins.ButtonCore</groupId>
|
||||||
<artifactId>ButtonCore</artifactId>
|
<artifactId>ButtonCore</artifactId>
|
||||||
<version>${env.TRAVIS_BRANCH}-SNAPSHOT</version>
|
<version>${branch}-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/net.sourceforge.htmlcleaner/htmlcleaner -->
|
<!-- https://mvnrepository.com/artifact/net.sourceforge.htmlcleaner/htmlcleaner -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -259,5 +259,23 @@
|
||||||
<!-- github server corresponds to entry in ~/.m2/settings.xml -->
|
<!-- github server corresponds to entry in ~/.m2/settings.xml -->
|
||||||
<github.global.server>githubo</github.global.server>
|
<github.global.server>githubo</github.global.server>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<branch>
|
||||||
|
master
|
||||||
|
</branch> <!-- Should be master if building ButtonCore locally - the CI will overwrite it (see below) -->
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>ci</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>env.TRAVIS_BRANCH</name>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<properties>
|
||||||
|
<!-- Override only if necessary -->
|
||||||
|
<branch>${env.TRAVIS_BRANCH}</branch>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
</project>
|
</project>
|
|
@ -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;
|
package buttondevteam.chat;
|
||||||
|
|
||||||
|
import buttondevteam.chat.components.flair.FlairStates;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import buttondevteam.lib.player.EnumPlayerData;
|
import buttondevteam.lib.player.EnumPlayerData;
|
||||||
import buttondevteam.lib.player.PlayerClass;
|
import buttondevteam.lib.player.PlayerClass;
|
||||||
import buttondevteam.lib.player.PlayerData;
|
import buttondevteam.lib.player.PlayerData;
|
||||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -46,14 +46,11 @@ public class ChatPlayer extends TBMCPlayerBase {
|
||||||
return data(null);
|
return data(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Location SavedLocation;
|
|
||||||
public boolean Working;
|
public boolean Working;
|
||||||
// public int Tables = 10;
|
// public int Tables = 10;
|
||||||
public boolean SendingLink = false;
|
|
||||||
public boolean RainbowPresserColorMode = false;
|
public boolean RainbowPresserColorMode = false;
|
||||||
public Color OtherColorMode = null;
|
public Color OtherColorMode = null;
|
||||||
public boolean ChatOnly = false;
|
public boolean ChatOnly = false;
|
||||||
public int LoginWarningCount = 0;
|
|
||||||
|
|
||||||
public static final int FlairTimeNonPresser = -1;
|
public static final int FlairTimeNonPresser = -1;
|
||||||
public static final int FlairTimeCantPress = -2;
|
public static final int FlairTimeCantPress = -2;
|
||||||
|
|
|
@ -7,10 +7,10 @@ import buttondevteam.chat.formatting.TellrawEvent;
|
||||||
import buttondevteam.chat.formatting.TellrawPart;
|
import buttondevteam.chat.formatting.TellrawPart;
|
||||||
import buttondevteam.chat.formatting.TellrawSerializer;
|
import buttondevteam.chat.formatting.TellrawSerializer;
|
||||||
import buttondevteam.chat.listener.PlayerListener;
|
import buttondevteam.chat.listener.PlayerListener;
|
||||||
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCChatEventBase;
|
import buttondevteam.lib.TBMCChatEventBase;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.Channel;
|
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import buttondevteam.lib.chat.Priority;
|
import buttondevteam.lib.chat.Priority;
|
||||||
import buttondevteam.lib.chat.TellrawSerializableEnum;
|
import buttondevteam.lib.chat.TellrawSerializableEnum;
|
||||||
|
@ -46,7 +46,7 @@ public class ChatProcessing {
|
||||||
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*");
|
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*");
|
||||||
private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*");
|
private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*");
|
||||||
private static final Pattern CODE_PATTERN = Pattern.compile("`");
|
private static final Pattern CODE_PATTERN = Pattern.compile("`");
|
||||||
private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]])\\]\\(([^()])\\)");
|
private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]]+)]\\(([^()]+)\\)");
|
||||||
private static final Pattern SOMEONE_PATTERN = Pattern.compile("@someone"); //TODO
|
private static final Pattern SOMEONE_PATTERN = Pattern.compile("@someone"); //TODO
|
||||||
private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~");
|
private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~");
|
||||||
private static final Color[] RainbowPresserColors = new Color[]{Color.Red, Color.Gold, Color.Yellow, Color.Green,
|
private static final Color[] RainbowPresserColors = new Color[]{Color.Red, Color.Gold, Color.Yellow, Color.Green,
|
||||||
|
@ -63,9 +63,8 @@ public class ChatProcessing {
|
||||||
.build(),
|
.build(),
|
||||||
ChatFormatter.builder().regex(STRIKETHROUGH_PATTERN).strikethrough(true).removeCharCount((short) 2).type(ChatFormatter.Type.Range)
|
ChatFormatter.builder().regex(STRIKETHROUGH_PATTERN).strikethrough(true).removeCharCount((short) 2).type(ChatFormatter.Type.Range)
|
||||||
.build(),
|
.build(),
|
||||||
ESCAPE_FORMATTER, ChatFormatter.builder().regex(URL_PATTERN).underlined(true).openlink("$1").type(ChatFormatter.Type.Excluder).build(),
|
ESCAPE_FORMATTER, ChatFormatter.builder().regex(NULL_MENTION_PATTERN).color(Color.DarkRed).build(), // Properly added a bug as a feature
|
||||||
ChatFormatter.builder().regex(NULL_MENTION_PATTERN).color(Color.DarkRed).build(), // Properly added a bug as a feature
|
ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder, section) -> {
|
||||||
ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder) -> {
|
|
||||||
if (!pingedconsole) {
|
if (!pingedconsole) {
|
||||||
System.out.print("\007");
|
System.out.print("\007");
|
||||||
pingedconsole = true; // Will set it to false in ProcessChat
|
pingedconsole = true; // Will set it to false in ProcessChat
|
||||||
|
@ -78,16 +77,21 @@ public class ChatProcessing {
|
||||||
ChatFormatter.builder().regex(CYAN_PATTERN).color(Color.Aqua).build(), // #55
|
ChatFormatter.builder().regex(CYAN_PATTERN).color(Color.Aqua).build(), // #55
|
||||||
ChatFormatter.builder().regex(CODE_PATTERN).color(Color.DarkGray).removeCharCount((short) 1).type(ChatFormatter.Type.Range)
|
ChatFormatter.builder().regex(CODE_PATTERN).color(Color.DarkGray).removeCharCount((short) 1).type(ChatFormatter.Type.Range)
|
||||||
.build(),
|
.build(),
|
||||||
ChatFormatter.builder().regex(MASKED_LINK_PATTERN).underlined(true).onmatch((match, builder) -> {
|
ChatFormatter.builder().regex(MASKED_LINK_PATTERN).underlined(true).onmatch((match, builder, section) -> {
|
||||||
return match; // TODO!
|
String text, link;
|
||||||
}).build());
|
if (section.Matches.size() < 2 || (text = section.Matches.get(0)).length() == 0 || (link = section.Matches.get(1)).length() == 0)
|
||||||
|
return "";
|
||||||
|
builder.setOpenlink(link);
|
||||||
|
return text;
|
||||||
|
}).type(ChatFormatter.Type.Excluder).build(),
|
||||||
|
ChatFormatter.builder().regex(URL_PATTERN).underlined(true).openlink("$1").type(ChatFormatter.Type.Excluder).build());
|
||||||
private static Gson gson = new GsonBuilder()
|
private static Gson gson = new GsonBuilder()
|
||||||
.registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum())
|
.registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum())
|
||||||
.registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection())
|
.registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection())
|
||||||
.registerTypeAdapter(Boolean.class, new TellrawSerializer.TwBool())
|
.registerTypeAdapter(Boolean.class, new TellrawSerializer.TwBool())
|
||||||
.registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create();
|
.registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create();
|
||||||
private static final String[] testPlayers = {"Koiiev", "iie", "Alisolarflare", "NorbiPeti", "Arsen_Derby_FTW", "carrot_lynx"};
|
private static final String[] testPlayers = {"Koiiev", "iie", "Alisolarflare", "NorbiPeti", "Arsen_Derby_FTW", "carrot_lynx"};
|
||||||
static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
|
public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
|
||||||
|
|
||||||
private ChatProcessing() {
|
private ChatProcessing() {
|
||||||
}
|
}
|
||||||
|
@ -114,7 +118,7 @@ public class ChatProcessing {
|
||||||
else //Due to the online player map, getPlayer() can be more efficient than getAs()
|
else //Due to the online player map, getPlayer() can be more efficient than getAs()
|
||||||
mp = e.getUser().getAs(ChatPlayer.class); //May be null
|
mp = e.getUser().getAs(ChatPlayer.class); //May be null
|
||||||
|
|
||||||
Color colormode = channel.color;
|
Color colormode = channel.Color().get();
|
||||||
if (mp != null && mp.OtherColorMode != null)
|
if (mp != null && mp.OtherColorMode != null)
|
||||||
colormode = mp.OtherColorMode;
|
colormode = mp.OtherColorMode;
|
||||||
if (message.startsWith(">"))
|
if (message.startsWith(">"))
|
||||||
|
@ -122,9 +126,9 @@ public class ChatProcessing {
|
||||||
// If greentext, ignore channel or player colors
|
// If greentext, ignore channel or player colors
|
||||||
|
|
||||||
ArrayList<ChatFormatter> formatters = addFormatters(colormode);
|
ArrayList<ChatFormatter> formatters = addFormatters(colormode);
|
||||||
if (colormode == channel.color && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color
|
if (colormode == channel.Color().get() && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color
|
||||||
final AtomicInteger rpc = new AtomicInteger(0);
|
final AtomicInteger rpc = new AtomicInteger(0);
|
||||||
formatters.add(ChatFormatter.builder().color(colormode).onmatch((match, cf) -> {
|
formatters.add(ChatFormatter.builder().color(colormode).onmatch((match, cf, s) -> {
|
||||||
cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]);
|
cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]);
|
||||||
return match;
|
return match;
|
||||||
}).build());
|
}).build());
|
||||||
|
@ -233,7 +237,7 @@ public class ChatProcessing {
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getChannelID(Channel channel, CommandSender sender, String origin) {
|
static String getChannelID(Channel channel, CommandSender sender, String origin) {
|
||||||
return ("[" + (MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName)
|
return ("[" + (MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get())
|
||||||
+ "]";
|
+ "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,14 +261,12 @@ public class ChatProcessing {
|
||||||
namesb.append(")");
|
namesb.append(")");
|
||||||
StringBuilder nicksb = new StringBuilder("(?i)(");
|
StringBuilder nicksb = new StringBuilder("(?i)(");
|
||||||
boolean addNickFormatter = false;
|
boolean addNickFormatter = false;
|
||||||
int index = 0;
|
|
||||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||||
final String nick = PlayerListener.nicknames.inverse().get(p.getUniqueId());
|
final String nick = PlayerListener.nicknames.inverse().get(p.getUniqueId());
|
||||||
if (nick != null) {
|
if (nick != null) {
|
||||||
nicksb.append(nick).append("|");
|
nicksb.append(nick).append("|");
|
||||||
addNickFormatter = true; //Add it even if there's only 1 player online (it was in the if)
|
addNickFormatter = true; //Add it even if there's only 1 player online (it was in the if)
|
||||||
}
|
}
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
nicksb.deleteCharAt(nicksb.length() - 1);
|
nicksb.deleteCharAt(nicksb.length() - 1);
|
||||||
nicksb.append(")");
|
nicksb.append(")");
|
||||||
|
@ -277,7 +279,7 @@ public class ChatProcessing {
|
||||||
};
|
};
|
||||||
|
|
||||||
formatters.add(ChatFormatter.builder().regex(Pattern.compile(namesb.toString())).color(Color.Aqua)
|
formatters.add(ChatFormatter.builder().regex(Pattern.compile(namesb.toString())).color(Color.Aqua)
|
||||||
.onmatch((match, builder) -> {
|
.onmatch((match, builder, section) -> {
|
||||||
Player p = Bukkit.getPlayer(match);
|
Player p = Bukkit.getPlayer(match);
|
||||||
Optional<String> pn = nottest ? Optional.empty()
|
Optional<String> pn = nottest ? Optional.empty()
|
||||||
: Arrays.stream(testPlayers).filter(tp -> tp.equalsIgnoreCase(match)).findAny();
|
: Arrays.stream(testPlayers).filter(tp -> tp.equalsIgnoreCase(match)).findAny();
|
||||||
|
@ -287,11 +289,11 @@ public class ChatProcessing {
|
||||||
}
|
}
|
||||||
ChatPlayer mpp = TBMCPlayer.getPlayer(nottest ? p.getUniqueId() : new UUID(0, 0), ChatPlayer.class);
|
ChatPlayer mpp = TBMCPlayer.getPlayer(nottest ? p.getUniqueId() : new UUID(0, 0), ChatPlayer.class);
|
||||||
if (nottest) {
|
if (nottest) {
|
||||||
if (PlayerListener.NotificationSound == null)
|
if (PluginMain.Instance.notificationSound().get().length() == 0)
|
||||||
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn
|
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn
|
||||||
else
|
else
|
||||||
p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f,
|
p.playSound(p.getLocation(), PluginMain.Instance.notificationSound().get(), 1.0f,
|
||||||
(float) PlayerListener.NotificationPitch);
|
PluginMain.Instance.notificationPitch().get());
|
||||||
}
|
}
|
||||||
String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
|
String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
|
||||||
return color + (nottest ? p.getName() : pn.get()) + "§r"; //Fix name casing, except when testing
|
return color + (nottest ? p.getName() : pn.get()) + "§r"; //Fix name casing, except when testing
|
||||||
|
@ -299,7 +301,7 @@ public class ChatProcessing {
|
||||||
|
|
||||||
if (addNickFormatter)
|
if (addNickFormatter)
|
||||||
formatters.add(ChatFormatter.builder().regex((Pattern.compile(nicksb.toString()))).color(Color.Aqua)
|
formatters.add(ChatFormatter.builder().regex((Pattern.compile(nicksb.toString()))).color(Color.Aqua)
|
||||||
.onmatch((match, builder) -> {
|
.onmatch((match, builder, section) -> {
|
||||||
if (PlayerListener.nicknames.containsKey(match.toLowerCase())) { //Made a stream and all that but I can actually store it lowercased
|
if (PlayerListener.nicknames.containsKey(match.toLowerCase())) { //Made a stream and all that but I can actually store it lowercased
|
||||||
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match.toLowerCase()));
|
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match.toLowerCase()));
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
|
@ -307,11 +309,11 @@ public class ChatProcessing {
|
||||||
+ match.toLowerCase() + " but was reported as online.");
|
+ match.toLowerCase() + " but was reported as online.");
|
||||||
return "§c" + match + "§r";
|
return "§c" + match + "§r";
|
||||||
}
|
}
|
||||||
if (PlayerListener.NotificationSound == null)
|
if (PluginMain.Instance.notificationSound().get().length() == 0)
|
||||||
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f);
|
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn
|
||||||
else
|
else
|
||||||
p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f,
|
p.playSound(p.getLocation(), PluginMain.Instance.notificationSound().get(), 1.0f,
|
||||||
(float) PlayerListener.NotificationPitch);
|
PluginMain.Instance.notificationPitch().get());
|
||||||
return PluginMain.essentials.getUser(p).getNickname();
|
return PluginMain.essentials.getUser(p).getNickname();
|
||||||
}
|
}
|
||||||
error.accept("Player nicknamed " + match.toLowerCase()
|
error.accept("Player nicknamed " + match.toLowerCase()
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,83 +1,51 @@
|
||||||
package buttondevteam.chat;
|
package buttondevteam.chat;
|
||||||
|
|
||||||
import buttondevteam.chat.commands.YeehawCommand;
|
import buttondevteam.chat.commands.YeehawCommand;
|
||||||
import buttondevteam.chat.commands.ucmds.TownColorCommand;
|
import buttondevteam.chat.components.announce.AnnouncerComponent;
|
||||||
|
import buttondevteam.chat.components.flair.FlairComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownyListener;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
||||||
import buttondevteam.chat.listener.PlayerListener;
|
import buttondevteam.chat.listener.PlayerListener;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.architecture.ButtonPlugin;
|
||||||
import buttondevteam.lib.chat.Channel.RecipientTestResult;
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
import buttondevteam.lib.architecture.ConfigData;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
|
||||||
import com.earth2me.essentials.Essentials;
|
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.chat.Chat;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
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.RegisteredServiceProvider;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.bukkit.scoreboard.Scoreboard;
|
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;
|
public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15.
|
||||||
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.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
|
||||||
// A user, which flair isn't obtainable:
|
// A user, which flair isn't obtainable:
|
||||||
// https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
|
// https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/
|
||||||
public static PluginMain Instance;
|
public static PluginMain Instance;
|
||||||
public static ConsoleCommandSender Console;
|
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 Scoreboard SB;
|
||||||
public static TownyUniverse TU;
|
|
||||||
private static ArrayList<Town> Towns;
|
|
||||||
private static ArrayList<Nation> Nations;
|
|
||||||
|
|
||||||
public static Channel TownChat;
|
public static Channel TownChat;
|
||||||
public static Channel NationChat;
|
public static Channel NationChat;
|
||||||
private static Channel RPChannel;
|
|
||||||
|
|
||||||
/**
|
public ConfigData<String> notificationSound() {
|
||||||
* <p>
|
return getIConfig().getData("notificationSound", "");
|
||||||
* This variable is used as a cache for flair state checking when reading the flair thread.
|
}
|
||||||
* </p>
|
|
||||||
* <p>
|
public ConfigData<Float> notificationPitch() {
|
||||||
* It's used because normally it has to load all associated player files every time to read the flair state
|
return getIConfig().getData("notificationPitch", 1.0f);
|
||||||
* </p>
|
}
|
||||||
*/
|
|
||||||
private Set<String> PlayersWithFlairs = new HashSet<>();
|
|
||||||
|
|
||||||
// Fired when plugin is first enabled
|
// Fired when plugin is first enabled
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void pluginEnable() {
|
||||||
Instance = this;
|
Instance = this;
|
||||||
PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials"));
|
PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials"));
|
||||||
|
|
||||||
|
@ -86,260 +54,26 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this);
|
TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this);
|
||||||
TBMCChatAPI.AddCommands(this, YeehawCommand.class);
|
TBMCChatAPI.AddCommands(this, YeehawCommand.class);
|
||||||
Console = this.getServer().getConsoleSender();
|
Console = this.getServer().getConsoleSender();
|
||||||
LoadFiles();
|
|
||||||
|
|
||||||
SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...]
|
SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...]
|
||||||
TU = ((Towny) Bukkit.getPluginManager().getPlugin("Towny")).getTownyUniverse();
|
|
||||||
Towns = new ArrayList<>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed
|
|
||||||
Nations = new ArrayList<>(TU.getNationsMap().values()); // Same here but with nations
|
|
||||||
|
|
||||||
TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns
|
Component.registerComponent(this, new TownyComponent());
|
||||||
NationColor.keySet().removeIf(n -> !TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations
|
|
||||||
|
|
||||||
TBMCChatAPI.RegisterChatChannel(
|
TBMCChatAPI.RegisterChatChannel(new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global
|
||||||
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
|
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(this, () -> {
|
|
||||||
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
|
||||||
if (dtp == null)
|
|
||||||
return;
|
|
||||||
for (val entry : TownColors.entrySet())
|
|
||||||
setTownColor(dtp, buttondevteam.chat.commands.ucmds.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue());
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!setupEconomy() || !setupPermissions())
|
if (!setupEconomy() || !setupPermissions())
|
||||||
TBMCCoreAPI.SendException("We're in trouble", new Exception("Failed to set up economy or permissions!"));
|
TBMCCoreAPI.SendException("We're in trouble", new Exception("Failed to set up economy or permissions!"));
|
||||||
|
|
||||||
new Thread(this::FlairGetterThreadMethod).start();
|
Component.registerComponent(this, new TownColorComponent());
|
||||||
new Thread(new AnnouncerThread()).start();
|
Component.registerComponent(this, new FlairComponent()); //The original purpose of this plugin
|
||||||
|
Component.registerComponent(this, new AnnouncerComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a town's color on Dynmap.
|
|
||||||
*
|
|
||||||
* @param dtp A reference for the Dynmap-Towny plugin
|
|
||||||
* @param town The town's name using the correct casing
|
|
||||||
* @param colors The town's colors
|
|
||||||
*/
|
|
||||||
public static void setTownColor(DynmapTownyPlugin dtp, String town, Color[] colors) {
|
|
||||||
Function<Color, Integer> c2i = c -> c.getRed() << 16 | c.getGreen() << 8 | c.getBlue();
|
|
||||||
try {
|
|
||||||
DTBridge.setTownColor(dtp, town, c2i.apply(colors[0]),
|
|
||||||
c2i.apply(colors.length > 1 ? colors[1] : colors[0]));
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("Failed to set town color for town " + town + "!", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean stop = false;
|
|
||||||
public static Essentials essentials = null;
|
public static Essentials essentials = null;
|
||||||
|
|
||||||
// Fired when plugin is disabled
|
// Fired when plugin is disabled
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void pluginDisable() {
|
||||||
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
|
|
||||||
*/
|
|
||||||
public static Map<String, Color[]> TownColors = new HashMap<>();
|
|
||||||
/**
|
|
||||||
* Names lowercased - nation color gets added to town colors when needed
|
|
||||||
*/
|
|
||||||
public static Map<String, Color> NationColor = new HashMap<>();
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static void LoadFiles() {
|
|
||||||
PluginMain.Instance.getLogger().info("Loading files...");
|
|
||||||
try {
|
|
||||||
File file = new File("TBMC/chatsettings.yml");
|
|
||||||
if (file.exists()) {
|
|
||||||
YamlConfiguration yc = new YamlConfiguration();
|
|
||||||
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()
|
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, v -> ((List<String>) v.getValue()).stream()
|
|
||||||
.map(Color::valueOf).toArray(Color[]::new))));
|
|
||||||
TownColorCommand.ColorCount = (byte) yc.getInt("towncolorcount", 1);
|
|
||||||
val ncs = yc.getConfigurationSection("nationcolors");
|
|
||||||
if (ncs != null)
|
|
||||||
NationColor.putAll(ncs.getValues(true).entrySet().stream()
|
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, v -> Color.valueOf((String) v.getValue()))));
|
|
||||||
PluginMain.Instance.getLogger().info("Loaded files!");
|
|
||||||
} else
|
|
||||||
PluginMain.Instance.getLogger().info("No files to load, first run probably.");
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("Error while loading chat files!", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SaveFiles() {
|
|
||||||
PluginMain.Instance.getLogger().info("Saving files...");
|
|
||||||
try {
|
|
||||||
File file = new File("TBMC/chatsettings.yml");
|
|
||||||
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);
|
|
||||||
yc.createSection("nationcolors", NationColor.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
|
|
||||||
v -> v.getValue().toString())));
|
|
||||||
yc.save(file);
|
|
||||||
PluginMain.Instance.getLogger().info("Saved files!");
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("Error while loading chat files!", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Permission permission = null;
|
public static Permission permission = null;
|
||||||
|
@ -355,16 +89,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
||||||
return (permission != null);
|
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() {
|
private boolean setupEconomy() {
|
||||||
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager()
|
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager()
|
||||||
.getRegistration(net.milkbowl.vault.economy.Economy.class);
|
.getRegistration(net.milkbowl.vault.economy.Economy.class);
|
||||||
|
@ -375,52 +99,4 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
||||||
return (economy != null);
|
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 = PluginMain.Nations.indexOf(nation);
|
|
||||||
if (index < 0) {
|
|
||||||
PluginMain.Nations.add(nation);
|
|
||||||
index = PluginMain.Nations.size() - 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
index = PluginMain.Towns.indexOf(town);
|
|
||||||
if (index < 0) {
|
|
||||||
PluginMain.Towns.add(town);
|
|
||||||
index = PluginMain.Towns.size() - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package buttondevteam.chat.commands;
|
package buttondevteam.chat.commands;
|
||||||
|
|
||||||
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.lib.TBMCChatEventBase;
|
import buttondevteam.lib.TBMCChatEventBase;
|
||||||
import buttondevteam.lib.chat.Channel;
|
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.TBMCCommandBase;
|
import buttondevteam.lib.chat.TBMCCommandBase;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.commands.ucmds;
|
||||||
|
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.lib.chat.ChatMessage;
|
import buttondevteam.lib.chat.ChatMessage;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -39,9 +39,9 @@ public class HistoryCommand extends UCommandBase {
|
||||||
sender.sendMessage("§6---- Chat History ----");
|
sender.sendMessage("§6---- Chat History ----");
|
||||||
Stream<Channel> stream;
|
Stream<Channel> stream;
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
stream = Channel.getChannels().stream();
|
stream = Channel.getChannels();
|
||||||
} else {
|
} 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()) {
|
if (!och.isPresent()) {
|
||||||
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
|
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
|
||||||
return true;
|
return true;
|
||||||
|
@ -54,7 +54,7 @@ public class HistoryCommand extends UCommandBase {
|
||||||
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
|
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
|
||||||
HistoryEntry e = arr[i];
|
HistoryEntry e = arr[i];
|
||||||
val cm = e.chatMessage;
|
val cm = e.chatMessage;
|
||||||
sender.sendMessage("[" + e.channel.DisplayName + "] " + cm.getSender().getName() + ": " + cm.getMessage());
|
sender.sendMessage("[" + e.channel.DisplayName().get() + "] " + cm.getSender().getName() + ": " + cm.getMessage());
|
||||||
sent.set(true);
|
sent.set(true);
|
||||||
}
|
}
|
||||||
if (!sent.get())
|
if (!sent.get())
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
package buttondevteam.chat.commands.ucmds.admin;
|
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class SaveCommand extends AdminCommandBase {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] GetHelpText(String alias) {
|
|
||||||
return new String[] { "§6---- Save config ----",
|
|
||||||
"This command saves the config file(s)" };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean OnCommand(CommandSender sender, String alias,
|
|
||||||
String[] args) {
|
|
||||||
PluginMain.SaveFiles(); // 2015.08.09.
|
|
||||||
sender.sendMessage("§bSaved files. Now you can edit them and reload if you want.§r");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
package buttondevteam.chat.components;
|
|
||||||
|
|
||||||
import buttondevteam.lib.architecture.Component;
|
|
||||||
import buttondevteam.lib.architecture.ConfigData;
|
|
||||||
|
|
||||||
public class TownColorComponent extends Component {
|
|
||||||
public ConfigData<Byte> colorCount() { //TODO
|
|
||||||
return getData("colorCount", (byte) 1, cc -> (byte) cc, cc -> (int) cc);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigData<Boolean> useNationColors() { //TODO
|
|
||||||
return getData("useNationColors", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void enable() {
|
|
||||||
//TODO: Don't register all commands automatically (welp)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void disable() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,7 @@
|
||||||
package buttondevteam.chat.commands.ucmds.announce;
|
package buttondevteam.chat.components.announce;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class AddCommand extends AnnounceCommandBase {
|
public class AddCommand extends AnnounceCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -29,7 +27,8 @@ public class AddCommand extends AnnounceCommandBase {
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
}
|
}
|
||||||
String finalmessage = sb.toString().replace('&', '§');
|
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");
|
sender.sendMessage("§bAnnouncement added. - Plase avoid using this command if possible, see /u announce add without args.§r");
|
||||||
return true;
|
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.chat.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
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.BlockCommandSender;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class EditCommand extends AnnounceCommandBase {
|
public class EditCommand extends AnnounceCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,9 +32,10 @@ public class EditCommand extends AnnounceCommandBase {
|
||||||
int index = Integer.parseInt(args[0]);
|
int index = Integer.parseInt(args[0]);
|
||||||
if (index > 100)
|
if (index > 100)
|
||||||
return false;
|
return false;
|
||||||
while (PluginMain.AnnounceMessages.size() <= index)
|
AnnouncerComponent component = (AnnouncerComponent) getComponent();
|
||||||
PluginMain.AnnounceMessages.add("");
|
while (component.AnnounceMessages().get().size() <= index)
|
||||||
PluginMain.AnnounceMessages.set(Integer.parseInt(args[0]),
|
component.AnnounceMessages().get().add("");
|
||||||
|
component.AnnounceMessages().get().set(Integer.parseInt(args[0]),
|
||||||
finalmessage1);
|
finalmessage1);
|
||||||
sender.sendMessage("Announcement edited.");
|
sender.sendMessage("Announcement edited.");
|
||||||
return true;
|
return true;
|
|
@ -1,9 +1,7 @@
|
||||||
package buttondevteam.chat.commands.ucmds.announce;
|
package buttondevteam.chat.components.announce;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class ListCommand extends AnnounceCommandBase {
|
public class ListCommand extends AnnounceCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -18,10 +16,11 @@ public class ListCommand extends AnnounceCommandBase {
|
||||||
sender.sendMessage("§bList of announce messages:§r");
|
sender.sendMessage("§bList of announce messages:§r");
|
||||||
sender.sendMessage("§bFormat: [index] message§r");
|
sender.sendMessage("§bFormat: [index] message§r");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (String message : PluginMain.AnnounceMessages)
|
AnnouncerComponent component = (AnnouncerComponent) getComponent();
|
||||||
|
for (String message : component.AnnounceMessages().get())
|
||||||
sender.sendMessage("[" + i++ + "] " + message);
|
sender.sendMessage("[" + i++ + "] " + message);
|
||||||
sender.sendMessage("§bCurrent wait time between announcements: "
|
sender.sendMessage("§bCurrent wait time between announcements: "
|
||||||
+ PluginMain.AnnounceTime / 60 / 1000 + " minute(s)§r");
|
+ component.AnnounceTime().get() / 60 / 1000 + " minute(s)§r");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package buttondevteam.chat.commands.ucmds.announce;
|
package buttondevteam.chat.components.announce;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class RemoveCommand extends AnnounceCommandBase {
|
public class RemoveCommand extends AnnounceCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,7 +21,7 @@ public class RemoveCommand extends AnnounceCommandBase {
|
||||||
sender.sendMessage("§cUsage: /u announce remove <index>");
|
sender.sendMessage("§cUsage: /u announce remove <index>");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
PluginMain.AnnounceMessages.remove(Integer.parseInt(args[0]));
|
((AnnouncerComponent) getComponent()).AnnounceMessages().get().remove(Integer.parseInt(args[0]));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package buttondevteam.chat.commands.ucmds.announce;
|
package buttondevteam.chat.components.announce;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
|
||||||
|
|
||||||
public class SetTimeCommand extends AnnounceCommandBase {
|
public class SetTimeCommand extends AnnounceCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -20,7 +18,7 @@ public class SetTimeCommand extends AnnounceCommandBase {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
PluginMain.AnnounceTime = Integer.parseInt(args[0]) * 60 * 1000;
|
((AnnouncerComponent) getComponent()).AnnounceTime().set(Integer.parseInt(args[0]) * 60 * 1000);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sender.sendMessage("§cMinutes argument must be a number. Got: "
|
sender.sendMessage("§cMinutes argument must be a number. Got: "
|
||||||
+ args[0]);
|
+ args[0]);
|
|
@ -1,18 +1,16 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.flair;
|
||||||
|
|
||||||
import java.util.Timer;
|
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.FlairStates;
|
|
||||||
import buttondevteam.chat.PlayerJoinTimerTask;
|
import buttondevteam.chat.PlayerJoinTimerTask;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
|
||||||
@CommandClass(modOnly = false)
|
@CommandClass(modOnly = false)
|
||||||
@OptionallyPlayerCommandClass(playerOnly = true)
|
@OptionallyPlayerCommandClass(playerOnly = true)
|
||||||
|
@ -68,7 +66,7 @@ public class AcceptCommand extends UCommandBase {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
PluginMain.Instance.DownloadFlair(mp);
|
FlairComponent.DownloadFlair(mp);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException(
|
TBMCCoreAPI.SendException(
|
||||||
"An error occured while downloading flair for " + player.getCustomName() + "!", e);
|
"An error occured while downloading flair for " + player.getCustomName() + "!", e);
|
||||||
|
@ -86,7 +84,7 @@ public class AcceptCommand extends UCommandBase {
|
||||||
}
|
}
|
||||||
String flair = mp.GetFormattedFlair();
|
String flair = mp.GetFormattedFlair();
|
||||||
mp.FlairState().set(FlairStates.Accepted);
|
mp.FlairState().set(FlairStates.Accepted);
|
||||||
PluginMain.ConfirmUserMessage(mp);
|
FlairComponent.ConfirmUserMessage(mp);
|
||||||
player.sendMessage("§bYour flair has been set:§r " + flair);
|
player.sendMessage("§bYour flair has been set:§r " + flair);
|
||||||
mp.Working = false;
|
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 {
|
public enum FlairStates {
|
||||||
Accepted, Ignored, Recognised, Commented, NoComment
|
Accepted, Ignored, Recognised, Commented, NoComment
|
|
@ -1,13 +1,12 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.flair;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.FlairStates;
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@CommandClass(modOnly = false)
|
@CommandClass(modOnly = false)
|
||||||
@OptionallyPlayerCommandClass(playerOnly = true)
|
@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.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
|
||||||
import buttondevteam.chat.FlairStates;
|
|
||||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
|
||||||
|
|
||||||
public class SetFlairCommand extends AdminCommandBase {
|
public class SetFlairCommand extends AdminCommandBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -44,14 +43,14 @@ public class SetFlairCommand extends AdminCommandBase {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean cheater = false;
|
boolean cheater;
|
||||||
if (args[2].equalsIgnoreCase("true"))
|
if (args[2].equalsIgnoreCase("true"))
|
||||||
cheater = true;
|
cheater = true;
|
||||||
else if (args[2].equalsIgnoreCase("false"))
|
else if (args[2].equalsIgnoreCase("false"))
|
||||||
cheater = false;
|
cheater = false;
|
||||||
else {
|
else {
|
||||||
sender.sendMessage("§cUnknown value for cheater parameter. Run without args to see usage.");
|
sender.sendMessage("§cUnknown value for cheater parameter.");
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
ChatPlayer mp = TBMCPlayerBase.getPlayer(p.getUniqueId(), ChatPlayer.class);
|
ChatPlayer mp = TBMCPlayerBase.getPlayer(p.getUniqueId(), ChatPlayer.class);
|
||||||
mp.SetFlair(flairtime, cheater);
|
mp.SetFlair(flairtime, cheater);
|
|
@ -1,8 +1,8 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||||
|
@ -34,7 +34,7 @@ public class NColorCommand extends UCommandBase {
|
||||||
Resident res;
|
Resident res;
|
||||||
Town town;
|
Town town;
|
||||||
try {
|
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) {
|
|| (town = res.getTown()) == null) {
|
||||||
player.sendMessage("§cYou need to be in a town.");
|
player.sendMessage("§cYou need to be in a town.");
|
||||||
return true;
|
return true;
|
||||||
|
@ -53,7 +53,7 @@ public class NColorCommand extends UCommandBase {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String[] nameparts = arg.split("[|:]");
|
String[] nameparts = arg.split("[|:]");
|
||||||
Color[] towncolors = PluginMain.TownColors.get(town.getName().toLowerCase());
|
Color[] towncolors = TownColorComponent.TownColors.get(town.getName().toLowerCase());
|
||||||
if (towncolors == null) {
|
if (towncolors == null) {
|
||||||
player.sendMessage("§cYour town doesn't have a color set. The town mayor can set it using /u towncolor.");
|
player.sendMessage("§cYour town doesn't have a color set. The town mayor can set it using /u towncolor.");
|
||||||
return true;
|
return true;
|
||||||
|
@ -71,8 +71,8 @@ public class NColorCommand extends UCommandBase {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations()
|
ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations()
|
||||||
.set(new ArrayList<>(Arrays.stream(nameparts).map(np -> np.length()).collect(Collectors.toList()))); // No byte[], no TIntArrayList
|
.set(new ArrayList<>(Arrays.stream(nameparts).map(String::length).collect(Collectors.toList()))); // No byte[], no TIntArrayList
|
||||||
PlayerJoinLeaveListener.updatePlayerColors(player);
|
TownColorComponent.updatePlayerColors(player);
|
||||||
player.sendMessage("§bName colors set: " + player.getDisplayName());
|
player.sendMessage("§bName colors set: " + player.getDisplayName());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||||
|
@ -26,8 +27,8 @@ public class NationColorCommand extends UCommandBase {
|
||||||
@Override
|
@Override
|
||||||
public boolean OnCommand(Player player, String alias, String[] args) {
|
public boolean OnCommand(Player player, String alias, String[] args) {
|
||||||
Resident res;
|
Resident res;
|
||||||
if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||||
&& (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) {
|
&& (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.");
|
player.sendMessage("§cYou need to be the king of a nation to set it's colors.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +45,6 @@ public class NationColorCommand extends UCommandBase {
|
||||||
player.sendMessage("§cCouldn't find your town/nation... Error reported.");
|
player.sendMessage("§cCouldn't find your town/nation... Error reported.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return buttondevteam.chat.commands.ucmds.admin.NationColorCommand.SetNationColor(player, alias, a);
|
return buttondevteam.chat.components.towncolors.admin.NationColorCommand.SetNationColor(player, alias, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
|
||||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||||
import com.palmergames.bukkit.towny.object.Resident;
|
import com.palmergames.bukkit.towny.object.Resident;
|
||||||
|
import lombok.val;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@CommandClass // TODO: /u u when annotation not present
|
@CommandClass // TODO: /u u when annotation not present
|
||||||
|
@ -14,7 +16,8 @@ public class TownColorCommand extends UCommandBase {
|
||||||
@Override
|
@Override
|
||||||
public String GetHelpText(String alias)[] {
|
public String GetHelpText(String alias)[] {
|
||||||
StringBuilder cns = new StringBuilder(" <colorname1>");
|
StringBuilder cns = new StringBuilder(" <colorname1>");
|
||||||
for (int i = 2; i <= ColorCount; i++)
|
val comp = (TownColorComponent) getComponent();
|
||||||
|
for (int i = 2; i <= comp.colorCount().get(); i++)
|
||||||
cns.append(" [colorname").append(i).append("]");
|
cns.append(" [colorname").append(i).append("]");
|
||||||
return new String[] { //
|
return new String[] { //
|
||||||
"§6---- Town Color ----", //
|
"§6---- Town Color ----", //
|
||||||
|
@ -26,18 +29,17 @@ public class TownColorCommand extends UCommandBase {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte ColorCount;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean OnCommand(Player player, String alias, String[] args) {
|
public boolean OnCommand(Player player, String alias, String[] args) {
|
||||||
Resident res;
|
Resident res;
|
||||||
if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
|
||||||
&& (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) {
|
&& (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.");
|
player.sendMessage("§cYou need to be the mayor of a town to set it's colors.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (args.length > ColorCount) {
|
val comp = (TownColorComponent) getComponent();
|
||||||
player.sendMessage("You can only use " + ColorCount + " color" + (ColorCount > 1 ? "s" : "") + ".");
|
if (args.length > comp.colorCount().get()) {
|
||||||
|
player.sendMessage("You can only use " + comp.colorCount().get() + " color" + (comp.colorCount().get() > 1 ? "s" : "") + ".");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String[] a = new String[args.length + 1];
|
String[] a = new String[args.length + 1];
|
||||||
|
@ -49,6 +51,6 @@ public class TownColorCommand extends UCommandBase {
|
||||||
player.sendMessage("§cCouldn't find your town... Error reported.");
|
player.sendMessage("§cCouldn't find your town... Error reported.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return buttondevteam.chat.commands.ucmds.admin.TownColorCommand.SetTownColor(player, alias, a);
|
return buttondevteam.chat.components.towncolors.admin.TownColorCommand.SetTownColor(player, alias, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,201 @@
|
||||||
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
|
import buttondevteam.chat.ChatPlayer;
|
||||||
|
import buttondevteam.chat.PluginMain;
|
||||||
|
import buttondevteam.chat.components.towncolors.admin.TCCount;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
|
import buttondevteam.core.ComponentManager;
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
import buttondevteam.lib.architecture.ComponentMetadata;
|
||||||
|
import buttondevteam.lib.architecture.ConfigData;
|
||||||
|
import buttondevteam.lib.chat.Color;
|
||||||
|
import com.earth2me.essentials.User;
|
||||||
|
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||||
|
import lombok.experimental.var;
|
||||||
|
import lombok.val;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.dynmap.towny.DTBridge;
|
||||||
|
import org.dynmap.towny.DynmapTownyPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ComponentMetadata(depends = TownyComponent.class)
|
||||||
|
public class TownColorComponent extends Component {
|
||||||
|
/**
|
||||||
|
* Names lowercased
|
||||||
|
*/
|
||||||
|
public static Map<String, Color[]> TownColors = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* Names lowercased - nation color gets added to town colors when needed
|
||||||
|
*/
|
||||||
|
public static Map<String, Color> NationColor = new HashMap<>();
|
||||||
|
|
||||||
|
public ConfigData<Byte> colorCount() {
|
||||||
|
return getConfig().getData("colorCount", (byte) 1, cc -> ((Integer) cc).byteValue(), Byte::intValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigData<Boolean> useNationColors() { //TODO
|
||||||
|
return getConfig().getData("useNationColors", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
protected void enable() {
|
||||||
|
//TODO: Don't register all commands automatically (welp)
|
||||||
|
Consumer<ConfigurationSection> loadTC = cs -> TownColorComponent.TownColors.putAll(cs.getValues(true).entrySet().stream()
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, v -> ((List<String>) v.getValue()).stream()
|
||||||
|
.map(Color::valueOf).toArray(Color[]::new))));
|
||||||
|
Consumer<ConfigurationSection> loadNC = ncs ->
|
||||||
|
TownColorComponent.NationColor.putAll(ncs.getValues(true).entrySet().stream()
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, v -> Color.valueOf((String) v.getValue()))));
|
||||||
|
var cs = getConfig().getConfig().getConfigurationSection("towncolors");
|
||||||
|
if (cs != null)
|
||||||
|
loadTC.accept(cs);
|
||||||
|
else
|
||||||
|
load_old(loadTC, null); //Load old data
|
||||||
|
var ncs = getConfig().getConfig().getConfigurationSection("nationcolors");
|
||||||
|
if (ncs != null)
|
||||||
|
loadNC.accept(ncs);
|
||||||
|
else
|
||||||
|
load_old(null, loadNC); //Why not choose by making different args null
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTask(getPlugin(), () -> {
|
||||||
|
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
||||||
|
if (dtp == null)
|
||||||
|
return;
|
||||||
|
for (val entry : TownColors.entrySet())
|
||||||
|
setTownColor(dtp, buttondevteam.chat.components.towncolors.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
registerCommand(new TownColorCommand());
|
||||||
|
registerCommand(new NationColorCommand());
|
||||||
|
registerCommand(new buttondevteam.chat.components.towncolors.admin.TownColorCommand());
|
||||||
|
registerCommand(new buttondevteam.chat.components.towncolors.admin.NationColorCommand());
|
||||||
|
registerCommand(new TCCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void disable() {
|
||||||
|
getConfig().getConfig().createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
|
||||||
|
v -> Arrays.stream(v.getValue()).map(Enum::toString).toArray(String[]::new))));
|
||||||
|
getConfig().getConfig().createSection("nationcolors", NationColor.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
|
||||||
|
v -> v.getValue().toString())));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a town's color on Dynmap.
|
||||||
|
*
|
||||||
|
* @param dtp A reference for the Dynmap-Towny plugin
|
||||||
|
* @param town The town's name using the correct casing
|
||||||
|
* @param colors The town's colors
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void setTownColor(DynmapTownyPlugin dtp, String town, Color[] colors) {
|
||||||
|
Function<Color, Integer> c2i = c -> c.getRed() << 16 | c.getGreen() << 8 | c.getBlue();
|
||||||
|
try {
|
||||||
|
DTBridge.setTownColor(dtp, town, c2i.apply(colors[0]),
|
||||||
|
c2i.apply(colors.length > 1 ? colors[1] : colors[0]));
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Failed to set town color for town " + town + "!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getPlayerNickname(Player player, User user, ChatPlayer cp) {
|
||||||
|
String nickname = user.getNick(true);
|
||||||
|
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 = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase());
|
||||||
|
if (res == null || !res.hasTown())
|
||||||
|
return name;
|
||||||
|
try {
|
||||||
|
Color[] clrs = Optional.ofNullable(
|
||||||
|
TownColors.get(res.getTown().getName().toLowerCase())
|
||||||
|
).orElse(new Color[]{Color.White}); //Use white as default town color
|
||||||
|
StringBuilder ret = new StringBuilder();
|
||||||
|
AtomicInteger prevlen = new AtomicInteger();
|
||||||
|
BiFunction<Color, Integer, String> anyColoredNamePart = (c, len) -> "§" //Len==0 if last part
|
||||||
|
+ Integer.toHexString(c.ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end
|
||||||
|
+ (len == 0 ? name.substring(prevlen.get())
|
||||||
|
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
|
||||||
|
BiFunction<Integer, Integer, String> coloredNamePart = (len, i)
|
||||||
|
-> anyColoredNamePart.apply(clrs[i], i + 1 == clrs.length ? 0 : len);
|
||||||
|
final int len = name.length() / (clrs.length + 1); //The above param is needed because this isn't always passed
|
||||||
|
Color nc;
|
||||||
|
/*if(res.getTown().hasNation()
|
||||||
|
&&(nc=PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase()))!=null)
|
||||||
|
len = name.length() / (clrs.length+1);
|
||||||
|
else
|
||||||
|
len = name.length() / clrs.length;*/
|
||||||
|
val nclar = cp.NameColorLocations().get();
|
||||||
|
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
|
||||||
|
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length + 1)) //+1: Nation color
|
||||||
|
ncl = null; // Reset if name length changed
|
||||||
|
//System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length());
|
||||||
|
if (!res.getTown().hasNation()
|
||||||
|
|| (nc = NationColor.get(res.getTown().getNation().getName().toLowerCase())) == null)
|
||||||
|
nc = Color.White;
|
||||||
|
ret.append(anyColoredNamePart.apply(nc, ncl == null ? len : ncl[0])); //Make first color the nation color
|
||||||
|
for (int i = 0; i < clrs.length; i++)
|
||||||
|
//ret.append(coloredNamePart.apply(ncl == null ? len : (nc==null?ncl[i]:ncl[i+1]), i));
|
||||||
|
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i + 1], i));
|
||||||
|
return ret.toString();
|
||||||
|
} catch (NotRegisteredException e) {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the component is enabled
|
||||||
|
*/
|
||||||
|
public static void updatePlayerColors(Player player) { //Probably while ingame (/u ncolor)
|
||||||
|
updatePlayerColors(player, ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the component is enabled
|
||||||
|
*/
|
||||||
|
public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames
|
||||||
|
if (!ComponentManager.isEnabled(TownColorComponent.class))
|
||||||
|
return;
|
||||||
|
User user = PluginMain.essentials.getUser(player);
|
||||||
|
user.setNickname(getPlayerNickname(player, user, cp));
|
||||||
|
user.setDisplayNick(); //These won't fire the nick change event
|
||||||
|
cp.FlairUpdate(); //Update in list
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void load_old(Consumer<ConfigurationSection> loadTC,
|
||||||
|
Consumer<ConfigurationSection> loadNC) {
|
||||||
|
PluginMain.Instance.getLogger().info("Loading files...");
|
||||||
|
try {
|
||||||
|
File file = new File("TBMC/chatsettings.yml");
|
||||||
|
if (file.exists()) {
|
||||||
|
YamlConfiguration yc = new YamlConfiguration();
|
||||||
|
yc.load(file);
|
||||||
|
ConfigurationSection cs;
|
||||||
|
if (loadTC != null && (cs = yc.getConfigurationSection("towncolors")) != null)
|
||||||
|
loadTC.accept(cs);
|
||||||
|
if (loadNC != null && (cs = yc.getConfigurationSection("nationcolors")) != null)
|
||||||
|
loadNC.accept(cs);
|
||||||
|
PluginMain.Instance.getLogger().info("Loaded files!");
|
||||||
|
} else
|
||||||
|
PluginMain.Instance.getLogger().info("No files to load, first run probably.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error while loading chat files!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package buttondevteam.chat.listener;
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
|
@ -16,21 +16,21 @@ import java.util.Objects;
|
||||||
public class TownyListener implements Listener {
|
public class TownyListener implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTownRename(RenameTownEvent event) {
|
public void onTownRename(RenameTownEvent event) {
|
||||||
val clrs = PluginMain.TownColors.remove(event.getOldName().toLowerCase());
|
val clrs = TownColorComponent.TownColors.remove(event.getOldName().toLowerCase());
|
||||||
if (clrs != null)
|
if (clrs != null)
|
||||||
PluginMain.TownColors.put(event.getTown().getName().toLowerCase(), clrs);
|
TownColorComponent.TownColors.put(event.getTown().getName().toLowerCase(), clrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler //Gets called on town load as well
|
@EventHandler //Gets called on town load as well
|
||||||
public void onTownJoin(TownAddResidentEvent event) {
|
public void onTownJoin(TownAddResidentEvent event) {
|
||||||
Player p = Bukkit.getPlayer(event.getResident().getName());
|
Player p = Bukkit.getPlayer(event.getResident().getName());
|
||||||
if (p != null)
|
if (p != null)
|
||||||
PlayerJoinLeaveListener.updatePlayerColors(p);
|
TownColorComponent.updatePlayerColors(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateTownMembers(Town town) {
|
public static void updateTownMembers(Town town) {
|
||||||
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
|
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
|
||||||
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
|
.filter(Objects::nonNull).forEach(TownColorComponent::updatePlayerColors);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
@ -47,7 +47,7 @@ public class TownyListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTownDelete(DeleteTownEvent event) {
|
public void onTownDelete(DeleteTownEvent event) {
|
||||||
PluginMain.TownColors.remove(event.getTownName().toLowerCase());
|
TownColorComponent.TownColors.remove(event.getTownName().toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
@ -61,9 +61,9 @@ public class TownyListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onNationRename(RenameNationEvent event) {
|
public void onNationRename(RenameNationEvent event) {
|
||||||
val clrs = PluginMain.NationColor.remove(event.getOldName().toLowerCase());
|
val clrs = TownColorComponent.NationColor.remove(event.getOldName().toLowerCase());
|
||||||
if (clrs != null)
|
if (clrs != null)
|
||||||
PluginMain.NationColor.put(event.getNation().getName().toLowerCase(), clrs);
|
TownColorComponent.NationColor.put(event.getNation().getName().toLowerCase(), clrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler //Gets called on town load as well
|
@EventHandler //Gets called on town load as well
|
||||||
|
@ -78,7 +78,7 @@ public class TownyListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onNationDelete(DeleteNationEvent event) {
|
public void onNationDelete(DeleteNationEvent event) {
|
||||||
PluginMain.NationColor.remove(event.getNationName().toLowerCase());
|
TownColorComponent.NationColor.remove(event.getNationName().toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
|
@ -1,7 +1,10 @@
|
||||||
package buttondevteam.chat.commands.ucmds.admin;
|
package buttondevteam.chat.components.towncolors.admin;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownyListener;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import com.palmergames.bukkit.towny.object.Nation;
|
import com.palmergames.bukkit.towny.object.Nation;
|
||||||
import com.palmergames.bukkit.towny.object.Town;
|
import com.palmergames.bukkit.towny.object.Town;
|
||||||
|
@ -31,7 +34,7 @@ public class NationColorCommand extends AdminCommandBase {
|
||||||
sender.sendMessage("§cYou can only use one color as a nation color.");
|
sender.sendMessage("§cYou can only use one color as a nation color.");
|
||||||
return true;
|
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) {
|
if (nation == null) {
|
||||||
sender.sendMessage("§cThe nation '" + args[0] + "' cannot be found.");
|
sender.sendMessage("§cThe nation '" + args[0] + "' cannot be found.");
|
||||||
return true;
|
return true;
|
||||||
|
@ -39,14 +42,14 @@ public class NationColorCommand extends AdminCommandBase {
|
||||||
val c = TownColorCommand.getColorOrSendError(args[1], sender);
|
val c = TownColorCommand.getColorOrSendError(args[1], sender);
|
||||||
if (!c.isPresent()) return true;
|
if (!c.isPresent()) return true;
|
||||||
if (!c.get().getName().equals(Color.White.getName())) { //Default nation color
|
if (!c.get().getName().equals(Color.White.getName())) { //Default nation color
|
||||||
for (val nc : PluginMain.NationColor.values()) {
|
for (val nc : TownColorComponent.NationColor.values()) {
|
||||||
if (nc.getName().equals(c.get().getName())) {
|
if (nc.getName().equals(c.get().getName())) {
|
||||||
sender.sendMessage("§cAnother nation already uses this color!");
|
sender.sendMessage("§cAnother nation already uses this color!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PluginMain.NationColor.put(args[0].toLowerCase(), c.get());
|
TownColorComponent.NationColor.put(args[0].toLowerCase(), c.get());
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> {
|
||||||
for (Town t : nation.getTowns())
|
for (Town t : nation.getTowns())
|
||||||
TownyListener.updateTownMembers(t);
|
TownyListener.updateTownMembers(t);
|
|
@ -1,5 +1,8 @@
|
||||||
package buttondevteam.chat.commands.ucmds.admin;
|
package buttondevteam.chat.components.towncolors.admin;
|
||||||
|
|
||||||
|
import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import lombok.val;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
public class TCCount extends AdminCommandBase {
|
public class TCCount extends AdminCommandBase {
|
||||||
|
@ -20,7 +23,8 @@ public class TCCount extends AdminCommandBase {
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
buttondevteam.chat.commands.ucmds.TownColorCommand.ColorCount = count;
|
val comp = (TownColorComponent) getComponent();
|
||||||
|
comp.colorCount().set(count);
|
||||||
sender.sendMessage("Color count set to " + count);
|
sender.sendMessage("Color count set to " + count);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
|
@ -1,7 +1,10 @@
|
||||||
package buttondevteam.chat.commands.ucmds.admin;
|
package buttondevteam.chat.components.towncolors.admin;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownyListener;
|
||||||
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import com.palmergames.bukkit.towny.object.Town;
|
import com.palmergames.bukkit.towny.object.Town;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
@ -35,28 +38,28 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
public static boolean SetTownColor(CommandSender sender, String alias, String[] args) {
|
public static boolean SetTownColor(CommandSender sender, String alias, String[] args) {
|
||||||
if (args.length < 2)
|
if (args.length < 2)
|
||||||
return false;
|
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.");
|
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Color[] clrs = new Color[args.length - 1];
|
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++) {
|
for (int i = 1; i < args.length; i++) {
|
||||||
val c = getColorOrSendError(args[i], sender);
|
val c = getColorOrSendError(args[i], sender);
|
||||||
if (!c.isPresent())
|
if (!c.isPresent())
|
||||||
return true;
|
return true;
|
||||||
clrs[i - 1] = c.get();
|
clrs[i - 1] = c.get();
|
||||||
}
|
}
|
||||||
for (Map.Entry<String, Color[]> other : PluginMain.TownColors.entrySet()) {
|
for (Map.Entry<String, Color[]> other : TownColorComponent.TownColors.entrySet()) {
|
||||||
Color nc, tnc;
|
Color nc, tnc;
|
||||||
try {
|
try {
|
||||||
nc = PluginMain.NationColor.get(PluginMain.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase());
|
nc = TownColorComponent.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
|
} catch (Exception e) { //Too lazy for lots of null-checks and it may throw exceptions anyways
|
||||||
nc = null;
|
nc = null;
|
||||||
}
|
}
|
||||||
if (nc == null) nc = Color.White; //Default nation color
|
if (nc == null) nc = Color.White; //Default nation color
|
||||||
try {
|
try {
|
||||||
tnc = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase());
|
tnc = TownColorComponent.NationColor.get(targetTown.getNation().getName().toLowerCase());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
tnc = null;
|
tnc = null;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +77,7 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PluginMain.TownColors.put(args[0].toLowerCase(), clrs);
|
TownColorComponent.TownColors.put(args[0].toLowerCase(), clrs);
|
||||||
TownyListener.updateTownMembers(targetTown);
|
TownyListener.updateTownMembers(targetTown);
|
||||||
|
|
||||||
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
||||||
|
@ -83,7 +86,7 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!");
|
PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
PluginMain.setTownColor(dtp, targetTown.getName(), clrs);
|
TownColorComponent.setTownColor(dtp, targetTown.getName(), clrs);
|
||||||
sender.sendMessage("§bColor(s) set.");
|
sender.sendMessage("§bColor(s) set.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -103,6 +106,6 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTownNameCased(String name) {
|
public static String getTownNameCased(String name) {
|
||||||
return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName();
|
return TownyComponent.TU.getTownsMap().get(name.toLowerCase()).getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
package buttondevteam.chat.components.towny;
|
||||||
|
|
||||||
|
import buttondevteam.chat.ChatProcessing;
|
||||||
|
import buttondevteam.chat.PluginMain;
|
||||||
|
import buttondevteam.component.channel.Channel;
|
||||||
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
|
import com.palmergames.bukkit.towny.TownyLogger;
|
||||||
|
import lombok.val;
|
||||||
|
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class TownyAnnouncer {
|
||||||
|
private static final Pattern LOG_TYPE_PATTERN = Pattern.compile("\\[(\\w+) (?:Msg|Message)](?: (\\w+):)?");
|
||||||
|
private static final Handler HANDLER = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord logRecord) {
|
||||||
|
if (logRecord.getMessage() == null) return;
|
||||||
|
val m = LOG_TYPE_PATTERN.matcher(logRecord.getMessage());
|
||||||
|
if (!m.find()) return;
|
||||||
|
String groupID = m.group(2); //The group ID is correctly cased
|
||||||
|
switch (String.valueOf(m.group(1))) { //valueOf: Handles null
|
||||||
|
case "Town":
|
||||||
|
TBMCChatAPI.SendSystemMessage(PluginMain.TownChat,
|
||||||
|
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, false), groupID),
|
||||||
|
logRecord.getMessage(), ChatProcessing.MCORIGIN);
|
||||||
|
break;
|
||||||
|
case "Nation":
|
||||||
|
TBMCChatAPI.SendSystemMessage(PluginMain.NationChat,
|
||||||
|
new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, true), groupID),
|
||||||
|
logRecord.getMessage(), ChatProcessing.MCORIGIN);
|
||||||
|
break;
|
||||||
|
case "Global":
|
||||||
|
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat,
|
||||||
|
Channel.RecipientTestResult.ALL,
|
||||||
|
logRecord.getMessage(), ChatProcessing.MCORIGIN);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SecurityException {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static void setup() {
|
||||||
|
TownyLogger.log.addHandler(HANDLER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setdown() {
|
||||||
|
TownyLogger.log.removeHandler(HANDLER);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,6 @@ import lombok.Data;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -33,7 +32,7 @@ public final class ChatFormatter {
|
||||||
boolean strikethrough;
|
boolean strikethrough;
|
||||||
boolean obfuscated;
|
boolean obfuscated;
|
||||||
Color color;
|
Color color;
|
||||||
BiFunction<String, ChatFormatter, String> onmatch;
|
TriFunc<String, ChatFormatter, FormattedSection, String> onmatch;
|
||||||
String openlink;
|
String openlink;
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
Priority priority = Priority.Normal;
|
Priority priority = Priority.Normal;
|
||||||
|
@ -54,6 +53,11 @@ public final class ChatFormatter {
|
||||||
Excluder
|
Excluder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface TriFunc<T1, T2, T3, R> {
|
||||||
|
R apply(T1 x1, T2 x2, T3 x3);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
|
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
|
||||||
/*
|
/*
|
||||||
* This method assumes that there is always a global formatter
|
* This method assumes that there is always a global formatter
|
||||||
|
@ -334,7 +338,7 @@ public final class ChatFormatter {
|
||||||
for (ChatFormatter formatter : section.Formatters) {
|
for (ChatFormatter formatter : section.Formatters) {
|
||||||
DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
|
DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
|
||||||
if (formatter.onmatch != null)
|
if (formatter.onmatch != null)
|
||||||
originaltext = formatter.onmatch.apply(originaltext, formatter);
|
originaltext = formatter.onmatch.apply(originaltext, formatter, section);
|
||||||
if (formatter.color != null)
|
if (formatter.color != null)
|
||||||
newtp.setColor(formatter.color);
|
newtp.setColor(formatter.color);
|
||||||
if (formatter.bold)
|
if (formatter.bold)
|
||||||
|
@ -356,7 +360,7 @@ public final class ChatFormatter {
|
||||||
&& newtp.isUnderlined() == lasttp.isUnderlined()
|
&& newtp.isUnderlined() == lasttp.isUnderlined()
|
||||||
&& newtp.isStrikethrough() == lasttp.isStrikethrough()
|
&& newtp.isStrikethrough() == lasttp.isStrikethrough()
|
||||||
&& newtp.isObfuscated() == lasttp.isObfuscated()
|
&& newtp.isObfuscated() == lasttp.isObfuscated()
|
||||||
&& (openlink == null ? lastlink == null : openlink.equals(lastlink))) {
|
&& Objects.equals(openlink, lastlink)) {
|
||||||
DebugCommand.SendDebugMessage("This part has the same properties as the previous one, combining.");
|
DebugCommand.SendDebugMessage("This part has the same properties as the previous one, combining.");
|
||||||
lasttp.setText(lasttp.getText() + originaltext);
|
lasttp.setText(lasttp.getText() + originaltext);
|
||||||
continue; //Combine parts with the same properties
|
continue; //Combine parts with the same properties
|
||||||
|
|
|
@ -3,12 +3,12 @@ package buttondevteam.chat.formatting;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
class FormattedSection {
|
public class FormattedSection {
|
||||||
int Start;
|
public int Start;
|
||||||
int End;
|
public int End;
|
||||||
ArrayList<ChatFormatter> Formatters = new ArrayList<ChatFormatter>();
|
public ArrayList<ChatFormatter> Formatters = new ArrayList<ChatFormatter>();
|
||||||
ArrayList<String> Matches = new ArrayList<String>();
|
public ArrayList<String> Matches = new ArrayList<String>();
|
||||||
ChatFormatter.Type type;
|
public ChatFormatter.Type type;
|
||||||
|
|
||||||
FormattedSection(ChatFormatter formatter, int start, int end, ArrayList<String> matches, ChatFormatter.Type type) {
|
FormattedSection(ChatFormatter formatter, int start, int end, ArrayList<String> matches, ChatFormatter.Type type) {
|
||||||
Start = start;
|
Start = start;
|
||||||
|
|
|
@ -1,31 +1,24 @@
|
||||||
package buttondevteam.chat.listener;
|
package buttondevteam.chat.listener;
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.FlairStates;
|
|
||||||
import buttondevteam.chat.PlayerJoinTimerTask;
|
import buttondevteam.chat.PlayerJoinTimerTask;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.commands.UnlolCommand;
|
import buttondevteam.chat.commands.UnlolCommand;
|
||||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.chat.components.flair.FlairComponent;
|
||||||
|
import buttondevteam.chat.components.flair.FlairStates;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.core.ComponentManager;
|
||||||
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
||||||
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
|
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
|
||||||
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
|
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
|
||||||
import com.earth2me.essentials.User;
|
|
||||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
|
||||||
import lombok.val;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.function.BiFunction;
|
|
||||||
|
|
||||||
public class PlayerJoinLeaveListener implements Listener {
|
public class PlayerJoinLeaveListener implements Listener {
|
||||||
|
|
||||||
|
@ -38,10 +31,11 @@ public class PlayerJoinLeaveListener implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) {
|
public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) {
|
||||||
ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class);
|
ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class);
|
||||||
Player p = Bukkit.getPlayer(cp.getUUID());
|
Player p = e.getPlayer();
|
||||||
|
|
||||||
|
if (ComponentManager.isEnabled(FlairComponent.class)) {
|
||||||
if (!cp.FlairState().get().equals(FlairStates.NoComment)) {
|
if (!cp.FlairState().get().equals(FlairStates.NoComment)) {
|
||||||
PluginMain.ConfirmUserMessage(cp);
|
FlairComponent.ConfirmUserMessage(cp);
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
PlayerJoinTimerTask tt = new PlayerJoinTimerTask() {
|
PlayerJoinTimerTask tt = new PlayerJoinTimerTask() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -52,6 +46,7 @@ public class PlayerJoinLeaveListener implements Listener {
|
||||||
tt.mp = cp;
|
tt.mp = cp;
|
||||||
timer.schedule(tt, 1000);
|
timer.schedule(tt, 1000);
|
||||||
} //TODO: Better Reddit integration (OAuth)
|
} //TODO: Better Reddit integration (OAuth)
|
||||||
|
}
|
||||||
|
|
||||||
String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname();
|
String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname();
|
||||||
|
|
||||||
|
@ -65,7 +60,7 @@ public class PlayerJoinLeaveListener implements Listener {
|
||||||
nwithoutformatting = p.getName();
|
nwithoutformatting = p.getName();
|
||||||
PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId());
|
PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId());
|
||||||
|
|
||||||
updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener
|
TownColorComponent.updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener
|
||||||
|
|
||||||
if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) {
|
if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) {
|
||||||
cp.ChatOnly = false;
|
cp.ChatOnly = false;
|
||||||
|
@ -85,60 +80,4 @@ public class PlayerJoinLeaveListener implements Listener {
|
||||||
UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer()));
|
UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getPlayerNickname(Player player, User user, ChatPlayer cp) {
|
|
||||||
String nickname = user.getNick(true);
|
|
||||||
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());
|
|
||||||
if (res == null || !res.hasTown())
|
|
||||||
return name;
|
|
||||||
try {
|
|
||||||
Color[] clrs = Optional.ofNullable(
|
|
||||||
PluginMain.TownColors.get(res.getTown().getName().toLowerCase())
|
|
||||||
).orElse(new Color[]{Color.White}); //Use white as default town color
|
|
||||||
StringBuilder ret = new StringBuilder();
|
|
||||||
AtomicInteger prevlen = new AtomicInteger();
|
|
||||||
BiFunction<Color, Integer, String> anyColoredNamePart = (c, len) -> "§" //Len==0 if last part
|
|
||||||
+ Integer.toHexString(c.ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end
|
|
||||||
+ (len == 0 ? name.substring(prevlen.get())
|
|
||||||
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
|
|
||||||
BiFunction<Integer, Integer, String> coloredNamePart = (len, i)
|
|
||||||
-> anyColoredNamePart.apply(clrs[i], i + 1 == clrs.length ? 0 : len);
|
|
||||||
final int len = name.length() / (clrs.length + 1); //The above param is needed because this isn't always passed
|
|
||||||
Color nc;
|
|
||||||
/*if(res.getTown().hasNation()
|
|
||||||
&&(nc=PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase()))!=null)
|
|
||||||
len = name.length() / (clrs.length+1);
|
|
||||||
else
|
|
||||||
len = name.length() / clrs.length;*/
|
|
||||||
val nclar = cp.NameColorLocations().get();
|
|
||||||
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
|
|
||||||
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length + 1)) //+1: Nation color
|
|
||||||
ncl = null; // Reset if name length changed
|
|
||||||
//System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length());
|
|
||||||
if (!res.getTown().hasNation()
|
|
||||||
|| (nc = PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase())) == null)
|
|
||||||
nc = Color.White;
|
|
||||||
ret.append(anyColoredNamePart.apply(nc, ncl == null ? len : ncl[0])); //Make first color the nation color
|
|
||||||
for (int i = 0; i < clrs.length; i++)
|
|
||||||
//ret.append(coloredNamePart.apply(ncl == null ? len : (nc==null?ncl[i]:ncl[i+1]), i));
|
|
||||||
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i + 1], i));
|
|
||||||
return ret.toString();
|
|
||||||
} catch (NotRegisteredException e) {
|
|
||||||
return nickname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updatePlayerColors(Player player) { //Probably while ingame (/u ncolor)
|
|
||||||
updatePlayerColors(player, ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
|
||||||
public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames
|
|
||||||
User user = PluginMain.essentials.getUser(player);
|
|
||||||
user.setNickname(getPlayerNickname(player, user, cp));
|
|
||||||
user.setDisplayNick(); //These won't fire the nick change event
|
|
||||||
cp.FlairUpdate(); //Update in list
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,16 @@ import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.ChatProcessing;
|
import buttondevteam.chat.ChatProcessing;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||||
|
import buttondevteam.chat.components.flair.FlairComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
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.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.*;
|
import buttondevteam.lib.chat.ChatMessage;
|
||||||
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
import buttondevteam.lib.player.ChromaGamerBase;
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
|
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
|
@ -46,15 +53,6 @@ public class PlayerListener implements Listener {
|
||||||
*/
|
*/
|
||||||
public static BiMap<String, UUID> nicknames = HashBiMap.create();
|
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"};
|
public final static String[] LaughStrings = new String[]{"xd", "lel", "lawl", "kek", "lmao", "hue", "hah", "rofl"};
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
|
@ -78,13 +76,13 @@ public class PlayerListener implements Listener {
|
||||||
int index = message.indexOf(" ");
|
int index = message.indexOf(" ");
|
||||||
val mp = ChromaGamerBase.getFromSender(sender);
|
val mp = ChromaGamerBase.getFromSender(sender);
|
||||||
String cmd;
|
String cmd;
|
||||||
final BiPredicate<Channel, String> checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase));
|
final BiPredicate<Channel, String> checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (Arrays.stream(chan.IDs().get()).anyMatch(cmd1::equalsIgnoreCase));
|
||||||
if (index == -1) { // Only the command is run
|
if (index == -1) { // Only the command is run
|
||||||
if (!(sender instanceof Player || sender instanceof ConsoleCommandSender))
|
if (!(sender instanceof Player || sender instanceof ConsoleCommandSender))
|
||||||
return false;
|
return false;
|
||||||
// ^^ We can only store player or console channels - Directly sending to channels would still work if they had an event
|
// ^^ 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);
|
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)) {
|
if (checkchid.test(channel, cmd)) {
|
||||||
Channel oldch = mp.channel().get();
|
Channel oldch = mp.channel().get();
|
||||||
if (oldch instanceof ChatRoom)
|
if (oldch instanceof ChatRoom)
|
||||||
|
@ -96,7 +94,7 @@ public class PlayerListener implements Listener {
|
||||||
if (channel instanceof ChatRoom)
|
if (channel instanceof ChatRoom)
|
||||||
((ChatRoom) channel).joinRoom(sender);
|
((ChatRoom) channel).joinRoom(sender);
|
||||||
}
|
}
|
||||||
sender.sendMessage("§6You are now talking in: §b" + mp.channel().get().DisplayName);
|
sender.sendMessage("§6You are now talking in: §b" + mp.channel().get().DisplayName().get());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +124,7 @@ public class PlayerListener implements Listener {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else
|
} 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
|
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);
|
TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, mp, message.substring(index + 1)).build(), channel);
|
||||||
return true;
|
return true;
|
||||||
|
@ -169,13 +167,10 @@ public class PlayerListener implements Listener {
|
||||||
public static boolean ActiveF = false;
|
public static boolean ActiveF = false;
|
||||||
public static ChatPlayer FPlayer = null;
|
public static ChatPlayer FPlayer = null;
|
||||||
public static BukkitTask Ftask = null;
|
public static BukkitTask Ftask = null;
|
||||||
public static int AlphaDeaths;
|
|
||||||
public static ArrayList<CommandSender> Fs = new ArrayList<>();
|
public static ArrayList<CommandSender> Fs = new ArrayList<>();
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerDeath(PlayerDeathEvent e) {
|
public void onPlayerDeath(PlayerDeathEvent e) {
|
||||||
if (e.getEntity().getName().equals("Alpha_Bacca44"))
|
|
||||||
AlphaDeaths++;
|
|
||||||
// MinigamePlayer mgp = Minigames.plugin.pdata.getMinigamePlayer(e.getEntity());
|
// MinigamePlayer mgp = Minigames.plugin.pdata.getMinigamePlayer(e.getEntity());
|
||||||
if (/* (mgp != null && !mgp.isInMinigame()) && */ new Random().nextBoolean()) { // Don't store Fs for NPCs
|
if (/* (mgp != null && !mgp.isInMinigame()) && */ new Random().nextBoolean()) { // Don't store Fs for NPCs
|
||||||
Runnable tt = () -> {
|
Runnable tt = () -> {
|
||||||
|
@ -245,9 +240,11 @@ public class PlayerListener implements Listener {
|
||||||
e.addInfo("Minecraft name: " + cp.PlayerName().get());
|
e.addInfo("Minecraft name: " + cp.PlayerName().get());
|
||||||
if (cp.UserName().get() != null && cp.UserName().get().length() > 0)
|
if (cp.UserName().get() != null && cp.UserName().get().length() > 0)
|
||||||
e.addInfo("Reddit name: " + cp.UserName().get());
|
e.addInfo("Reddit name: " + cp.UserName().get());
|
||||||
|
if (ComponentManager.isEnabled(FlairComponent.class)) {
|
||||||
final String flair = cp.GetFormattedFlair(e.getTarget() != InfoTarget.MCCommand);
|
final String flair = cp.GetFormattedFlair(e.getTarget() != InfoTarget.MCCommand);
|
||||||
if (flair.length() > 0)
|
if (flair.length() > 0)
|
||||||
e.addInfo("/r/TheButton flair: " + flair);
|
e.addInfo("/r/TheButton flair: " + flair);
|
||||||
|
}
|
||||||
e.addInfo(String.format("Respect: %.2f", cp.getF()));
|
e.addInfo(String.format("Respect: %.2f", cp.getF()));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
TBMCCoreAPI.SendException("Error while providing chat info for player " + e.getPlayer().getFileName(), ex);
|
TBMCCoreAPI.SendException("Error while providing chat info for player " + e.getPlayer().getFileName(), ex);
|
||||||
|
@ -265,7 +262,7 @@ public class PlayerListener implements Listener {
|
||||||
for (Player p : Bukkit.getOnlinePlayers())
|
for (Player p : Bukkit.getOnlinePlayers())
|
||||||
if (e.shouldSendTo(p))
|
if (e.shouldSendTo(p))
|
||||||
p.sendMessage("§c!§r["
|
p.sendMessage("§c!§r["
|
||||||
+ e.getChannel().DisplayName + "] <" + (e.getSender() instanceof Player
|
+ e.getChannel().DisplayName().get() + "] <" + (e.getSender() instanceof Player
|
||||||
? ((Player) e.getSender()).getDisplayName() : e.getSender().getName())
|
? ((Player) e.getSender()).getDisplayName() : e.getSender().getName())
|
||||||
+ "> " + e.getMessage());
|
+ "> " + e.getMessage());
|
||||||
TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex);
|
TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex);
|
||||||
|
@ -287,7 +284,7 @@ public class PlayerListener implements Listener {
|
||||||
nicknames.inverse().forcePut(e.getAffected().getBase().getUniqueId(), ChatColor.stripColor(nick).toLowerCase());
|
nicknames.inverse().forcePut(e.getAffected().getBase().getUniqueId(), ChatColor.stripColor(nick).toLowerCase());
|
||||||
|
|
||||||
Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> {
|
Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> {
|
||||||
PlayerJoinLeaveListener.updatePlayerColors(e.getAffected().getBase()); //Won't fire this event again
|
TownColorComponent.updatePlayerColors(e.getAffected().getBase()); //Won't fire this event again
|
||||||
}, 1);
|
}, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,9 +37,9 @@ depend:
|
||||||
- Votifier
|
- Votifier
|
||||||
- Vault
|
- Vault
|
||||||
- ButtonCore
|
- ButtonCore
|
||||||
- Dynmap-Towny
|
|
||||||
soft-depend:
|
soft-depend:
|
||||||
- Minigames
|
- Minigames
|
||||||
|
- Dynmap-Towny
|
||||||
permissions:
|
permissions:
|
||||||
tbmc.admin:
|
tbmc.admin:
|
||||||
description: Gives access to /un- commands and /u admin commands
|
description: Gives access to /un- commands and /u admin commands
|
||||||
|
|
|
@ -7,8 +7,8 @@ import buttondevteam.chat.formatting.TellrawEvent;
|
||||||
import buttondevteam.chat.formatting.TellrawEvent.ClickAction;
|
import buttondevteam.chat.formatting.TellrawEvent.ClickAction;
|
||||||
import buttondevteam.chat.formatting.TellrawEvent.HoverAction;
|
import buttondevteam.chat.formatting.TellrawEvent.HoverAction;
|
||||||
import buttondevteam.chat.formatting.TellrawPart;
|
import buttondevteam.chat.formatting.TellrawPart;
|
||||||
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.core.TestPrepare;
|
import buttondevteam.core.TestPrepare;
|
||||||
import buttondevteam.lib.chat.Channel;
|
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
@ -70,6 +70,11 @@ public class ChatFormatIT {
|
||||||
.setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT,
|
.setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT,
|
||||||
new TellrawPart("Click to open").setColor(Color.Blue)))
|
new TellrawPart("Click to open").setColor(Color.Blue)))
|
||||||
.setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://norbipeti.github.io/test?test&test#test"))));
|
.setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://norbipeti.github.io/test?test&test#test"))));
|
||||||
|
list.add(new ChatFormatIT(sender, "[hmm](https://norbipeti.github.io/test)", new TellrawPart("hmm")
|
||||||
|
.setColor(Color.White).setUnderlined(true)
|
||||||
|
.setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT,
|
||||||
|
new TellrawPart("Click to open").setColor(Color.Blue)))
|
||||||
|
.setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://norbipeti.github.io/test"))));
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue