3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-12-27 16:40:12 +01:00

Use new SoundEvent type

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-03-01 14:23:13 +01:00
Ursprung 445d798a17
Commit 39e9d6dd04
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
2 geänderte Dateien mit 43 neuen und 36 gelöschten Zeilen

Datei anzeigen

@ -18,7 +18,10 @@
package com.viaversion.viabackwards.api.rewriters; package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viaversion.api.minecraft.Holder;
import com.viaversion.viaversion.api.minecraft.SoundEvent;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
@ -27,12 +30,12 @@ public class SoundRewriter<C extends ClientboundPacketType> extends com.viaversi
private final BackwardsProtocol<C, ?, ?, ?> protocol; private final BackwardsProtocol<C, ?, ?, ?> protocol;
public SoundRewriter(BackwardsProtocol<C, ?, ?, ?> protocol) { public SoundRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol) {
super(protocol); super(protocol);
this.protocol = protocol; this.protocol = protocol;
} }
public void registerNamedSound(C packetType) { public void registerNamedSound(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() { protocol.registerClientbound(packetType, new PacketHandlers() {
@Override @Override
public void register() { public void register() {
@ -42,7 +45,7 @@ public class SoundRewriter<C extends ClientboundPacketType> extends com.viaversi
}); });
} }
public void registerStopSound(C packetType) { public void registerStopSound(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() { protocol.registerClientbound(packetType, new PacketHandlers() {
@Override @Override
public void register() { public void register() {
@ -53,8 +56,8 @@ public class SoundRewriter<C extends ClientboundPacketType> extends com.viaversi
public PacketHandler getNamedSoundHandler() { public PacketHandler getNamedSoundHandler() {
return wrapper -> { return wrapper -> {
String soundId = wrapper.get(Type.STRING, 0); final String soundId = wrapper.get(Type.STRING, 0);
String mappedId = protocol.getMappingData().getMappedNamedSound(soundId); final String mappedId = protocol.getMappingData().getMappedNamedSound(soundId);
if (mappedId == null) { if (mappedId == null) {
return; return;
} }
@ -69,15 +72,15 @@ public class SoundRewriter<C extends ClientboundPacketType> extends com.viaversi
public PacketHandler getStopSoundHandler() { public PacketHandler getStopSoundHandler() {
return wrapper -> { return wrapper -> {
byte flags = wrapper.passthrough(Type.BYTE); final byte flags = wrapper.passthrough(Type.BYTE);
if ((flags & 0x02) == 0) return; // No sound specified if ((flags & 0x02) == 0) return; // No sound specified
if ((flags & 0x01) != 0) { if ((flags & 0x01) != 0) {
wrapper.passthrough(Type.VAR_INT); // Source wrapper.passthrough(Type.VAR_INT); // Source
} }
String soundId = wrapper.read(Type.STRING); final String soundId = wrapper.read(Type.STRING);
String mappedId = protocol.getMappingData().getMappedNamedSound(soundId); final String mappedId = protocol.getMappingData().getMappedNamedSound(soundId);
if (mappedId == null) { if (mappedId == null) {
// No mapping found // No mapping found
wrapper.write(Type.STRING, soundId); wrapper.write(Type.STRING, soundId);
@ -94,38 +97,41 @@ public class SoundRewriter<C extends ClientboundPacketType> extends com.viaversi
} }
@Override @Override
public void register1_19_3Sound(C packetType) { public void register1_19_3Sound(final C packetType) {
protocol.registerClientbound(packetType, get1_19_3SoundHandler()); protocol.registerClientbound(packetType, get1_19_3SoundHandler());
} }
public PacketHandler get1_19_3SoundHandler() { public PacketHandler get1_19_3SoundHandler() {
return wrapper -> { return wrapper -> {
final int soundId = wrapper.read(Type.VAR_INT); Holder<SoundEvent> soundEventHolder = wrapper.read(Type.SOUND_EVENT);
if (soundId != 0) { if (soundEventHolder.isDirect()) {
final int mappedId = idRewriter.rewrite(soundId - 1); // Normalize the id wrapper.write(Type.SOUND_EVENT, rewriteSoundEvent(wrapper, soundEventHolder));
return;
}
final int mappedId = idRewriter.rewrite(soundEventHolder.id());
if (mappedId == -1) { if (mappedId == -1) {
wrapper.cancel(); wrapper.cancel();
return; return;
} }
wrapper.write(Type.VAR_INT, mappedId + 1); if (mappedId != soundEventHolder.id()) {
return; soundEventHolder = new Holder<>(mappedId);
} }
// Is followed by the resource loation wrapper.write(Type.SOUND_EVENT, soundEventHolder);
wrapper.write(Type.VAR_INT, 0);
String soundIdentifier = wrapper.read(Type.STRING);
final String mappedIdentifier = protocol.getMappingData().getMappedNamedSound(soundIdentifier);
if (mappedIdentifier != null) {
if (mappedIdentifier.isEmpty()) {
wrapper.cancel();
return;
}
soundIdentifier = mappedIdentifier;
}
wrapper.write(Type.STRING, soundIdentifier);
}; };
} }
public Holder<SoundEvent> rewriteSoundEvent(final PacketWrapper wrapper, final Holder<SoundEvent> soundEventHolder) {
final SoundEvent soundEvent = soundEventHolder.value();
final String mappedIdentifier = protocol.getMappingData().getMappedNamedSound(soundEvent.identifier());
if (mappedIdentifier != null) {
if (!mappedIdentifier.isEmpty()) {
return new Holder<>(soundEvent.withIdentifier(mappedIdentifier));
}
wrapper.cancel();
}
return soundEventHolder;
}
} }

Datei anzeigen

@ -30,9 +30,11 @@ import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.Nonce
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1; import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Holder;
import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature;
import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.RegistryType;
import com.viaversion.viaversion.api.minecraft.SoundEvent;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_3; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_3;
import com.viaversion.viaversion.api.minecraft.signature.SignableCommandArgumentsProvider; import com.viaversion.viaversion.api.minecraft.signature.SignableCommandArgumentsProvider;
import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata; import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata;
@ -349,9 +351,9 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
} }
private @Nullable String rewriteSound(final PacketWrapper wrapper) throws Exception { private @Nullable String rewriteSound(final PacketWrapper wrapper) throws Exception {
final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id final Holder<SoundEvent> holder = wrapper.read(Type.SOUND_EVENT);
if (soundId != -1) { if (holder.hasId()) {
final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); final int mappedId = MAPPINGS.getSoundMappings().getNewId(holder.id());
if (mappedId == -1) { if (mappedId == -1) {
wrapper.cancel(); wrapper.cancel();
return null; return null;
@ -362,8 +364,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
} }
// Convert the resource location to the corresponding integer id // Convert the resource location to the corresponding integer id
final String soundIdentifier = wrapper.read(Type.STRING); final String soundIdentifier = holder.value().identifier();
wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range
final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier);
if (mappedIdentifier == null) { if (mappedIdentifier == null) {
return soundIdentifier; return soundIdentifier;