From b5e8e0f93c0702546377178220541485477ad528 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 1 Oct 2017 01:27:14 +0200 Subject: [PATCH] *Hacked* town colors into Dynmap-Towny --- pom.xml | 10 ++++ .../java/buttondevteam/chat/PluginMain.java | 18 ++++++ .../palmergames/bukkit/TownyChat/Chat.java | 4 ++ src/main/java/org/dynmap/towny/DTBridge.java | 56 +++++++++++++++++++ src/main/resources/plugin.yml | 1 + 5 files changed, 89 insertions(+) create mode 100644 src/main/java/com/palmergames/bukkit/TownyChat/Chat.java create mode 100644 src/main/java/org/dynmap/towny/DTBridge.java diff --git a/pom.xml b/pom.xml index bed1af4..b3f2606 100644 --- a/pom.xml +++ b/pom.xml @@ -195,6 +195,16 @@ 1.12.1-R0.1-SNAPSHOT provided + + com.github.webbukkit + Dynmap-Towny + master-SNAPSHOT + + + com.github.webbukkit + Dynmap + master-SNAPSHOT + ButtonChat diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 3c0dc73..c4d39a7 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -12,6 +12,8 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.Scoreboard; +import org.dynmap.towny.DTBridge; +import org.dynmap.towny.DynmapTownyPlugin; import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.TagNode; @@ -53,6 +55,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TimeZone; +import java.util.function.Function; import java.util.stream.Collectors; public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. @@ -108,6 +111,21 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. return true; // TODO: Allow hiding it }, "You need to show the RP chat in order to speak in it."))); + Bukkit.getScheduler().runTask(this, () -> { + 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); + } + } + }); + setupChat(); setupEconomy(); setupPermissions(); diff --git a/src/main/java/com/palmergames/bukkit/TownyChat/Chat.java b/src/main/java/com/palmergames/bukkit/TownyChat/Chat.java new file mode 100644 index 0000000..00b766f --- /dev/null +++ b/src/main/java/com/palmergames/bukkit/TownyChat/Chat.java @@ -0,0 +1,4 @@ +package com.palmergames.bukkit.TownyChat; + +public class Chat { +} diff --git a/src/main/java/org/dynmap/towny/DTBridge.java b/src/main/java/org/dynmap/towny/DTBridge.java new file mode 100644 index 0000000..a78c39e --- /dev/null +++ b/src/main/java/org/dynmap/towny/DTBridge.java @@ -0,0 +1,56 @@ +package org.dynmap.towny; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.dynmap.bukkit.DynmapPlugin; +import org.dynmap.markers.MarkerAPI; + +import lombok.val; + +public class DTBridge { + /** + * Sets the town color on Dynmap. + * + * @param dtp + * The Dynmap-Towny plugin + * @param townname + * The name of the town + * @param strokecolor + * The stroke color in RGB format + * @param fillcolor + * The fill color in RGB format + * @throws Exception + * When couldn't set the town color + */ + public static void setTownColor(DynmapTownyPlugin dtp, String townname, int strokecolor, int fillcolor) + throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, // Keeping these because why not + IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException { + Class cl = Class.forName(DynmapTownyPlugin.class.getName() + "$AreaStyle"); + Field field = DynmapTownyPlugin.class.getDeclaredField("cusstyle"); + field.setAccessible(true); // DOesn't allow accessing it from the same package, if it's from a different plugin + @SuppressWarnings("unchecked") + val map = (Map) field.get(dtp); + Object style = map.get(townname); + if (style == null) { + Constructor c = cl.getDeclaredConstructor(FileConfiguration.class, String.class, MarkerAPI.class); + c.setAccessible(true); + style = c.newInstance(dtp.getConfig(), "custstyle" + townname, + ((DynmapPlugin) Bukkit.getPluginManager().getPlugin("dynmap")).getMarkerAPI()); + map.put(townname, style); + } + set(cl, style, "fillcolor", fillcolor); + set(cl, style, "strokecolor", strokecolor); + } + + private static void set(Class cl, Object style, String fieldname, T value) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + Field field = cl.getDeclaredField(fieldname); + field.setAccessible(true); + field.set(style, value); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index de34be8..7f4f733 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -35,6 +35,7 @@ depend: - Votifier - Vault - ButtonCore +- Dynmap-Towny soft-depend: - Minigames permissions: