From 9e8c5998b35f184b9dbaa8b348d371522e46ed43 Mon Sep 17 00:00:00 2001 From: games647 Date: Mon, 2 Oct 2017 09:24:10 +0200 Subject: [PATCH] Checked cast for JsonObject (Fixes #18) --- pom.xml | 2 +- .../mcmmoaction/listener/MessageListener.java | 26 ++++++++++++++----- .../mcmmoaction/listener/PlayerListener.java | 3 ++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 92078ce..dc540a7 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ jar mcMMOAction - 2.1.1 + 2.1.2 2016 https://dev.bukkit.org/bukkit-plugins/mcmmoaction/ diff --git a/src/main/java/com/github/games647/mcmmoaction/listener/MessageListener.java b/src/main/java/com/github/games647/mcmmoaction/listener/MessageListener.java index 1722156..e9f5576 100644 --- a/src/main/java/com/github/games647/mcmmoaction/listener/MessageListener.java +++ b/src/main/java/com/github/games647/mcmmoaction/listener/MessageListener.java @@ -6,9 +6,11 @@ import com.comphenix.protocol.wrappers.EnumWrappers.ChatType; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.github.games647.mcmmoaction.mcMMOAction; +import com.google.common.base.Enums; import com.google.common.collect.ImmutableSet; import com.google.gson.Gson; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.util.Collection; @@ -16,6 +18,7 @@ import java.util.stream.Collectors; import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.chat.ComponentSerializer; import org.bukkit.entity.Player; @@ -28,6 +31,8 @@ public class MessageListener extends PacketAdapter { private final Pattern pluginTagPattern = Pattern.compile(Pattern.quote("[mcMMO] ")); private final Gson gson = new Gson(); + private final boolean shouldRemoveHover; + //compile the pattern just once - remove the comma so it also detect numbers like (10,000) private final Pattern numberRemover = Pattern.compile("[,0-9]"); @@ -37,6 +42,8 @@ public class MessageListener extends PacketAdapter { public MessageListener(mcMMOAction plugin, Collection messages) { super(params().plugin(plugin).types(CHAT)); + shouldRemoveHover = !Enums.getIfPresent(HoverEvent.Action.class, "SHOW_ENTITY").isPresent(); + this.plugin = plugin; this.localizedMessages = ImmutableSet.copyOf(messages .stream() @@ -65,12 +72,15 @@ public void onPacketSending(PacketEvent packetEvent) { } String json = message.getJson(); - String cleanedJson = gson.toJson(cleanJsonFromHover(json)); - if (cleanedJson == null) { + if (shouldRemoveHover) { + json = gson.toJson(cleanJsonFromHover(json)); + } + + if (json == null) { return; } - BaseComponent chatComponent = ComponentSerializer.parse(cleanedJson)[0]; + BaseComponent chatComponent = ComponentSerializer.parse(json)[0]; if (chatComponent != null && isMcMMOMessage(chatComponent.toPlainText())) { writeChatPosition(packet); @@ -104,9 +114,13 @@ private void writeChatPosition(PacketContainer packet) { } } - private JsonObject cleanJsonFromHover(String json) { - JsonObject jsonComponent = gson.fromJson(json, JsonObject.class); - return cleanJsonFromHover(jsonComponent); + private JsonElement cleanJsonFromHover(String json) { + JsonElement jsonComponent = gson.fromJson(json, JsonElement.class); + if (jsonComponent instanceof JsonObject) { + return cleanJsonFromHover((JsonObject) jsonComponent); + } + + return jsonComponent; } private JsonObject cleanJsonFromHover(JsonObject jsonComponent) { diff --git a/src/main/java/com/github/games647/mcmmoaction/listener/PlayerListener.java b/src/main/java/com/github/games647/mcmmoaction/listener/PlayerListener.java index 7c28217..a46fc25 100644 --- a/src/main/java/com/github/games647/mcmmoaction/listener/PlayerListener.java +++ b/src/main/java/com/github/games647/mcmmoaction/listener/PlayerListener.java @@ -8,6 +8,7 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; @@ -25,7 +26,7 @@ public void onPlayerQuit(PlayerQuitEvent quitEvent) { plugin.getActionBarDisabled().remove(player.getUniqueId()); } - @EventHandler + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onExperienceGain(McMMOPlayerXpGainEvent experienceEvent) { Player player = experienceEvent.getPlayer(); if (plugin.isProgressEnabled(player.getUniqueId())) {