From a0bb11e1bdac6baf62f8f6d202f6784c2d600407 Mon Sep 17 00:00:00 2001 From: LOOHP Date: Sun, 28 Feb 2021 03:15:43 +0800 Subject: [PATCH] Add support for PaperMC's new Adventure API Component (#1103) --- pom.xml | 6 ++ .../reflect/cloning/BukkitCloner.java | 5 ++ .../wrappers/AdventureComponentConverter.java | 57 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/main/java/com/comphenix/protocol/wrappers/AdventureComponentConverter.java diff --git a/pom.xml b/pom.xml index 565ea61c..09146742 100644 --- a/pom.xml +++ b/pom.xml @@ -293,6 +293,12 @@ ${spigot.version} provided + + net.kyori + adventure-text-serializer-gson + 4.5.1 + provided + net.bytebuddy diff --git a/src/main/java/com/comphenix/protocol/reflect/cloning/BukkitCloner.java b/src/main/java/com/comphenix/protocol/reflect/cloning/BukkitCloner.java index 14af6d42..e4c80801 100644 --- a/src/main/java/com/comphenix/protocol/reflect/cloning/BukkitCloner.java +++ b/src/main/java/com/comphenix/protocol/reflect/cloning/BukkitCloner.java @@ -88,6 +88,11 @@ public class BukkitCloner implements Cloner { fromManual(ComponentConverter::getBaseComponentArrayClass, source -> ComponentConverter.clone((BaseComponent[]) source)); } catch (Throwable ignored) { } + + try { + fromManual(AdventureComponentConverter::getComponentClass, source -> + AdventureComponentConverter.clone(source)); + } catch (Throwable ignored) { } } private Function findCloner(Class type) { diff --git a/src/main/java/com/comphenix/protocol/wrappers/AdventureComponentConverter.java b/src/main/java/com/comphenix/protocol/wrappers/AdventureComponentConverter.java new file mode 100644 index 00000000..40bb77f1 --- /dev/null +++ b/src/main/java/com/comphenix/protocol/wrappers/AdventureComponentConverter.java @@ -0,0 +1,57 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.wrappers; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +/** + * Utility class for converting between the Adventure API Component and ProtocolLib's wrapper + *

+ * Note: The Adventure API Component is not included in CraftBukkit, Bukkit or Spigot and but is present in PaperMC. + */ +public class AdventureComponentConverter { + + private AdventureComponentConverter() { + } + + /** + * Converts a {@link WrappedChatComponent} into a {@link Component} + * @param wrapper ProtocolLib wrapper + * @return Component + */ + public static Component fromWrapper(WrappedChatComponent wrapper) { + return GsonComponentSerializer.gson().deserialize(wrapper.getJson()); + } + + /** + * Converts a {@link Component} into a ProtocolLib wrapper + * @param components Component + * @return ProtocolLib wrapper + */ + public static WrappedChatComponent fromComponent(Component component) { + return WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(component)); + } + + public static Class getComponentClass() { + return Component.class; + } + + public static Component clone(Object component) { + return (Component) component; + } +}