Skip to content

Commit

Permalink
Category sorting (#1)
Browse files Browse the repository at this point in the history
- Categories of the same mod are now sorted together in the wildcard tab.
- Vanilla categories are sorted in the same order as in the vanilla screen.
- Vanilla categories and tab are now sorted before modded ones.
  • Loading branch information
Estecka authored Mar 9, 2024
1 parent 1385cf0 commit c277a3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# v1
## 1.0
Initial release

## 1.1
- Categories of the same mod are now sorted together in the wildcard tab.
- Vanilla categories are sorted in the same order as in the vanilla screen.
- Vanilla categories and tab are now sorted before modded ones.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ loader_version=0.15.7
fabric_version=0.87.2+1.19.4

# Mod Properties
mod_version=1.0.0
mod_version=1.1.0
maven_group=tk.estecka.cloth-gamerules
archives_base_name=cloth-gamerules

Expand Down
36 changes: 28 additions & 8 deletions src/main/java/tk/estecka/clothgamerules/GamerulesMenuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import me.shedaniel.clothconfig2.gui.entries.TextListEntry;
import me.shedaniel.clothconfig2.impl.builders.AbstractFieldBuilder;
import me.shedaniel.clothconfig2.impl.builders.StringFieldBuilder;
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
import me.shedaniel.clothconfig2.impl.builders.TextDescriptionBuilder;
import net.fabricmc.fabric.api.gamerule.v1.CustomGameRuleCategory;
Expand Down Expand Up @@ -56,23 +55,21 @@ static public Screen CreateScreen(Screen parent, Text title, GameRules rules, Ga
final ConfigBuilder builder = ConfigBuilder.create();
final ConfigEntryBuilder entries = builder.entryBuilder();

Map<String, ConfigCategory> tabs = new HashMap<>();
// Map<Identifier, SubCategoryBuilder> subs = new HashMap<>();
Map<Identifier, CategoryEntries> subs = new HashMap<>();
Map<Identifier, GameRules.Category> vanillaCats = new HashMap<>();

builder.setParentScreen(parent);
builder.setTitle(title);
builder.setSavingRunnable(() -> onClose.accept(Optional.of(rules)));

final var wildcard = builder.getOrCreateCategory(WILDCARD_TITLE);
builder.setFallbackCategory(wildcard);

GameRules.accept(new GameRules.Visitor() {
@Override public <T extends Rule<T>> void visit(Key<T> key, Type<T> type){
IRuleCategory cat = GetCategory(key);
Identifier catId = cat.GetId();

tabs.computeIfAbsent(catId.getNamespace(), ns -> builder.getOrCreateCategory(Text.literal(ns)));
vanillaCats.computeIfAbsent(catId, __-> key.getCategory());

// var sub = subs.computeIfAbsent(catId, id -> entries.startSubCategory(cat.GetTitle()));
var sub = subs.computeIfAbsent(catId, id -> new CategoryEntries(entries, cat));

Expand All @@ -88,12 +85,34 @@ static public Screen CreateScreen(Screen parent, Text title, GameRules rules, Ga
}
});

var sortedSubs = subs.entrySet().stream().sorted((a,b)->{
Identifier idA=a.getKey(), idB=b.getKey();
boolean mA, mB;
mA = a.getKey().getNamespace().equals("minecraft");
mB = b.getKey().getNamespace().equals("minecraft");

// Sort vanilla categories above modded ones.
if (mA != mB)
return -Boolean.compare(mA, mB);
// Sort vanilla rules in the same order as the vanilla screen.
else if (mA && mB)
return vanillaCats.get(idA).compareTo(vanillaCats.get(idB));
else {
int diff = idA.getNamespace().compareTo(idB.getNamespace());
if (diff != 0)
return diff;
else
return idA.getPath().compareTo(idB.getPath());
}
});

for (var entry : subs.entrySet()) {
Map<String, ConfigCategory> tabs = new HashMap<>();
final var wildcard = builder.getOrCreateCategory(WILDCARD_TITLE);
for (var entry : sortedSubs.toList()) {
Identifier id = entry.getKey();
ConfigCategory tab = tabs.get(id.getNamespace());
// SubCategoryBuilder sub = entry.getValue();
CategoryEntries sub = entry.getValue();
ConfigCategory tab = tabs.computeIfAbsent(id.getNamespace(), ns -> builder.getOrCreateCategory(Text.literal(ns)));

// sub.setExpanded(true);
// tab.addEntry(sub.build());
Expand All @@ -102,6 +121,7 @@ static public Screen CreateScreen(Screen parent, Text title, GameRules rules, Ga
sub.entries.forEach(e -> {tab.addEntry(e); wildcard.addEntry(e);});
}

builder.setFallbackCategory(wildcard);
return builder.build();
}

Expand Down

0 comments on commit c277a3f

Please sign in to comment.