From 415e61a40147a670ac9921d739304fc2d899266a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 12 Aug 2019 02:50:18 +0200 Subject: [PATCH] isChatOn fix, tellRaw start --- pom.xml | 18 ++++-- .../java/buttondevteam/chat/VanillaUtils.java | 64 +++++++++++++++++-- .../chat/components/towny/TownyComponent.java | 5 +- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 5626ea8..6bca684 100644 --- a/pom.xml +++ b/pom.xml @@ -217,12 +217,18 @@ 1.16.16 provided - - org.spigotmc - spigot - 1.12.2-R0.1-SNAPSHOT - provided - + + org.spigotmc + spigot + 1.12.2-R0.1-SNAPSHOT + provided + + com.github.webbukkit Dynmap-Towny diff --git a/src/main/java/buttondevteam/chat/VanillaUtils.java b/src/main/java/buttondevteam/chat/VanillaUtils.java index 5cbd16c..b1a3586 100644 --- a/src/main/java/buttondevteam/chat/VanillaUtils.java +++ b/src/main/java/buttondevteam/chat/VanillaUtils.java @@ -1,18 +1,65 @@ package buttondevteam.chat; +import buttondevteam.core.MainPlugin; import buttondevteam.lib.TBMCChatEvent; import lombok.experimental.UtilityClass; -import net.minecraft.server.v1_12_R1.EntityHuman.EnumChatVisibility; +import lombok.val; +import net.minecraft.server.v1_12_R1.ChatComponentUtils; +import net.minecraft.server.v1_12_R1.IChatBaseComponent; import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer; import org.bukkit.entity.Player; +import java.util.function.Predicate; + @UtilityClass public class VanillaUtils { public int getMCScoreIfChatOn(Player p, TBMCChatEvent e) { - if (!(p instanceof CraftPlayer) || ((CraftPlayer) p).getHandle().getChatFlags() == EnumChatVisibility.FULL) // Only send if client allows chat + try { + if (isChatOn(p)) // Only send if client allows chat + return e.getMCScore(p); + else + return -1; + } catch (NoClassDefFoundError ex) { + MainPlugin.Instance.getLogger().warning("Compatibility error, can't check if the chat is hidden by the player."); return e.getMCScore(p); - else - return -1; + } + } + + private Predicate isChatOn; + + private boolean isChatOn(Player p) { + try { + if (isChatOn == null) { + val cl = p.getClass(); + if (!cl.getSimpleName().contains("CraftPlayer")) return true; // p instanceof CraftPlayer + val hm = cl.getMethod("getHandle"); + val handle = hm.invoke(p); //p.getHandle() + val vpcl = handle.getClass(); + val gcfm = vpcl.getMethod("getChatFlags"); + Class encl; + try { + encl = Class.forName(handle.getClass().getPackage().getName() + ".EnumChatVisibility"); + } catch (ClassNotFoundException e) { + encl = Class.forName(handle.getClass().getPackage().getName() + ".EntityHuman$EnumChatVisibility"); + } + val ff = encl.getField("FULL"); + val full = ff.get(null); // EnumChatVisibility.FULL + isChatOn = pl -> { + try { + val ph = hm.invoke(pl); //pl.getHandle() + val flags = gcfm.invoke(ph); //handle.getChatFlags() + return flags == full; //TODO: It's only checked if not global + } catch (Exception e) { + e.printStackTrace(); + return true; + } + }; + } + return isChatOn.test(p); + } catch (Exception e) { + e.printStackTrace(); + return true; + } } /*private String version; @@ -23,4 +70,13 @@ public class VanillaUtils { "org.bukkit.craftbukkit.v", "_R1").orElse("1_8").replace("_", ""); return Short.parseShort(v); }*/ + + public String tellRaw(Player p, String json) { + try { + ChatComponentUtils.filterForDisplay(((CraftPlayer) p).getHandle(), //TODO: Reflection + IChatBaseComponent.ChatSerializer.a(json), ((CraftPlayer) p).getHandle()); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java index dbfe93c..dc4777b 100644 --- a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java +++ b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java @@ -1,5 +1,6 @@ package buttondevteam.chat.components.towny; +import buttondevteam.chat.ChatUtils; import buttondevteam.chat.PluginMain; import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.core.component.channel.Channel; @@ -51,8 +52,8 @@ public class TownyComponent extends Component { if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) { ((List) json.getExtra()).add(0, new TellrawPart("[SPY]")); String jsonstr = toJson.apply(json); - Bukkit.getServer().dispatchCommand(PluginMain.Console, String.format( - "tellraw @a[score_%s=1000,score_%s_min=1000] %s", channel.ID, channel.ID, jsonstr)); + ChatUtils.dispatchConsoleCommand(String.format( + "tellraw @a[score_%s=1000,score_%s_min=1000] %s", channel.ID, channel.ID, jsonstr), true); } }