package me.minidigger.voxelgameslib.utils;

import javax.annotation.Nonnull;
import me.minidigger.voxelgameslib.user.GamePlayer;
import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import net.kyori.text.serializer.ComponentSerializer;
import org.bukkit.Bukkit;

/**
 * Small util for chat related stuff
 */
public class ChatUtil {

  /**
   * Serialises a array of base components into a single string (by calling #toPlainText on them)
   *
   * @param comps the base components to serialize into text
   * @return the readable text
   */
  public static String toPlainText(@Nonnull Component... comps) {
    StringBuilder sb = new StringBuilder();
    for (Component comp : comps) {
      toPlainText(sb, comp);
    }
    return sb.toString();
  }

  private static void toPlainText(StringBuilder sb, Component component) {
    if (component instanceof TextComponent) {
      sb.append(((TextComponent) component).content());
    }
    if (component.children().size() > 0) {
      component.children().forEach(c -> toPlainText(sb, c));
    }
  }

  /**
   * Sends the message to the user
   *
   * @param gameUser the user that should get the message
   * @param message the message to send to the user
   */
  public static void sendMessage(GamePlayer gameUser, Component message) {
    String json = ComponentSerializer.serialize(message);
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + gameUser.getPlayer().getName() + " " + json);
  }
}

@@ -37,16 +37,49 @@
 utils;%0A%0A
+import java.lang.reflect.Method;%0A
 import j
@@ -103,16 +103,16 @@
 onnull;%0A
-
 import m
@@ -376,16 +376,1007 @@
 Util %7B%0A%0A
+  private static String NMS_PREFIX = Bukkit.getServer().getClass().getPackage().getName()%0A      .replace(%22org.bukkit.craftbukkit%22, %22net.minecraft.server%22).concat(%22.%22);%0A  private static final Method CRAFTPLAYER_GETHANDLE_METHOD;%0A  private static final Method CHATSERIALIZER_A_METHOD;%0A  private static final Method ENTITYPLAYER_SENDMESSAGE_METHOD;%0A%0A  static %7B%0A    try %7B%0A      CRAFTPLAYER_GETHANDLE_METHOD = Class.forName(%0A          Bukkit.getServer().getClass().getPackage().getName().concat(%22.entity.CraftPlayer%22))%0A          .getDeclaredMethod(%22getHandle%22);%0A      CHATSERIALIZER_A_METHOD = Class%0A          .forName(NMS_PREFIX.concat(%22IChatBaseComponent$ChatSerializer%22))%0A          .getDeclaredMethod(%22a%22, String.class);%0A      ENTITYPLAYER_SENDMESSAGE_METHOD = Class.forName(NMS_PREFIX.concat(%22EntityPlayer%22))%0A          .getDeclaredMethod(%22sendMessage%22, Class.forName(NMS_PREFIX.concat(%22IChatBaseComponent%22)));%0A    %7D catch (final Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A
   /**%0A  
@@ -2328,21 +2328,179 @@
     
-S
 tr
-ing json =
+y %7B%0A      ENTITYPLAYER_SENDMESSAGE_METHOD%0A          .invoke(CRAFTPLAYER_GETHANDLE_METHOD.invoke(gameUser.getPlayer()),%0A              CHATSERIALIZER_A_METHOD.invoke(null,
  Com
@@ -2538,122 +2538,89 @@
 age)
+))
 ;%0A    
-Bukkit.dispatchCommand(Bukkit.getConsoleSender(), %22tellraw %22 + gameUser.getPlayer().getName() + %22 %22 + json);
+%7D catch (Exception e) %7B%0A      throw new RuntimeException(%22wut%22, e);%0A    %7D
 %0A  %7D

replace tellraw send impl with reflection (thanks kashike!