Skip to content

Commit

Permalink
Add explicit config option to expire sheet music. Adjust related clas…
Browse files Browse the repository at this point in the history
…ses to suit.
  • Loading branch information
Aeronica committed Jan 1, 2024
1 parent 2a91020 commit 4e2506e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 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 @@ -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]",
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
Expand All @@ -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(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -238,7 +237,7 @@ public static List<ITextComponent> 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);
Expand All @@ -250,7 +249,7 @@ public static ITextComponent getFormattedSheetMusicDaysLeft(ItemStack sheetMusic
* Client or Server Side
* <p></p>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)
{
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -579,19 +579,16 @@ else if (!((PlayerEntity) pEntity).inventory.add(new ItemStack(ModItems.SCRAP_IT
* Server Side
* <p></p>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;
}

/**
Expand Down

0 comments on commit 4e2506e

Please sign in to comment.