Skip to content

Commit b61d728

Browse files
committed
feat: initial work on creative mode tab sorting
1 parent 3aec8de commit b61d728

5 files changed

Lines changed: 72 additions & 3 deletions

File tree

common/src/main/java/io/github/jamalam360/sort_it_out/SortItOut.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.jamalam360.sort_it_out;
22

3+
import dev.architectury.event.events.common.LifecycleEvent;
34
import io.github.jamalam360.jamlib.JamLib;
45
import io.github.jamalam360.sort_it_out.command.SortItOutCommands;
56
import io.github.jamalam360.sort_it_out.network.PacketHandlers;
7+
import io.github.jamalam360.sort_it_out.util.CreativeModeTabLookup;
68
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
79
import net.minecraft.resources.Identifier;
810
import net.minecraft.server.level.ServerPlayer;
@@ -21,6 +23,7 @@ public static void init() {
2123
JamLib.checkForJarRenaming(SortItOut.class);
2224
SortItOutCommands.register();
2325
PacketHandlers.register();
26+
LifecycleEvent.SERVER_STARTED.register((server) -> CreativeModeTabLookup.INSTANCE.build(server.overworld()));
2427
}
2528

2629
public static Identifier id(String path) {

common/src/main/java/io/github/jamalam360/sort_it_out/command/SortItOutCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private static void registerCommands(CommandDispatcher<CommandSourceStack> dispa
5252
.then(sortingComparator("comparator" + 1).executes(ctx -> setComparators(ctx, 2))
5353
.then(sortingComparator("comparator" + 2).executes(ctx -> setComparators(ctx, 3))
5454
.then(sortingComparator("comparator" + 3).executes(ctx -> setComparators(ctx, 4))
55+
.then(sortingComparator("comparator" + 4).executes(ctx -> setComparators(ctx, 5)))
5556
)
5657
)
5758
)

common/src/main/java/io/github/jamalam360/sort_it_out/preference/UserPreferences.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public Comparator<ItemStack> createComparator() {
2222
for (SortingComparator sortingComparator : this.comparators) {
2323
Comparator<ItemStack> chain = switch (sortingComparator) {
2424
case DISPLAY_NAME -> Comparators.DISPLAY_NAME;
25+
case CREATIVE_TAB -> Comparators.CREATIVE_TAB;
2526
case NAMESPACE -> Comparators.NAMESPACE;
2627
case COUNT -> Comparators.COUNT;
2728
case DURABILITY -> Comparators.DURABILITY;
@@ -36,6 +37,7 @@ public Comparator<ItemStack> createComparator() {
3637
// When adding a new comparator, ensure that the command chain is long enough in {@link SortItOutCommands}
3738
public enum SortingComparator {
3839
DISPLAY_NAME,
40+
CREATIVE_TAB,
3941
NAMESPACE,
4042
COUNT,
4143
DURABILITY
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.github.jamalam360.sort_it_out.util;
22

33
import net.minecraft.core.registries.BuiltInRegistries;
4-
import net.minecraft.world.item.Item;
4+
import net.minecraft.world.item.CreativeModeTab;
55
import net.minecraft.world.item.ItemStack;
6-
import net.minecraft.world.item.TooltipFlag;
7-
import net.minecraft.world.level.Level;
86

97
import java.util.Comparator;
108

@@ -14,4 +12,13 @@ public class Comparators {
1412
public static final Comparator<ItemStack> DURABILITY = Comparator.comparingInt(ItemStack::getDamageValue).reversed();
1513
public static final Comparator<ItemStack> DISPLAY_NAME = Comparator.comparing((stack) -> stack.getDisplayName().getString());
1614
public static final Comparator<ItemStack> NAMESPACE = Comparator.comparing((stack) -> BuiltInRegistries.ITEM.getKey(stack.getItem()).getNamespace());
15+
public static final Comparator<ItemStack> CREATIVE_TAB = Comparator.comparing((stack) -> {
16+
CreativeModeTab tab = CreativeModeTabLookup.INSTANCE.lookup(stack);
17+
18+
if (tab == null) {
19+
return "z";
20+
} else {
21+
return tab.getDisplayName().getString();
22+
}
23+
});
1724
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.jamalam360.sort_it_out.util;
2+
3+
import io.github.jamalam360.sort_it_out.SortItOut;
4+
import net.minecraft.core.registries.BuiltInRegistries;
5+
import net.minecraft.resources.ResourceKey;
6+
import net.minecraft.world.item.CreativeModeTab;
7+
import net.minecraft.world.item.Item;
8+
import net.minecraft.world.item.ItemStack;
9+
import net.minecraft.world.level.Level;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.Collection;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
public class CreativeModeTabLookup {
17+
public static final CreativeModeTabLookup INSTANCE = new CreativeModeTabLookup();
18+
private final HashMap<Item, CreativeModeTab> lookup;
19+
20+
private CreativeModeTabLookup() {
21+
this.lookup = new HashMap<>();
22+
}
23+
24+
@Nullable
25+
public CreativeModeTab lookup(ItemStack stack) {
26+
return this.lookup.get(stack.getItem());
27+
}
28+
29+
public void build(Level level) {
30+
this.lookup.clear();
31+
32+
for (Map.Entry<ResourceKey<CreativeModeTab>, CreativeModeTab> entry : BuiltInRegistries.CREATIVE_MODE_TAB.entrySet()) {
33+
CreativeModeTab tab = entry.getValue();
34+
35+
try {
36+
tab.buildContents(new CreativeModeTab.ItemDisplayParameters(level.enabledFeatures(), false, level.registryAccess()));
37+
} catch (Exception e) {
38+
SortItOut.LOGGER.error("Failed to build tab contents for {}", entry.getKey(), e);
39+
}
40+
41+
this.associate(tab, tab.getDisplayItems());
42+
}
43+
44+
SortItOut.LOGGER.info("Built creative tab lookup with {} entries", this.lookup.size());
45+
}
46+
47+
private void associate(CreativeModeTab tab, Collection<ItemStack> displayItems) {
48+
for (ItemStack stack : displayItems) {
49+
if (this.lookup.containsKey(stack.getItem())) {
50+
continue;
51+
}
52+
53+
this.lookup.put(stack.getItem(), tab);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)