Skip to content

Commit

Permalink
Sounds
Browse files Browse the repository at this point in the history
- Add sound event asset generation
  • Loading branch information
Notenoughmail committed May 19, 2024
1 parent 153d694 commit cda4fb7
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public void defaultHandheldItemModel(ResourceLocation id) {
public void particle(ResourceLocation id, Consumer<ParticleGenerator> consumer) {
generator.particle(id, consumer);
}

public void sounds(String namespace, Consumer<SoundGenerator> consumer) {
generator.sounds(namespace, consumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public void generate(Map<ResourceLocation, GeneratedData> map) {

ClientEvents.HIGH_ASSETS.post(ScriptType.CLIENT, new GenerateClientAssetsEventJS(generator));

generator.buildSounds();

for (var lang : ClientEvents.LANG.findUniqueExtraIds(ScriptType.CLIENT)) {
var l = String.valueOf(lang);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void setFriction(float f) {
public void setColor(Color color, boolean alpha) {
setColor(color.getRgbJS());
if (alpha) {
// setAlpha(color.getArgbJS()); TODO: Eugh bitwise math
setAlpha((color.getArgbJS() >> 24) / 255.0F);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package dev.latvian.mods.kubejs.client;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class SoundGenerator {

private final Map<String, SoundEntry> entries = new HashMap<>();

public void addSound(String path, Consumer<SoundEntry> consumer) {
if (entries.containsKey(path)) {
consumer.accept(entries.get(path));
} else {
entries.put(path, Util.make(new SoundEntry(), consumer));
}
}

public JsonObject toJson() {
final JsonObject json = new JsonObject();
entries.forEach((path, entry) -> json.add(path, entry.toJson()));
return json;
}

public static class SoundEntry {

private boolean replace = false;
@Nullable
private String subtitle;
private final List<SoundInstance> sounds = new ArrayList<>();

public SoundEntry replace(boolean b) {
replace = b;
return this;
}

public SoundEntry subtitle(String subtitle) {
this.subtitle = subtitle;
return this;
}

public SoundEntry sounds(ResourceLocation... sounds) {
this.sounds.addAll(Stream.of(sounds).map(SoundInstance::new).toList());
return this;
}

public SoundEntry sound(ResourceLocation file, Consumer<SoundInstance> consumer) {
sounds.add(Util.make(new SoundInstance(file), consumer));
return this;
}

public JsonObject toJson() {
final JsonObject json = new JsonObject();
if (replace) {
json.addProperty("replace", true);
}
if (subtitle != null) {
json.addProperty("subtitle", subtitle);
}
if (!sounds.isEmpty()) {
final JsonArray array = new JsonArray(sounds.size());
sounds.forEach(instance -> array.add(instance.toJson()));
json.add("sounds" ,array);
}
return json;
}
}

public static class SoundInstance {

private final ResourceLocation fileLocation;
private boolean complex = false;
private float volume = 1.0F;
private float pitch = 1.0F;
private int weight = 1;
private boolean stream = false;
private int attenuationDistance = 16;
private boolean preload = false;
private boolean isEventReference = false;


public SoundInstance(ResourceLocation fileLocation) {
this.fileLocation = fileLocation;
}

private SoundInstance complex() {
complex = true;
return this;
}

public SoundInstance volume(float f) {
volume = Mth.clamp(f, 0.0F, 1.0F);
return complex();
}

public SoundInstance pitch(float f) {
pitch = Mth.clamp(f, 0.0F, 1.0F);
return complex();
}

public SoundInstance weight(int i) {
weight = i;
return complex();
}

public SoundInstance stream(boolean b) {
stream = b;
return complex();
}

public SoundInstance attenuationDistance(int i) {
attenuationDistance = i;
return complex();
}

public SoundInstance preload(boolean b) {
preload = b;
return complex();
}

public SoundInstance asReferenceToEvent() {
isEventReference = true;
return complex();
}

public JsonElement toJson() {
if (!complex) {
return new JsonPrimitive(fileLocation.toString());
}

final JsonObject json = new JsonObject();
json.addProperty("name", fileLocation.toString());
json.addProperty("volume", volume);
json.addProperty("pitch", pitch);
json.addProperty("weight", weight);
json.addProperty("stream", stream);
json.addProperty("attenuation_distance", attenuationDistance);
json.addProperty("preload", preload);
if (isEventReference) {
json.addProperty("type", "event");
}
return json;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS {
return Screen.hasAltDown();
}

// TODO: A different name may be better, or perhaps this should be in ClientLevelKJS
// PR Review: A different name may be better, or perhaps this should be in ClientLevelKJS
default KubeAnimatedParticle kjs$customParticle(SpriteSet spriteSet, double x, double y, double z) {
return kjs$customParticle(kjs$self().level, spriteSet, x, y, z);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.MultipartBlockStateGenerator;
import dev.latvian.mods.kubejs.client.ParticleGenerator;
import dev.latvian.mods.kubejs.client.SoundGenerator;
import dev.latvian.mods.kubejs.client.StencilTexture;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.script.data.GeneratedData;
Expand All @@ -22,10 +23,12 @@

public class AssetJsonGenerator extends ResourceGenerator {
private final Map<String, StencilTexture> stencils;
private final Map<String, SoundGenerator> sounds;

public AssetJsonGenerator(Map<ResourceLocation, GeneratedData> m) {
super(ConsoleJS.CLIENT, m);
this.stencils = new HashMap<>();
this.sounds = new HashMap<>();
}

public void blockState(ResourceLocation id, Consumer<VariantBlockStateGenerator> consumer) {
Expand Down Expand Up @@ -53,6 +56,14 @@ public void particle(ResourceLocation id, Consumer<ParticleGenerator> consumer)
json(new ResourceLocation(id.getNamespace(), "particles/" + id.getPath()), gen.toJson());
}

public void sounds(String mod, Consumer<SoundGenerator> consumer) {
if (sounds.containsKey(mod)) {
consumer.accept(sounds.get(mod));
} else {
sounds.put(mod, Util.make(new SoundGenerator(), consumer));
}
}

public static ResourceLocation asItemModelLocation(ResourceLocation id) {
return new ResourceLocation(id.getNamespace(), "models/item/" + id.getPath());
}
Expand Down Expand Up @@ -87,4 +98,8 @@ public void stencil(ResourceLocation target, String stencil, JsonObject colors)
add(new ResourceLocation(target.getNamespace(), "textures/" + target.getPath() + ".png.mcmeta"), () -> st1.mcmeta, false);
}
}

public void buildSounds() {
sounds.forEach((mod, gen) -> json(new ResourceLocation(mod, "sounds"), gen.toJson()));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package dev.latvian.mods.kubejs.misc;

import dev.latvian.mods.kubejs.client.SoundGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.registry.BuilderBase;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;

import java.util.function.Consumer;

public class SoundEventBuilder extends BuilderBase<SoundEvent> {

public transient Consumer<SoundGenerator.SoundEntry> gen;

public SoundEventBuilder(ResourceLocation i) {
super(i);
gen = e -> e.sounds(id);
}

public SoundEventBuilder sound(Consumer<SoundGenerator.SoundEntry> consumer) {
gen = consumer;
return this;
}

@Override
Expand All @@ -19,4 +32,9 @@ public final RegistryInfo getRegistryType() {
public SoundEvent createObject() {
return SoundEvent.createVariableRangeEvent(id);
}

@Override
public void generateAssetJsons(AssetJsonGenerator generator) {
generator.sounds(id.getNamespace(), g -> g.addSound(id.getPath(), gen));
}
}

0 comments on commit cda4fb7

Please sign in to comment.