From e510667215ff915fc6629ba810cd5c519853e6f0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 9 Mar 2019 23:02:46 +0100 Subject: [PATCH] Rename, fix, nc toggle Renamed plugin Fixed duplicate chat handling (DC->MC) Implemented nation color toggle --- .../java/buttondevteam/chat/PluginMain.java | 4 +- .../announce/AnnouncerComponent.java | 3 +- .../chat/components/flair/FlairComponent.java | 3 +- .../chat/components/fun/FunComponent.java | 3 +- .../towncolors/TownColorComponent.java | 56 ++++++++++++------- .../components/towncolors/TownyListener.java | 5 ++ .../towncolors/admin/TownColorCommand.java | 31 +++++----- .../chat/components/towny/TownyComponent.java | 2 +- src/main/resources/plugin.yml | 2 +- 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 7a8bec7..b623489 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -9,6 +9,7 @@ import buttondevteam.chat.components.towncolors.TownColorComponent; import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.chat.listener.PlayerJoinLeaveListener; import buttondevteam.chat.listener.PlayerListener; +import buttondevteam.core.MainPlugin; import buttondevteam.core.component.channel.Channel; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.ButtonPlugin; @@ -17,7 +18,6 @@ import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.TBMCChatAPI; import com.earth2me.essentials.Essentials; -import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; @@ -50,6 +50,7 @@ public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15 TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerJoinLeaveListener(), this); TBMCChatAPI.AddCommands(this, YeehawCommand.class); + MainPlugin.Instance.setChatHandlerEnabled(false); //Disable Core chat handler Console = this.getServer().getConsoleSender(); SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...] @@ -79,7 +80,6 @@ public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15 public static Permission permission = null; public static Economy economy = null; - public static Chat chat = null; private boolean setupPermissions() { RegisteredServiceProvider permissionProvider = getServer().getServicesManager() diff --git a/src/main/java/buttondevteam/chat/components/announce/AnnouncerComponent.java b/src/main/java/buttondevteam/chat/components/announce/AnnouncerComponent.java index f841a2b..123b2c4 100644 --- a/src/main/java/buttondevteam/chat/components/announce/AnnouncerComponent.java +++ b/src/main/java/buttondevteam/chat/components/announce/AnnouncerComponent.java @@ -1,5 +1,6 @@ package buttondevteam.chat.components.announce; +import buttondevteam.chat.PluginMain; import buttondevteam.core.component.channel.Channel; import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.architecture.Component; @@ -9,7 +10,7 @@ import org.bukkit.Bukkit; import java.util.ArrayList; -public class AnnouncerComponent extends Component implements Runnable { +public class AnnouncerComponent extends Component implements Runnable { public ConfigData> AnnounceMessages() { return getConfig().getData("announceMessages", new ArrayList<>(0)); } diff --git a/src/main/java/buttondevteam/chat/components/flair/FlairComponent.java b/src/main/java/buttondevteam/chat/components/flair/FlairComponent.java index 3243205..89b4598 100644 --- a/src/main/java/buttondevteam/chat/components/flair/FlairComponent.java +++ b/src/main/java/buttondevteam/chat/components/flair/FlairComponent.java @@ -1,6 +1,7 @@ package buttondevteam.chat.components.flair; import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.PluginMain; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.ConfigData; @@ -21,7 +22,7 @@ import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.*; -public class FlairComponent extends Component { +public class FlairComponent extends Component { private ConfigData FlairThreadURL() { return getConfig().getData("flairThreadURL", "https://www.reddit.com/r/Chromagamers/comments/51ys94/flair_thread_for_the_mc_server/"); } diff --git a/src/main/java/buttondevteam/chat/components/fun/FunComponent.java b/src/main/java/buttondevteam/chat/components/fun/FunComponent.java index 9053635..c11a84a 100644 --- a/src/main/java/buttondevteam/chat/components/fun/FunComponent.java +++ b/src/main/java/buttondevteam/chat/components/fun/FunComponent.java @@ -11,7 +11,6 @@ import buttondevteam.lib.architecture.Component; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; -import lombok.Getter; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -29,7 +28,7 @@ import org.bukkit.scheduler.BukkitTask; import java.util.ArrayList; import java.util.Random; -public class FunComponent extends Component implements Listener { +public class FunComponent extends Component implements Listener { private final static String[] LaughStrings = new String[]{"xd", "lel", "lawl", "kek", "lmao", "hue", "hah", "rofl"}; private boolean ActiveF = false; private ChatPlayer FPlayer = null; diff --git a/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java index 663768c..8b6314f 100644 --- a/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java @@ -14,6 +14,7 @@ import buttondevteam.lib.player.TBMCPlayerJoinEvent; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Nation; +import lombok.Getter; import lombok.experimental.var; import lombok.val; import org.bukkit.Bukkit; @@ -35,7 +36,7 @@ import java.util.function.Function; import java.util.stream.Collectors; @ComponentMetadata(depends = TownyComponent.class) -public class TownColorComponent extends Component implements Listener { +public class TownColorComponent extends Component implements Listener { /** * Names lowercased */ @@ -49,17 +50,22 @@ public class TownColorComponent extends Component implements Listener { return getConfig().getData("colorCount", (byte) 1, cc -> ((Integer) cc).byteValue(), Byte::intValue); } - public ConfigData useNationColors() { //TODO + public ConfigData useNationColors() { return getConfig().getData("useNationColors", true); } + @Getter + private static TownColorComponent component; + @SuppressWarnings("unchecked") @Override protected void enable() { + component = this; //TODO: Don't register all commands automatically (welp) Consumer loadTC = cs -> TownColorComponent.TownColors.putAll(cs.getValues(true).entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, v -> ((List) v.getValue()).stream() .map(Color::valueOf).toArray(Color[]::new)))); + boolean usenc = useNationColors().get(); Consumer loadNC = ncs -> TownColorComponent.NationColor.putAll(ncs.getValues(true).entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, v -> Color.valueOf((String) v.getValue())))); @@ -68,14 +74,17 @@ public class TownColorComponent extends Component implements Listener { 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 + if (usenc) { + 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 + if (usenc) + 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"); @@ -86,7 +95,9 @@ public class TownColorComponent extends Component implements Listener { val town = TownyComponent.TU.getTownsMap().get(entry.getKey()); Nation nation; Color nc; - if (!town.hasNation() || (nation = town.getNation()) == null || (nc = NationColor.get(nation.getName().toLowerCase())) == null) + if (!useNationColors().get()) + nc = null; + else if (!town.hasNation() || (nation = town.getNation()) == null || (nc = NationColor.get(nation.getName().toLowerCase())) == null) nc = Color.White; setTownColor(dtp, buttondevteam.chat.components.towncolors.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue(), nc); } catch (Exception e) { @@ -96,9 +107,11 @@ public class TownColorComponent extends Component implements Listener { }); registerCommand(new TownColorCommand()); - registerCommand(new NationColorCommand()); + if (useNationColors().get()) + registerCommand(new NationColorCommand()); registerCommand(new buttondevteam.chat.components.towncolors.admin.TownColorCommand()); - registerCommand(new buttondevteam.chat.components.towncolors.admin.NationColorCommand()); + if (useNationColors().get()) + registerCommand(new buttondevteam.chat.components.towncolors.admin.NationColorCommand()); registerCommand(new TCCount()); registerListener(new TownyListener()); registerListener(this); @@ -108,8 +121,9 @@ public class TownColorComponent extends Component implements Listener { 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()))); + if (useNationColors().get()) + getConfig().getConfig().createSection("nationcolors", NationColor.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, + v -> v.getValue().toString()))); } /** @@ -157,18 +171,20 @@ public class TownColorComponent extends Component implements Listener { len = name.length() / (clrs.length+1); else len = name.length() / clrs.length;*/ + boolean usenc = component.useNationColors().get(); 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 + if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length + (usenc ? 1 : 0))) //+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 + if (usenc) { + 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)); + ret.append(coloredNamePart.apply(ncl == null ? len : (usenc ? ncl[i + 1] : ncl[i]), i)); return ret.toString(); } catch (NotRegisteredException e) { return nickname; diff --git a/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java b/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java index 6a9c28b..28501d9 100644 --- a/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java @@ -61,6 +61,7 @@ public class TownyListener implements Listener { @EventHandler public void onNationRename(RenameNationEvent event) { + if (!TownColorComponent.getComponent().useNationColors().get()) return; val clrs = TownColorComponent.NationColor.remove(event.getOldName().toLowerCase()); if (clrs != null) TownColorComponent.NationColor.put(event.getNation().getName().toLowerCase(), clrs); @@ -68,21 +69,25 @@ public class TownyListener implements Listener { @EventHandler //Gets called on town load as well public void onNationJoin(NationAddTownEvent event) { + if (!TownColorComponent.getComponent().useNationColors().get()) return; updateTownMembers(event.getTown()); } @EventHandler public void onNationLeave(NationRemoveTownEvent event) { + if (!TownColorComponent.getComponent().useNationColors().get()) return; updateTownMembers(event.getTown()); //The town still has it's colours } @EventHandler public void onNationDelete(DeleteNationEvent event) { + if (!TownColorComponent.getComponent().useNationColors().get()) return; TownColorComponent.NationColor.remove(event.getNationName().toLowerCase()); } @EventHandler public void onNationCreate(NewNationEvent event) { + if (!TownColorComponent.getComponent().useNationColors().get()) return; Player p = Bukkit.getPlayer(event.getNation().getCapital().getMayor().getName()); if (p != null) p.sendMessage("ยง6Use /u nationcolor to set a color for the nation."); diff --git a/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java index b4d5391..7993b49 100644 --- a/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java @@ -51,21 +51,26 @@ public class TownColorCommand extends AdminCommandBase { clrs[i - 1] = c.get(); } Color tnc; - try { - tnc = TownColorComponent.NationColor.get(targetTown.getNation().getName().toLowerCase()); - } catch (Exception e) { - tnc = null; - } - if (tnc == null) tnc = Color.White; //Default nation color - TODO: Make configurable + boolean usenc = TownColorComponent.getComponent().useNationColors().get(); + if (usenc) { + try { + tnc = TownColorComponent.NationColor.get(targetTown.getNation().getName().toLowerCase()); + } catch (Exception e) { + tnc = null; + } + if (tnc == null) tnc = Color.White; //Default nation color - TODO: Make configurable + } else tnc = null; for (Map.Entry other : TownColorComponent.TownColors.entrySet()) { Color nc; - try { - 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 - nc = null; - } - if (nc == null) nc = Color.White; //Default nation color - if (nc.getName().equals(tnc.getName())) { + if (usenc) { + try { + 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 + nc = null; + } + if (nc == null) nc = Color.White; //Default nation color + } else nc = null; + if (!usenc || nc.getName().equals(tnc.getName())) { int C = 0; if (clrs.length == other.getValue().length) for (int i = 0; i < clrs.length; i++) diff --git a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java index 6f3d283..dbfe93c 100644 --- a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java +++ b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java @@ -22,7 +22,7 @@ import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; -public class TownyComponent extends Component { +public class TownyComponent extends Component { public static TownyUniverse TU; private static ArrayList Towns; private static ArrayList Nations; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 25ff051..d618f6b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ -name: ButtonChat +name: Thorpe-Chat main: buttondevteam.chat.PluginMain version: 4.0 commands: