2020-05-23 10:56:27 +02:00
|
|
|
package us.myles.ViaVersion.api.rewriters;
|
|
|
|
|
2020-06-05 20:30:09 +02:00
|
|
|
import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
|
2020-05-23 10:56:27 +02:00
|
|
|
import us.myles.ViaVersion.api.protocol.Protocol;
|
|
|
|
import us.myles.ViaVersion.api.remapper.PacketRemapper;
|
|
|
|
import us.myles.ViaVersion.api.type.Type;
|
|
|
|
|
|
|
|
public class SoundRewriter {
|
2020-05-23 11:14:50 +02:00
|
|
|
protected final Protocol protocol;
|
|
|
|
protected final IdRewriteFunction idRewriter;
|
2020-05-23 10:56:27 +02:00
|
|
|
|
2020-08-16 16:24:06 +02:00
|
|
|
public SoundRewriter(Protocol protocol) {
|
|
|
|
this.protocol = protocol;
|
|
|
|
this.idRewriter = id -> protocol.getMappingData().getSoundMappings().getNewId(id);
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:56:27 +02:00
|
|
|
public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) {
|
|
|
|
this.protocol = protocol;
|
|
|
|
this.idRewriter = idRewriter;
|
|
|
|
}
|
|
|
|
|
2020-06-05 23:06:00 +02:00
|
|
|
// The same for entity sound
|
2020-06-05 20:30:09 +02:00
|
|
|
public void registerSound(ClientboundPacketType packetType) {
|
2020-06-05 23:06:00 +02:00
|
|
|
protocol.registerOutgoing(packetType, new PacketRemapper() {
|
2020-05-23 10:56:27 +02:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-05 23:06:00 +02:00
|
|
|
});
|
2020-05-23 10:56:27 +02:00
|
|
|
}
|
|
|
|
}
|