From 7cbfc4a6811fcf7551b13a42cf6da59835cc9bb2 Mon Sep 17 00:00:00 2001 From: Sylv Date: Mon, 28 Oct 2024 16:11:54 -0400 Subject: [PATCH] refactor(day 12): simplify Broken level system --- gradle.properties | 2 +- .../data_components/Broken.java | 35 ++++++++++++++++++ .../item/ConditionalText.java | 3 ++ .../legacy_landscape/item/JappasWandItem.java | 33 ++++------------- .../legacy_landscape/item/LegacyItems.java | 7 +++- .../legacy_landscape/item/TooltipItem.java | 37 ++++++++++++++++--- .../mixin/Mixin_MutableComponent.java | 14 +++++++ .../assets/legacy_landscape/lang/en_us.json | 5 ++- 8 files changed, 100 insertions(+), 36 deletions(-) diff --git a/gradle.properties b/gradle.properties index 23fe24f..80111b9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ mod_name=Legacy Landscape # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=EUPL-1.2 # The mod version. See https://semver.org/ -mod_version=0.2.0+1.21.1 +mod_version=0.3.0+1.21.1 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/gay/sylv/legacy_landscape/data_components/Broken.java b/src/main/java/gay/sylv/legacy_landscape/data_components/Broken.java index ac86130..e1b3579 100644 --- a/src/main/java/gay/sylv/legacy_landscape/data_components/Broken.java +++ b/src/main/java/gay/sylv/legacy_landscape/data_components/Broken.java @@ -23,4 +23,39 @@ public record Broken(int level) { ); public static final Broken UNBROKEN = new Broken(0); + + /** + * A {@link Broken} with level {@code -1} that indicates that conditions checking this will always be equal. + */ + public static final Broken ALWAYS = new Broken(-1); + + /** + * A LUT to avoid duplication. + */ + private static final Broken[] LEVELS = new Broken[256]; + + static { + for (int i = 0; i < LEVELS.length; i++) { + LEVELS[i] = new Broken(i); + } + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Broken broken) { + return broken.level == this.level || broken.level == -1 || this.level == -1; + } + + return false; + } + + /** + * Returns a pre-cached value of {@link Broken}. The maximum level is {@code 255}, and the minimum level is {@code 0}. + * @param level The level of the {@link Broken}. + * @return The corresponding {@link Broken} record. + */ + public static Broken of(final int level) { + assert level < LEVELS.length; + return LEVELS[level]; + } } diff --git a/src/main/java/gay/sylv/legacy_landscape/item/ConditionalText.java b/src/main/java/gay/sylv/legacy_landscape/item/ConditionalText.java index 33a9a83..92983fe 100644 --- a/src/main/java/gay/sylv/legacy_landscape/item/ConditionalText.java +++ b/src/main/java/gay/sylv/legacy_landscape/item/ConditionalText.java @@ -1,5 +1,6 @@ package gay.sylv.legacy_landscape.item; +import gay.sylv.legacy_landscape.data_components.Broken; import net.minecraft.world.item.TooltipFlag; import org.jetbrains.annotations.NotNull; @@ -8,4 +9,6 @@ public interface ConditionalText { @NotNull Predicate legacy_landscape$getCondition(); void legacy_landscape$setCondition(TooltipCondition condition); + @NotNull Broken legacy_landscape$getBroken(); + void legacy_landscape$setBroken(Broken broken); } diff --git a/src/main/java/gay/sylv/legacy_landscape/item/JappasWandItem.java b/src/main/java/gay/sylv/legacy_landscape/item/JappasWandItem.java index 85e7320..c4cf11c 100644 --- a/src/main/java/gay/sylv/legacy_landscape/item/JappasWandItem.java +++ b/src/main/java/gay/sylv/legacy_landscape/item/JappasWandItem.java @@ -22,7 +22,6 @@ import net.minecraft.world.level.chunk.LevelChunk; import org.jetbrains.annotations.NotNull; -import java.util.List; import java.util.Objects; import java.util.Optional; @@ -32,32 +31,14 @@ public JappasWandItem(Properties properties) { } @Override - public void appendHoverText( - @NotNull ItemStack stack, @NotNull TooltipContext context, @NotNull List tooltipComponents, @NotNull TooltipFlag tooltipFlag + protected boolean showTooltip( + @NotNull ItemStack stack, + @NotNull TooltipContext context, + @NotNull Component tooltip, + @NotNull TooltipFlag tooltipFlag ) { - tooltipComponents.addAll( - getTooltip() - .stream() - .filter( - tooltip -> { - boolean condition = ((ConditionalText) tooltip) - .legacy_landscape$getCondition() - .test(tooltipFlag); - if (stack.getOrDefault(LegacyComponents.BROKEN, Broken.UNBROKEN).level() == 1) { - if (!tooltip.contains(Component.translatable("tooltip.legacy_landscape.jappas_wand.1"))) { - return condition; - } else { - return false; - } - } else if (!tooltip.contains(Component.translatable("tooltip.legacy_landscape.jappas_wand.2"))) { - return condition; - } else { - return false; - } - } - ) - .toList() - ); + Broken brokenLevel = ((ConditionalText) tooltip).legacy_landscape$getBroken(); + return brokenLevel.equals(stack.getOrDefault(LegacyComponents.BROKEN, Broken.UNBROKEN)); } @Override diff --git a/src/main/java/gay/sylv/legacy_landscape/item/LegacyItems.java b/src/main/java/gay/sylv/legacy_landscape/item/LegacyItems.java index 310f84c..f1bf7a5 100644 --- a/src/main/java/gay/sylv/legacy_landscape/item/LegacyItems.java +++ b/src/main/java/gay/sylv/legacy_landscape/item/LegacyItems.java @@ -1,5 +1,6 @@ package gay.sylv.legacy_landscape.item; +import gay.sylv.legacy_landscape.data_components.Broken; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; import net.minecraft.world.item.Item; @@ -51,14 +52,16 @@ public final class LegacyItems { ) .tooltip( TooltipCondition::hasModifierKey, - Component.translatable("tooltip.legacy_landscape.jappas_wand.2") + Broken.of(1), + Component.translatable("broken.1.tooltip.legacy_landscape.jappas_wand.1") .withStyle( ChatFormatting.DARK_PURPLE ) ) .tooltip( TooltipCondition::hasModifierKey, - Component.translatable("tooltip.legacy_landscape.jappas_wand.3") + Broken.of(1), + Component.translatable("broken.1.tooltip.legacy_landscape.jappas_wand.2") .withStyle( ChatFormatting.DARK_RED, ChatFormatting.BOLD diff --git a/src/main/java/gay/sylv/legacy_landscape/item/TooltipItem.java b/src/main/java/gay/sylv/legacy_landscape/item/TooltipItem.java index 683d0c3..95ba767 100644 --- a/src/main/java/gay/sylv/legacy_landscape/item/TooltipItem.java +++ b/src/main/java/gay/sylv/legacy_landscape/item/TooltipItem.java @@ -1,5 +1,6 @@ package gay.sylv.legacy_landscape.item; +import gay.sylv.legacy_landscape.data_components.Broken; import net.minecraft.ChatFormatting; import net.minecraft.core.component.DataComponentType; import net.minecraft.network.chat.Component; @@ -22,8 +23,22 @@ public TooltipItem(Properties properties) { this.tooltip = properties.tooltip; } - protected List getTooltip() { - return tooltip; + /** + * Controls if the tooltip should be shown based on the given context. + * @param stack The {@link ItemStack} to show the tooltip on. + * @param context The {@link net.minecraft.world.item.Item.TooltipContext}. + * @param tooltip The {@link Component} of the tooltip. + * @param tooltipFlag The {@link TooltipFlag}s of the tooltip. + * @return whether the tooltip should be shown. + */ + @SuppressWarnings("unused") + protected boolean showTooltip( + @NotNull ItemStack stack, + @NotNull TooltipContext context, + @NotNull Component tooltip, + @NotNull TooltipFlag tooltipFlag + ) { + return true; } @Override @@ -37,9 +52,13 @@ public void appendHoverText( tooltip .stream() .filter( - tooltip -> ((ConditionalText) tooltip) - .legacy_landscape$getCondition() - .test(tooltipFlag) + tooltip -> { + boolean condition = ((ConditionalText) tooltip) + .legacy_landscape$getCondition() + .test(tooltipFlag); + boolean showTooltip = showTooltip(stack, context, tooltip, tooltipFlag); + return condition && showTooltip; + } ) .toList() ); @@ -59,10 +78,18 @@ public Properties tooltip(Predicate condition, Component tooltip) { return this; } + public Properties tooltip(Predicate condition, Broken broken, Component tooltip) { + ((ConditionalText) tooltip).legacy_landscape$setCondition(new TooltipCondition(condition)); + ((ConditionalText) tooltip).legacy_landscape$setBroken(broken); + this.tooltip.add(tooltip); + return this; + } + public Properties moreInfo() { return this .tooltip( TooltipCondition.not(TooltipCondition::hasModifierKey), + Broken.ALWAYS, Component.translatable("tooltip.legacy_landscape.more_info") .withStyle( ChatFormatting.ITALIC, diff --git a/src/main/java/gay/sylv/legacy_landscape/mixin/Mixin_MutableComponent.java b/src/main/java/gay/sylv/legacy_landscape/mixin/Mixin_MutableComponent.java index ab9b19e..6927b34 100644 --- a/src/main/java/gay/sylv/legacy_landscape/mixin/Mixin_MutableComponent.java +++ b/src/main/java/gay/sylv/legacy_landscape/mixin/Mixin_MutableComponent.java @@ -1,5 +1,6 @@ package gay.sylv.legacy_landscape.mixin; +import gay.sylv.legacy_landscape.data_components.Broken; import gay.sylv.legacy_landscape.item.ConditionalText; import gay.sylv.legacy_landscape.item.TooltipCondition; import net.minecraft.network.chat.MutableComponent; @@ -19,6 +20,9 @@ public final class Mixin_MutableComponent implements ConditionalText { @Unique private TooltipCondition legacy_landscape$condition; + @Unique + private Broken legacy_landscape$broken; + @Override public @NotNull Predicate legacy_landscape$getCondition() { return Objects.requireNonNullElse( @@ -31,4 +35,14 @@ public final class Mixin_MutableComponent implements ConditionalText { public void legacy_landscape$setCondition(TooltipCondition condition) { legacy_landscape$condition = condition; } + + @Override + public @NotNull Broken legacy_landscape$getBroken() { + return Objects.requireNonNullElse(legacy_landscape$broken, Broken.UNBROKEN); + } + + @Override + public void legacy_landscape$setBroken(Broken broken) { + legacy_landscape$broken = broken; + } } diff --git a/src/main/resources/assets/legacy_landscape/lang/en_us.json b/src/main/resources/assets/legacy_landscape/lang/en_us.json index edd99c4..1137808 100644 --- a/src/main/resources/assets/legacy_landscape/lang/en_us.json +++ b/src/main/resources/assets/legacy_landscape/lang/en_us.json @@ -4,13 +4,14 @@ "block.legacy_landscape.turf": "Turf", "item.legacy_landscape.diamond": "Noob Diamond", "item.legacy_landscape.jappas_wand": "Jappa's Wand", + "broken.1.item.legacy_landscape.jappas_wand": "Decaying Dust", "item.legacy_landscape.ore_dust": "Ore Dust", "itemGroup.legacy_landscape.creative": "Legacy Creative Items", "itemGroup.legacy_landscape.legacy_landscape": "Legacy Landscape", "tooltip.legacy_landscape.more_info": "Hold Alt, Control, or Shift for more information.", "tooltip.legacy_landscape.jappas_wand.1": "Use this wand on land to reconcile it with the present.", - "tooltip.legacy_landscape.jappas_wand.2": "A waste byproduct from breaking a wand.", - "tooltip.legacy_landscape.jappas_wand.3": "Irreversibly decays chunks when used.", + "broken.1.tooltip.legacy_landscape.jappas_wand.1": "A waste byproduct from breaking a wand.", + "broken.1.tooltip.legacy_landscape.jappas_wand.2": "Irreversibly decays chunks when used.", "tooltip.legacy_landscape.ore_dust.1": "But if you close your eyes, does it almost feel like nothing's changed at all?", "tooltip.legacy_landscape.ore_dust.2": "Use this item on any block in a chunk to convert it into a legacy chunk." }