-
Notifications
You must be signed in to change notification settings - Fork 120
Add an API to allow mods to do their own EMI searching #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
embeddedt
wants to merge
4
commits into
emilyploszaj:1.20.4
Choose a base branch
from
embeddedt:search-api
base: 1.20.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e060a0b
Add an API to allow mods to do their own EMI searching
embeddedt 53d6fdc
Api -> [none], [none] -> Impl
embeddedt 8f5403f
More search refactoring - remove magic static vars, etc.
embeddedt 962235f
Explicitly cancel previous search when starting new one
embeddedt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
xplat/src/main/java/dev/emi/emi/api/search/EmiSearchManagerApi.java
This file contains hidden or 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,24 @@ | ||
| package dev.emi.emi.api.search; | ||
|
|
||
| import dev.emi.emi.api.stack.EmiIngredient; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * A search manager controls searching for items using EMI infrastructure. | ||
| */ | ||
| public interface EmiSearchManagerApi { | ||
embeddedt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * {@return the current list of stacks matching the last search query} | ||
| */ | ||
| List<? extends EmiIngredient> getStacks(); | ||
embeddedt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Search for ingredients matching the given query string. The list returned by {@link EmiSearchManagerApi#getStacks()} | ||
| * will not update until after the returned future completes. | ||
| * @param query the query string to use when searching | ||
| * @return a future that completes with the updated list | ||
| */ | ||
| CompletableFuture<List<? extends EmiIngredient>> search(String query); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
98 changes: 98 additions & 0 deletions
98
xplat/src/main/java/dev/emi/emi/search/EmiSearchManager.java
This file contains hidden or 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,98 @@ | ||
| package dev.emi.emi.search; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import dev.emi.emi.api.search.EmiSearchManagerApi; | ||
| import dev.emi.emi.api.stack.EmiIngredient; | ||
| import dev.emi.emi.api.stack.EmiStack; | ||
| import dev.emi.emi.registry.EmiStackList; | ||
| import dev.emi.emi.runtime.EmiLog; | ||
| import dev.emi.emi.screen.EmiScreenManager; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public class EmiSearchManager implements EmiSearchManagerApi { | ||
| private EmiSearch.CompiledQuery compiledQuery; | ||
| private List<? extends EmiIngredient> stacks = EmiStackList.stacks; | ||
| private volatile SearchWorker currentWorker; | ||
|
|
||
| public CompletableFuture<List<? extends EmiIngredient>> search(String query) { | ||
| synchronized (this) { | ||
| SearchWorker worker = new SearchWorker(query, EmiScreenManager.getSearchSource()); | ||
embeddedt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| currentWorker = worker; | ||
|
|
||
| EmiSearch.executor.execute(worker); | ||
| return worker.getCompletionFuture(); | ||
| } | ||
| } | ||
|
|
||
| public List<? extends EmiIngredient> getStacks() { | ||
| return this.stacks; | ||
| } | ||
|
|
||
| public EmiSearch.CompiledQuery getCompiledQuery() { | ||
| return this.compiledQuery; | ||
| } | ||
|
|
||
| class SearchWorker implements Runnable { | ||
| private final String query; | ||
| private final List<? extends EmiIngredient> source; | ||
| private final CompletableFuture<List<? extends EmiIngredient>> completion; | ||
|
|
||
| SearchWorker(String query, List<? extends EmiIngredient> source) { | ||
| this.query = query; | ||
| this.source = source; | ||
| this.completion = new CompletableFuture<>(); | ||
| } | ||
|
|
||
| public CompletableFuture<List<? extends EmiIngredient>> getCompletionFuture() { | ||
| return completion; | ||
| } | ||
|
|
||
| private void apply(List<? extends EmiIngredient> stacks) { | ||
| synchronized (EmiSearchManager.this) { | ||
| if(this == currentWorker) { | ||
| EmiSearchManager.this.stacks = stacks; | ||
| EmiSearchManager.this.currentWorker = null; | ||
| } | ||
| } | ||
| completion.complete(stacks); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| EmiSearch.CompiledQuery compiled = new EmiSearch.CompiledQuery(query); | ||
| compiledQuery = compiled; | ||
| if (compiled.isEmpty()) { | ||
| apply(source); | ||
| return; | ||
| } | ||
| List<EmiIngredient> stacks = Lists.newArrayList(); | ||
| int processed = 0; | ||
| for (EmiIngredient stack : source) { | ||
| if (processed++ >= 1024) { | ||
| processed = 0; | ||
| if (this != currentWorker) { | ||
| apply(source); | ||
embeddedt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| } | ||
| List<EmiStack> ess = stack.getEmiStacks(); | ||
| // TODO properly support ingredients? | ||
| if (ess.size() == 1) { | ||
| EmiStack es = ess.get(0); | ||
| if (compiled.test(es)) { | ||
| stacks.add(stack); | ||
| } | ||
| } | ||
| } | ||
| apply(List.copyOf(stacks)); | ||
| } catch (Exception e) { | ||
| EmiLog.error("Error when attempting to search:"); | ||
| e.printStackTrace(); | ||
| apply(source); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.