From 06ea5199874d9dfec63b9452563069d35677750a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 1 Oct 2017 20:07:15 +0200 Subject: [PATCH] Inst. update on Dynmap, cmd for mayors, fix Well, update every ~5 mins on Dynmap, as DT updates Command for setting town color count added too --- .../java/buttondevteam/chat/PluginMain.java | 24 ++++--- .../chat/commands/ucmds/TownColorCommand.java | 67 +++++++++++++++++++ .../chat/commands/ucmds/admin/TCCount.java | 27 ++++++++ .../ucmds/admin/TownColorCommand.java | 16 ++++- 4 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index c4d39a7..4947bef 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -18,6 +18,7 @@ import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.TagNode; import buttondevteam.chat.commands.YeehawCommand; +import buttondevteam.chat.commands.ucmds.TownColorCommand; import buttondevteam.chat.listener.PlayerListener; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Channel; @@ -115,15 +116,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); if (dtp == null) return; - for (val entry : TownColors.entrySet()) { - Function c2i = c -> c.getRed() << 4 | c.getGreen() << 2 | c.getBlue(); - try { - DTBridge.setTownColor(dtp, entry.getKey(), c2i.apply(entry.getValue()[0]), - c2i.apply(entry.getValue().length > 1 ? entry.getValue()[1] : entry.getValue()[0])); - } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to set town color for town " + entry.getKey() + "!", e); - } - } + for (val entry : TownColors.entrySet()) + setTownColor(dtp, entry.getKey(), entry.getValue()); }); setupChat(); @@ -134,6 +128,16 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. new Thread(new AnnouncerThread()).start(); } + public static void setTownColor(DynmapTownyPlugin dtp, String town, Color[] colors) { + Function 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; @@ -300,6 +304,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. TownColors.putAll(cs.getValues(true).entrySet().stream() .collect(Collectors.toMap(k -> k.getKey(), v -> ((List) v.getValue()).stream() .map(c -> Color.valueOf(c)).toArray(Color[]::new)))); + TownColorCommand.ColorCount = (byte) yc.getInt("towncolorcount", 1); } PluginMain.Instance.getLogger().info("Loaded files!"); } catch (Exception e) { @@ -319,6 +324,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. yc.set("alphadeaths", PlayerListener.AlphaDeaths); yc.createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(k -> k.getKey(), v -> Arrays.stream(v.getValue()).map(c -> c.toString()).toArray(String[]::new)))); + yc.set("towncolorcount", TownColorCommand.ColorCount); yc.save(file); PluginMain.Instance.getLogger().info("Saved files!"); } catch (Exception e) { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java new file mode 100644 index 0000000..b667ea7 --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java @@ -0,0 +1,67 @@ +package buttondevteam.chat.commands.ucmds; + +import java.util.Arrays; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.dynmap.towny.DynmapTownyPlugin; + +import com.mysql.fabric.xmlrpc.base.Array; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Resident; + +import buttondevteam.chat.PluginMain; +import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.chat.Color; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.OptionallyPlayerCommandClass; +import lombok.val; + +@CommandClass // TODO: /u u when annotation not present +@OptionallyPlayerCommandClass(playerOnly = true) +public class TownColorCommand extends UCommandBase { + @Override + public String GetHelpText(String alias)[] { + String cns = " "; + for (int i = 2; i <= ColorCount; i++) + cns += " [colorname" + i + "]"; + return new String[] { // + "§6---- Town Color ----", // + "This command allows setting a color for a town.", // + "The town will be shown with this color on Dynmap and all players in the town will appear in chat with these colors.", // + "The colors will split the name evenly.", // + "Usage: /" + GetCommandPath() + cns, // + "Example: /" + GetCommandPath() + " blue" // + }; + } + + public static byte ColorCount; + + @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."); + return true; + } + if (args.length > ColorCount) { + player.sendMessage("You can only use " + ColorCount + " color" + (ColorCount > 1 ? "(s)" : "") + "."); + return true; + } + String[] a = new String[args.length + 1]; + System.arraycopy(args, 0, a, 1, args.length); + try { + a[0] = res.getTown().getName().toLowerCase(); + } catch (NotRegisteredException e) { + TBMCCoreAPI.SendException("Failed to set town color for player " + player + "!", e); + player.sendMessage("§cCouldn't find your town... Error reported."); + return true; + } + return buttondevteam.chat.commands.ucmds.admin.TownColorCommand.SetTownColor(player, alias, a); + } +} diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java new file mode 100644 index 0000000..277dff6 --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java @@ -0,0 +1,27 @@ +package buttondevteam.chat.commands.ucmds.admin; + +import org.bukkit.command.CommandSender; + +public class TCCount extends AdminCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[] { // + "§6---- Town Color Count", // + "Sets how many colors can be used for a town." // + }; + } + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + byte count; + try { + if (args.length == 0 || (count = Byte.parseByte(args[0])) <= 0) + return false; + } catch (NumberFormatException e) { + return false; + } + buttondevteam.chat.commands.ucmds.TownColorCommand.ColorCount = count; + sender.sendMessage("Color count set to " + count); + 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 7a9b82f..568a509 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -2,7 +2,9 @@ package buttondevteam.chat.commands.ucmds.admin; import java.util.Arrays; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.dynmap.towny.DynmapTownyPlugin; import buttondevteam.chat.PluginMain; import buttondevteam.lib.chat.Color; @@ -23,9 +25,13 @@ public class TownColorCommand extends AdminCommandBase { @Override public boolean OnCommand(CommandSender sender, String alias, String[] args) { + return SetTownColor(sender, alias, args); + } + + public static boolean SetTownColor(CommandSender sender, String alias, String[] args) { if (args.length < 2) return false; - if (!PluginMain.TU.getTownsMap().containsKey(args[0])) { + if (!PluginMain.TU.getTownsMap().containsKey(args[0].toLowerCase())) { sender.sendMessage("§cThe town '" + args[0] + "' cannot be found."); return true; } @@ -39,7 +45,13 @@ public class TownColorCommand extends AdminCommandBase { } clrs[i - 1] = c.get(); } - PluginMain.TownColors.put(args[0], clrs); + PluginMain.TownColors.put(args[0].toLowerCase(), clrs); + val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); + if (dtp == null) { + sender.sendMessage("§cDynmap-Towny couldn'5 be found to set town color."); + return true; + } + PluginMain.setTownColor(dtp, args[0].toLowerCase(), clrs); sender.sendMessage("§bColor(s) set."); return true; }