Skip to content

Commit

Permalink
refactor(day 12): simplify Broken level system
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Oct 28, 2024
1 parent cdee1b7 commit 7cbfc4a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,4 +9,6 @@
public interface ConditionalText {
@NotNull Predicate<TooltipFlag> legacy_landscape$getCondition();
void legacy_landscape$setCondition(TooltipCondition condition);
@NotNull Broken legacy_landscape$getBroken();
void legacy_landscape$setBroken(Broken broken);
}
33 changes: 7 additions & 26 deletions src/main/java/gay/sylv/legacy_landscape/item/JappasWandItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,32 +31,14 @@ public JappasWandItem(Properties properties) {
}

@Override
public void appendHoverText(
@NotNull ItemStack stack, @NotNull TooltipContext context, @NotNull List<Component> 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
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/gay/sylv/legacy_landscape/item/LegacyItems.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
37 changes: 32 additions & 5 deletions src/main/java/gay/sylv/legacy_landscape/item/TooltipItem.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,8 +23,22 @@ public TooltipItem(Properties properties) {
this.tooltip = properties.tooltip;
}

protected List<Component> 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
Expand All @@ -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()
);
Expand All @@ -59,10 +78,18 @@ public Properties tooltip(Predicate<TooltipFlag> condition, Component tooltip) {
return this;
}

public Properties tooltip(Predicate<TooltipFlag> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TooltipFlag> legacy_landscape$getCondition() {
return Objects.requireNonNullElse(
Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/main/resources/assets/legacy_landscape/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}

0 comments on commit 7cbfc4a

Please sign in to comment.