Skip to content

Commit

Permalink
allow sound block override channel
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaIndustries committed Nov 28, 2024
1 parent 9196608 commit c27ac51
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ authors=HavenKing, ModFest
contributors=Chai, Sisby folk, ShiroJR, Superkat32, maximumpower55, CallMeEcho, quaternary, comp500, LemmaEOF, acikek, TheEpicBlock, SkyNotTheLimit, Patbox, AmyMialee
license=CC0-1.0
# Mod Version
baseVersion=1.7.3
baseVersion=1.7.4
# Branch Metadata
branch=1.21
tagBranch=1.21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SoundPlayerBlockEntity extends BlockEntity {
public float distance = 16;
public boolean relative = false;
public Vec3d soundPosition;
public boolean cancelOthers = false;

public PositionedSoundLoop nowPlaying = null;

Expand Down Expand Up @@ -133,12 +134,16 @@ public static void clientTick(World world, BlockPos pos, BlockState state, Sound
player,
entity.getPos()
);
LOGGER.info("done? " + (entity.nowPlaying == null ? "null" : entity.nowPlaying.isDone()));

// 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);
LOGGER.info("playing");
entity.nowPlaying = sound;
// LOGGER.info("playing");
if (sound.inRange()) {
if (entity.cancelOthers) mc.getSoundManager().stopSounds(null, entity.category);
mc.getSoundManager().play(sound);
entity.nowPlaying = sound;
}
}
}
}
Expand Down Expand Up @@ -184,12 +189,16 @@ public void setDone() {
@Override
public void tick() {
// stops track-stacking when reloading the block
if (!(this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance) ||
if (!inRange() ||
!(this.player.getWorld().getBlockEntity(this.soundBlockPos) instanceof SoundPlayerBlockEntity be && !this.isDifferentFrom(be.nowPlaying))) {
setDone();
}
}

public boolean inRange() {
return this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance;
}

// @Override
// public boolean canPlay() {
// return this.player.squaredDistanceTo(this.soundBlockPos.toCenterPos()) <= this.squaredDistance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SoundPlayerBlockEditScreen extends GlowcaseScreen {

private TextFieldWidget soundId;
private ButtonWidget categoryButton;
private ButtonWidget cancelOthersButton;

private TextFieldWidget volume;
private TextFieldWidget pitch;
Expand Down Expand Up @@ -52,6 +53,12 @@ protected void init() {
}).dimensions(7 * width / 10, 20, 2 * width / 10, 20).build();
this.addDrawableChild(this.categoryButton);

this.cancelOthersButton = new ButtonWidget.Builder(Text.of(Boolean.toString(soundBlock.cancelOthers)), (action) -> {
soundBlock.cancelOthers = !soundBlock.cancelOthers;
this.cancelOthersButton.setMessage(Text.of(Boolean.toString(soundBlock.cancelOthers)));
}).dimensions(7 * width / 10, 50, 2 * width / 10, 20).build();
this.addDrawableChild(this.cancelOthersButton);

this.volume = new TextFieldWidget(
this.client.textRenderer,
width / 10, 110,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public record SoundPlayerBlockEntityRenderer(BlockEntityRendererFactory.Context

@Override
public void render(SoundPlayerBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
//if (BlockEntityRenderUtil.shouldRenderPlaceholder(entity.getPos()))
if (BlockEntityRenderUtil.shouldRenderPlaceholder(entity.getPos()))
BlockEntityRenderUtil.renderPlaceholder(entity, ITEM_TEXTURE, 1.0F, matrices, vertexConsumers, context.getRenderDispatcher().camera);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public record C2SEditSoundBlock(SoundInfo soundInfo, PositionalInfo positionalIn

public static C2SEditSoundBlock of(SoundPlayerBlockEntity be) {
return new C2SEditSoundBlock(
new SoundInfo(be.soundId, be.category.toString(), be.volume, be.pitch, be.repeatDelay),
new SoundInfo(be.soundId, be.category.toString(), be.volume, be.pitch, be.repeatDelay, be.cancelOthers),
new PositionalInfo(be.distance, be.relative, be.soundPosition),
be.getPos()
);
Expand All @@ -49,6 +49,7 @@ public void receive(ServerWorld world, BlockEntity blockEntity) {
be.volume = soundInfo.volume;
be.pitch = soundInfo.pitch;
be.repeatDelay = soundInfo.repeatDelay;
be.cancelOthers = soundInfo.cancelOthers;

be.distance = positionalInfo.distance;
be.relative = positionalInfo.relative;
Expand All @@ -58,13 +59,14 @@ public void receive(ServerWorld world, BlockEntity blockEntity) {
be.dispatch();
}

public record SoundInfo(Identifier id, String category, float volume, float pitch, int repeatDelay) {
public record SoundInfo(Identifier id, String category, float volume, float pitch, int repeatDelay, boolean cancelOthers) {
public static final PacketCodec<RegistryByteBuf, SoundInfo> PACKET_CODEC = PacketCodec.tuple(
Identifier.PACKET_CODEC, SoundInfo::id,
PacketCodecs.STRING, SoundInfo::category,
PacketCodecs.FLOAT, SoundInfo::volume,
PacketCodecs.FLOAT, SoundInfo::pitch,
PacketCodecs.INTEGER, SoundInfo::repeatDelay,
PacketCodecs.BOOL, SoundInfo::cancelOthers,
SoundInfo::new
);
}
Expand Down

0 comments on commit c27ac51

Please sign in to comment.