Skip to content

Commit

Permalink
Allow SheetMusic to NOT expire. Setting config value sheetMusicLifeIn…
Browse files Browse the repository at this point in the history
…Days to 99999 prevents expiration. The SheetMusic days left tooltip is not displayed when expiration disabled.
  • Loading branch information
Aeronica committed Dec 24, 2023
1 parent a1cf1f1 commit ef2adde
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/generated/resources/assets/mxtune/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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_life_in_days": "Sheet Music Life in Days",
"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]",
"entity.mxtune.music_venue_info": "Music Venue Info Panel",
Expand Down Expand Up @@ -260,7 +260,6 @@
"tooltip.mxtune.music_venue_tool_item.shift_help_05": "Survival: Owners may remove their own Venues. OP/Creative players can remove any Venue.",
"tooltip.mxtune.music_venue_tool_item.shift_help_06": "Venues are only visible when this tool is on the HotBar.",
"tooltip.mxtune.sheet_music.days_left": "Days left: %s",
"tooltip.mxtune.sheet_music.days_left_error": "Days left: nn",
"tooltip.mxtune.sheet_music.duration_error": "h:mm:ss",
"tooltip.mxtune.sheet_music.empty": " - Empty - ",
"tooltip.mxtune.sheet_music.n_of_m_instrument_name": "%s of %s %s"
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/aeronicamc/mods/mxtune/config/MXTuneConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class MXTuneConfig
{
public static int SHEET_MUSIC_NO_EXPIRATION = 99999;
private MXTuneConfig() { /* NOP */ }

/** Client Configuration Settings */
Expand Down Expand Up @@ -70,9 +71,9 @@ public Server(final ForgeConfigSpec.Builder builder)
.defineInRange("listenerRange",24,10, 64);

sheetMusicLifeInDays = builder
.comment("Sheet Music Life in Days before it breaks")
.comment("Sheet Music Life in Days before it breaks. 99999 = no expiration.")
.translation("config.mxtune.server.sheet_music_life_in_days")
.defineInRange("sheetMusicLifeInDays", 30, 2, Integer.MAX_VALUE);
.defineInRange("sheetMusicLifeInDays", SHEET_MUSIC_NO_EXPIRATION, 2, SHEET_MUSIC_NO_EXPIRATION);

builder.pop();
}
Expand All @@ -82,6 +83,8 @@ public Server(final ForgeConfigSpec.Builder builder)

public static int getSheetMusicLifeInDays() { return SERVER.sheetMusicLifeInDays.get(); }

public static boolean doesSheetMusicExpire() { return SERVER.sheetMusicLifeInDays.get() == SHEET_MUSIC_NO_EXPIRATION;}

public static int getDoubleClickTimeMS() { return CLIENT.doubleClickTime.get(); }

public static String getMmlLink() { return CLIENT.site.get(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ private void addItems()
addTooltip("music_score.n_part_score", "%s Part Score");
addTooltip("music_score.parts_error", "No parts found");
addTooltip("sheet_music.days_left", "Days left: %s");
addTooltip("sheet_music.days_left_error", "Days left: nn");
addTooltip("sheet_music.duration_error", "h:mm:ss");
addTooltip("sheet_music.empty", " - Empty - ");
addTooltip("sheet_music.n_of_m_instrument_name", "%s of %s %s");
Expand Down Expand Up @@ -240,7 +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_life_in_days", "Sheet Music Life in Days");
addConfig("server.sheet_music_life_in_days", "Sheet Music Life in Days.");
}

private void addEntities()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ public void appendHoverText(ItemStack pStack, @Nullable World pLevel, List<IText
if(!getFormattedExtraText(iMusic).equals(StringTextComponent.EMPTY))
pTooltip.add(getFormattedExtraText(iMusic));
pTooltip.add(SheetMusicHelper.getFormattedMusicDuration(iMusic));
pTooltip.add(SheetMusicHelper.getFormattedSheetMusicDaysLeft(iMusic));
ITextComponent daysLeft = SheetMusicHelper.getFormattedSheetMusicDaysLeft(iMusic);
if (!StringTextComponent.EMPTY.equals(daysLeft))
pTooltip.add(daysLeft);
}
if (Screen.hasShiftDown())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void appendHoverText(ItemStack pStack, @Nullable World pLevel, List<IText
pTooltip.add(getFormattedExtraText(pStack));
}
pTooltip.add(getFormattedMusicDuration(pStack));
pTooltip.add(getFormattedSheetMusicDaysLeft(pStack));
ITextComponent daysLeft = SheetMusicHelper.getFormattedSheetMusicDaysLeft(pStack);
if (!StringTextComponent.EMPTY.equals(daysLeft))
pTooltip.add(daysLeft);
}

@Override
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/aeronicamc/mods/mxtune/util/SheetMusicHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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 All @@ -67,9 +68,6 @@ public class SheetMusicHelper
.withStyle(TextFormatting.RED);
private final static String SHEET_MUSIC_DAYS_LEFT = "tooltip.mxtune.sheet_music.days_left";

private final static ITextComponent SHEET_MUSIC_DAYS_LEFT_ERROR =
new TranslationTextComponent("tooltip.mxtune.sheet_music.days_left_error")
.withStyle(TextFormatting.RED);
private final static ITextComponent MUSIC_SCORE_PART_ERROR =
new TranslationTextComponent("tooltip.mxtune.music_score.parts_error")
.withStyle(TextFormatting.RED);
Expand Down Expand Up @@ -240,21 +238,21 @@ public static List<ITextComponent> getFormattedMusicScoreParts(ItemStack musicSc
public static ITextComponent getFormattedSheetMusicDaysLeft(ItemStack sheetMusicStack)
{
long daysLeft = getSheetMusicDaysLeft(sheetMusicStack);
if (daysLeft != 99999)
if (daysLeft != SHEET_MUSIC_NO_EXPIRATION)
return new TranslationTextComponent(SHEET_MUSIC_DAYS_LEFT, getSheetMusicDaysLeft(sheetMusicStack))
.withStyle(TextFormatting.GRAY)
.withStyle(TextFormatting.ITALIC);
else
return SHEET_MUSIC_DAYS_LEFT_ERROR;
return StringTextComponent.EMPTY;
}

/**
* Client or Server Side
* <p></p>Get the sheet music days left as a {@link long}.
* @param sheetMusicStack The sheet music stack.
* @return days left or 99999 if there was a parse or item stack error.
* @return days left or SHEET_MUSIC_NO_EXPIRATION(99999) if there was a parse or item stack error.
*/
public static long getSheetMusicDaysLeft(ItemStack sheetMusicStack)
public static int getSheetMusicDaysLeft(ItemStack sheetMusicStack)
{
String keyDateTimeString = getMusicTextKey(sheetMusicStack);
if (keyDateTimeString != null)
Expand All @@ -265,13 +263,14 @@ public static long getSheetMusicDaysLeft(ItemStack sheetMusicStack)
keyDateTime = LocalDateTime.parse(keyDateTimeString);
} catch (DateTimeException e)
{
return 99999;
LOGGER.warn("Failure to parse SheetMusic key: {}", keyDateTimeString);
return SHEET_MUSIC_NO_EXPIRATION;
}
LocalDateTime keyPlusDaysLeft = keyDateTime.plusDays(MXTuneConfig.getSheetMusicLifeInDays());
LocalDateTime now = LocalDateTime.now(ZoneId.of("GMT0"));
return Math.max(Duration.between(now, keyPlusDaysLeft).getSeconds() / 86400L, 0L);
return (int) Math.max(Duration.between(now, keyPlusDaysLeft).getSeconds() / 86400L, 0L);
}
return 99999;
return SHEET_MUSIC_NO_EXPIRATION;
}

/**
Expand Down Expand Up @@ -461,7 +460,7 @@ public static boolean writeIMusic(ItemStack itemStack, String musicTitle, byte[]
*/
public static void scrapSheetMusicIfExpired(ItemStack pStack, World pLevel, Entity pEntity, int pItemSlot, boolean pIsSelected)
{
if (!pLevel.isClientSide() && !pStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pIsSelected)
if (!pLevel.isClientSide() && !pStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pIsSelected && !MXTuneConfig.doesSheetMusicExpire())
{
String key = getMusicTextKey(((PlayerEntity) pEntity).inventory.getItem(pItemSlot));
boolean canReap = getSheetMusicDaysLeft(pStack) == 0L;
Expand Down Expand Up @@ -516,7 +515,7 @@ public static void scrapSheetMusicIfExpired(ItemStack pStack, World pLevel, Enti
*/
public static void scrapSheetMusicInInstrumentIfExpired(@Nullable Slot slot, ItemStack pInstrumentStack, World pLevel, Entity pEntity, @Nullable BlockPos blockPos)
{
if (!pLevel.isClientSide() && !pInstrumentStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pEntity.isSpectator())
if (!pLevel.isClientSide() && !pInstrumentStack.isEmpty() && (pEntity instanceof PlayerEntity) && !pEntity.isSpectator() && !MXTuneConfig.doesSheetMusicExpire())
{
int multiplier = 0;
ItemStack sheetMusic = removeSheetMusicFromIInstrument(pInstrumentStack);
Expand Down

0 comments on commit ef2adde

Please sign in to comment.