generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] Implement a search bar (#4)
* Implement search bar in language menu * Cleanup code * Update gradle and fix CI * Add accesswidener to fabric.mod.json * Pick StateManager changes * [skip ci] Bump version * [skip ci] Cleanup code
- Loading branch information
Showing
18 changed files
with
251 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
7 changes: 7 additions & 0 deletions
7
src/main/java/club/chachy/lazylanguageloader/client/api/language/LanguageMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package club.chachy.lazylanguageloader.client.api.language; | ||
|
||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
|
||
public interface LanguageMatcher { | ||
boolean matches(String input, LanguageDefinition entry); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/club/chachy/lazylanguageloader/client/api/scroll/Scrollable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package club.chachy.lazylanguageloader.client.api.scroll; | ||
|
||
public interface Scrollable { | ||
boolean hasScrolled(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/club/chachy/lazylanguageloader/client/impl/language/CodeLanguageMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package club.chachy.lazylanguageloader.client.impl.language; | ||
|
||
import club.chachy.lazylanguageloader.client.api.language.LanguageMatcher; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
|
||
import java.util.Locale; | ||
|
||
public class CodeLanguageMatcher implements LanguageMatcher { | ||
@Override | ||
public boolean matches(String input, LanguageDefinition entry) { | ||
return entry.getCode().toLowerCase(Locale.ROOT).contains(input.toLowerCase(Locale.ROOT)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/club/chachy/lazylanguageloader/client/impl/language/NameLanguageMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package club.chachy.lazylanguageloader.client.impl.language; | ||
|
||
import club.chachy.lazylanguageloader.client.api.language.LanguageMatcher; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
|
||
import java.util.Locale; | ||
|
||
public class NameLanguageMatcher implements LanguageMatcher { | ||
@Override | ||
public boolean matches(String input, LanguageDefinition entry) { | ||
return entry.getName().toLowerCase(Locale.ROOT).contains(input.toLowerCase(Locale.ROOT)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/club/chachy/lazylanguageloader/client/impl/language/RegionLanguageMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package club.chachy.lazylanguageloader.client.impl.language; | ||
|
||
import club.chachy.lazylanguageloader.client.api.language.LanguageMatcher; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
|
||
import java.util.Locale; | ||
|
||
public class RegionLanguageMatcher implements LanguageMatcher { | ||
@Override | ||
public boolean matches(String input, LanguageDefinition entry) { | ||
return entry.getRegion().toLowerCase(Locale.ROOT).contains(input.toLowerCase(Locale.ROOT)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/club/chachy/lazylanguageloader/client/impl/state/StateManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package club.chachy.lazylanguageloader.client.impl.state; | ||
|
||
import club.chachy.lazylanguageloader.client.api.language.LanguageMatcher; | ||
import club.chachy.lazylanguageloader.client.impl.language.CodeLanguageMatcher; | ||
import club.chachy.lazylanguageloader.client.impl.language.NameLanguageMatcher; | ||
import club.chachy.lazylanguageloader.client.impl.language.RegionLanguageMatcher; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
import net.minecraft.resource.ResourceReloader; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class StateManager { | ||
private static final List<LanguageMatcher> languageMatchers = new ArrayList<>(); | ||
private static final List<ResourceReloader> reloaders = new ArrayList<>(); | ||
private static boolean resourceLoadViaLanguage = false; | ||
|
||
static { | ||
languageMatchers.addAll(Arrays.asList(new CodeLanguageMatcher(), new NameLanguageMatcher(), new RegionLanguageMatcher())); | ||
} | ||
|
||
public static boolean isResourceLoadViaLanguage() { | ||
return resourceLoadViaLanguage; | ||
} | ||
|
||
public static void setResourceLoadViaLanguage(boolean resourceLoadViaLanguage) { | ||
StateManager.resourceLoadViaLanguage = resourceLoadViaLanguage; | ||
} | ||
|
||
public static List<ResourceReloader> getResourceReloaders() { | ||
return reloaders; | ||
} | ||
|
||
/** | ||
* If any developer wants to workaround lazy-language-loader you could depend on it via Jitpack and add your resource reloader here | ||
* if not it will not be reloaded. Sadly if your resource reloader doesn't derive from SearchManager or LanguageManager there isn't much | ||
* more I can do to determine that you do stuff with languages... | ||
* | ||
* @param reloader Reloader to be used on language reloads | ||
*/ | ||
public static void addResourceReloader(ResourceReloader reloader) { | ||
reloaders.add(reloader); | ||
} | ||
|
||
public static boolean isMatchable(String input, LanguageDefinition definition) { | ||
return languageMatchers.stream().anyMatch((m) -> m.matches(input, definition)); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ent/mixin/MixinLanguageOptionsScreen.java → ...mizations/MixinLanguageOptionsScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...n/MixinReloadableResourceManagerImpl.java → ...s/MixinReloadableResourceManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...main/java/club/chachy/lazylanguageloader/client/mixin/searchbar/MixinEntryListWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package club.chachy.lazylanguageloader.client.mixin.searchbar; | ||
|
||
import club.chachy.lazylanguageloader.client.api.scroll.Scrollable; | ||
import net.minecraft.client.gui.widget.EntryListWidget; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(EntryListWidget.class) | ||
public class MixinEntryListWidget implements Scrollable { | ||
private boolean scrolled; | ||
|
||
@Inject(method = "mouseScrolled", at = @At("HEAD")) | ||
private void mouseScrolled(double mouseX, double mouseY, double amount, CallbackInfoReturnable<Boolean> cir) { | ||
scrolled = true; | ||
} | ||
|
||
@Override | ||
public boolean hasScrolled() { | ||
return scrolled; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...ava/club/chachy/lazylanguageloader/client/mixin/searchbar/MixinLanguageOptionsScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package club.chachy.lazylanguageloader.client.mixin.searchbar; | ||
|
||
import club.chachy.lazylanguageloader.client.api.scroll.Scrollable; | ||
import club.chachy.lazylanguageloader.client.impl.state.StateManager; | ||
import club.chachy.lazylanguageloader.client.mixin.searchbar.accessor.LanguageEntryAccessor; | ||
import net.minecraft.client.gui.screen.Screen; | ||
import net.minecraft.client.gui.screen.option.LanguageOptionsScreen; | ||
import net.minecraft.client.gui.widget.TextFieldWidget; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
import net.minecraft.text.LiteralText; | ||
import net.minecraft.text.Text; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Mixin(LanguageOptionsScreen.class) | ||
public class MixinLanguageOptionsScreen extends Screen { | ||
@Shadow | ||
private LanguageOptionsScreen.LanguageSelectionListWidget languageSelectionList; | ||
|
||
private List<LanguageOptionsScreen.LanguageSelectionListWidget.LanguageEntry> initialComponents; | ||
|
||
protected MixinLanguageOptionsScreen(Text title) { | ||
super(title); | ||
} | ||
|
||
@Inject( | ||
method = "init", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/client/gui/screen/option/LanguageOptionsScreen;addSelectableChild(Lnet/minecraft/client/gui/Element;)Lnet/minecraft/client/gui/Element;", | ||
shift = At.Shift.AFTER | ||
) | ||
) | ||
private void init(CallbackInfo ci) { | ||
initialComponents = new ArrayList<>(languageSelectionList.children()); | ||
|
||
int w = width / 5; | ||
TextFieldWidget searchText = new TextFieldWidget(textRenderer, width - (w + 5), 11, w, 15, LiteralText.EMPTY); | ||
searchText.setChangedListener(this::handleText); | ||
|
||
addDrawableChild(searchText); | ||
} | ||
|
||
private void handleText(String text) { | ||
List<LanguageOptionsScreen.LanguageSelectionListWidget.LanguageEntry> children = languageSelectionList.children(); | ||
|
||
if (text.isBlank() || text.isBlank()) { | ||
int initialSize = initialComponents.size(); | ||
int currentSize = children.size(); | ||
|
||
if (initialSize != currentSize) { | ||
languageSelectionList.replaceEntries(initialComponents); | ||
} | ||
} else { | ||
for (LanguageOptionsScreen.LanguageSelectionListWidget.LanguageEntry entry : initialComponents) { | ||
LanguageDefinition def = ((LanguageEntryAccessor) entry).getLanguageDefinition(); | ||
|
||
if (StateManager.isMatchable(text, def)) { | ||
safeAdd(entry); | ||
} else { | ||
languageSelectionList.removeEntry(entry); | ||
} | ||
} | ||
} | ||
fixScroll(); | ||
} | ||
|
||
@Unique | ||
private void fixScroll() { | ||
if (((Scrollable) languageSelectionList).hasScrolled()) { | ||
languageSelectionList.setScrollAmount(languageSelectionList.getScrollAmount()); | ||
} else { | ||
languageSelectionList.centerScrollOn(languageSelectionList.getSelectedOrNull()); | ||
} | ||
} | ||
|
||
@Unique | ||
private void safeAdd(LanguageOptionsScreen.LanguageSelectionListWidget.LanguageEntry entry) { | ||
if (!languageSelectionList.children().contains(entry)) { | ||
languageSelectionList.addEntry(entry); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...club/chachy/lazylanguageloader/client/mixin/searchbar/accessor/LanguageEntryAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package club.chachy.lazylanguageloader.client.mixin.searchbar.accessor; | ||
|
||
import net.minecraft.client.gui.screen.option.LanguageOptionsScreen; | ||
import net.minecraft.client.resource.language.LanguageDefinition; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(LanguageOptionsScreen.LanguageSelectionListWidget.LanguageEntry.class) | ||
public interface LanguageEntryAccessor { | ||
@Accessor | ||
LanguageDefinition getLanguageDefinition(); | ||
} |
33 changes: 0 additions & 33 deletions
33
src/main/java/club/chachy/lazylanguageloader/client/state/StateManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +0,0 @@ | ||
package club.chachy.lazylanguageloader.client.state; | ||
|
||
import net.minecraft.resource.ResourceReloader; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StateManager { | ||
private static boolean resourceLoadViaLanguage = false; | ||
private static final List<ResourceReloader> reloaders = new ArrayList<>(); | ||
|
||
public static boolean isResourceLoadViaLanguage() { | ||
return resourceLoadViaLanguage; | ||
} | ||
|
||
public static void setResourceLoadViaLanguage(boolean resourceLoadViaLanguage) { | ||
StateManager.resourceLoadViaLanguage = resourceLoadViaLanguage; | ||
} | ||
|
||
public static List<ResourceReloader> getResourceReloaders() { | ||
return reloaders; | ||
} | ||
|
||
/** | ||
* If any developer wants to workaround lazy-language-loader you could depend on it via Jitpack and add your resource reloader here | ||
* if not it will not be reloaded. Sadly if your resource reloader doesn't derive from SearchManager or LanguageManager there isn't much | ||
* more I can do to determine that you do stuff with languages... | ||
* @param reloader Reloader to be used on language reloads | ||
*/ | ||
public static void addResourceReloader(ResourceReloader reloader) { | ||
reloaders.add(reloader); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
accessWidener v1 named | ||
|
||
accessible class net/minecraft/client/gui/screen/option/LanguageOptionsScreen$LanguageSelectionListWidget | ||
accessible method net/minecraft/client/gui/widget/EntryListWidget removeEntry (Lnet/minecraft/client/gui/widget/EntryListWidget$Entry;)Z | ||
accessible method net/minecraft/client/gui/widget/EntryListWidget addEntry (Lnet/minecraft/client/gui/widget/EntryListWidget$Entry;)I | ||
accessible method net/minecraft/client/gui/widget/EntryListWidget centerScrollOn (Lnet/minecraft/client/gui/widget/EntryListWidget$Entry;)V | ||
accessible method net/minecraft/client/gui/widget/EntryListWidget replaceEntries (Ljava/util/Collection;)V |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters