Skip to content

Commit 24f6c4f

Browse files
committed
Implement hashing for potion contents
1 parent 9409c7d commit 24f6c4f

File tree

10 files changed

+266
-1
lines changed

10 files changed

+266
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2025 ViaVersion and contributors
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
package com.viaversion.viaversion.api.minecraft.codec;
24+
25+
import com.viaversion.viaversion.api.type.Type;
26+
import java.util.function.Consumer;
27+
28+
public class DelegatingOps implements Ops {
29+
30+
private final Ops delegate;
31+
32+
public DelegatingOps(final Ops delegate) {
33+
this.delegate = delegate;
34+
}
35+
36+
@Override
37+
public CodecContext context() {
38+
return delegate.context();
39+
}
40+
41+
@Override
42+
public void writeByte(final byte b) {
43+
delegate.writeByte(b);
44+
}
45+
46+
@Override
47+
public void writeBytes(final byte[] array) {
48+
delegate.writeBytes(array);
49+
}
50+
51+
@Override
52+
public void writeBoolean(final boolean b) {
53+
delegate.writeBoolean(b);
54+
}
55+
56+
@Override
57+
public void writeShort(final short s) {
58+
delegate.writeShort(s);
59+
}
60+
61+
@Override
62+
public void writeString(final CharSequence sequence) {
63+
delegate.writeString(sequence);
64+
}
65+
66+
@Override
67+
public void writeInt(final int i) {
68+
delegate.writeInt(i);
69+
}
70+
71+
@Override
72+
public void writeLong(final long l) {
73+
delegate.writeLong(l);
74+
}
75+
76+
@Override
77+
public void writeFloat(final float f) {
78+
delegate.writeFloat(f);
79+
}
80+
81+
@Override
82+
public void writeDouble(final double d) {
83+
delegate.writeDouble(d);
84+
}
85+
86+
@Override
87+
public void writeInts(final int[] array) {
88+
delegate.writeInts(array);
89+
}
90+
91+
@Override
92+
public void writeLongs(final long[] array) {
93+
delegate.writeLongs(array);
94+
}
95+
96+
@Override
97+
public void writeList(final Consumer<ListSerializer> consumer) {
98+
delegate.writeList(consumer);
99+
}
100+
101+
@Override
102+
public void writeMap(final Consumer<MapSerializer> consumer) {
103+
delegate.writeMap(consumer);
104+
}
105+
106+
@Override
107+
public <V> void write(final Type<V> type, final V value) {
108+
delegate.write(type, value);
109+
}
110+
}

api/src/main/java/com/viaversion/viaversion/api/minecraft/codec/Ops.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,7 @@ default <V> MapSerializer writeOptional(final String key, final Type<V> type, @N
129129
MapSerializer writeList(String key, Consumer<ListSerializer> consumer);
130130

131131
MapSerializer writeMap(String key, Consumer<MapSerializer> consumer);
132+
133+
<T> MapSerializer writeInlinedMap(Type<T> valueType, T value);
132134
}
133135
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2025 ViaVersion and contributors
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
package com.viaversion.viaversion.api.minecraft.codec;
24+
25+
import com.viaversion.viaversion.api.type.Type;
26+
import java.util.function.Consumer;
27+
28+
public class ThrowingOps implements Ops {
29+
30+
@Override
31+
public CodecContext context() {
32+
throw new UnsupportedOperationException();
33+
}
34+
35+
@Override
36+
public void writeByte(final byte b) {
37+
throw new UnsupportedOperationException();
38+
}
39+
40+
@Override
41+
public void writeBytes(final byte[] array) {
42+
throw new UnsupportedOperationException();
43+
}
44+
45+
@Override
46+
public void writeBoolean(final boolean b) {
47+
throw new UnsupportedOperationException();
48+
}
49+
50+
@Override
51+
public void writeShort(final short s) {
52+
throw new UnsupportedOperationException();
53+
}
54+
55+
@Override
56+
public void writeString(final CharSequence sequence) {
57+
throw new UnsupportedOperationException();
58+
}
59+
60+
@Override
61+
public void writeInt(final int i) {
62+
throw new UnsupportedOperationException();
63+
}
64+
65+
@Override
66+
public void writeLong(final long l) {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
@Override
71+
public void writeFloat(final float f) {
72+
throw new UnsupportedOperationException();
73+
}
74+
75+
@Override
76+
public void writeDouble(final double d) {
77+
throw new UnsupportedOperationException();
78+
}
79+
80+
@Override
81+
public void writeInts(final int[] array) {
82+
throw new UnsupportedOperationException();
83+
}
84+
85+
@Override
86+
public void writeLongs(final long[] array) {
87+
throw new UnsupportedOperationException();
88+
}
89+
90+
@Override
91+
public void writeList(final Consumer<ListSerializer> consumer) {
92+
throw new UnsupportedOperationException();
93+
}
94+
95+
@Override
96+
public void writeMap(final Consumer<MapSerializer> consumer) {
97+
throw new UnsupportedOperationException();
98+
}
99+
100+
@Override
101+
public <V> void write(final Type<V> type, final V value) {
102+
throw new UnsupportedOperationException();
103+
}
104+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ public final class EnumTypes {
4848
public static final FakeEnumType RABBIT_VARIANT = new FakeEnumType(List.of("brown", "white", "black", "white_splotched", "gold", "salt"), of(99, "evil"));
4949
// Pretty much enums, but with a resource location
5050
public static final RegistryValueType VILLAGER_TYPE = new RegistryValueType("desert", "jungle", "plains", "savanna", "snow", "swamp", "taiga");
51+
public static final RegistryValueType POTION = new RegistryValueType("water", "mundane", "thick", "awkward", "night_vision", "long_night_vision", "invisibility", "long_invisibility", "leaping", "long_leaping", "strong_leaping", "fire_resistance", "long_fire_resistance", "swiftness", "long_swiftness", "strong_swiftness", "slowness", "long_slowness", "strong_slowness", "turtle_master", "long_turtle_master", "strong_turtle_master", "water_breathing", "long_water_breathing", "healing", "strong_healing", "harming", "strong_harming", "poison", "long_poison", "strong_poison", "regeneration", "long_regeneration", "strong_regeneration", "strength", "long_strength", "strong_strength", "weakness", "long_weakness", "luck", "slow_falling", "long_slow_falling", "wind_charged", "weaving", "oozing", "infested");
52+
public static final RegistryValueType MOB_EFFECT = new RegistryValueType("speed", "slowness", "haste", "mining_fatigue", "strength", "instant_health", "instant_damage", "jump_boost", "nausea", "regeneration", "resistance", "fire_resistance", "water_breathing", "invisibility", "blindness", "night_vision", "hunger", "weakness", "poison", "wither", "health_boost", "absorption", "saturation", "glowing", "levitation", "luck", "unluck", "slow_falling", "conduit_power", "dolphins_grace", "bad_omen", "hero_of_the_village", "darkness", "trial_omen", "raid_omen", "wind_charged", "weaving", "oozing", "infested");
5153
}

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

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

25+
import com.viaversion.viaversion.api.minecraft.codec.Ops;
2526
import com.viaversion.viaversion.api.type.Type;
2627
import com.viaversion.viaversion.api.type.Types;
2728
import com.viaversion.viaversion.util.Copyable;
@@ -85,6 +86,15 @@ public void write(final ByteBuf buffer, final PotionContents value) {
8586
PotionEffect.ARRAY_TYPE.write(buffer, value.customEffects);
8687
Types.OPTIONAL_STRING.write(buffer, value.customName);
8788
}
89+
90+
@Override
91+
public void write(final Ops ops, final PotionContents value) {
92+
ops.writeMap(map -> map
93+
.writeOptional("potion", EnumTypes.POTION, value.potion)
94+
.writeOptional("custom_color", Types.INT, value.customColor)
95+
.writeOptional("custom_effects", PotionEffect.ARRAY_TYPE, value.customEffects, new PotionEffect[0])
96+
.writeOptional("custom_name", Types.STRING, value.customName));
97+
}
8898
};
8999

90100
@Override

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package com.viaversion.viaversion.api.minecraft.item.data;
2424

25+
import com.viaversion.viaversion.api.minecraft.codec.Ops;
2526
import com.viaversion.viaversion.api.type.Type;
2627
import com.viaversion.viaversion.api.type.Types;
2728
import com.viaversion.viaversion.api.type.types.ArrayType;
@@ -42,6 +43,13 @@ public void write(final ByteBuf buffer, final PotionEffect value) {
4243
Types.VAR_INT.writePrimitive(buffer, value.effect);
4344
PotionEffectData.TYPE.write(buffer, value.effectData);
4445
}
46+
47+
@Override
48+
public void write(final Ops ops, final PotionEffect value) {
49+
ops.writeMap(map -> map
50+
.write("id", EnumTypes.MOB_EFFECT, value.effect)
51+
.writeInlinedMap(PotionEffectData.TYPE, value.effectData));
52+
}
4553
};
4654
public static final Type<PotionEffect[]> ARRAY_TYPE = new ArrayType<>(TYPE);
4755

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package com.viaversion.viaversion.api.minecraft.item.data;
2424

25+
import com.viaversion.viaversion.api.minecraft.codec.Ops;
2526
import com.viaversion.viaversion.api.type.OptionalType;
2627
import com.viaversion.viaversion.api.type.Type;
2728
import com.viaversion.viaversion.api.type.Types;
@@ -52,6 +53,17 @@ public void write(final ByteBuf buffer, final PotionEffectData value) {
5253
buffer.writeBoolean(value.showIcon);
5354
OPTIONAL_TYPE.write(buffer, value.hiddenEffect);
5455
}
56+
57+
@Override
58+
public void write(final Ops ops, final PotionEffectData value) {
59+
ops.writeMap(map -> map
60+
.writeOptional("amplifier", Types.UNSIGNED_BYTE, (short) value.amplifier, (short) 0)
61+
.writeOptional("duration", Types.INT, value.duration, 0)
62+
.writeOptional("ambient", Types.BOOLEAN, value.ambient, false)
63+
.writeOptional("show_particles", Types.BOOLEAN, value.showParticles, true)
64+
.writeOptional("show_icon", Types.BOOLEAN, value.showIcon)
65+
.writeOptional("hidden_effect", TYPE, value.hiddenEffect));
66+
}
5567
};
5668
public static final Type<PotionEffectData> OPTIONAL_TYPE = new OptionalType<>(TYPE) {
5769
};

api/src/main/java/com/viaversion/viaversion/api/type/types/RegistryValueType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public RegistryValueType(final String... names) {
4141
public void write(final Ops ops, final Integer value) {
4242
Types.STRING.write(ops, names[value]);
4343
}
44+
45+
public String[] names() {
46+
return names;
47+
}
4448
}

common/src/main/java/com/viaversion/viaversion/codec/CodecRegistryContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public record CodecRegistryContext(Protocol<?, ?, ?, ?> protocol, SerializerVers
3535
StructuredDataKey.TRIM1_21_5, StructuredDataKey.TOOL1_21_5, StructuredDataKey.PROVIDES_TRIM_MATERIAL,
3636
StructuredDataKey.CONSUMABLE1_21_2, StructuredDataKey.JUKEBOX_PLAYABLE1_21_5, StructuredDataKey.INSTRUMENT1_21_5,
3737
StructuredDataKey.EQUIPPABLE1_21_5, StructuredDataKey.REPAIRABLE, StructuredDataKey.DEATH_PROTECTION,
38-
StructuredDataKey.BLOCKS_ATTACKS, StructuredDataKey.SUSPICIOUS_STEW_EFFECTS, StructuredDataKey.POTION_CONTENTS1_21_2,
38+
StructuredDataKey.BLOCKS_ATTACKS, StructuredDataKey.SUSPICIOUS_STEW_EFFECTS,
3939
StructuredDataKey.BANNER_PATTERNS, StructuredDataKey.POT_DECORATIONS, StructuredDataKey.BREAK_SOUND,
4040
StructuredDataKey.WOLF_VARIANT, StructuredDataKey.WOLF_SOUND_VARIANT, StructuredDataKey.PIG_VARIANT,
4141
StructuredDataKey.COW_VARIANT, StructuredDataKey.CHICKEN_VARIANT, StructuredDataKey.FROG_VARIANT,

common/src/main/java/com/viaversion/viaversion/codec/hash/HashOps.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.viaversion.viaversion.codec.hash;
1919

2020
import com.viaversion.viaversion.api.minecraft.codec.CodecContext;
21+
import com.viaversion.viaversion.api.minecraft.codec.ThrowingOps;
2122
import com.viaversion.viaversion.api.minecraft.codec.hash.Hasher;
2223
import com.viaversion.viaversion.api.type.Type;
2324
import com.viaversion.viaversion.api.type.Types;
@@ -221,6 +222,18 @@ public MapSerializer writeMap(final String key, final Consumer<MapSerializer> co
221222
return this;
222223
}
223224

225+
@Override
226+
public <T> MapSerializer writeInlinedMap(final Type<T> valueType, final T value) {
227+
// Only allow writeMap calls here
228+
valueType.write(new ThrowingOps() {
229+
@Override
230+
public void writeMap(final Consumer<MapSerializer> consumer) {
231+
consumer.accept(MapHashBuilder.this);
232+
}
233+
}, value);
234+
return this;
235+
}
236+
224237
public void applyHashToParent() {
225238
if (entries.isEmpty()) {
226239
hashBuilder.writeBytesDirect(EMPTY_MAP);

0 commit comments

Comments
 (0)