package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets;

import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.BlockRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;

public class WorldPackets {

    public static void register(Protocol protocol) {
        BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_16To1_15_2::getNewBlockStateId, Protocol1_16To1_15_2::getNewBlockId);

        // Block action
        blockRewriter.registerBlockAction(0x0B, 0x0B);

        // Block Change
        blockRewriter.registerBlockChange(0x0C, 0x0C);

        // Multi Block Change
        blockRewriter.registerMultiBlockChange(0x10, 0x10);

        // Acknowledge player digging
        blockRewriter.registerAcknowledgePlayerDigging(0x08, 0x08);

        // Chunk Data
        protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() {
            @Override
            public void registerMap() {
                handler(wrapper -> {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.passthrough(new Chunk1_15Type(clientWorld));
                    for (int s = 0; s < 16; s++) {
                        ChunkSection section = chunk.getSections()[s];
                        if (section == null) continue;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            int old = section.getPaletteEntry(i);
                            section.setPaletteEntry(i, Protocol1_16To1_15_2.getNewBlockStateId(old));
                        }
                    }
                });
            }
        });

        // Effect
        blockRewriter.registerEffect(0x23, 0x23, 1010, 2001, InventoryPackets::getNewItemId);

        // Spawn Particle
        blockRewriter.registerSpawnParticle(Type.DOUBLE, 0x24, 0x24, 3, 23, 32,
                WorldPackets::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM);
    }

    public static int getNewParticleId(int id) {
        if (id >= 27) {
            id += 1; // soul flame
        }
        return id;
    }
}

@@ -62,16 +62,254 @@
 ckets;%0A%0A
+import com.github.steveice10.opennbt.tag.builtin.CompoundTag;%0Aimport com.github.steveice10.opennbt.tag.builtin.IntArrayTag;%0Aimport com.github.steveice10.opennbt.tag.builtin.StringTag;%0Aimport com.github.steveice10.opennbt.tag.builtin.Tag;%0A
 import u
@@ -355,16 +355,16 @@
 .Chunk;%0A
-
 import u
@@ -617,24 +617,84 @@
 .type.Type;%0A
+import us.myles.ViaVersion.api.type.types.UUIDIntArrayType;%0A
 import us.my
@@ -964,16 +964,40 @@
 World;%0A%0A
+import java.util.UUID;%0A%0A
 public c
@@ -2429,32 +2429,1607 @@
                %7D
+%0A%0A                    if (chunk.getBlockEntities() == null) return;%0A                    for (CompoundTag blockEntity : chunk.getBlockEntities()) %7B%0A                        String id = ((StringTag) blockEntity.get(%22id%22)).getValue();%0A                        if (id.equals(%22minecraft:conduit%22)) %7B%0A                            StringTag targetUuidTag = blockEntity.remove(%22target_uuid%22);%0A                            if (targetUuidTag == null) continue;%0A%0A%0A                            // target_uuid -%3E Target%0A                            UUID targetUuid = UUID.fromString(targetUuidTag.getValue());%0A                            blockEntity.put(new IntArrayTag(%22Target%22, UUIDIntArrayType.uuidToIntArray(targetUuid)));%0A                        %7D else if (id.equals(%22minecraft:skull%22)) %7B%0A                            CompoundTag ownerTag = blockEntity.remove(%22Owner%22);%0A                            if (ownerTag == null) continue;%0A%0A                            StringTag ownerUuidTag = ownerTag.remove(%22Id%22);%0A                            UUID ownerUuid = UUID.fromString(ownerUuidTag.getValue());%0A                            ownerTag.put(new IntArrayTag(%22Id%22, UUIDIntArrayType.uuidToIntArray(ownerUuid)));%0A%0A                            // Owner -%3E SkullOwner%0A                            CompoundTag skullOwnerTag = new CompoundTag(%22SkullOwner%22);%0A                            for (Tag tag : ownerTag) %7B%0A                                skullOwnerTag.put(tag);%0A                            %7D%0A                            blockEntity.put(skullOwnerTag);%0A                        %7D%0A                    %7D
 %0A               
@@ -4478,17 +4478,17 @@
   id += 
-1
+2
 ; // sou
@@ -4494,16 +4494,22 @@
 ul flame
+, soul
 %0A       

Fix block entity uuid nbt, fix particles