package protocolsupport.protocol.transformer.v_1_7.utils;

import java.util.ArrayList;
import java.util.List;

import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;

import net.minecraft.server.v1_8_R3.Item;
import net.minecraft.server.v1_8_R3.MinecraftKey;
import net.minecraft.server.v1_8_R3.MinecraftServer;
import net.minecraft.server.v1_8_R3.MojangsonParser;
import net.minecraft.server.v1_8_R3.NBTTagCompound;

public class ChatJsonConverter {

	public static String convert(String message) {
		BaseComponent[] components = ComponentSerializer.parse(message);
		BaseComponent[] newcomponents = new BaseComponent[components.length];
		for (int i = 0; i < components.length; i++) {
			BaseComponent component = components[i];
			List<BaseComponent> extra = component.getExtra();
			BaseComponent newcomponent = fixComponent(component);
			List<BaseComponent> newextra = new ArrayList<BaseComponent>();
			if (extra != null) {
				for (BaseComponent child : extra) {
					newextra.add(fixComponent(child));
				}
				newcomponent.setExtra(newextra);
			}
			newcomponents[i] = newcomponent;
		}
		return ComponentSerializer.toString(newcomponents);
	}

	private static BaseComponent fixComponent(BaseComponent component) {
		HoverEvent event = component.getHoverEvent();
		if (event != null) {
			if (event.getAction() == HoverEvent.Action.SHOW_ITEM) {
				try {
					String itemstackjson = event.getValue()[0].toPlainText();
					NBTTagCompound compound = MojangsonParser.parse(itemstackjson);
					String itemid = compound.getString("id");
					Item item = Item.REGISTRY.get(new MinecraftKey(itemid));
					if (item != null) {
						compound.setInt("id", Item.getId(item));
					}
					component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new BaseComponent[] { new TextComponent(compound.toString()) } ));
				} catch (Throwable t) {
					if (MinecraftServer.getServer().isDebugging()) {
						t.printStackTrace();
					}
				}
			}
		}
		return component;
	}

}

@@ -56,499 +56,8 @@
 s;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport net.md_5.bungee.api.chat.BaseComponent;%0Aimport net.md_5.bungee.api.chat.HoverEvent;%0Aimport net.md_5.bungee.api.chat.TextComponent;%0Aimport net.md_5.bungee.chat.ComponentSerializer;%0A%0Aimport net.minecraft.server.v1_8_R3.Item;%0Aimport net.minecraft.server.v1_8_R3.MinecraftKey;%0Aimport net.minecraft.server.v1_8_R3.MinecraftServer;%0Aimport net.minecraft.server.v1_8_R3.MojangsonParser;%0Aimport net.minecraft.server.v1_8_R3.NBTTagCompound;%0A%0A
 publ
@@ -86,16 +86,49 @@
 rter %7B%0A%0A
+%09//TODO: actually transform json%0A
 %09public 
@@ -173,1482 +173,22 @@
 %7B%0A%09%09
-BaseComponent%5B%5D components = ComponentSerializer.parse(message);%0A%09%09BaseComponent%5B%5D newcomponents = new BaseComponent%5Bcomponents.length%5D;%0A%09%09for (int i = 0; i %3C components.length; i++) %7B%0A%09%09%09BaseComponent component = components%5Bi%5D;%0A%09%09%09List%3CBaseComponent%3E extra = component.getExtra();%0A%09%09%09BaseComponent newcomponent = fixComponent(component);%0A%09%09%09List%3CBaseComponent%3E newextra = new ArrayList%3CBaseComponent%3E();%0A%09%09%09if (extra != null) %7B%0A%09%09%09%09for (BaseComponent child : extra) %7B%0A%09%09%09%09%09newextra.add(fixComponent(child));%0A%09%09%09%09%7D%0A%09%09%09%09newcomponent.setExtra(newextra);%0A%09%09%09%7D%0A%09%09%09newcomponents%5Bi%5D = newcomponent;%0A%09%09%7D%0A%09%09return ComponentSerializer.toString(newcomponents);%0A%09%7D%0A%0A%09private static BaseComponent fixComponent(BaseComponent component) %7B%0A%09%09HoverEvent event = component.getHoverEvent();%0A%09%09if (event != null) %7B%0A%09%09%09if (event.getAction() == HoverEvent.Action.SHOW_ITEM) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09String itemstackjson = event.getValue()%5B0%5D.toPlainText();%0A%09%09%09%09%09NBTTagCompound compound = MojangsonParser.parse(itemstackjson);%0A%09%09%09%09%09String itemid = compound.getString(%22id%22);%0A%09%09%09%09%09Item item = Item.REGISTRY.get(new MinecraftKey(itemid));%0A%09%09%09%09%09if (item != null) %7B%0A%09%09%09%09%09%09compound.setInt(%22id%22, Item.getId(item));%0A%09%09%09%09%09%7D%0A%09%09%09%09%09component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new BaseComponent%5B%5D %7B new TextComponent(compound.toString()) %7D ));%0A%09%09%09%09%7D catch (Throwable t) %7B%0A%09%09%09%09%09if (MinecraftServer.getServer().isDebugging()) %7B%0A%09%09%09%09%09%09t.printStackTrace();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return component
+return message
 ;%0A%09%7D

Remove chat json converting, spigot base components don't work.