Wrote a compatibility method for 1.8

Affects issues:
- Fixed #1600
This commit is contained in:
Risto Lahtela 2020-11-06 10:32:33 +02:00
parent 7420e46df2
commit 572cf0cb6d

View File

@ -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<BaseComponent> 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])));
}
}