diff --git a/src/generated/resources/assets/mxtune/lang/en_us.json b/src/generated/resources/assets/mxtune/lang/en_us.json index c1f7c92a..6685e93c 100644 --- a/src/generated/resources/assets/mxtune/lang/en_us.json +++ b/src/generated/resources/assets/mxtune/lang/en_us.json @@ -13,6 +13,7 @@ "config.mxtune.client.double_click_time_ms": "Double-click time in milliseconds for GUI widgets", "config.mxtune.client.mml_Link": "MML Site Link", "config.mxtune.server.listener_range": "Listener Range", + "config.mxtune.server.sheet_music_expires": "Sheet Music Expires", "config.mxtune.server.sheet_music_life_in_days": "Sheet Music Life in Days.", "container.mxtune.block_music.more": "%s More", "entity.mxtune.music_source": "[MusicSource]", diff --git a/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java b/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java index 08a0793f..0a2eef70 100644 --- a/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java +++ b/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java @@ -238,7 +238,7 @@ private static long reapSheetMusic(boolean whatIf) @Nullable public static String addMusicText(String musicText) { - LocalDateTime key = null; + LocalDateTime key; key = nextKey(); String filename = "--error--"; try @@ -253,7 +253,7 @@ public static String addMusicText(String musicText) LOGGER.error(" Failed write: {}", filename, e); key = null; } - return key != null ? key.toString() : null; + return (key != null) ? key.toString() : null; } /** @@ -274,8 +274,10 @@ public static String getMusicText(@Nullable String key) String yearMonthFolders = String.format("%s/%d/%02d", SERVER_FOLDER, localDateTime.getYear(), localDateTime.getMonthValue()); filename = toSafeFileNameKey(key); path = FileHelper.getCacheFile(yearMonthFolders, filename, LogicalSide.SERVER, false); - } catch (IOException|DateTimeParseException e) { - LOGGER.error("getMusicText error on file or key parse: " + key, e); + } catch (DateTimeParseException e) { + LOGGER.error("getMusicText error on key parse: {}", key, e); + } catch (IOException e){ + LOGGER.error("getMusicText error on file: {}", filename, e); } try { if (path != null && !Files.exists(path)) throw new MXTuneException("File does not exist: " + path); @@ -285,7 +287,7 @@ public static String getMusicText(@Nullable String key) try (GZIPInputStream gzipInputStream = new GZIPInputStream(Files.newInputStream(path))) { musicText = IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8); } catch (IOException e) { - LOGGER.error("getMusicText unexpected file error: {}", key, e); + LOGGER.error("getMusicText file error: {}", filename, e); musicText = null; } } diff --git a/src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java b/src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java index c53d4d84..7d0a855a 100644 --- a/src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java +++ b/src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java @@ -26,7 +26,7 @@ public class MXTuneConfig { - public static int SHEET_MUSIC_NO_EXPIRATION = 99999; + public static int SHEET_MUSIC_MAX_DAYS = 999999; private MXTuneConfig() { /* NOP */ } /** Client Configuration Settings */ @@ -58,6 +58,7 @@ public static class Server { public final IntValue listenerRange; + public final ForgeConfigSpec.BooleanValue sheetMusicExpires; public final IntValue sheetMusicLifeInDays; public Server(final ForgeConfigSpec.Builder builder) @@ -70,10 +71,15 @@ public Server(final ForgeConfigSpec.Builder builder) .translation("config.mxtune.server.listener_range") .defineInRange("listenerRange",24,10, 64); + sheetMusicExpires = builder + .comment("Sheet Music Expires") + .translation("config.mxtune.server.sheet_music_expires") + .define("sheetMusicExpires", false); + sheetMusicLifeInDays = builder - .comment("Sheet Music Life in Days before it breaks. 99999 = no expiration.") + .comment("Sheet Music Life in Days before it breaks. valid only when expiration is enabled.") .translation("config.mxtune.server.sheet_music_life_in_days") - .defineInRange("sheetMusicLifeInDays", SHEET_MUSIC_NO_EXPIRATION, 2, SHEET_MUSIC_NO_EXPIRATION); + .defineInRange("sheetMusicLifeInDays", SHEET_MUSIC_MAX_DAYS, 2, SHEET_MUSIC_MAX_DAYS); builder.pop(); } @@ -83,7 +89,7 @@ public Server(final ForgeConfigSpec.Builder builder) public static int getSheetMusicLifeInDays() { return SERVER.sheetMusicLifeInDays.get(); } - public static boolean sheetMusicExpires() { return SERVER.sheetMusicLifeInDays.get() != SHEET_MUSIC_NO_EXPIRATION;} + public static boolean sheetMusicExpires() { return SERVER.sheetMusicExpires.get(); } public static int getDoubleClickTimeMS() { return CLIENT.doubleClickTime.get(); } diff --git a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java index 4dbb4d55..9759ee9c 100644 --- a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java +++ b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java @@ -239,6 +239,7 @@ private void addConfigs() addConfig("client.double_click_time_ms", "Double-click time in milliseconds for GUI widgets"); addConfig("client.mml_Link", "MML Site Link"); addConfig("server.listener_range", "Listener Range"); + addConfig("server.sheet_music_expires", "Sheet Music Expires"); addConfig("server.sheet_music_life_in_days", "Sheet Music Life in Days."); } diff --git a/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java b/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java index b00eb07a..9f1e5077 100644 --- a/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java +++ b/src/main/java/aeronicamc/mods/mxtune/managers/PlayManager.java @@ -84,7 +84,7 @@ public static int playMusic(World world, BlockPos blockPos) } } else { world.playSound(null, blockPos, ModSoundEvents.FAILURE.get(), SoundCategory.BLOCKS, 1F, 1F); - LOGGER.warn("MusicBlock contains bad SheetMusic. File read error: {}", musicPlayer); + LOGGER.warn("MusicBlock contains unreadable SheetMusic. File read error: {}", musicPlayer); LOGGER.warn("Music key(s) not found. level: {}, pos: {}", world.dimension(), blockPos); } } diff --git a/src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java b/src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java index c2bceaf2..6cfb2251 100644 --- a/src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java +++ b/src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java @@ -43,7 +43,6 @@ import java.util.List; import java.util.UUID; -import static aeronicamc.mods.mxtune.config.MXTuneConfig.SHEET_MUSIC_NO_EXPIRATION; import static java.lang.Thread.sleep; import static net.minecraftforge.common.util.Constants.NBT; @@ -238,7 +237,7 @@ public static List getFormattedMusicScoreParts(ItemStack musicSc public static ITextComponent getFormattedSheetMusicDaysLeft(ItemStack sheetMusicStack) { int daysLeft = getSheetMusicDaysLeft(sheetMusicStack); - if (daysLeft != SHEET_MUSIC_NO_EXPIRATION && MXTuneConfig.sheetMusicExpires()) + if (MXTuneConfig.sheetMusicExpires()) return new TranslationTextComponent(SHEET_MUSIC_DAYS_LEFT, daysLeft) .withStyle(TextFormatting.GRAY) .withStyle(TextFormatting.ITALIC); @@ -250,7 +249,7 @@ public static ITextComponent getFormattedSheetMusicDaysLeft(ItemStack sheetMusic * Client or Server Side *

Get the sheet music days left as a {@link int}. * @param sheetMusicStack The sheet music stack. - * @return days left or SHEET_MUSIC_NO_EXPIRATION(99999) if there was a parse or item stack error. + * @return days left or SHEET_MUSIC_MAX_DAYS(999999) if there was a parse or item stack error. */ public static int getSheetMusicDaysLeft(ItemStack sheetMusicStack) { @@ -264,13 +263,13 @@ public static int getSheetMusicDaysLeft(ItemStack sheetMusicStack) } catch (DateTimeException e) { LOGGER.warn("Failure to parse SheetMusic key: {}", keyDateTimeString); - return SHEET_MUSIC_NO_EXPIRATION; + return MXTuneConfig.SHEET_MUSIC_MAX_DAYS; } LocalDateTime keyPlusDaysLeft = keyDateTime.plusDays(MXTuneConfig.getSheetMusicLifeInDays()); LocalDateTime now = LocalDateTime.now(ZoneId.of("GMT0")); - return (int) Math.max(Duration.between(now, keyPlusDaysLeft).getSeconds() / 86400L, 0L); + return MXTuneConfig.sheetMusicExpires() ? (int) Math.max(Duration.between(now, keyPlusDaysLeft).getSeconds() / 86400L, 0L) : MXTuneConfig.SHEET_MUSIC_MAX_DAYS; } - return SHEET_MUSIC_NO_EXPIRATION; + return MXTuneConfig.SHEET_MUSIC_MAX_DAYS; } /** @@ -463,7 +462,7 @@ public static void scrapSheetMusicIfExpired(ItemStack pStack, World pLevel, Enti if (!pLevel.isClientSide() && !pStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pIsSelected) { String key = getMusicTextKey(((PlayerEntity) pEntity).inventory.getItem(pItemSlot)); - boolean canReap = getSheetMusicDaysLeft(pStack) == 0; + boolean canReap = (getSheetMusicDaysLeft(pStack) == 0) && MXTuneConfig.sheetMusicExpires(); if (key != null && canReap) { ModDataStore.removeSheetMusic(key); @@ -518,15 +517,16 @@ public static void scrapSheetMusicInInstrumentIfExpired(@Nullable Slot slot, Ite if (!pLevel.isClientSide() && !pInstrumentStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pEntity.isSpectator()) { int multiplier = 0; - ItemStack sheetMusic = removeSheetMusicFromIInstrument(pInstrumentStack); + ItemStack sheetMusic = getIMusicFromIInstrument(pInstrumentStack); String key = getMusicTextKey(sheetMusic); - boolean canReap = getSheetMusicDaysLeft(sheetMusic) == 0; + boolean canReap = (getSheetMusicDaysLeft(sheetMusic) == 0) && MXTuneConfig.sheetMusicExpires(); if (key != null && canReap) { if (slot != null) { multiplier += slot.index; slot.setChanged(); } + removeSheetMusicFromIInstrument(pInstrumentStack); ModDataStore.removeSheetMusic(key); Misc.audiblePingPlayer((PlayerEntity)pEntity, ModSoundEvents.CRUMPLE_PAPER.get()); @@ -579,19 +579,16 @@ else if (!((PlayerEntity) pEntity).inventory.add(new ItemStack(ModItems.SCRAP_IT * Server Side *

Remove a {@link SheetMusicItem} stack from an {@link IInstrument}/{@link MultiInstItem} * @param pStack The {@link IInstrument}/{@link MultiInstItem} stack - * @return a {@link SheetMusicItem} stack or empty stack */ - private static ItemStack removeSheetMusicFromIInstrument(ItemStack pStack) + private static void removeSheetMusicFromIInstrument(ItemStack pStack) { ItemStack sheetMusic = SheetMusicHelper.getIMusicFromIInstrument(pStack); - if (!sheetMusic.isEmpty() && SheetMusicHelper.getSheetMusicDaysLeft(sheetMusic) == 0) + if (!sheetMusic.isEmpty()) { MultiInstInventory inv = new MultiInstInventory(pStack); - ItemStack stack = inv.removeItem(0, 1); + inv.removeItem(0, 1); inv.setChanged(); - return stack.copy(); } - return ItemStack.EMPTY; } /**