Skip to content

Commit

Permalink
add cave dust particle
Browse files Browse the repository at this point in the history
calculate position inside blocks
changed menu
changed modid to cavedust
change some default values
fix bug where user got softlocked in the particle cycle in multiplayer
  • Loading branch information
LizIsTired committed May 17, 2024
1 parent 39cf0a0 commit 4e1a122
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 76 deletions.
21 changes: 13 additions & 8 deletions src/main/java/net/lizistired/cavedust/CaveDust.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.lizistired.cavedust;

//minecraft imports
import net.minecraft.block.Blocks;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -40,17 +39,20 @@ public net.lizistired.cavedust.CaveDustConfig getConfig() {
return config;
}

public static int WHITE_ASH_ID = Registries.PARTICLE_TYPE.getRawId(ParticleTypes.WHITE_ASH);
public static int WHITE_ASH_ID = Registries.PARTICLE_TYPE.getRawId(CaveDustServer.CAVE_DUST);
public static int PARTICLE_AMOUNT = 0;




@Override
public void onInitializeClient() {
//config path and loading
Path CaveDustFolder = GamePaths.getConfigDirectory().resolve("cavedust");
config = new CaveDustConfig(CaveDustFolder.getParent().resolve("cavedust.json"), this);
config.load();
registerKeyBindings();
ParticleFactoryRegistry.getInstance().register(CaveDustServer.CAVE_DUST, CaveDustParticleFactory.Factory::new);

//register end client tick to create cave dust function, using end client tick for async
ClientTickEvents.END_CLIENT_TICK.register(this::createCaveDust);
Expand Down Expand Up @@ -78,20 +80,23 @@ private void createCaveDust(MinecraftClient client) {

for (int i = 0; i < PARTICLE_AMOUNT; i++) {
try {
int x = (int) (client.player.getPos().getX() + (int) generateRandomDouble(config.getDimensionsX() * -1, config.getDimensionsX()));
int y = (int) (client.player.getPos().getY() + (int) generateRandomDouble(config.getDimensionsY() * -1, config.getDimensionsY()));
int z = (int) (client.player.getPos().getZ() + (int) generateRandomDouble(config.getDimensionsZ() * -1, config.getDimensionsZ()));
int x = (int) (client.player.getPos().getX() + (int) generateRandomDouble(config.getDimensionWidth() *-1, config.getDimensionWidth()));
int y = (int) (client.player.getEyePos().getY() + (int) generateRandomDouble(config.getDimensionHeight() *-1, config.getDimensionHeight()));
int z = (int) (client.player.getPos().getZ() + (int) generateRandomDouble(config.getDimensionWidth() *-1, config.getDimensionWidth()));
double miniX = (x + Math.random());
double miniY = (y + Math.random());
double miniZ = (z + Math.random());
BlockPos particlePos = new BlockPos(x, y, z);

if (shouldParticlesSpawn(client, config, particlePos)) {
if (client.world.getBlockState(particlePos).isAir()) {
world.addParticle(config.getParticle(), x, y, z, config.getVelocityRandomnessRandom(), config.getVelocityRandomnessRandom(), config.getVelocityRandomnessRandom());
world.addParticle(config.getParticle(), miniX, miniY, miniZ, config.getVelocityRandomnessRandom() * 0.01, config.getVelocityRandomnessRandom() * 0.01, config.getVelocityRandomnessRandom() * 0.01);
}
}
}
catch (NullPointerException e) {
LOGGER.error(String.valueOf(e));
getConfig().setParticleID(WHITE_ASH_ID);
//getConfig().setParticleID(WHITE_ASH_ID);
}
}
}
Expand Down
56 changes: 22 additions & 34 deletions src/main/java/net/lizistired/cavedust/CaveDustConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public class CaveDustConfig extends JsonFile {
private transient final net.lizistired.cavedust.CaveDust CaveDust;


private int dimensionX = 5;
private int dimensionY = 5;
private int dimensionZ = 5;
private int velocityRandomness = 1;
private int width = 10;
private int height = 10;
private int velocityRandomness = 0;

private boolean caveDustEnabled = true;
private boolean seaLevelCheck = true;
Expand All @@ -37,40 +36,28 @@ public CaveDustConfig(Path file, net.lizistired.cavedust.CaveDust caveDust) {
this.CaveDust = caveDust;
}

public float setDimensionsX(float size){
if (this.dimensionX != size) {
this.dimensionX = (int)size;
public float setDimensionWidth(float size){
if (this.width != size) {
this.width = (int)size;
save();
}
return getDimensionsX();
return getDimensionWidth();
}

public float setDimensionsY(float size){
if (this.dimensionY != size) {
this.dimensionY = (int)size;
public float setDimensionHeight(float size){
if (this.height != size) {
this.height = (int)size;
save();
}
return getDimensionsY();
return getDimensionHeight();
}

public float setDimensionsZ(float size){
if (this.dimensionZ != size) {
this.dimensionZ = (int)size;
save();
}
return getDimensionsZ();
}

public float getDimensionsX(){
return dimensionX;
public float getDimensionWidth(){
return width;
}

public float getDimensionsY(){
return dimensionY;
}

public float getDimensionsZ(){
return dimensionZ;
public float getDimensionHeight(){
return height;
}

public float setUpperLimit(float upperLimit){
Expand Down Expand Up @@ -145,10 +132,11 @@ public ParticleEffect getParticle(){
try {
return (ParticleEffect) Registries.PARTICLE_TYPE.get(new Identifier(Registries.PARTICLE_TYPE.getEntry(getParticleID()).get().getKey().get().getValue().toString().toLowerCase()));
} catch (ClassCastException e) {
MinecraftClient.getInstance().player.sendMessage(Text.literal("Issue loading particle, defaulting to white ash particle!"), false);
setParticleID(WHITE_ASH_ID);
MinecraftClient.getInstance().player.sendMessage(Text.translatable("debug.cavedust.particleerror"), true);
LOGGER.error("Cannot spawn particle, check config.");
iterateParticle();
save();
return ParticleTypes.WHITE_ASH;
return getParticle();
}
}

Expand Down Expand Up @@ -210,15 +198,15 @@ public int getParticleID(){
}

public void resetConfig(){
dimensionX = 5;
dimensionY = 5;
dimensionZ = 5;
width = 10;
height = 10;

upperLimit = 64;
lowerLimit = -64;

particleMultiplier = 1;
particleMultiplierMultiplier = 10;
velocityRandomness = 0;

seaLevelCheck = true;
caveDustEnabled = true;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/net/lizistired/cavedust/CaveDustParticleFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.lizistired.cavedust;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.particle.*;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.DefaultParticleType;

public class CaveDustParticleFactory extends SpriteBillboardParticle {
private final SpriteProvider spriteProvider;
CaveDustParticleFactory(ClientWorld clientWorld, double x, double y, double z, double velocityX, double velocityY, double velocityZ, SpriteProvider spriteProvider) {
super(clientWorld, x, y, z);
this.spriteProvider = spriteProvider; //Sets the sprite provider from above to the sprite provider in the constructor method
this.maxAge = 200; //20 ticks = 1 second
this.scale = 0.1f;
this.velocityX = velocityX; //The velX from the constructor parameters
this.velocityY = -0.007f; //Allows the particle to slowly fall
this.velocityZ = velocityZ;
this.x = x; //The x from the constructor parameters
this.y = y;
this.z = z;
this.collidesWithWorld = true;
this.alpha = 1.0f; //Setting the alpha to 1.0f means there will be no opacity change until the alpha value is changed
this.setSpriteForAge(spriteProvider); //Required
}

@Override
public void tick() {
super.tick();
if(this.alpha < 0.0f){
this.markDead();
}
this.alpha -= 0.005f;
}

@Override
public ParticleTextureSheet getType() {
return ParticleTextureSheet.PARTICLE_SHEET_TRANSLUCENT;
}

@Environment(EnvType.CLIENT)
public static class Factory implements ParticleFactory<DefaultParticleType> {
private final SpriteProvider spriteProvider;

public Factory(SpriteProvider spriteProvider) {
this.spriteProvider = spriteProvider;
}


public Particle createParticle(DefaultParticleType type, ClientWorld world, double x, double y, double z, double velocityX, double velocityY, double velocityZ) {
return new CaveDustParticleFactory(world, x, y, z, velocityX, velocityY, velocityZ, this.spriteProvider);
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/lizistired/cavedust/CaveDustServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.lizistired.cavedust;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.particle.v1.FabricParticleTypes;
import net.minecraft.particle.DefaultParticleType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class CaveDustServer implements ModInitializer {
public static final DefaultParticleType CAVE_DUST = FabricParticleTypes.simple();
/**
* Runs the mod initializer.
*/
@Override
public void onInitialize() {
Registry.register(Registries.PARTICLE_TYPE, new Identifier("cavedust", "cave_dust"), CAVE_DUST);
}
}
34 changes: 17 additions & 17 deletions src/main/java/net/lizistired/cavedust/ModMenuConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import net.lizistired.cavedust.utils.TranslatableTextHelper;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.NoSuchElementException;

public class ModMenuConfigScreen extends GameGui {
public ModMenuConfigScreen(@Nullable Screen parent) {
super(Text.translatable("menu.cavedust.title"), parent);
Expand Down Expand Up @@ -73,30 +73,26 @@ public void init() {
})).getStyle().setText("Particle: " + (getNameOfParticle()))
.setTooltip(Text.translatable("menu.cavedust.particle.tooltip"));

addButton(new Slider(left += 220, row -= 96, 1, 50, config.getDimensionsX()))
.onChange(config::setDimensionsX)
.setTextFormat(transText::formatMaxX)
.getStyle().setTooltip(Text.translatable("menu.cavedust.X.tooltip"));

addButton(new Slider(left, row += 24, 1, 50, config.getDimensionsY()))
.onChange(config::setDimensionsY)
.setTextFormat(transText::formatMaxY)
.getStyle().setTooltip(Text.translatable("menu.cavedust.Y.tooltip"));
addButton(new Slider(left += 220, row -= 96, 1, 50, config.getDimensionWidth()))
.onChange(config::setDimensionWidth)
.setTextFormat(transText::formatMaxWidth)
.getStyle().setTooltip(Text.translatable("menu.cavedust.width.tooltip"));

addButton(new Slider(left, row += 24, 1, 50, config.getDimensionsZ()))
.onChange(config::setDimensionsZ)
.setTextFormat(transText::formatMaxZ)
.getStyle().setTooltip(Text.translatable("menu.cavedust.Z.tooltip"));
addButton(new Slider(left, row += 24, 1, 50, config.getDimensionHeight()))
.onChange(config::setDimensionHeight)
.setTextFormat(transText::formatMaxHeight)
.getStyle().setTooltip(Text.translatable("menu.cavedust.height.tooltip"));

addButton(new Slider(left, row += 24, 0, 10, config.getVelocityRandomness()))
.onChange(config::setVelocityRandomness)
.setTextFormat(transText::formatVelocityRandomness)
.getStyle().setTooltip(Text.translatable("menu.cavedust.velocityrandomness.tooltip"));


addButton(new Button(left -= 110, row += 60).onClick(sender -> {
addButton(new Button(left -= 110, row += 120).onClick(sender -> {
config.resetConfig();
finish();
client.setScreen(new ModMenuConfigScreen(parent));
})).getStyle().setText(Text.translatable("menu.cavedust.reset")).setTooltip(Text.translatable("menu.cavedust.reset.tooltip"));

addButton(new Button(left, row += 24)
Expand All @@ -114,6 +110,10 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic
private String getNameOfParticle(){
CaveDustConfig config = CaveDust.getInstance().getConfig();
config.load();
return Registries.PARTICLE_TYPE.getEntry(config.getParticleID()).get().getKey().get().getValue().toString();
try {
return Registries.PARTICLE_TYPE.getEntry(config.getParticleID()).get().getKey().get().getValue().toString();
} catch (NoSuchElementException e){
return "null";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Mixin(DebugHud.class)
public abstract class MixinDebugScreenOverlay {
@Inject(method = "getRightText", at = @At("RETURN"))
private void appendShaderPackText(CallbackInfoReturnable<List<String>> cir) {
private void appendDebugText(CallbackInfoReturnable<List<String>> cir) {
List<String> messages = cir.getReturnValue();

messages.add("");
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/lizistired/cavedust/utils/MathHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static int generateRandomInt(int min, int max) {
* @return Random number (double)
*/
public static double generateRandomDouble(double min, double max) {
return ThreadLocalRandom.current().nextDouble(min, max);
try {
return ThreadLocalRandom.current().nextDouble(min, max);
} catch (IllegalArgumentException e) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ParticleSpawnUtil {
private static float timer;
public static boolean shouldParticlesSpawn;


/**
* Returns true if particles should spawn.
* @param client MinecraftClient
Expand Down Expand Up @@ -67,6 +68,7 @@ public static boolean shouldParticlesSpawn(MinecraftClient client, CaveDustConfi
|| client.world == null
|| !client.world.getDimension().bedWorks()
|| (client.world.getBottomY() > pos.getY())
//|| client.world.getBiome(Objects.requireNonNull(pos)).matchesKey(LUSH_CAVES))
|| client.world.getBiome(Objects.requireNonNull(pos)).matchesKey(LUSH_CAVES))

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import javax.swing.*;

public class TranslatableTextHelper {
public Text formatMaxX(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.X", (int)Math.floor(slider.getValue()));
public Text formatMaxWidth(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.width", (int)Math.floor(slider.getValue()));
}
public Text formatMaxY(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.Y", (int)Math.floor(slider.getValue()));
}
public Text formatMaxZ(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.Z", (int)Math.floor(slider.getValue()));
public Text formatMaxHeight(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.height", (int)Math.floor(slider.getValue()));
}
public Text formatUpperLimit(AbstractSlider<Float> slider) {
return Text.translatable("menu.cavedust.upperlimit", (int)Math.floor(slider.getValue()));
Expand Down
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"menu.cavedust.global.true": "Cave Dust: Enabled",
"menu.cavedust.global.tooltip.false": "Enable cave dust particles?",
"menu.cavedust.global.tooltip.true": "Disable cave dust particles?",
"menu.cavedust.X": "X bounds: %s",
"menu.cavedust.Y": "Y bounds: %s",
"menu.cavedust.Z": "Z bounds: %s",
"menu.cavedust.X.tooltip": "X axis bounds for particle spawning.",
"menu.cavedust.Y.tooltip": "Y axis bounds for particle spawning.",
"menu.cavedust.Z.tooltip": "Z axis bounds for particle spawning.",
"menu.cavedust.width": "Width bounds: %s",
"menu.cavedust.height": "Height bounds: %s",
"menu.cavedust.width.tooltip": "Maximum width to spawn particle.",
"menu.cavedust.height.tooltip": "Maximum height to spawn particle.",
"menu.cavedust.upperlimit": "Upper limit height: %s",
"menu.cavedust.lowerlimit": "Lower limit height: %s",
"menu.cavedust.upperlimit.tooltip": "The height where particles will fade out and stop spawning (uses player y).",
Expand Down Expand Up @@ -37,6 +35,7 @@

"debug.cavedust.toggle.true": "(Cave Dust) Enabled particles",
"debug.cavedust.toggle.false": "(Cave Dust) Disabled particles",
"debug.cavedust.reload": "(Cave Dust) Reloaded config"
"debug.cavedust.reload": "(Cave Dust) Reloaded config",
"debug.cavedust.particleerror": "(Cave Dust) Error setting particle, skipping to next particle!"

}
5 changes: 5 additions & 0 deletions src/main/resources/assets/cavedust/particles/cave_dust.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"textures": [
"minecraft:generic_0"
]
}
Loading

0 comments on commit 4e1a122

Please sign in to comment.