SoundEventRegistrationSerializer .java

net.minecraft.client.resources.sounds.SoundEventRegistrationSerializer (class, 77 lines): members: DEFAULT_FLOAT, deserialize. strings: "Invalid volume".

net.minecraft.client.resources.soundsclass77 linesclient3.1 KB

Implements

API Surface (1)

SoundEventRegistration deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)

Fields & Constants (1)

DEFAULT_FLOAT

String literals (15)

"entry"
"replace"
"subtitle"
"sounds"
"sound"
"volume"
"Invalid volume"
"pitch"
"Invalid pitch"
"weight"
"Invalid weight"
"preload"
"stream"
"attenuation_distance"
"Invalid type"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

SoundEventRegistrationSerializer.java77 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.resources.sounds;
2
3
import com.google.common.collect.Lists;
4
import com.google.gson.JsonArray;
5
import com.google.gson.JsonDeserializationContext;
6
import com.google.gson.JsonDeserializer;
7
import com.google.gson.JsonElement;
8
import com.google.gson.JsonObject;
9
import com.google.gson.JsonParseException;
10
import java.lang.reflect.Type;
11
import java.util.List;
12
import java.util.Objects;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import net.minecraft.resources.Identifier;
16
import net.minecraft.util.GsonHelper;
17
import net.minecraft.util.valueproviders.ConstantFloat;
18
import net.minecraft.util.valueproviders.FloatProvider;
19
import org.apache.commons.lang3.Validate;
20
21
@Environment(EnvType.CLIENT)
22
public class SoundEventRegistrationSerializer implements JsonDeserializer<SoundEventRegistration> {
23
	private static final FloatProvider DEFAULT_FLOAT = ConstantFloat.of(1.0F);
24
25
	public SoundEventRegistration deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
26
		JsonObject object = GsonHelper.convertToJsonObject(json, "entry");
27
		boolean replace = GsonHelper.getAsBoolean(object, "replace", false);
28
		String subtitle = GsonHelper.getAsString(object, "subtitle", null);
29
		List<Sound> sounds = this.getSounds(object);
30
		return new SoundEventRegistration(sounds, replace, subtitle);
31
	}
32
33
	private List<Sound> getSounds(final JsonObject object) {
34
		List<Sound> result = Lists.newArrayList();
35
		if (object.has("sounds")) {
36
			JsonArray array = GsonHelper.getAsJsonArray(object, "sounds");
37
38
			for (int i = 0; i < array.size(); i++) {
39
				JsonElement element = array.get(i);
40
				if (GsonHelper.isStringValue(element)) {
41
					Identifier name = Identifier.parse(GsonHelper.convertToString(element, "sound"));
42
					result.add(new Sound(name, DEFAULT_FLOAT, DEFAULT_FLOAT, 1, Sound.Type.FILE, false, false, 16));
43
				} else {
44
					result.add(this.getSound(GsonHelper.convertToJsonObject(element, "sound")));
45
				}
46
			}
47
		}
48
49
		return result;
50
	}
51
52
	private Sound getSound(final JsonObject object) {
53
		Identifier name = Identifier.parse(GsonHelper.getAsString(object, "name"));
54
		Sound.Type type = this.getType(object, Sound.Type.FILE);
55
		float volume = GsonHelper.getAsFloat(object, "volume", 1.0F);
56
		Validate.isTrue(volume > 0.0F, "Invalid volume");
57
		float pitch = GsonHelper.getAsFloat(object, "pitch", 1.0F);
58
		Validate.isTrue(pitch > 0.0F, "Invalid pitch");
59
		int weight = GsonHelper.getAsInt(object, "weight", 1);
60
		Validate.isTrue(weight > 0, "Invalid weight");
61
		boolean preload = GsonHelper.getAsBoolean(object, "preload", false);
62
		boolean stream = GsonHelper.getAsBoolean(object, "stream", false);
63
		int attenuationDistance = GsonHelper.getAsInt(object, "attenuation_distance", 16);
64
		return new Sound(name, ConstantFloat.of(volume), ConstantFloat.of(pitch), weight, type, stream, preload, attenuationDistance);
65
	}
66
67
	private Sound.Type getType(final JsonObject sound, final Sound.Type fallback) {
68
		Sound.Type type = fallback;
69
		if (sound.has("type")) {
70
			type = Sound.Type.getByName(GsonHelper.getAsString(sound, "type"));
71
			Objects.requireNonNull(type, "Invalid type");
72
		}
73
74
		return type;
75
	}
76
}
77

Decompiled Minecraft 26.3 source of SoundEventRegistrationSerializer (net/minecraft/client/resources/sounds/SoundEventRegistrationSerializer.java) with Mojang official mappings, stored verbatim on the site.