Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParticleTypes and SoundEvents #836

Open
wants to merge 5 commits into
base: 2001
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.latvian.mods.kubejs.client.DebugInfoEventJS;
import dev.latvian.mods.kubejs.client.GenerateClientAssetsEventJS;
import dev.latvian.mods.kubejs.client.LangEventJS;
import dev.latvian.mods.kubejs.client.ParticleProviderRegistryEventJS;
import dev.latvian.mods.kubejs.client.painter.screen.PaintScreenEventJS;
import dev.latvian.mods.kubejs.event.EventGroup;
import dev.latvian.mods.kubejs.event.EventHandler;
Expand All @@ -25,4 +26,5 @@ public interface ClientEvents {
EventHandler PAINT_SCREEN = GROUP.client("paintScreen", () -> PaintScreenEventJS.class);
EventHandler ATLAS_SPRITE_REGISTRY = GROUP.client("atlasSpriteRegistry", () -> AtlasSpriteRegistryEventJS.class).extra(Extra.REQUIRES_ID);
EventHandler LANG = GROUP.client("lang", () -> LangEventJS.class).extra(Extra.REQUIRES_STRING);
EventHandler PARTICLE_PROVIDER_REGISTRY = GROUP.client("particleProviderRegistry", () -> ParticleProviderRegistryEventJS.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public void defaultHandheldItemModel(ResourceLocation id) {
model.texture("layer0", id.getNamespace() + ":item/" + id.getPath());
});
}

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
@@ -0,0 +1,101 @@
package dev.latvian.mods.kubejs.client;

import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.rhino.mod.util.color.Color;
import it.unimi.dsi.fastutil.floats.Float2IntFunction;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.SimpleAnimatedParticle;
import net.minecraft.client.particle.SpriteSet;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.RandomSource;

public class KubeAnimatedParticle extends SimpleAnimatedParticle {

private Float2IntFunction lightColorFunction;

public KubeAnimatedParticle(ClientLevel clientLevel, SpriteSet spriteSet, double x, double y, double z) {
super(clientLevel, x, y, z, spriteSet, 0.0125F);
setLifetime(20);
setSpriteFromAge(spriteSet);
lightColorFunction = super::getLightColor;
}

public void setGravity(float g) {
this.gravity = g;
}

@Info(value = "Sets the friction value of the particle, the particle's motion is multiplied by this value every tick")
public void setFriction(float f) {
this.friction = f;
}

public void setColor(Color color, boolean alpha) {
setColor(color.getRgbJS());
if (alpha) {
setAlpha((color.getArgbJS() >>> 24) / 255.0F);
}
}

public void setColor(Color color) {
setColor(color, false);
}

public void setPhysicality(boolean hasPhysics) {
this.hasPhysics = hasPhysics;
}

public void setFasterWhenYMotionBlocked(boolean b) {
speedUpWhenYMotionIsBlocked = b;
}

public void setLightColor(Float2IntFunction function) {
lightColorFunction = function;
}

// Getters for protected values

public ClientLevel getLevel() {
return level;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getZ() {
return z;
}

public double getXSpeed() {
return xd;
}

public double getYSpeed() {
return yd;
}

public double getZSpeed() {
return zd;
}

public SpriteSet getSpriteSet() {
return sprites;
}

public TextureAtlasSprite getSprite() {
return sprite;
}

public RandomSource getRandom() {
return random;
}

@Override
public int getLightColor(float f) {
return lightColorFunction.get(f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientLifecycleEvent;
import dev.architectury.event.events.client.ClientPlayerEvent;
import dev.architectury.event.events.client.ClientTooltipEvent;
import dev.architectury.hooks.client.screen.ScreenAccess;
Expand Down Expand Up @@ -57,6 +58,7 @@ public void init() {
ClientGuiEvent.RENDER_POST.register(Painter.INSTANCE::guiScreenDraw);
ClientGuiEvent.INIT_PRE.register(this::guiPreInit);
ClientGuiEvent.INIT_POST.register(this::guiPostInit);
ClientLifecycleEvent.CLIENT_STARTED.register(this::clientStart);
//ClientTextureStitchEvent.POST.register(this::postAtlasStitch);
}

Expand Down Expand Up @@ -170,6 +172,10 @@ private void guiPostInit(Screen screen, ScreenAccess access) {
}
}

private void clientStart(Minecraft mc) {
ClientEvents.PARTICLE_PROVIDER_REGISTRY.post(new ParticleProviderRegistryEventJS());
}

/*private void postAtlasStitch(TextureAtlas atlas) {
if (!ClientProperties.get().getExportAtlases()) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.latvian.mods.kubejs.client;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.List;

public class ParticleGenerator {

public transient List<String> textures = new ArrayList<>();

public ParticleGenerator texture(ResourceLocation location) {
textures.add(location.toString());
return this;
}

public ParticleGenerator textures(List<String> textures) {
this.textures = textures;
return this;
}

public JsonObject toJson() {
final JsonObject json = new JsonObject();
final JsonArray textures = new JsonArray(this.textures.size());
this.textures.forEach(textures::add);
json.add("textures", textures);
return json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.latvian.mods.kubejs.client;

import dev.architectury.registry.client.particle.ParticleProviderRegistry;
import dev.latvian.mods.kubejs.event.EventJS;
import net.minecraft.Util;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleProvider;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;

import java.util.function.Consumer;

public class ParticleProviderRegistryEventJS extends EventJS {

public <T extends ParticleOptions> void register(ParticleType<T> type, ParticleProvider<T> provider) {
ParticleProviderRegistry.register(type, provider);
}

public <T extends ParticleOptions> void registerSimple(ParticleType<T> type, Consumer<KubeAnimatedParticle> particle) {
ParticleProviderRegistry.register(type, set -> (t, level, x, y, z, dx, dy, dz) -> Util.make(Util.make(() -> {
var kube = new KubeAnimatedParticle(level, set, x, y, z);
kube.setParticleSpeed(dx, dy, dz);
return kube;
}), particle));
}

public <T extends ParticleOptions> void registerSimple(ParticleType<T> type) {
registerSimple(type, particle -> {});
}

@FunctionalInterface
public interface ParticleProvider<T extends ParticleOptions> extends ParticleProviderRegistry.DeferredParticleProvider<T> {
Particle create(T particleOptions, ClientLevel level, ParticleProviderRegistry.ExtendedSpriteSet spriteSet, double x, double y, double z, double xSpeed, double ySpeed, double zSpeed);

@Override
default net.minecraft.client.particle.ParticleProvider<T> create(ParticleProviderRegistry.ExtendedSpriteSet spriteSet) {
return (t, level, x, y, z, xSpeed, ySpeed, zSpeed) -> create(t, level, spriteSet, x, y, z, xSpeed, ySpeed, zSpeed);
}
}
}
Loading