diff --git a/src/main/java/net/lizistired/cavedust/CaveDust.java b/src/main/java/net/lizistired/cavedust/CaveDust.java index 5e52ed4..515b725 100644 --- a/src/main/java/net/lizistired/cavedust/CaveDust.java +++ b/src/main/java/net/lizistired/cavedust/CaveDust.java @@ -1,7 +1,12 @@ package net.lizistired.cavedust; //minecraft imports +import net.lizistired.cavedust.mixin.ClientWorldAccessor; import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Util; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; //other imports import com.minelittlepony.common.util.GamePaths; @@ -13,6 +18,9 @@ import org.slf4j.LoggerFactory; import java.nio.file.Path; //static imports import static net.lizistired.cavedust.utils.MathHelper.*; +import static net.lizistired.cavedust.utils.MathHelper.generateRandomDouble; +import static net.lizistired.cavedust.utils.ParticleSpawnUtil.shouldParticlesSpawn; +import static net.lizistired.cavedust.utils.KeybindingHelper.*; public class CaveDust implements ClientModInitializer { @@ -32,36 +40,53 @@ public class CaveDust implements ClientModInitializer { return config; } + @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(); + //register end client tick to create cave dust function, using end client tick for async ClientTickEvents.END_CLIENT_TICK.register(this::createCaveDust); } private void createCaveDust(MinecraftClient client) { + if (keyBinding1.wasPressed()){ + getConfig().toggleCaveDust(); + LOGGER.info("Toggled dust"); + client.player.sendSystemMessage(new TranslatableText("debug.cavedust.toggle." + config.getCaveDustEnabled()), Util.NIL_UUID); + } + if (keyBinding2.wasPressed()){ + getConfig().load(); + LOGGER.info("Reloaded config"); + client.player.sendSystemMessage(new TranslatableText("debug.cavedust.reload"), Util.NIL_UUID); + } + //ensure world is not null if (client.world == null) return; World world = client.world; + //LOGGER.info(String.valueOf(((ClientWorldAccessor) client.world.getLevelProperties()).getFlatWorld())); + // ) + double probabilityNormalized = normalize(config.getLowerLimit(), config.getUpperLimit(), client.player.getBlockY()); - if (shouldParticlesSpawn(client, config)) { - double probabilityNormalized = normalize(config.getLowerLimit(), config.getUpperLimit(), client.player.getBlockY()); + for (int i = 0; i < probabilityNormalized * config.getParticleMultiplier() * 10; i++) { + try { + double x = client.player.getPos().getX() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxX()); + double y = client.player.getPos().getY() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxY()); + double z = client.player.getPos().getZ() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxZ()); + BlockPos particlePos = new BlockPos(x, y, z); - for (int i = 0; i < probabilityNormalized * config.getParticleMultiplier(); i++) { - try { - double x = client.player.getPos().getX() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxX()); - double y = client.player.getPos().getY() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxY()); - double z = client.player.getPos().getZ() + generateRandomDouble(config.getDimensionsMinX(), config.getDimensionsMaxZ()); + if (!shouldParticlesSpawn(client, config, particlePos)){return;} - world.addParticle(config.getParticle(), x, y, z, 0, 0, 0);} - catch (NullPointerException e) { - LOGGER.error(String.valueOf(e)); - getConfig().setParticle("minecraft:white_ash"); - } + world.addParticle(config.getParticle(), x, y, z, config.getVelocityRandomnessRandom(), config.getVelocityRandomnessRandom(), config.getVelocityRandomnessRandom()); + } + catch (NullPointerException e) { + LOGGER.error(String.valueOf(e)); + getConfig().setParticle("minecraft:white_ash"); } } } diff --git a/src/main/java/net/lizistired/cavedust/CaveDustConfig.java b/src/main/java/net/lizistired/cavedust/CaveDustConfig.java index ceea361..b480042 100644 --- a/src/main/java/net/lizistired/cavedust/CaveDustConfig.java +++ b/src/main/java/net/lizistired/cavedust/CaveDustConfig.java @@ -6,6 +6,7 @@ import net.minecraft.particle.ParticleTypes; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import static net.lizistired.cavedust.CaveDust.*; +import static net.lizistired.cavedust.utils.MathHelper.*; import java.nio.file.Path; @@ -19,13 +20,16 @@ public class CaveDustConfig extends JsonFile { private int dimensionMinX = -5; private int dimensionMinY = -5; private int dimensionMinZ = -5; + private int velocityRandomness = 1; private boolean caveDustEnabled = true; private String particleName = "white_ash"; private boolean seaLevelCheck = true; + private boolean superFlatStatus = false; + //private boolean enhancedDetection = true; private float upperLimit = 64; private float lowerLimit = -64; - private float particleMultiplier = 1; + private int particleMultiplier = 1; public CaveDustConfig(Path file, CaveDust caveDust) { super(file); @@ -136,12 +140,12 @@ public class CaveDustConfig extends JsonFile { return lowerLimit; } - public float getParticleMultiplier(){ + public int getParticleMultiplier(){ return particleMultiplier; } public float setParticleMultiplier(float particleMultiplier){ - this.particleMultiplier = particleMultiplier; + this.particleMultiplier = (int) particleMultiplier; save(); return getParticleMultiplier(); } @@ -183,6 +187,41 @@ public class CaveDustConfig extends JsonFile { return getSeaLevelCheck(); } + public float getVelocityRandomnessRandom(){ + if (velocityRandomness == 0) {return 0;} + return (float) generateRandomDouble(-velocityRandomness, velocityRandomness); + } + + public float getVelocityRandomness(){ + return velocityRandomness; + } + + public float setVelocityRandomness(float velocityRandomness){ + this.velocityRandomness = (int) velocityRandomness; + save(); + return getVelocityRandomness(); + } + + public boolean getSuperFlatStatus(){ + return superFlatStatus; + } + + public boolean setSuperFlatStatus(){ + superFlatStatus = !superFlatStatus; + save(); + return getSuperFlatStatus(); + } + + /*public boolean getEnhancedDetection(){ + return enhancedDetection; + } + + public boolean setEnhancedDetection(){ + enhancedDetection = !enhancedDetection; + save(); + return getEnhancedDetection(); + }*/ + public void resetConfig(){ dimensionMinX = -5; dimensionMinY = -5; diff --git a/src/main/java/net/lizistired/cavedust/CaveDustModMenuFactory.java b/src/main/java/net/lizistired/cavedust/CaveDustModMenuFactory.java index 1fac9b0..333c6f0 100644 --- a/src/main/java/net/lizistired/cavedust/CaveDustModMenuFactory.java +++ b/src/main/java/net/lizistired/cavedust/CaveDustModMenuFactory.java @@ -6,6 +6,6 @@ import com.terraformersmc.modmenu.api.ModMenuApi; public class CaveDustModMenuFactory implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { - return net.lizistired.cavedust.ModMenuScreen::new; + return ModMenuConfigScreen::new; } } diff --git a/src/main/java/net/lizistired/cavedust/ModMenuScreen.java b/src/main/java/net/lizistired/cavedust/ModMenuConfigScreen.java similarity index 75% rename from src/main/java/net/lizistired/cavedust/ModMenuScreen.java rename to src/main/java/net/lizistired/cavedust/ModMenuConfigScreen.java index 0618b4d..e2cc247 100644 --- a/src/main/java/net/lizistired/cavedust/ModMenuScreen.java +++ b/src/main/java/net/lizistired/cavedust/ModMenuConfigScreen.java @@ -9,8 +9,8 @@ import net.minecraft.text.TranslatableText; import javax.annotation.Nullable; -public class ModMenuScreen extends GameGui { - public ModMenuScreen(@Nullable Screen parent) { +public class ModMenuConfigScreen extends GameGui { + public ModMenuConfigScreen(@Nullable Screen parent) { super(new TranslatableText("menu.cavedust.title"), parent); } @@ -32,6 +32,18 @@ public class ModMenuScreen extends GameGui { .setText("menu.cavedust.global." + config.getCaveDustEnabled()) .setTooltip(new TranslatableText("menu.cavedust.global.tooltip." + config.getCaveDustEnabled())); + /*addButton(new Button(left, row += 24).onClick(sender -> { + sender.getStyle().setText("menu.cavedust.enhanceddetection." + config.setEnhancedDetection()).setTooltip(new TranslatableText("menu.cavedust.enhanceddetection.tooltip")); + })).getStyle() + .setText("menu.cavedust.enhanceddetection." + config.getEnhancedDetection()) + .setTooltip(new TranslatableText("menu.cavedust.enhanceddetection.tooltip"));*/ + + addButton(new Button(left, row += 24).onClick(sender -> { + sender.getStyle().setText("menu.cavedust.superflatstatus." + config.setSuperFlatStatus()).setTooltip(new TranslatableText("menu.cavedust.superflatstatus.tooltip")); + })).getStyle() + .setText("menu.cavedust.superflatstatus." + config.getSuperFlatStatus()) + .setTooltip(new TranslatableText("menu.cavedust.superflatstatus.tooltip")); + addButton(new Slider(left += -110, row += 24, -50, 0, config.getDimensionsMinX())) .onChange(config::setDimensionsMinX) .setTextFormat(transText::formatMinX) @@ -72,18 +84,23 @@ public class ModMenuScreen extends GameGui { .setTextFormat(transText::formatLowerLimit) .getStyle().setTooltip(new TranslatableText("menu.cavedust.lowerlimit.tooltip")); - addButton(new Slider(left, row += 24, 0, 10, config.getParticleMultiplier())) + addButton(new Slider(left, row += 24, 1, 100, config.getParticleMultiplier())) .onChange(config::setParticleMultiplier) .setTextFormat(transText::formatParticleMultiplier) .getStyle().setTooltip(new TranslatableText("menu.cavedust.particlemultiplier.tooltip")); + addButton(new Slider(left, row += 24, 0, 10, config.getVelocityRandomness())) + .onChange(config::setVelocityRandomness) + .setTextFormat(transText::formatVelocityRandomness) + .getStyle().setTooltip(new TranslatableText("menu.cavedust.velocityrandomness.tooltip")); + addButton(new Button(left, row += 24).onClick(sender -> { config.resetConfig(); finish(); })).getStyle().setText(new TranslatableText("menu.cavedust.reset")).setTooltip(new TranslatableText("menu.cavedust.reset.tooltip")); - addButton(new Button(left, row += 180) + addButton(new Button(left, row += 60) .onClick(sender -> finish())).getStyle() .setText("gui.done"); } diff --git a/src/main/java/net/lizistired/cavedust/mixin/ClientWorldAccessor.java b/src/main/java/net/lizistired/cavedust/mixin/ClientWorldAccessor.java new file mode 100644 index 0000000..e550ad4 --- /dev/null +++ b/src/main/java/net/lizistired/cavedust/mixin/ClientWorldAccessor.java @@ -0,0 +1,12 @@ +package net.lizistired.cavedust.mixin; + +import net.minecraft.client.world.ClientWorld; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(ClientWorld.Properties.class) +public interface ClientWorldAccessor { + @Accessor + boolean getFlatWorld(); +} + diff --git a/src/main/java/net/lizistired/cavedust/mixin/MixinDebugScreenOverlay.java b/src/main/java/net/lizistired/cavedust/mixin/MixinDebugScreenOverlay.java index eabaa8c..710c970 100644 --- a/src/main/java/net/lizistired/cavedust/mixin/MixinDebugScreenOverlay.java +++ b/src/main/java/net/lizistired/cavedust/mixin/MixinDebugScreenOverlay.java @@ -1,8 +1,10 @@ package net.lizistired.cavedust.mixin; +import net.lizistired.cavedust.CaveDustConfig; import net.minecraft.client.gui.hud.DebugHud; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -13,6 +15,7 @@ import java.lang.management.ManagementFactory; import java.util.List; import java.util.Objects; import static net.lizistired.cavedust.utils.MathHelper.*; +import static net.lizistired.cavedust.utils.ParticleSpawnUtil.shouldParticlesSpawn; @Mixin(DebugHud.class) public abstract class MixinDebugScreenOverlay { diff --git a/src/main/java/net/lizistired/cavedust/utils/KeybindingHelper.java b/src/main/java/net/lizistired/cavedust/utils/KeybindingHelper.java new file mode 100644 index 0000000..543b62b --- /dev/null +++ b/src/main/java/net/lizistired/cavedust/utils/KeybindingHelper.java @@ -0,0 +1,29 @@ +package net.lizistired.cavedust.utils; + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.StickyKeyBinding; +import net.minecraft.client.util.InputUtil; +import org.lwjgl.glfw.GLFW; + +public class KeybindingHelper { + + public static KeyBinding keyBinding1; + public static KeyBinding keyBinding2; + + + public static void registerKeyBindings(){ + keyBinding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding( + "key.cavedust.toggle", + InputUtil.Type.KEYSYM,// The translation key of the keybinding's name // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. + GLFW.GLFW_KEY_KP_ADD, // The keycode of the key + "category.cavedust.spook" // The translation key of the keybinding's category. + )); + keyBinding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding( + "key.cavedust.reload", // The translation key of the keybinding's name + InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. + GLFW.GLFW_KEY_KP_ENTER, // The keycode of the key + "category.cavedust.spook" // The translation key of the keybinding's category. + )); + } +} diff --git a/src/main/java/net/lizistired/cavedust/utils/ParticleSpawnUtil.java b/src/main/java/net/lizistired/cavedust/utils/ParticleSpawnUtil.java index 6b69762..5e6f529 100644 --- a/src/main/java/net/lizistired/cavedust/utils/ParticleSpawnUtil.java +++ b/src/main/java/net/lizistired/cavedust/utils/ParticleSpawnUtil.java @@ -1,7 +1,9 @@ package net.lizistired.cavedust.utils; import net.lizistired.cavedust.CaveDustConfig; +import net.lizistired.cavedust.mixin.ClientWorldAccessor; import net.minecraft.client.MinecraftClient; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.Objects; @@ -12,6 +14,13 @@ import static net.minecraft.world.biome.BiomeKeys.LUSH_CAVES; public class ParticleSpawnUtil { private static float timer; public static boolean shouldParticlesSpawn; + + /** + * Returns true if particles should spawn. + * @param client MinecraftClient + * @param config CaveDustConfig + * @return boolean + */ public static boolean shouldParticlesSpawn(MinecraftClient client, CaveDustConfig config) { //checks if the config is enabled, if the game isn't paused, if the world is valid, if the particle is valid and if the player isn't in a lush caves biome @@ -19,7 +28,8 @@ public class ParticleSpawnUtil { || client.isPaused() || client.world == null || !client.world.getDimension().isBedWorking() - || Objects.requireNonNull(client.player).isSubmergedInWater()) + || Objects.requireNonNull(client.player).isSubmergedInWater() + || Objects.equals(client.world.getBiomeKey(Objects.requireNonNull(client.player.getBlockPos())), Optional.of(LUSH_CAVES))) { timer = 0; shouldParticlesSpawn = false; @@ -30,7 +40,51 @@ public class ParticleSpawnUtil { int seaLevel = world.getSeaLevel(); if (!client.player.clientWorld.isSkyVisible(client.player.getBlockPos())) { - if (client.player.getBlockPos().getY() < seaLevel){ + if (client.player.getBlockPos().getY() + 2 < seaLevel){ + timer = timer + 1; + if (timer > 10){ + timer = 10; + shouldParticlesSpawn = true; + return true; + } + } + } + shouldParticlesSpawn = false; + return false; + } + + /** + * Returns true if particles should spawn (uses particle position instead of player). + * @param client MinecraftClient + * @param config CaveDustConfig + * @param pos BlockPos + * @return boolean + */ + public static boolean shouldParticlesSpawn(MinecraftClient client, CaveDustConfig config, BlockPos pos) { + + //checks if the config is enabled, if the game isn't paused, if the world is valid, if the particle is valid and if the player isn't in a lush caves biome + if (!config.getCaveDustEnabled() + || client.isPaused() + || client.world == null + || !client.world.getDimension().isBedWorking() + || Objects.requireNonNull(client.player).isSubmergedInWater() + || Objects.equals(client.world.getBiomeKey(Objects.requireNonNull(pos)), Optional.of(LUSH_CAVES))) + { + timer = 0; + shouldParticlesSpawn = false; + return false; + } + if(!config.getSuperFlatStatus()) { + if (((ClientWorldAccessor) client.world.getLevelProperties()).getFlatWorld()) { + return false; + } + } + + World world = client.world; + int seaLevel = world.getSeaLevel(); + + if (!client.player.clientWorld.isSkyVisible(pos)) { + if (pos.getY() + 2 < seaLevel){ timer = timer + 1; if (timer > 10){ timer = 10; diff --git a/src/main/java/net/lizistired/cavedust/utils/TranslatableTextHelper.java b/src/main/java/net/lizistired/cavedust/utils/TranslatableTextHelper.java index 81f0f95..6b02d17 100644 --- a/src/main/java/net/lizistired/cavedust/utils/TranslatableTextHelper.java +++ b/src/main/java/net/lizistired/cavedust/utils/TranslatableTextHelper.java @@ -32,4 +32,7 @@ public class TranslatableTextHelper { public Text formatParticleMultiplier(AbstractSlider slider) { return new TranslatableText("menu.cavedust.particlemultiplier", (int)Math.floor(slider.getValue())); } + public Text formatVelocityRandomness(AbstractSlider slider) { + return new TranslatableText("menu.cavedust.velocityrandomness", (int)Math.floor(slider.getValue())); + } } diff --git a/src/main/java/net/lizistired/cavedust/utils/WindowFocusHelper.java b/src/main/java/net/lizistired/cavedust/utils/WindowFocusHelper.java index 7509746..86ba5d6 100644 --- a/src/main/java/net/lizistired/cavedust/utils/WindowFocusHelper.java +++ b/src/main/java/net/lizistired/cavedust/utils/WindowFocusHelper.java @@ -1,4 +1,4 @@ -package net.lizistired.caveDust.utils; +package net.lizistired.cavedust.utils; import net.minecraft.client.MinecraftClient; diff --git a/src/main/resources/assets/modid/lang/en_us.json b/src/main/resources/assets/modid/lang/en_us.json index 49f7e50..64b10ce 100644 --- a/src/main/resources/assets/modid/lang/en_us.json +++ b/src/main/resources/assets/modid/lang/en_us.json @@ -23,5 +23,22 @@ "menu.cavedust.reset": "Reset settings", "menu.cavedust.reset.tooltip": "Are you sure you want to reset all settings?", "menu.cavedust.particlemultiplier": "Particle multiplier: %s", - "menu.cavedust.particlemultiplier.tooltip": "Multiplies the amount of particles at any given depth." + "menu.cavedust.particlemultiplier.tooltip": "Multiplies the amount of particles at any given depth.", + "menu.cavedust.velocityrandomness": "Velocity randomness: %s", + "menu.cavedust.velocityrandomness.tooltip": "The randomness of the velocity of the particles.", + "menu.cavedust.enhanceddetection.true": "Enhanced detection: Enabled", + "menu.cavedust.enhanceddetection.false": "Enhanced detection: Disabled", + "menu.cavedust.enhanceddetection.tooltip": "Enhanced detection enables more accurate checks, using the particles position\n instead of the player, has some performance impact.", + "menu.cavedust.superflatstatus.true": "Superflat particles: Enabled", + "menu.cavedust.superflatstatus.false": "Superflat particles: Disabled", + "menu.cavedust.superflatstatus.tooltip": "Should particles spawn on superflat worlds?", + + "key.cavedust.reload": "Reload Config", + "key.cavedust.toggle": "Toggle Particles", + "category.cavedust.spook": "Cave Dust", + + "debug.cavedust.toggle.true": "(Cave Dust) Enabled particles", + "debug.cavedust.toggle.false": "(Cave Dust) Disabled particles", + "debug.cavedust.reload": "(Cave Dust) Reloaded config" + } \ No newline at end of file diff --git a/src/main/resources/dust.mixins.json b/src/main/resources/dust.mixins.json index bfe0ac1..923f4b3 100644 --- a/src/main/resources/dust.mixins.json +++ b/src/main/resources/dust.mixins.json @@ -4,6 +4,7 @@ "package": "net.lizistired.cavedust.mixin", "compatibilityLevel": "JAVA_16", "mixins": [ + "ClientWorldAccessor" ], "client": [ ],