Skip to content

Commit

Permalink
2.6.0 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Jul 5, 2020
1 parent 655e34f commit 83356ce
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/com/loohp/interactivechat/Listeners/ChatPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ public void onPacketSending(PacketEvent event) {
debug++;
basecomponent = CustomPlaceholderDisplay.process(basecomponent, sender, reciever, rawMessageKey, unix);
debug++;
basecomponent = InteractiveChat.FilterUselessColorCodes ? ChatComponentUtils.cleanUpLegacyText(basecomponent, reciever) : ChatComponentUtils.respectClientColorSettingsWithoutCleanUp(basecomponent, reciever);
if (InteractiveChat.version.isPost1_16()) {
if (!sender.isPresent() || (sender.isPresent() && sender.get().hasPermission("interactivechat.customfont.translate"))) {
basecomponent = ChatComponentUtils.translatePluginFontFormatting(basecomponent);
}
}
debug++;
basecomponent = InteractiveChat.FilterUselessColorCodes ? ChatComponentUtils.cleanUpLegacyText(basecomponent, reciever) : ChatComponentUtils.respectClientColorSettingsWithoutCleanUp(basecomponent, reciever);
String json = ComponentSerializer.toString(basecomponent);
boolean longerThanMaxLength = false;
if (((InteractiveChat.version.isLegacy() || InteractiveChat.protocolManager.getProtocolVersion(reciever) < 393) && json.length() > 30000) || (!InteractiveChat.version.isLegacy() && json.length() > 262000)) {
Expand Down
66 changes: 65 additions & 1 deletion src/com/loohp/interactivechat/Utils/ChatColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.loohp.interactivechat.InteractiveChat;

Expand All @@ -13,6 +15,7 @@
public class ChatColorUtils {

private static Set<Character> colors = new HashSet<Character>();
private static Pattern colorFormating = Pattern.compile("(?=(?<!\\\\)|(?<=\\\\\\\\))\\[[^\\]]*?color=#[0-9a-fA-F]{6}[^\\[]*?\\]");

static {
colors.add('0');
Expand Down Expand Up @@ -165,14 +168,75 @@ public static String addColorToEachWord(String text, String leadingColor) {
return sb.toString();
}

public static String translateAlternateColorCodes(char code, String text) {
public static String hexToColorCode(String hex) {
if (hex == null) {
return hex;
}

int pos = hex.indexOf("#");
if (pos < 0 || hex.length() < (pos + 7)) {
return "§x§F§F§F§F§F§F";
}
return "§x§" + String.valueOf(hex.charAt(1)) + "§" + String.valueOf(hex.charAt(2)) + "§" + String.valueOf(hex.charAt(3)) + "§" + String.valueOf(hex.charAt(4)) + "§" + String.valueOf(hex.charAt(5)) + "§" + String.valueOf(hex.charAt(6));
}

public static String translatePluginColorFormatting(String text) {
while (true) {
Matcher matcher = colorFormating.matcher(text);

if (matcher.find()) {
String foramtedColor = matcher.group().toLowerCase();
int start = matcher.start();
int pos = foramtedColor.indexOf("color");
int absPos = text.indexOf("color", start);
int end = matcher.end();

if (pos < 0) {
continue;
}

String colorCode = hexToColorCode(foramtedColor.substring(pos + 6, pos + 13));

StringBuilder sb = new StringBuilder(text);
sb.insert(end, colorCode);

sb.delete(absPos, absPos + 13);

while (sb.charAt(absPos) == ',' || sb.charAt(absPos) == ' ') {
sb.deleteCharAt(absPos);
}

while (sb.charAt(absPos - 1) == ',' || sb.charAt(absPos - 1) == ' ') {
sb.deleteCharAt(absPos - 1);
absPos--;
}

if (sb.charAt(absPos) == ']' && sb.charAt(absPos - 1) == '[') {
sb.deleteCharAt(absPos - 1);
sb.deleteCharAt(absPos - 1);
}

text = sb.toString();
} else {
break;
}
}

return text;
}

public static String translateAlternateColorCodes(char code, String text) {
if (text == null) {
return text;
}

if (text.length() < 2) {
return text;
}

if (InteractiveChat.version.isPost1_16()) {
text = translatePluginColorFormatting(text);
}

for (int i = 0; i < text.length() - 1; i++) {
if (text.charAt(i) == code) {
Expand Down
148 changes: 148 additions & 0 deletions src/com/loohp/interactivechat/Utils/ChatComponentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
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;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.bukkit.entity.Player;
Expand All @@ -30,6 +34,8 @@ public class ChatComponentUtils {
private static Class<?> chatHoverEventClass;
private static MethodHandle hoverEventGetValueMethod;

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

public static void setupLegacy() {
try {
chatHoverEventClass = Class.forName("net.md_5.bungee.api.chat.HoverEvent");
Expand Down Expand Up @@ -66,6 +72,14 @@ public static boolean areSimilar(BaseComponent base1, BaseComponent base2, boole
if (base1.isUnderlined() != base2.isUnderlined()) {
return false;
}
if ((base1.getFontRaw() == null && base2.getFontRaw() != null) || (base1.getFontRaw() != null && base2.getFontRaw() == null)) {
return false;
}
if (base1.getFontRaw() != null && base2.getFontRaw() != null) {
if (!base1.getFontRaw().equals(base2.getFontRaw())) {
return false;
}
}
if (compareText && !base1.toLegacyText().equals(base2.toLegacyText())) {
return false;
}
Expand Down Expand Up @@ -96,12 +110,32 @@ public static boolean areSimilarNoEvents(BaseComponent base1, BaseComponent base
if (base1.isUnderlined() != base2.isUnderlined()) {
return false;
}
if ((base1.getFontRaw() == null && base2.getFontRaw() != null) || (base1.getFontRaw() != null && base2.getFontRaw() == null)) {
return false;
}
if (base1.getFontRaw() != null && base2.getFontRaw() != null) {
if (!base1.getFontRaw().equals(base2.getFontRaw())) {
return false;
}
}
if (compareText && !base1.toLegacyText().equals(base2.toLegacyText())) {
return false;
}
return true;
}

public static boolean areFontsSimilar(BaseComponent base1, BaseComponent base2) {
if ((base1.getFontRaw() == null && base2.getFontRaw() != null) || (base1.getFontRaw() != null && base2.getFontRaw() == null)) {
return false;
}
if (base1.getFontRaw() != null && base2.getFontRaw() != null) {
if (!base1.getFontRaw().equals(base2.getFontRaw())) {
return false;
}
}
return true;
}

public static boolean areEventsSimilar(BaseComponent base1, BaseComponent base2) {
boolean clickSim = false;
boolean hoverSim = false;
Expand Down Expand Up @@ -270,6 +304,9 @@ public static BaseComponent cleanUpLegacyText(BaseComponent basecomponent, Playe
} else {
each.copyFormatting(base, FormatRetention.EVENTS, false);
}
if (InteractiveChat.version.isPost1_16()) {
each.setFont(base.getFontRaw());
}
//Bukkit.getConsoleSender().sendMessage(ComponentSerializer.toString(each).replace("§", "&"));
});
newlist.addAll(texts);
Expand Down Expand Up @@ -334,5 +371,116 @@ public static BaseComponent join(BaseComponent[] basecomponentarray) {
return join(basecomponentarray[0], Arrays.copyOfRange(basecomponentarray, 1, basecomponentarray.length));
}
}

public static BaseComponent translatePluginFontFormatting(BaseComponent basecomponent) {
List<BaseComponent> list = CustomStringUtils.loadExtras(basecomponent);
List<BaseComponent> newlist = new ArrayList<BaseComponent>();
Optional<String> currentFont = Optional.empty();

for (BaseComponent each : list) {
if (each.getFontRaw() != null) {
currentFont = Optional.of(each.getFontRaw());
}

if (each instanceof TextComponent) {
List<TextComponent> textlist = new LinkedList<TextComponent>();
TextComponent textcomponent = (TextComponent) each;
String text = textcomponent.getText();

while (true) {
Matcher matcher = fontFormating.matcher(text);

if (matcher.find()) {
String foramtedFont = matcher.group().toLowerCase();

String colorCodesInside = ChatColorUtils.getLastColors(foramtedFont);
String striped = ChatColor.stripColor(foramtedFont);

int lengthDiff = foramtedFont.length() - striped.length();

foramtedFont = striped;

int start = matcher.start();
int pos = foramtedFont.indexOf("font");
int end = matcher.end() - lengthDiff;

if (pos < 0) {
continue;
}

int fontLength = foramtedFont.indexOf(",", pos);
if (fontLength < 0) {
fontLength = foramtedFont.indexOf(" ", pos);
}
if (fontLength < 0) {
fontLength = foramtedFont.indexOf("]", pos);
}
fontLength = fontLength - pos;

StringBuilder sb = new StringBuilder(text);

sb.delete(start, matcher.end());
sb.insert(start, foramtedFont);

int absPos = sb.indexOf("font", start);

String nextFont = foramtedFont.substring(pos + 5, pos + fontLength);

sb.delete(absPos, absPos + fontLength);
sb.insert(end - fontLength, colorCodesInside);

while (sb.charAt(absPos) == ',' || sb.charAt(absPos) == ' ') {
sb.deleteCharAt(absPos);
}

while (sb.charAt(absPos - 1) == ',' || sb.charAt(absPos - 1) == ' ') {
sb.deleteCharAt(absPos - 1);
absPos--;
}

if (sb.charAt(absPos) == ']' && sb.charAt(absPos - 1) == '[') {
sb.deleteCharAt(absPos - 1);
sb.deleteCharAt(absPos - 1);
}

absPos--;

TextComponent before = new TextComponent(textcomponent);
before.setText(sb.substring(0, absPos));
if (currentFont.isPresent()) {
before.setFont(currentFont.get());
}
textlist.add(before);

text = sb.substring(absPos);

currentFont = (nextFont.length() == 0 || nextFont.equalsIgnoreCase("null") || nextFont.equalsIgnoreCase("reset")) ? Optional.empty() : Optional.of(nextFont);
} else {
TextComponent before = new TextComponent(textcomponent);
before.setText(text);
if (currentFont.isPresent()) {
before.setFont(currentFont.get());
}
textlist.add(before);
break;
}
}
newlist.addAll(textlist);
} else {
if (currentFont.isPresent()) {
each.setFont(currentFont.get());
}
newlist.add(each);
}
}

TextComponent product = new TextComponent("");
for (int i = 0; i < newlist.size(); i++) {
BaseComponent each = newlist.get(i);
product.addExtra(each);
}

return product;
}

}
18 changes: 15 additions & 3 deletions src/com/loohp/interactivechat/Utils/CustomStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static List<BaseComponent> loadExtras(List<BaseComponent> baseComp) {
} else {
text.copyFormatting(each);
}
list.add(text);
list.add(text);
} else {
list.add(each);
}
Expand All @@ -150,8 +150,12 @@ public static List<BaseComponent> loadExtras(List<BaseComponent> baseComp) {
list.add(noExtra);
}
for (BaseComponent extra : loadExtras(each.getExtra())) {
extra = copyFormattingNoReplace(extra, noExtra);
if (extra instanceof TextComponent && text != null && ChatComponentUtils.areEventsSimilar(extra, text)) {
if (InteractiveChat.version.isLegacy() && !InteractiveChat.version.equals(MCVersion.V1_12)) {
extra = copyFormattingNoReplace(extra, noExtra);
} else {
extra.copyFormatting(noExtra, false);
}
if (extra instanceof TextComponent && text != null && ChatComponentUtils.areEventsSimilar(extra, text) && ChatComponentUtils.areFontsSimilar(extra, text)) {
TextComponent extraNoExtra = (TextComponent) extra.duplicate();
if (extraNoExtra.getExtra() != null) {
extraNoExtra.getExtra().clear();
Expand Down Expand Up @@ -219,6 +223,11 @@ public static BaseComponent copyFormattingNoReplace(BaseComponent set, BaseCompo
set.setInsertion(get.getInsertion());
}
}
if (InteractiveChat.version.isPost1_16()) {
if (set.getFontRaw() == null) {
set.setFont(get.getFontRaw());
}
}
return set;
}

Expand All @@ -244,6 +253,9 @@ public static BaseComponent copyFormatting(BaseComponent set, BaseComponent get)
set.setObfuscated(get.isObfuscated());
set.setStrikethrough(get.isStrikethrough());
set.setUnderlined(get.isUnderlined());
if (InteractiveChat.version.isPost1_16()) {
set.setFont(get.getFontRaw());
}
return set;
}

Expand Down
4 changes: 4 additions & 0 deletions src/com/loohp/interactivechat/Utils/MCVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ public boolean isOld() {
public boolean isSupported() {
return !unsupported;
}

public boolean isPost1_16() {
return this.ordinal() <= MCVersion.V1_16.ordinal();
}

}
5 changes: 4 additions & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: InteractiveChat
author: loohp
version: 2.5.2
version: 2.6.0
main: com.loohp.interactivechat.InteractiveChat
api-version: 1.13
description: Make the chat interactive
Expand Down Expand Up @@ -39,6 +39,9 @@ permissions:
interactivechat.chatcolor.translate:
description: Allows you to use alternate color code
default: true
interactivechat.customfont.translate:
description: Allows you to use alternate color code
default: true
interactivechat.update:
description: Allows you to recieve update notifications
default: op

0 comments on commit 83356ce

Please sign in to comment.