Towny event broadcasting to Discord and masked links #96
15 changed files with 556 additions and 530 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>
|
|
@ -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,
|
||||||
|
@ -285,11 +285,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
|
||||||
|
@ -305,11 +305,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,42 +1,30 @@
|
||||||
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.TownColorComponent;
|
|
||||||
import buttondevteam.chat.components.announce.AnnouncerComponent;
|
import buttondevteam.chat.components.announce.AnnouncerComponent;
|
||||||
import buttondevteam.chat.components.flair.FlairComponent;
|
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.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.component.channel.Channel;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.ButtonPlugin;
|
||||||
import buttondevteam.lib.architecture.Component;
|
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 com.earth2me.essentials.Essentials;
|
import com.earth2me.essentials.Essentials;
|
||||||
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.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
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 java.io.File;
|
public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15.
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -47,9 +35,17 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
||||||
public static Channel TownChat;
|
public static Channel TownChat;
|
||||||
public static Channel NationChat;
|
public static Channel NationChat;
|
||||||
|
|
||||||
|
public ConfigData<String> notificationSound() {
|
||||||
|
return getIConfig().getData("notificationSound", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigData<Float> notificationPitch() {
|
||||||
|
return getIConfig().getData("notificationPitch", 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
// 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"));
|
||||||
|
|
||||||
|
@ -58,24 +54,13 @@ 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_...]
|
||||||
|
|
||||||
Component.registerComponent(this, new TownyComponent());
|
Component.registerComponent(this, new TownyComponent());
|
||||||
TownColors.keySet().removeIf(t -> !TownyComponent.TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns
|
|
||||||
NationColor.keySet().removeIf(n -> !TownyComponent.TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations
|
|
||||||
|
|
||||||
TBMCChatAPI.RegisterChatChannel(new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global
|
TBMCChatAPI.RegisterChatChannel(new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(this, () -> {
|
|
||||||
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
|
|
||||||
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!"));
|
||||||
|
|
||||||
|
@ -84,85 +69,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
|
||||||
Component.registerComponent(this, new AnnouncerComponent());
|
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 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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");
|
|
||||||
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.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;
|
||||||
|
|
|
@ -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 getConfig().getData("colorCount", (byte) 1, cc -> (byte) cc, cc -> (int) cc);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigData<Boolean> useNationColors() { //TODO
|
|
||||||
return getConfig().getData("useNationColors", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void enable() {
|
|
||||||
//TODO: Don't register all commands automatically (welp)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void disable() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +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.components.towny.TownyComponent;
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
|
|
||||||
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;
|
||||||
|
@ -54,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;
|
||||||
|
@ -72,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,5 +1,6 @@
|
||||||
package buttondevteam.chat.commands.ucmds;
|
package buttondevteam.chat.components.towncolors;
|
||||||
|
|
||||||
|
import buttondevteam.chat.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.chat.components.towny.TownyComponent;
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
import buttondevteam.lib.chat.CommandClass;
|
||||||
|
@ -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.commands.ucmds.UCommandBase;
|
||||||
import buttondevteam.chat.components.towny.TownyComponent;
|
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,8 +29,6 @@ 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;
|
||||||
|
@ -36,8 +37,9 @@ public class TownColorCommand extends UCommandBase {
|
||||||
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 -> (byte) cc, cc -> (int) cc);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,8 +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.commands.ucmds.admin.AdminCommandBase;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownyListener;
|
||||||
import buttondevteam.chat.components.towny.TownyComponent;
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
|
||||||
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;
|
||||||
|
@ -40,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,8 +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.commands.ucmds.admin.AdminCommandBase;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownyListener;
|
||||||
import buttondevteam.chat.components.towny.TownyComponent;
|
import buttondevteam.chat.components.towny.TownyComponent;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
|
||||||
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;
|
||||||
|
@ -48,16 +50,16 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
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(TownyComponent.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;
|
||||||
}
|
}
|
||||||
|
@ -75,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");
|
||||||
|
@ -84,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;
|
||||||
}
|
}
|
|
@ -7,28 +7,19 @@ import buttondevteam.chat.commands.UnlolCommand;
|
||||||
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
import buttondevteam.chat.commands.ucmds.HistoryCommand;
|
||||||
import buttondevteam.chat.components.flair.FlairComponent;
|
import buttondevteam.chat.components.flair.FlairComponent;
|
||||||
import buttondevteam.chat.components.flair.FlairStates;
|
import buttondevteam.chat.components.flair.FlairStates;
|
||||||
import buttondevteam.chat.components.towny.TownyComponent;
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
import buttondevteam.core.ComponentManager;
|
import buttondevteam.core.ComponentManager;
|
||||||
import buttondevteam.lib.chat.Color;
|
|
||||||
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.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 {
|
||||||
|
|
||||||
|
@ -70,7 +61,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;
|
||||||
|
@ -90,60 +81,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 = TownyComponent.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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ 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.flair.FlairComponent;
|
||||||
|
import buttondevteam.chat.components.towncolors.TownColorComponent;
|
||||||
import buttondevteam.component.channel.Channel;
|
import buttondevteam.component.channel.Channel;
|
||||||
import buttondevteam.component.channel.ChatChannelRegisterEvent;
|
import buttondevteam.component.channel.ChatChannelRegisterEvent;
|
||||||
import buttondevteam.component.channel.ChatRoom;
|
import buttondevteam.component.channel.ChatRoom;
|
||||||
|
@ -52,9 +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 String NotificationSound;
|
|
||||||
public static double NotificationPitch;
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -286,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue