Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.jamalam360.sort_it_out;

import dev.architectury.event.events.common.LifecycleEvent;
import io.github.jamalam360.jamlib.JamLib;
import io.github.jamalam360.sort_it_out.command.SortItOutCommands;
import io.github.jamalam360.sort_it_out.network.PacketHandlers;
import io.github.jamalam360.sort_it_out.util.CreativeModeTabLookup;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -21,6 +23,7 @@ public static void init() {
JamLib.checkForJarRenaming(SortItOut.class);
SortItOutCommands.register();
PacketHandlers.register();
LifecycleEvent.SERVER_STARTED.register((server) -> CreativeModeTabLookup.INSTANCE.build(server.overworld()));
}

public static Identifier id(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private static void registerCommands(CommandDispatcher<CommandSourceStack> dispa
.then(sortingComparator("comparator" + 1).executes(ctx -> setComparators(ctx, 2))
.then(sortingComparator("comparator" + 2).executes(ctx -> setComparators(ctx, 3))
.then(sortingComparator("comparator" + 3).executes(ctx -> setComparators(ctx, 4))
.then(sortingComparator("comparator" + 4).executes(ctx -> setComparators(ctx, 5)))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public Comparator<ItemStack> createComparator() {
for (SortingComparator sortingComparator : this.comparators) {
Comparator<ItemStack> chain = switch (sortingComparator) {
case DISPLAY_NAME -> Comparators.DISPLAY_NAME;
case CREATIVE_TAB -> Comparators.CREATIVE_TAB;
case NAMESPACE -> Comparators.NAMESPACE;
case COUNT -> Comparators.COUNT;
case DURABILITY -> Comparators.DURABILITY;
Expand All @@ -36,6 +37,7 @@ public Comparator<ItemStack> createComparator() {
// When adding a new comparator, ensure that the command chain is long enough in {@link SortItOutCommands}
public enum SortingComparator {
DISPLAY_NAME,
CREATIVE_TAB,
NAMESPACE,
COUNT,
DURABILITY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.github.jamalam360.sort_it_out.util;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;

import java.util.Comparator;

Expand All @@ -14,4 +12,13 @@ public class Comparators {
public static final Comparator<ItemStack> DURABILITY = Comparator.comparingInt(ItemStack::getDamageValue).reversed();
public static final Comparator<ItemStack> DISPLAY_NAME = Comparator.comparing((stack) -> stack.getDisplayName().getString());
public static final Comparator<ItemStack> NAMESPACE = Comparator.comparing((stack) -> BuiltInRegistries.ITEM.getKey(stack.getItem()).getNamespace());
public static final Comparator<ItemStack> CREATIVE_TAB = Comparator.comparing((stack) -> {
CreativeModeTab tab = CreativeModeTabLookup.INSTANCE.lookup(stack);

if (tab == null) {
return "z";
} else {
return tab.getDisplayName().getString();
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.jamalam360.sort_it_out.util;

import io.github.jamalam360.sort_it_out.SortItOut;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class CreativeModeTabLookup {
public static final CreativeModeTabLookup INSTANCE = new CreativeModeTabLookup();
private final HashMap<Item, CreativeModeTab> lookup;

private CreativeModeTabLookup() {
this.lookup = new HashMap<>();
}

@Nullable
public CreativeModeTab lookup(ItemStack stack) {
return this.lookup.get(stack.getItem());
}

public void build(Level level) {
this.lookup.clear();

for (Map.Entry<ResourceKey<CreativeModeTab>, CreativeModeTab> entry : BuiltInRegistries.CREATIVE_MODE_TAB.entrySet()) {
CreativeModeTab tab = entry.getValue();

try {
tab.buildContents(new CreativeModeTab.ItemDisplayParameters(level.enabledFeatures(), false, level.registryAccess()));
} catch (Exception e) {
SortItOut.LOGGER.error("Failed to build tab contents for {}", entry.getKey(), e);
}

this.associate(tab, tab.getDisplayItems());
}

SortItOut.LOGGER.info("Built creative tab lookup with {} entries", this.lookup.size());
}

private void associate(CreativeModeTab tab, Collection<ItemStack> displayItems) {
for (ItemStack stack : displayItems) {
if (this.lookup.containsKey(stack.getItem())) {
continue;
}

this.lookup.put(stack.getItem(), tab);
}
}
}
Loading