Skip to content

Commit 1d5070a

Browse files
committed
Allow mapping sound event holders to empty sounds
1 parent f612adb commit 1d5070a

File tree

12 files changed

+50
-13
lines changed

12 files changed

+50
-13
lines changed

api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ public int getNewSoundId(final int id) {
208208
}
209209

210210
@Override
211-
public int getOldSoundId(final int i) {
212-
return soundMappings.getNewIdOrDefault(i, 0);
211+
public int getOldSoundId(final int id) {
212+
return soundMappings.inverse().getNewIdOrDefault(id, 0);
213213
}
214214

215215
@Override

api/src/main/java/com/viaversion/viaversion/api/minecraft/Holder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
2626
import java.util.function.Function;
27+
import java.util.function.Supplier;
2728

2829
public interface Holder<T> {
2930

@@ -89,7 +90,18 @@ static <T> Holder<T> of(final T value) {
8990
* @param rewriteFunction the function to rewrite the id
9091
* @return a new holder with the id rewritten, or self
9192
*/
92-
Holder<T> updateId(final Int2IntFunction rewriteFunction);
93+
default Holder<T> updateId(final Int2IntFunction rewriteFunction) {
94+
return this.updateId(rewriteFunction, null);
95+
}
96+
97+
/**
98+
* Returns a new holder with the id rewritten using the given function, or self if this is a direct holder or the id did not change.
99+
*
100+
* @param rewriteFunction the function to rewrite the id
101+
* @param ifMissing a supplier that provides a new holder if the id is missing (i.e. -1)
102+
* @return a new holder with the id rewritten, or self
103+
*/
104+
Holder<T> updateId(Int2IntFunction rewriteFunction, Supplier<Holder<T>> ifMissing);
93105

94106
/**
95107
* Returns a new holder with the value rewritten using the given function, or self if this is an id holder or the value did not change.

api/src/main/java/com/viaversion/viaversion/api/minecraft/IdHolder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.base.Preconditions;
2626
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
2727
import java.util.function.Function;
28+
import java.util.function.Supplier;
2829

2930
record IdHolder<T>(int id) implements Holder<T> {
3031

@@ -48,12 +49,15 @@ public T value() {
4849
}
4950

5051
@Override
51-
public Holder<T> updateId(final Int2IntFunction rewriteFunction) {
52+
public Holder<T> updateId(final Int2IntFunction rewriteFunction, final Supplier<Holder<T>> ifMissing) {
5253
final int rewrittenId = rewriteFunction.applyAsInt(id);
5354
if (rewrittenId == id) {
5455
return this;
5556
}
5657
if (rewrittenId == -1) {
58+
if (ifMissing != null) {
59+
return ifMissing.get();
60+
}
5761
throw new IllegalArgumentException("Received invalid id in updateId");
5862
}
5963
return Holder.of(rewrittenId);

api/src/main/java/com/viaversion/viaversion/api/minecraft/SoundEvent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@
2222
*/
2323
package com.viaversion.viaversion.api.minecraft;
2424

25+
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
2526
import org.checkerframework.checker.nullness.qual.Nullable;
2627

2728
public record SoundEvent(String identifier, @Nullable Float fixedRange) {
2829

30+
private static final Holder<SoundEvent> UNKNOWN = Holder.of(new SoundEvent("viaversion:unknown", 0F));
31+
2932
public SoundEvent withIdentifier(final String identifier) {
3033
return new SoundEvent(identifier, this.fixedRange);
3134
}
35+
36+
public static @Nullable Holder<SoundEvent> rewriteHolder(@Nullable final Holder<SoundEvent> holder, final Int2IntFunction soundRewriteFunction) {
37+
if (holder == null) {
38+
return null;
39+
}
40+
return holder.updateId(soundRewriteFunction, () -> UNKNOWN);
41+
}
3242
}

api/src/main/java/com/viaversion/viaversion/api/minecraft/ValueHolder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
2626
import java.util.function.Function;
27+
import java.util.function.Supplier;
2728

2829
record ValueHolder<T>(T value) implements Holder<T> {
2930

@@ -43,7 +44,7 @@ public int id() {
4344
}
4445

4546
@Override
46-
public Holder<T> updateId(final Int2IntFunction rewriteFunction) {
47+
public Holder<T> updateId(final Int2IntFunction rewriteFunction, final Supplier<Holder<T>> ifMissing) {
4748
return this;
4849
}
4950

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlocksAttacks.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
*/
2323
package com.viaversion.viaversion.api.minecraft.item.data;
2424

25+
import com.viaversion.viaversion.api.connection.UserConnection;
2526
import com.viaversion.viaversion.api.minecraft.Holder;
2627
import com.viaversion.viaversion.api.minecraft.HolderSet;
2728
import com.viaversion.viaversion.api.minecraft.SoundEvent;
29+
import com.viaversion.viaversion.api.protocol.Protocol;
2830
import com.viaversion.viaversion.api.type.Type;
2931
import com.viaversion.viaversion.api.type.Types;
3032
import com.viaversion.viaversion.api.type.types.ArrayType;
33+
import com.viaversion.viaversion.util.Rewritable;
3134
import io.netty.buffer.ByteBuf;
3235
import org.checkerframework.checker.nullness.qual.Nullable;
3336

@@ -39,7 +42,7 @@ public record BlocksAttacks(
3942
@Nullable String bypassedByTag,
4043
@Nullable Holder<SoundEvent> blockSound,
4144
@Nullable Holder<SoundEvent> disableSound
42-
) {
45+
) implements Rewritable {
4346

4447
public static final Type<BlocksAttacks> TYPE = new Type<>(BlocksAttacks.class) {
4548

@@ -67,6 +70,13 @@ public void write(final ByteBuf buffer, final BlocksAttacks value) {
6770
}
6871
};
6972

73+
@Override
74+
public BlocksAttacks rewrite(final UserConnection connection, final Protocol<?, ?, ?, ?> protocol, final boolean clientbound) {
75+
final Holder<SoundEvent> blockSound = SoundEvent.rewriteHolder(this.blockSound, Rewritable.soundRewriteFunction(protocol, clientbound));
76+
final Holder<SoundEvent> disableSound = SoundEvent.rewriteHolder(this.disableSound, Rewritable.soundRewriteFunction(protocol, clientbound));
77+
return new BlocksAttacks(this.blockDelaySeconds, this.disableCooldownScale, this.damageReductions, this.itemDamage, this.bypassedByTag, blockSound, disableSound);
78+
}
79+
7080
public record DamageReduction(float horizontalBlockingAngle, @Nullable HolderSet type, float base, float factor) {
7181

7282
public static final Type<DamageReduction> TYPE = new Type<>(DamageReduction.class) {

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Consumable1_21_2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void write(final ByteBuf buffer, final ApplyStatusEffects value) {
116116

117117
@Override
118118
public Consumable1_21_2 rewrite(final UserConnection connection, final Protocol<?, ?, ?, ?> protocol, final boolean clientbound) {
119-
final Holder<SoundEvent> soundHolder = this.sound.updateId(Rewritable.soundRewriteFunction(protocol, clientbound));
119+
final Holder<SoundEvent> soundHolder = SoundEvent.rewriteHolder(this.sound, Rewritable.soundRewriteFunction(protocol, clientbound));
120120
return soundHolder == this.sound ? this : new Consumable1_21_2(consumeSeconds, animationType, soundHolder, hasConsumeParticles, consumeEffects);
121121
}
122122

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Equippable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ public void write(final ByteBuf buffer, final Equippable value) {
137137

138138
@Override
139139
public Equippable rewrite(final UserConnection connection, final Protocol<?, ?, ?, ?> protocol, final boolean clientbound) {
140-
final Holder<SoundEvent> soundEvent = this.soundEvent.updateId(Rewritable.soundRewriteFunction(protocol, clientbound));
141-
final Holder<SoundEvent> shearingSound = this.shearingSound.updateId(Rewritable.soundRewriteFunction(protocol, clientbound));
140+
final Holder<SoundEvent> soundEvent = SoundEvent.rewriteHolder(this.soundEvent, Rewritable.soundRewriteFunction(protocol, clientbound));
141+
final Holder<SoundEvent> shearingSound = SoundEvent.rewriteHolder(this.shearingSound, Rewritable.soundRewriteFunction(protocol, clientbound));
142142
return soundEvent == this.soundEvent && shearingSound == this.shearingSound ? this : new Equippable(equipmentSlot, soundEvent, model, cameraOverlay, allowedEntities, dispensable, swappable, damageOnHurt, equipOnInteract, canBeSheared, shearingSound);
143143
}
144144
}

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_20_5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void writeDirect(final ByteBuf buffer, final Instrument1_20_5 value) {
5252

5353
@Override
5454
public Instrument1_20_5 rewrite(final UserConnection connection, final Protocol<?, ?, ?, ?> protocol, final boolean clientbound) {
55-
final Holder<SoundEvent> soundEvent = this.soundEvent.updateId(Rewritable.soundRewriteFunction(protocol, clientbound));
55+
final Holder<SoundEvent> soundEvent = SoundEvent.rewriteHolder(this.soundEvent, Rewritable.soundRewriteFunction(protocol, clientbound));
5656
return soundEvent == this.soundEvent ? this : new Instrument1_20_5(soundEvent, useDuration, range);
5757
}
5858
}

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_21_2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void writeDirect(final ByteBuf buffer, final Instrument1_21_2 value) {
5959

6060
@Override
6161
public Instrument1_21_2 rewrite(final UserConnection connection, final Protocol<?, ?, ?, ?> protocol, final boolean clientbound) {
62-
final Holder<SoundEvent> soundEvent = this.soundEvent.updateId(Rewritable.soundRewriteFunction(protocol, clientbound));
62+
final Holder<SoundEvent> soundEvent = SoundEvent.rewriteHolder(this.soundEvent, Rewritable.soundRewriteFunction(protocol, clientbound));
6363
return soundEvent == this.soundEvent ? this : new Instrument1_21_2(soundEvent, useDuration, range, description);
6464
}
6565

0 commit comments

Comments
 (0)