Skip to content

Commit

Permalink
Warn player when Music Block has no instruments containing Sheet Music.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeronica committed Jan 3, 2024
1 parent c9d1e6d commit 1f4d87c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/generated/resources/assets/mxtune/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/aeronicamc/mods/mxtune/blocks/MusicBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 1f4d87c

Please sign in to comment.