Skip to content

Commit

Permalink
Update random sound emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Feb 23, 2022
1 parent f867ffa commit 2919216
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ loader_version=0.12.12
fabric_version=0.45.2+1.18
satin_version=1.7.2

mod_version = 4.1.2
mod_version = 4.2.0
maven_group = net.ludocrypt
archives_base_name = limlib
22 changes: 8 additions & 14 deletions src/main/java/net/ludocrypt/limlib/api/LimLibClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.ChunkStatus;

public class LimLibClient implements ClientModInitializer {

Expand All @@ -34,18 +31,15 @@ public void onInitializeClient() {
ClientTickEvents.START_WORLD_TICK.register((world) -> {
MinecraftClient client = MinecraftClient.getInstance();
ClientPlayerEntity player = client.player;
Iterable<BlockPos.Mutable> iterable = BlockPos.iterateInSquare(player.getBlockPos(), 8, Direction.DOWN, Direction.NORTH);
for (BlockPos.Mutable mutable : iterable) {
BlockPos pos = mutable.toImmutable();
Iterable<BlockPos> iterable = BlockPos.iterateOutwards(player.getBlockPos(), 8, 8, 8);
for (BlockPos pos : iterable) {
if (pos.isWithinDistance(player.getBlockPos(), 16)) {
if (world.getChunk(pos).getStatus().isAtLeast(ChunkStatus.FULL)) {
BlockState state = world.getBlockState(pos);
if (state != null) {
if (state.getBlock()instanceof RandomSoundEmitter rse) {
if (world.getRandom().nextDouble() < rse.getChance(world, state)) {
Vec3d relativePos = rse.getRelativePos(world, state);
world.playSound(pos.getX() + relativePos.getX(), pos.getY() + relativePos.getY(), pos.getZ() + relativePos.getZ(), rse.getSound(world, state), SoundCategory.AMBIENT, rse.getVolume(world, state), rse.getPitch(world, state), true);
}
BlockState state = world.getBlockState(pos);
if (state != null) {
if (state.getBlock()instanceof RandomSoundEmitter rse) {
if (world.getRandom().nextDouble() < rse.getChance(world, state, pos)) {
Vec3d relativePos = rse.getRelativePos(world, state, pos);
world.playSound(pos.getX() + relativePos.getX(), pos.getY() + relativePos.getY(), pos.getZ() + relativePos.getZ(), rse.getSound(world, state, pos), rse.getSoundCategory(world, state, pos), rse.getVolume(world, state, pos), rse.getPitch(world, state, pos), true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package net.ludocrypt.limlib.api.sound;

import net.minecraft.block.BlockState;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public interface RandomSoundEmitter {

public SoundEvent getSound(World world, BlockState state);
public SoundEvent getSound(World world, BlockState state, BlockPos pos);

public Vec3d getRelativePos(World world, BlockState state);
public Vec3d getRelativePos(World world, BlockState state, BlockPos pos);

public double getChance(World world, BlockState state);
public double getChance(World world, BlockState state, BlockPos pos);

public float getVolume(World world, BlockState state);
public float getVolume(World world, BlockState state, BlockPos pos);

public float getPitch(World world, BlockState state);
public float getPitch(World world, BlockState state, BlockPos pos);

public SoundCategory getSoundCategory(World world, BlockState state, BlockPos pos);

}

0 comments on commit 2919216

Please sign in to comment.