From 572cf0cb6d371192c595b101ba83c90d195d819a Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Fri, 6 Nov 2020 10:32:33 +0200 Subject: [PATCH] Wrote a compatibility method for 1.8 Affects issues: - Fixed #1600 --- .../plan/commands/use/BukkitPartBuilder.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitPartBuilder.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitPartBuilder.java index a896040d4..6eb35c923 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitPartBuilder.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitPartBuilder.java @@ -16,14 +16,17 @@ */ package com.djrapitops.plan.commands.use; +import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.TextComponent; import org.apache.commons.text.TextStringBuilder; +import org.bukkit.ChatColor; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; class BukkitPartBuilder implements MessageBuilder { @@ -44,8 +47,11 @@ class BukkitPartBuilder implements MessageBuilder { @Override public MessageBuilder addPart(String text) { BukkitPartBuilder nextPart = new BukkitPartBuilder(this); - // appendLegacy cannot be used as it was added after 1.8 - nextPart.part.append(TextComponent.fromLegacyText(text)); + try { + nextPart.part.appendLegacy(text); + } catch (NoSuchMethodError oldVersion_1_8) { + nextPart.part.append(ChatColor.translateAlternateColorCodes('ยง', text)); + } return nextPart; } @@ -105,8 +111,28 @@ class BukkitPartBuilder implements MessageBuilder { if (sender != null) { sender.sender.spigot().sendMessage(part.create()); } else if (previous != null) { - previous.part.append(part.create()); + try { + previous.part.append(part.create()); + } catch (NoSuchMethodError oldVersion_1_8) { + sendOld(); + return; + } previous.send(); } } + + // Compatibility for 1.8 + private void sendOld() { + List components = new ArrayList<>(); + + // Jumping through a bunch of hoops to implement recursive append function dynamically + BukkitPartBuilder current = this; + while (current.sender == null) { + components.addAll(0, Arrays.asList(current.part.create())); + current = current.previous; + } + + // CommandSender#spigot#sendMessage(BaseComponent[]) is not supported on 1.8 + current.sender.sender.sendMessage(BaseComponent.toLegacyText(components.toArray(new BaseComponent[0]))); + } }