From 241992571e181ca4cf3d35c9abf35e446eb16012 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 28 Oct 2018 23:35:20 +0100 Subject: [PATCH] Nation color works! Getting Essentials on enable Both nation and town colors are white by default --- .../buttondevteam/chat/ChatProcessing.java | 3 -- .../java/buttondevteam/chat/PluginMain.java | 1 + .../commands/ucmds/NationColorCommand.java | 47 ++++++++++++++++++- .../chat/commands/ucmds/TownColorCommand.java | 3 -- .../ucmds/admin/NationColorCommand.java | 46 +++++++++++++++++- .../ucmds/admin/TownColorCommand.java | 8 +++- .../listener/PlayerJoinLeaveListener.java | 10 ++-- 7 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 8f1b7a5..6591cd6 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -14,7 +14,6 @@ import buttondevteam.lib.chat.*; import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerBase; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; import com.google.common.collect.Lists; import com.google.gson.Gson; @@ -94,8 +93,6 @@ public class ChatProcessing { CommandSender sender = e.getSender(); String message = e.getMessage(); long processstart = System.nanoTime(); - if (PluginMain.essentials == null) - PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); Player player = (sender instanceof Player ? (Player) sender : null); User user = PluginMain.essentials.getUser(player); diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index d7b7ad1..8a79abb 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -78,6 +78,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. @Override public void onEnable() { Instance = this; + PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java index cf22e9a..cbfb0de 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java @@ -1,4 +1,49 @@ package buttondevteam.chat.commands.ucmds; -public class NationColorCommand { +import buttondevteam.chat.PluginMain; +import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.OptionallyPlayerCommandClass; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Resident; +import org.bukkit.entity.Player; + +@CommandClass +@OptionallyPlayerCommandClass(playerOnly = true) +public class NationColorCommand extends UCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[]{ // + "§6---- Nation Color ----", // + "This command allows setting a color for a nation.", // + "Each town in the nation will have it's first color (border) set to this color.", // + "See the help text for /u towncolor for more details.", // + "Usage: /" + GetCommandPath() + " ", // + "Example: /" + GetCommandPath() + " blue" // + }; + } + + @Override + public boolean OnCommand(Player player, String alias, String[] args) { + Resident res; + if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase()) + && (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) { + player.sendMessage("§cYou need to be the king of a nation to set it's colors."); + return true; + } + if (args.length > 1) { + player.sendMessage("You can only use one color."); + return true; + } + String[] a = new String[args.length + 1]; + System.arraycopy(args, 0, a, 1, args.length); + try { + a[0] = res.getTown().getNation().getName(); + } catch (NotRegisteredException e) { + TBMCCoreAPI.SendException("Failed to set nation color for player " + player + "!", e); + player.sendMessage("§cCouldn't find your town/nation... Error reported."); + return true; + } + return buttondevteam.chat.commands.ucmds.admin.NationColorCommand.SetNationColor(player, alias, a); + } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java index 197a574..648daf1 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java @@ -31,9 +31,6 @@ public class TownColorCommand extends UCommandBase { @Override public boolean OnCommand(Player player, String alias, String[] args) { Resident res; - // System.out.println("contains: " + PluginMain.TU.getResidentMap().contains(player.getName().toLowerCase())); - // System.out.println("res: " + PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())); - // System.out.println("mayor: " + PluginMain.TU.getResidentMap().get(player.getName().toLowerCase()).isMayor()); if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase()) && (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) { player.sendMessage("§cYou need to be the mayor of a town to set it's colors."); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java index c2dd682..60564c5 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java @@ -1,4 +1,48 @@ package buttondevteam.chat.commands.ucmds.admin; -public class NationColorCommand { //TODO +import buttondevteam.chat.PluginMain; +import buttondevteam.chat.listener.TownyListener; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Town; +import lombok.val; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class NationColorCommand extends AdminCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[]{ // + "§6---- Nation color ----", // + "Sets the color of the nation.", // + "Usage: /u admin nationcolor " // + }; + } + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + return SetNationColor(sender, alias, args); + } + + public static boolean SetNationColor(CommandSender sender, String alias, String[] args) { + if (args.length < 2) + return false; + if (args.length > 2) { + sender.sendMessage("§cYou can only use one color as a nation color."); + return true; + } + final Nation nation = PluginMain.TU.getNationsMap().get(args[0].toLowerCase()); + if (nation == null) { + sender.sendMessage("§cThe nation '" + args[0] + "' cannot be found."); + return true; + } + val c = TownColorCommand.getColorOrSendError(args[1], sender); + if (!c.isPresent()) return true; + PluginMain.NationColor.put(args[0].toLowerCase(), c.get()); + Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> { + for (Town t : nation.getTowns()) + TownyListener.updateTownMembers(t); + }); + sender.sendMessage("§bNation color set to §" + TownColorCommand.getColorText(c.get())); + return true; + } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java index e718b6f..e1cd357 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -60,16 +60,20 @@ public class TownColorCommand extends AdminCommandBase { return true; } - private static Optional getColorOrSendError(String name, CommandSender sender) { + public static Optional getColorOrSendError(String name, CommandSender sender) { val c = Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(name)).findAny(); if (!c.isPresent()) { //^^ Skip black sender.sendMessage("§cThe color '" + name + "' cannot be found."); //ˇˇ Skip black - sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(col -> String.format("§%x%s§r", col.ordinal(), col.getName())).collect(Collectors.joining(", "))); + sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(TownColorCommand::getColorText).collect(Collectors.joining(", "))); sender.sendMessage("§cMake sure to type them exactly as shown above."); } return c; } + public static String getColorText(Color col) { + return String.format("§%x%s§r", col.ordinal(), col.getName()); + } + public static String getTownNameCased(String name) { return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName(); } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 0038a98..ec88341 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -9,7 +9,6 @@ import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import lombok.val; @@ -22,6 +21,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; import java.util.Arrays; +import java.util.Optional; import java.util.Timer; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiFunction; @@ -36,8 +36,6 @@ public class PlayerJoinLeaveListener implements Listener { @EventHandler public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) { - if (PluginMain.essentials == null) - PluginMain.essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); Player p = Bukkit.getPlayer(cp.getUUID()); @@ -95,9 +93,9 @@ public class PlayerJoinLeaveListener implements Listener { if (res == null || !res.hasTown()) return name; try { - val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); - if (clrs == null) - return name; + 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 anyColoredNamePart = (c, len) -> "§" //Len==0 if last part