package me.deftware.client.framework.network.packets;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import me.deftware.client.framework.network.PacketWrapper;
import net.minecraft.item.ItemStack;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.SetTradeOffersS2CPacket;
import net.minecraft.village.TradeOffer;

public class SPacketTradeOffers extends PacketWrapper {

    public SPacketTradeOffers(Packet<?> packet) {
        super(packet);
    }

    public JsonObject getJson() {
        SetTradeOffersS2CPacket packet = ((SetTradeOffersS2CPacket) this.packet);
        JsonObject json = new JsonObject();
        // Villager meta
        json.addProperty("experience", packet.getExperience());
        json.addProperty("level", packet.getLevelProgress());
        JsonArray trades = new JsonArray();
        for (TradeOffer offer : packet.getOffers()) {
            JsonObject trade = new JsonObject();
            // Items
            trade.add("first", getItem(offer.getAdjustedFirstBuyItem()));
            if (!offer.getSecondBuyItem().isEmpty()) {
                trade.add("second", getItem(offer.getSecondBuyItem()));
            }
            trade.add("sell", getItem(offer.getSellItem()));
            // Meta
            trade.addProperty("xp", offer.getMerchantExperience());
            trade.addProperty("uses", offer.getUses());
            trade.addProperty("maxUses", offer.getMaxUses());
            trades.add(trade);
        }
        json.add("trades", trades);
        return json;
    }

    private JsonObject getItem(ItemStack stack) {
        JsonObject json = new JsonObject();
        json.addProperty("count", stack.getCount());
        json.addProperty("id", stack.getItem().getTranslationKey());
        return json;
    }

}

@@ -176,16 +176,114 @@
 rapper;%0A
+import net.minecraft.enchantment.Enchantment;%0Aimport net.minecraft.enchantment.EnchantmentHelper;%0A
 import n
@@ -460,16 +460,39 @@
 Offer;%0A%0A
+import java.util.Map;%0A%0A
 public c
@@ -1897,24 +1897,431 @@
 ationKey());
+%0A%0A        Map%3CEnchantment, Integer%3E enchantments = EnchantmentHelper.get(stack);%0A        if (!enchantments.isEmpty()) %7B%0A            JsonArray meta = new JsonArray();%0A            for (Map.Entry%3CEnchantment, Integer%3E enchantment : enchantments.entrySet()) %7B%0A                meta.add(enchantment.getKey().getName(enchantment.getValue()).getString());%0A            %7D%0A            json.add(%22meta%22, meta);%0A        %7D
 %0A        ret

Add enchantments to trade packet