Skip to content

Commit

Permalink
sound block fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
afamiliarquiet authored and HamaIndustries committed Nov 28, 2024
1 parent 9903c1b commit 3b6210a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 61 deletions.
22 changes: 19 additions & 3 deletions src/main/java/dev/hephaestus/glowcase/Glowcase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import dev.hephaestus.glowcase.block.*;
import dev.hephaestus.glowcase.block.entity.*;
import dev.hephaestus.glowcase.block.HyperlinkBlock;
import dev.hephaestus.glowcase.block.ItemAcceptorBlock;
import dev.hephaestus.glowcase.block.ItemDisplayBlock;
import dev.hephaestus.glowcase.block.OutlineBlock;
import dev.hephaestus.glowcase.block.ParticleDisplayBlock;
import dev.hephaestus.glowcase.block.PopupBlock;
import dev.hephaestus.glowcase.block.SoundPlayerBlock;
import dev.hephaestus.glowcase.block.SpriteBlock;
import dev.hephaestus.glowcase.block.TextBlock;
import dev.hephaestus.glowcase.block.entity.HyperlinkBlockEntity;
import dev.hephaestus.glowcase.block.entity.ItemAcceptorBlockEntity;
import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity;
import dev.hephaestus.glowcase.block.entity.OutlineBlockEntity;
import dev.hephaestus.glowcase.block.entity.ParticleDisplayBlockEntity;
import dev.hephaestus.glowcase.block.entity.PopupBlockEntity;
import dev.hephaestus.glowcase.block.entity.SoundPlayerBlockEntity;
import dev.hephaestus.glowcase.block.entity.SpriteBlockEntity;
import dev.hephaestus.glowcase.block.entity.TextBlockEntity;
import dev.hephaestus.glowcase.compat.PolydexCompatibility;
import dev.hephaestus.glowcase.item.LockItem;
import net.fabricmc.api.ModInitializer;
Expand Down Expand Up @@ -45,7 +61,7 @@ public class Glowcase implements ModInitializer {

public static final Supplier<SoundPlayerBlock> SOUND_BLOCK = registerBlock("sound_block", SoundPlayerBlock::new);
public static final Supplier<BlockItem> SOUND_BLOCK_ITEM = registerItem("sound_block", () -> new BlockItem(SOUND_BLOCK.get(), new Item.Settings()));
public static final Supplier<BlockEntityType<SoundPlayerBlockEntity>> SOUND_BLOCK_ENTITY = registerBlockEntity("particle_display", () -> BlockEntityType.Builder.create(SoundPlayerBlockEntity::new, SOUND_BLOCK.get()).build(null));
public static final Supplier<BlockEntityType<SoundPlayerBlockEntity>> SOUND_BLOCK_ENTITY = registerBlockEntity("sound_block", () -> BlockEntityType.Builder.create(SoundPlayerBlockEntity::new, SOUND_BLOCK.get()).build(null));

public static final Supplier<TextBlock> TEXT_BLOCK = registerBlock("text_block", TextBlock::new);
public static final Supplier<BlockItem> TEXT_BLOCK_ITEM = registerItem("text_block", () -> new BlockItem(TEXT_BLOCK.get(), new Item.Settings()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.sound.AbstractSoundInstance;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.TickableSoundInstance;
Expand Down Expand Up @@ -42,14 +41,11 @@ public class SoundPlayerBlockEntity extends BlockEntity {
public boolean relative = false;
public Vec3d soundPosition;

private AbstractSoundInstance nowPlaying = null;
public PositionedSoundLoop nowPlaying = null;

public SoundPlayerBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.SOUND_BLOCK_ENTITY.get(), pos, state);
this.soundPosition = pos.toCenterPos();
// this.soundX = pos.getX();
// this.soundY = pos.getY();
// this.soundZ = pos.getZ();
}

public void cycleCategory() {
Expand Down Expand Up @@ -128,62 +124,26 @@ public Packet<ClientPlayPacketListener> toUpdatePacket() {
public static void clientTick(World world, BlockPos pos, BlockState state, SoundPlayerBlockEntity entity) {
MinecraftClient mc = MinecraftClient.getInstance();
if (mc.player instanceof ClientPlayerEntity player) {
// if (entity.sound == null) return;
//
// RegistryWrapper.WrapperLookup lookup = world.getRegistryManager();
// RegistryKey<SoundEvent> key = RegistryKey.of(RegistryKeys.SOUND_EVENT, entity.sound);
//
// Optional<RegistryEntry.Reference<SoundEvent>> optionalSound =
// lookup.getWrapperOrThrow(RegistryKeys.SOUND_EVENT).getOptional(key);
// if (optionalSound.isEmpty()) return;
//
// player.playSoundToPlayer(optionalSound.get().value(), SoundCategory.BLOCKS, 1, 1);

// TODO: check if any of the other properties changed beyond id. need an equals check or something
if (entity.nowPlaying == null || !entity.nowPlaying.getId().equals(entity.soundId) || (entity.nowPlaying instanceof TickableSoundInstance tickable && tickable.isDone())) {
// AbstractSoundInstance sound = new PositionedSoundLoop(
// entity.sound, SoundCategory.BLOCKS,
// 1, 1,
// SoundInstance.createRandom(),
// true, 0,
// SoundInstance.AttenuationType.LINEAR,
// pos.getX(), pos.getY(), pos.getZ(),
// false
// );
// change xyz to 000 when switching to relative (and back to blockpos for absolute)
// AbstractSoundInstance sound = new PositionedSoundLoop(
// entity.soundId, SoundCategory.BLOCKS,
// 1, 1,
// 0,
// 16,
// false,
// pos.getX(), pos.getY(), pos.getZ(),
// player
// );
AbstractSoundInstance sound = new PositionedSoundLoop(
entity.soundId, entity.category,
entity.volume, entity.pitch, entity.repeatDelay,
entity.distance,
entity.relative,
entity.soundPosition,
player,
entity.getPos()
);

LOGGER.info("playing sound");
// WeightedSoundSet sounds = mc.getSoundManager().get(entity.sound);
// if (sounds != null) {
// sounds.getSound(SoundInstance.createRandom());
//
// }
PositionedSoundLoop sound = new PositionedSoundLoop(
entity.soundId, entity.category,
entity.volume, entity.pitch, entity.repeatDelay,
entity.distance,
entity.relative,
entity.soundPosition,
player,
entity.getPos()
);
LOGGER.info("done? " + (entity.nowPlaying == null ? "null" : entity.nowPlaying.isDone()));
if (entity.nowPlaying == null || entity.nowPlaying.isDone() || entity.nowPlaying.isDifferentFrom(sound)) {
mc.getSoundManager().stop(entity.nowPlaying);
mc.getSoundManager().play(sound);
//if (mc.player.clientWorld.isPosLoaded(pos))
LOGGER.info("playing");
entity.nowPlaying = sound;
}
}
}

// I don't think the repeat is necessary on this at this point
public static class PositionedSoundLoop extends PositionedSoundInstance implements TickableSoundInstance {
private final PlayerEntity player;
private final BlockPos soundBlockPos;
Expand Down Expand Up @@ -224,14 +184,31 @@ public void setDone() {
@Override
public void tick() {
// stops track-stacking when reloading the block
if (!canPlay()) {
if (!(this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance) ||
!(this.player.getWorld().getBlockEntity(this.soundBlockPos) instanceof SoundPlayerBlockEntity be && !this.isDifferentFrom(be.nowPlaying))) {
setDone();
}
}

@Override
public boolean canPlay() {
return this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance;
// @Override
// public boolean canPlay() {
// return this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance;
// }

public boolean isDifferentFrom(PositionedSoundLoop other) {
return !(
other != null &&
this.id.equals(other.id) &&
this.category.equals(other.category) &&
this.volume == other.volume &&
this.pitch == other.pitch &&
this.repeatDelay == other.repeatDelay &&
this.squaredDistance == other.squaredDistance &&
this.relative == other.relative &&
this.x == other.x &&
this.y == other.y &&
this.z == other.z
);
}
}
}

0 comments on commit 3b6210a

Please sign in to comment.