Skip to content

Commit

Permalink
Fix bad narration text on light source settings, improve search.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Oct 22, 2024
1 parent d03b73d commit 972c8f5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

import com.electronwill.nightconfig.core.Config;
import dev.lambdaurora.spruceui.SpruceTexts;
import dev.lambdaurora.spruceui.option.SpruceBooleanOption;
import dev.lambdaurora.spruceui.option.SpruceOption;
import dev.lambdaurora.spruceui.option.SpruceToggleBooleanOption;
import net.minecraft.TextFormatting;
import net.minecraft.network.chat.Text;
import org.jetbrains.annotations.Nullable;

Expand All @@ -33,7 +31,7 @@ public LightSourceSettingEntry(String key, boolean defaultValue, @Nullable Confi
@Override
protected SpruceOption buildOption(@Nullable Text tooltip) {
return new Option(
this.getOptionKey(),
this.key(),
this::get,
this::set,
tooltip
Expand All @@ -42,12 +40,20 @@ protected SpruceOption buildOption(@Nullable Text tooltip) {

public static final class Option extends SpruceToggleBooleanOption {
public Option(String key, Supplier<Boolean> getter, Consumer<Boolean> setter, @Nullable Text tooltip) {
super(key, getter, setter, tooltip);
super(key, getter, setter, tooltip, false);
}

@Override
public Text getDisplayText() {
return Text.empty();
boolean value = this.get();
Text toggleText = SpruceTexts.getToggleText(value);

return this.getDisplayText(toggleText);
}

@Override
public Text getDisplayText(Text value) {
return Text.translatable("spruceui.options.generic", this.getPrefix(), value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import dev.lambdaurora.lambdynlights.accessor.DynamicLightHandlerHolder;
import dev.lambdaurora.spruceui.Position;
import dev.lambdaurora.spruceui.SpruceTexts;
import dev.lambdaurora.spruceui.background.Background;
import dev.lambdaurora.spruceui.background.EmptyBackground;
import dev.lambdaurora.spruceui.background.SimpleColorBackground;
Expand All @@ -23,6 +24,7 @@
import dev.lambdaurora.spruceui.widget.container.SpruceEntryListWidget;
import dev.lambdaurora.spruceui.widget.container.SpruceParentWidget;
import dev.lambdaurora.spruceui.widget.text.SpruceTextFieldWidget;
import dev.yumi.commons.TriState;
import net.minecraft.TextFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.narration.NarratedElementType;
Expand Down Expand Up @@ -54,10 +56,7 @@ public LightSourceListWidget(Position position, int width, int height, SpruceTex
}

var list = Stream.of(input.split(" "))
.map(part -> part.startsWith("@")
? Text.literal(part + " ").withStyle(TextFormatting.AQUA)
: Text.literal(part + " ")
)
.map(this::stylizeFilterPart)
.map(Text::getVisualOrderText)
.toList();
return FormattedCharSequence.fromList(list);
Expand All @@ -81,6 +80,24 @@ private void update(@Nullable String filter) {
}
}

private TriState evaluateValueFilter(String filter) {
if (filter.isBlank()) {
return TriState.DEFAULT;
}

if (filter.equalsIgnoreCase(SpruceTexts.OPTIONS_ON.getString())) {
return TriState.TRUE;
} else if (filter.equalsIgnoreCase(SpruceTexts.OPTIONS_OFF.getString())) {
return TriState.FALSE;
}

return switch (filter) {
case "true", "1", "on" -> TriState.TRUE;
case "false", "0", "off" -> TriState.FALSE;
default -> TriState.DEFAULT;
};
}

private boolean checkFilter(LightSourceEntry entry, @NotNull List<String> filter) {
var name = entry.option.lambdynlights$getName().getString().toLowerCase();

Expand All @@ -91,6 +108,16 @@ private boolean checkFilter(LightSourceEntry entry, @NotNull List<String> filter
return false;
}

continue;
} else if (part.startsWith("$")) {
var valueFilter = this.evaluateValueFilter(part.substring(1));

if (valueFilter == TriState.DEFAULT) continue;

if (entry.option.lambdynlights$getSetting().get() != valueFilter.toBoolean()) {
return false;
}

continue;
}

Expand All @@ -102,6 +129,20 @@ private boolean checkFilter(LightSourceEntry entry, @NotNull List<String> filter
return true;
}

private Text stylizeFilterPart(String filter) {
if (filter.startsWith("@")) {
return Text.literal(filter + " ").withStyle(TextFormatting.AQUA);
} else if (filter.startsWith("$")) {
var valueFilter = this.evaluateValueFilter(filter.substring(1));
return Text.literal(filter + " ").withStyle(switch (valueFilter) {
case TriState.TRUE, TriState.FALSE -> TextFormatting.GOLD;
default -> TextFormatting.RED;
});
} else {
return Text.literal(filter + " ");
}
}

/**
* Adds a single option entry. The option will use all the width available.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public abstract class EntityTypeMixin<T extends Entity> implements DynamicLightH
@Shadow
public abstract Text getDescription();

@Shadow public abstract String getDescriptionId();

@Unique
private DynamicLightHandler<T> lambdynlights$lightHandler;
@Unique
Expand All @@ -52,9 +54,9 @@ public abstract class EntityTypeMixin<T extends Entity> implements DynamicLightH
return null;
}

this.lambdynlights$setting = new LightSourceSettingEntry("light_sources.settings.entities."
+ id.namespace() + '.' + id.path().replace('/', '.'),
true, null, null);
this.lambdynlights$setting = new LightSourceSettingEntry(this.getDescriptionId(),
true, null, null
);
LambDynLights.get().config.load(this.lambdynlights$setting);
}

Expand Down

0 comments on commit 972c8f5

Please sign in to comment.