From 1f4d87ccc4c44c6440b3c5f19f9af27770365cb1 Mon Sep 17 00:00:00 2001 From: Aeronica Date: Wed, 3 Jan 2024 12:07:03 -0600 Subject: [PATCH] Warn player when Music Block has no instruments containing Sheet Music. --- .../resources/assets/mxtune/lang/en_us.json | 1 + .../mods/mxtune/blocks/MusicBlock.java | 8 ++++---- .../mxtune/datagen/MXTuneLanguageProvider.java | 1 + .../mods/mxtune/managers/PlayManager.java | 18 +++++++++++------- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/generated/resources/assets/mxtune/lang/en_us.json b/src/generated/resources/assets/mxtune/lang/en_us.json index 6685e93c..d8de5c0b 100644 --- a/src/generated/resources/assets/mxtune/lang/en_us.json +++ b/src/generated/resources/assets/mxtune/lang/en_us.json @@ -32,6 +32,7 @@ "enum.mxtune.tool_state.type.start": "Start", "errors.mxtune.midi_system_util.no_sound_bank_loaded": "No SoundBank Loaded", "errors.mxtune.mml_server_side_validation_failure": "Server side music validation error", + "errors.mxtune.sheet_music_not_present": "No instrument(s) with sheet music present in Music Block!", "errors.mxtune.sheet_music_too_old": "The Sheet Music is unreadable!", "errors.mxtune.sheet_music_write_failure": "Unable to write Sheet Music!", "gui.mxtune.button.back_rs_in.disabled": "Input Disabled", diff --git a/src/main/java/aeronicamc/mods/mxtune/blocks/MusicBlock.java b/src/main/java/aeronicamc/mods/mxtune/blocks/MusicBlock.java index 4691cef2..4891f092 100644 --- a/src/main/java/aeronicamc/mods/mxtune/blocks/MusicBlock.java +++ b/src/main/java/aeronicamc/mods/mxtune/blocks/MusicBlock.java @@ -157,7 +157,7 @@ public ActionResultType use(BlockState state, World worldIn, BlockPos pos, Playe // but I have not found another solution yet. if (musicBlockEntity.notHeld()) { - boolean isPlaying = canPlayOrStopMusic(worldIn, state, pos, musicBlockEntity, false); + boolean isPlaying = canPlayOrStopMusic(worldIn, state, pos, musicBlockEntity, player, false); if (isPlaying) musicBlockEntity.setLastPlay(true); setPlayingState(worldIn, pos, state, isPlaying); @@ -182,7 +182,7 @@ private boolean invertShiftIfLocked(PlayerEntity player, World level, BlockPos b return LockableHelper.isLocked(FakePlayerFactory.getMinecraft((ServerWorld) level), level, blockPos) != player.isShiftKeyDown(); } - private boolean canPlayOrStopMusic(World pLevel, BlockState pState, BlockPos pPos, MusicBlockEntity musicBlockEntity, Boolean noPlay) + private boolean canPlayOrStopMusic(World pLevel, BlockState pState, BlockPos pPos, MusicBlockEntity musicBlockEntity, @Nullable PlayerEntity playerEntity, Boolean noPlay) { int playId = PlayManager.getEntitiesPlayId(musicBlockEntity.getMusicSourceEntityId()); if (PlayManager.isActivePlayId(playId) || pState.getValue(PLAYING)) @@ -192,7 +192,7 @@ private boolean canPlayOrStopMusic(World pLevel, BlockState pState, BlockPos pPo } if (!noPlay) { - playId = PlayManager.playMusic(pLevel, pPos); + playId = PlayManager.playMusic(pLevel, pPos, playerEntity); return playId != PlayIdSupplier.INVALID && !pState.getValue(PLAYING); } return false; @@ -271,7 +271,7 @@ public void neighborChanged(BlockState pState, World pLevel, BlockPos pPos, Bloc { if (isSidePowered) { - boolean isPlaying = canPlayOrStopMusic(pLevel, pState, pPos, musicBlockEntity, false); + boolean isPlaying = canPlayOrStopMusic(pLevel, pState, pPos, musicBlockEntity, null, false); if (isPlaying) musicBlockEntity.setLastPlay(true); setPlayingState(pLevel, pPos, pState, isPlaying); diff --git a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java index 9759ee9c..1dfa10e0 100644 --- a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java +++ b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java @@ -270,6 +270,7 @@ private void addErrors() addError("midi_system_util.no_sound_bank_loaded","No SoundBank Loaded"); addError("mml_server_side_validation_failure", "Server side music validation error"); addError("sheet_music_too_old", "The Sheet Music is unreadable!"); + addError("sheet_music_not_present", "No instrument(s) with sheet music present in Music Block!"); addError("sheet_music_write_failure", "Unable to write Sheet Music!"); } diff --git a/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java b/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java index 9f1e5077..e5c14d2a 100644 --- a/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java +++ b/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java @@ -17,7 +17,6 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.World; @@ -58,11 +57,13 @@ public static int playMusic(PlayerEntity playerIn) /** * For playing music from a block, e.g. Band Amp. - * @param world the world of course - * @param blockPos position of block instrument + * + * @param world the world of course + * @param blockPos position of block instrument + * @param playerEntity * @return a unique play id or {@link aeronicamc.mods.mxtune.managers.PlayIdSupplier#INVALID} if unable to play */ - public static int playMusic(World world, BlockPos blockPos) + public static int playMusic(World world, BlockPos blockPos, @Nullable PlayerEntity playerEntity) { int playId = INVALID; IMusicPlayer musicPlayer; @@ -83,9 +84,12 @@ public static int playMusic(World world, BlockPos blockPos) world.addFreshEntity(musicSource); } } else { - world.playSound(null, blockPos, ModSoundEvents.FAILURE.get(), SoundCategory.BLOCKS, 1F, 1F); - LOGGER.warn("MusicBlock contains unreadable SheetMusic. File read error: {}", musicPlayer); - LOGGER.warn("Music key(s) not found. level: {}, pos: {}", world.dimension(), blockPos); + if (playerEntity != null) { + Misc.audiblePingPlayer(playerEntity, ModSoundEvents.FAILURE.get()); + playerEntity.displayClientMessage(new TranslationTextComponent("errors.mxtune.sheet_music_not_present"), false); + } + LOGGER.warn("MusicBlock contains unreadable SheetMusic or File read error: {}", musicPlayer); + LOGGER.warn("Music key(s) not found? level: {}, pos: {}", world.dimension(), blockPos); } } }