Skip to content

Commit

Permalink
Checked cast for JsonObject (Fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
games647 committed Oct 2, 2017
1 parent 765df41 commit 9e8c599
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>jar</packaging>

<name>mcMMOAction</name>
<version>2.1.1</version>
<version>2.1.2</version>
<inceptionYear>2016</inceptionYear>
<url>https://dev.bukkit.org/bukkit-plugins/mcmmoaction/</url>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
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;
import java.util.regex.Pattern;
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;
Expand All @@ -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]");

Expand All @@ -37,6 +42,8 @@ public class MessageListener extends PacketAdapter {
public MessageListener(mcMMOAction plugin, Collection<String> 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()
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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())) {
Expand Down

0 comments on commit 9e8c599

Please sign in to comment.