Skip to content

Commit

Permalink
Code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
aratakileo committed Nov 26, 2023
1 parent c27d1f6 commit d3f495c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SuggestionsAPI implements ClientModInitializer {
private final static ArrayList<Injector> injectors = new ArrayList<>();
private final static ArrayList<Supplier<@NotNull List<Suggestion>>> resourceDependedSuggestionContainers = new ArrayList<>();

private static HashMap<String, Suggestion> dynamicSuggestions;
private static HashMap<String, Suggestion> tempSuggestions;
private static boolean areResourcesLoaded = false;

@Override
Expand Down Expand Up @@ -76,8 +76,8 @@ public static void removeSuggestion(@NotNull String suggestionText) {
public static @Nullable Suggestion getSuggestion(@NotNull String suggestionText) {
return suggestions.containsKey(suggestionText)
? suggestions.get(suggestionText)
: Objects.nonNull(dynamicSuggestions)
? dynamicSuggestions.get(suggestionText)
: Objects.nonNull(tempSuggestions)
? tempSuggestions.get(suggestionText)
: null;
}

Expand All @@ -89,7 +89,7 @@ public static void registerSuggestionsInjector(@NotNull Injector injector) {
return new SuggestionsProcessor.Builder(
suggestions,
injectors,
newDynamicSuggestions -> dynamicSuggestions = newDynamicSuggestions
newTempSuggestions -> tempSuggestions = newTempSuggestions
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public class SuggestionsProcessor {
private final int wordStart;
private final HashMap<String, Suggestion> suggestions;
private final ArrayList<Injector> injectors;
private final Consumer<HashMap<String, Suggestion>> dynamicSuggestionsConsumer;
private final Consumer<HashMap<String, Suggestion>> tempSuggestionsConsumer;
private final BiConsumer<String, List<com.mojang.brigadier.suggestion.Suggestion>> newSuggestionsApplier;

private SuggestionsProcessor(
@NotNull HashMap<String, Suggestion> suggestions,
@NotNull ArrayList<Injector> injectors,
@NotNull Consumer<HashMap<String, Suggestion>> dynamicSuggestionsConsumer,
@NotNull Consumer<HashMap<String, Suggestion>> tempSuggestionsConsumer,
@NotNull BiConsumer<String, List<com.mojang.brigadier.suggestion.Suggestion>> newSuggestionsApplier,
@NotNull String textUptoCursor,
int wordStart
) {
this.textUptoCursor = textUptoCursor;
this.suggestions = suggestions;
this.injectors = injectors;
this.dynamicSuggestionsConsumer = dynamicSuggestionsConsumer;
this.tempSuggestionsConsumer = tempSuggestionsConsumer;
this.newSuggestionsApplier = newSuggestionsApplier;
this.wordStart = wordStart;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public boolean process() {

final var applicableMojangSuggestions = new ArrayList<com.mojang.brigadier.suggestion.Suggestion>();

var dynamicSuggestions = new HashMap<String, Suggestion>();
var tempSuggestions = new HashMap<String, Suggestion>();

for (final var injectorEntry: suggestionsInjectorsBuffer.entrySet()) {
final var injector = injectorEntry.getKey();
Expand All @@ -150,11 +150,11 @@ public boolean process() {
suggestion.getSuggestionText()
));

dynamicSuggestions.put(suggestion.getSuggestionText(), suggestion);
tempSuggestions.put(suggestion.getSuggestionText(), suggestion);
}
}

dynamicSuggestionsConsumer.accept(dynamicSuggestions);
tempSuggestionsConsumer.accept(tempSuggestions);

suggestions.forEach((suggestionText, suggestion) -> {
if (suggestion.shouldShowFor(currentExpression))
Expand Down Expand Up @@ -195,7 +195,7 @@ public boolean process() {
public static @Nullable SuggestionsProcessor from(
@NotNull HashMap<String, Suggestion> suggestions,
@NotNull ArrayList<Injector> injectors,
@NotNull Consumer<HashMap<String, Suggestion>> dynamicSuggestionsConsumer,
@NotNull Consumer<HashMap<String, Suggestion>> tempSuggestionsConsumer,
@NotNull BiConsumer<String, List<com.mojang.brigadier.suggestion.Suggestion>> newSuggestionsApplier,
@NotNull String textUptoCursor
) {
Expand All @@ -212,7 +212,7 @@ public boolean process() {
return new SuggestionsProcessor(
suggestions,
injectors,
dynamicSuggestionsConsumer,
tempSuggestionsConsumer,
newSuggestionsApplier,
textUptoCursor,
wordStart
Expand All @@ -222,19 +222,19 @@ public boolean process() {
public static class Builder {
private final HashMap<String, Suggestion> suggestions;
private final ArrayList<Injector> injectors;
private final Consumer<HashMap<String, Suggestion>> dynamicSuggestionsConsumer;
private final Consumer<HashMap<String, Suggestion>> tempSuggestionsConsumer;

private String textUptoCursor;
private BiConsumer<String, List<com.mojang.brigadier.suggestion.Suggestion>> newSuggestionsApplier;

public Builder(
@NotNull HashMap<String, Suggestion> suggestions,
@NotNull ArrayList<Injector> injectors,
@NotNull Consumer<HashMap<String, Suggestion>> dynamicSuggestionsConsumer
@NotNull Consumer<HashMap<String, Suggestion>> tempSuggestionsConsumer
) {
this.suggestions = suggestions;
this.injectors = injectors;
this.dynamicSuggestionsConsumer = dynamicSuggestionsConsumer;
this.tempSuggestionsConsumer = tempSuggestionsConsumer;
}

public Builder setOtherValues(
Expand All @@ -250,7 +250,7 @@ public Builder setOtherValues(
return SuggestionsProcessor.from(
suggestions,
injectors,
dynamicSuggestionsConsumer,
tempSuggestionsConsumer,
newSuggestionsApplier,
textUptoCursor
);
Expand Down

0 comments on commit d3f495c

Please sign in to comment.