Skip to content

Commit

Permalink
Fixes legacy bungeecord api problems with other plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Aug 13, 2020
1 parent 2a8e329 commit 92c2076
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>InteractiveChat</groupId>
<artifactId>InteractiveChat</artifactId>
<version>2.6.10</version>
<version>2.6.11</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
Expand Down
13 changes: 5 additions & 8 deletions src/com/loohp/interactivechat/InteractiveChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
import com.loohp.interactivechat.ObjectHolders.ICPlaceholder;
import com.loohp.interactivechat.ObjectHolders.MentionPair;
import com.loohp.interactivechat.Updater.Updater;
import com.loohp.interactivechat.Utils.ChatComponentUtils;
import com.loohp.interactivechat.Utils.ItemNBTUtils;
import com.loohp.interactivechat.Utils.MCVersion;
import com.loohp.interactivechat.Utils.MaterialUtils;
import com.loohp.interactivechat.Utils.PotionUtils;
import com.loohp.interactivechat.Utils.RarityUtils;

import net.md_5.bungee.api.chat.TextComponent;

public class InteractiveChat extends JavaPlugin {

public static Plugin plugin = null;
Expand Down Expand Up @@ -216,16 +217,12 @@ public void onEnable() {
ClientSettingPackets.clientSettingsListener();

try {
Class.forName("net.md_5.bungee.api.chat.hover.content.Content");
new TextComponent("Legacy Bungeecord Chat API Test").getHoverEvent().getContents();
legacyChatAPI = false;
} catch (ClassNotFoundException e) {
} catch (Exception | NoSuchMethodError e) {
legacyChatAPI = true;
};

if (legacyChatAPI) {
ChatComponentUtils.setupLegacy();
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[InteractiveChat] Legacy Bungeecord Chat API detected, using legacy methods...");
}
};

getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[InteractiveChat] InteractiveChat has been Enabled!");

Expand Down
60 changes: 20 additions & 40 deletions src/com/loohp/interactivechat/Utils/ChatComponentUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.loohp.interactivechat.Utils;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -31,23 +28,11 @@

public class ChatComponentUtils {

private static Class<?> chatHoverEventClass;
private static MethodHandle hoverEventGetValueMethod;

private static Pattern fontFormating = Pattern.compile("(?=(?<!\\\\)|(?<=\\\\\\\\))\\[[^\\]]*?font=[0-9a-zA-Z:_]*[^\\[]*?\\]");
private static Pattern fontEscape = Pattern.compile("\\\\\\[ *?font=[0-9a-zA-Z:_]* *?\\]");

private static String validFont = "^([0-9a-zA-Z_]+:)?[0-9a-zA-Z_]+$";

public static void setupLegacy() {
try {
chatHoverEventClass = Class.forName("net.md_5.bungee.api.chat.HoverEvent");
hoverEventGetValueMethod = MethodHandles.lookup().findVirtual(chatHoverEventClass, "getValue", MethodType.methodType(BaseComponent[].class));
} catch (Throwable e) {
e.printStackTrace();
}
}

public static boolean areSimilar(BaseComponent base1, BaseComponent base2, boolean compareText) {
if (!areEventsSimilar(base1, base2)) {
return false;
Expand Down Expand Up @@ -175,25 +160,23 @@ public static boolean areEventsSimilar(BaseComponent base1, BaseComponent base2)
HoverEvent hover2 = base2.getHoverEvent();
if (hover1.getAction().equals(hover2.getAction())) {
if (InteractiveChat.legacyChatAPI) {
try {
BaseComponent[] basecomponentarray1 = (BaseComponent[]) hoverEventGetValueMethod.invoke(hover1);
BaseComponent[] basecomponentarray2 = (BaseComponent[]) hoverEventGetValueMethod.invoke(hover2);
if (basecomponentarray1.length == basecomponentarray2.length) {
hoverSim = true;
for (int i = 0; i < basecomponentarray1.length && i < basecomponentarray2.length ; i++) {
BaseComponent bc1 = basecomponentarray1[i];
BaseComponent bc2 = basecomponentarray2[i];
if (!(bc1 == null && bc2 == null) && (bc1 == null || bc2 == null)) {
hoverSim = false;
break;
} else if (!areSimilarNoEvents(bc1, bc2, true)) {
hoverSim = false;
break;
}
@SuppressWarnings("deprecation")
BaseComponent[] basecomponentarray1 = hover1.getValue();
@SuppressWarnings("deprecation")
BaseComponent[] basecomponentarray2 = hover2.getValue();
if (basecomponentarray1.length == basecomponentarray2.length) {
hoverSim = true;
for (int i = 0; i < basecomponentarray1.length && i < basecomponentarray2.length ; i++) {
BaseComponent bc1 = basecomponentarray1[i];
BaseComponent bc2 = basecomponentarray2[i];
if (!(bc1 == null && bc2 == null) && (bc1 == null || bc2 == null)) {
hoverSim = false;
break;
} else if (!areSimilarNoEvents(bc1, bc2, true)) {
hoverSim = false;
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
} else {
List<Content> contents1 = hover1.getContents();
Expand Down Expand Up @@ -259,18 +242,15 @@ public static boolean areEventsSimilar(BaseComponent base1, BaseComponent base2)
return clickSim && hoverSim;
}

@SuppressWarnings("deprecation")
public static BaseComponent removeHoverEventColor(BaseComponent baseComponent) {
if (baseComponent.getHoverEvent() != null) {
if (InteractiveChat.legacyChatAPI) {
try {
for (BaseComponent each : (BaseComponent[]) hoverEventGetValueMethod.invoke(baseComponent.getHoverEvent())) {
each.setColor(ChatColor.WHITE);
if (each instanceof TextComponent) {
((TextComponent) each).setText(ChatColor.stripColor(((TextComponent) each).getText()));
}
for (BaseComponent each : baseComponent.getHoverEvent().getValue()) {
each.setColor(ChatColor.WHITE);
if (each instanceof TextComponent) {
((TextComponent) each).setText(ChatColor.stripColor(((TextComponent) each).getText()));
}
} catch (Throwable e) {
e.printStackTrace();
}
} else {
int j = 0;
Expand Down

0 comments on commit 92c2076

Please sign in to comment.