package us.myles.ViaVersion.api.rewriters;

import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;

public class SoundRewriter {
    protected final Protocol protocol;
    protected final IdRewriteFunction idRewriter;

    public SoundRewriter(Protocol protocol) {
        this.protocol = protocol;
        this.idRewriter = id -> protocol.getMappingData().getSoundMappings().getNewId(id);
    }

    public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) {
        this.protocol = protocol;
        this.idRewriter = idRewriter;
    }

    // The same for entity sound
    public void registerSound(ClientboundPacketType packetType) {
        protocol.registerOutgoing(packetType, new PacketRemapper() {
            @Override
            public void registerMap() {
                map(Type.VAR_INT); // Sound Id
                handler(wrapper -> {
                    int soundId = wrapper.get(Type.VAR_INT, 0);
                    int mappedId = idRewriter.rewrite(soundId);
                    if (mappedId == -1) {
                        wrapper.cancel();
                    } else if (soundId != mappedId) {
                        wrapper.set(Type.VAR_INT, 0, mappedId);
                    }
                });
            }
        });
    }
}

@@ -150,16 +150,71 @@
 otocol;%0A
+import us.myles.ViaVersion.api.remapper.PacketHandler;%0A
 import u
@@ -1057,16 +1057,129 @@
 handler(
+getSoundHandler());%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public PacketHandler getSoundHandler() %7B%0A        return 
 wrapper 
@@ -1183,24 +1183,16 @@
 er -%3E %7B%0A
-        
         
@@ -1247,32 +1247,24 @@
             
-        
 int mappedId
@@ -1307,24 +1307,16 @@
         
- 
-       
 if (mapp
@@ -1341,32 +1341,24 @@
             
-      
-  
 wrapper.canc
@@ -1363,24 +1363,16 @@
 ncel();%0A
-        
         
@@ -1421,32 +1421,24 @@
             
-  
-      
 wrapper.set(
@@ -1469,32 +1469,34 @@
 d);%0A            
+%7D%0A
         %7D%0A      
@@ -1492,53 +1492,8 @@
    %7D
-%0A                %7D);%0A            %7D%0A        %7D)
 ;%0A  

Add method to get handler in sound rewriter