diff --git a/common/src/client/java/io/github/jamalam360/sort_it_out/client/SortItOutClient.java b/common/src/client/java/io/github/jamalam360/sort_it_out/client/SortItOutClient.java index 46d2eae..f17cf67 100644 --- a/common/src/client/java/io/github/jamalam360/sort_it_out/client/SortItOutClient.java +++ b/common/src/client/java/io/github/jamalam360/sort_it_out/client/SortItOutClient.java @@ -18,6 +18,7 @@ import io.github.jamalam360.sort_it_out.network.C2SRequestSortPacket; import io.github.jamalam360.sort_it_out.preference.ServerUserPreferences; import io.github.jamalam360.sort_it_out.sort.ContainerSorterUtil; +import io.github.jamalam360.sort_it_out.util.CreativeModeTabLookup; import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; @@ -50,6 +51,7 @@ public static void init() { KeyMappingRegistry.register(sortKeyMapping); ClientTickEvent.CLIENT_LEVEL_POST.register(SortItOutClient::postLevelTick); ClientPlayLifecycleEvents.JOIN.register((mc) -> CONFIG.get().sync()); + ClientPlayLifecycleEvents.JOIN.register((mc) -> CreativeModeTabLookup.INSTANCE.build(mc.level)); ClientScreenInputEvent.KEY_RELEASED_PRE.register(SortItOutClient::keyReleased); ClientScreenInputEvent.MOUSE_RELEASED_PRE.register(SortItOutClient::mouseReleased); ClientGuiEvent.RENDER_CONTAINER_FOREGROUND.register(SortItOutClient::renderContainerForeground); diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/SortItOut.java b/common/src/main/java/io/github/jamalam360/sort_it_out/SortItOut.java index d4541ed..ef5a22d 100644 --- a/common/src/main/java/io/github/jamalam360/sort_it_out/SortItOut.java +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/SortItOut.java @@ -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; @@ -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) { diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/command/ServerTranslationsHelper.java b/common/src/main/java/io/github/jamalam360/sort_it_out/command/ServerTranslationsHelper.java index fdfee99..79c469c 100644 --- a/common/src/main/java/io/github/jamalam360/sort_it_out/command/ServerTranslationsHelper.java +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/command/ServerTranslationsHelper.java @@ -35,6 +35,7 @@ public static String getTranslation(String language, String key) { LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.comparators.namespace", "By Namespace"); LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.comparators.count", "By Quantity"); LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.comparators.durability", "By Durability"); + LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.comparators.creative_tab", "By Creative Tab"); LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.slotSortingTrigger", "Slot Sorting Trigger"); LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.slotSortingTrigger.tooltip", "The action to use to trigger sorting (along with the keybind and sort buttons, if the mod is installed clientside)"); LANGUAGES.computeIfAbsent("en_us", (ignored) -> new HashMap<>()).put("config.sort_it_out.client_preferences.slotSortingTrigger.press_offhand_key", "Press Offhand Swap Key (replaces vanilla behaviour)"); diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/command/SortItOutCommands.java b/common/src/main/java/io/github/jamalam360/sort_it_out/command/SortItOutCommands.java index 4dcc2e0..6bd6002 100644 --- a/common/src/main/java/io/github/jamalam360/sort_it_out/command/SortItOutCommands.java +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/command/SortItOutCommands.java @@ -3,9 +3,11 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.context.CommandContext; import dev.architectury.event.events.common.CommandRegistrationEvent; import dev.architectury.networking.NetworkManager; +import dev.architectury.platform.Platform; import io.github.jamalam360.jamlib.config.ConfigManager; import io.github.jamalam360.sort_it_out.network.BidirectionalUserPreferencesUpdatePacket; import io.github.jamalam360.sort_it_out.preference.ServerUserPreferences; @@ -13,12 +15,23 @@ import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.entity.BarrelBlockEntity; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import static com.mojang.brigadier.arguments.BoolArgumentType.bool; +import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static io.github.jamalam360.sort_it_out.command.Arguments.*; import static io.github.jamalam360.sort_it_out.command.CommandFeedback.*; import static net.minecraft.commands.Commands.argument; @@ -27,6 +40,10 @@ public class SortItOutCommands { public static void register() { CommandRegistrationEvent.EVENT.register(SortItOutCommands::registerCommands); + + if (Platform.isDevelopmentEnvironment()) { + CommandRegistrationEvent.EVENT.register(SortItOutCommands::registerDevCommands); + } } private static void registerCommands(CommandDispatcher dispatcher, CommandBuildContext register, Commands.CommandSelection selection) { @@ -52,6 +69,7 @@ private static void registerCommands(CommandDispatcher 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))) ) ) ) @@ -67,6 +85,19 @@ private static void registerCommands(CommandDispatcher dispa ); } + private static void registerDevCommands(CommandDispatcher dispatcher, CommandBuildContext register, Commands.CommandSelection selection) { + dispatcher.register( + literal("sortitoutdev") + .then( + literal("barrel") + .then( + argument("size", integer(1, 27)) + .executes(SortItOutCommands::spawnBarrel) + ) + ) + ); + } + private static void modifyConfig(CommandContext ctx, Consumer modifier) { ConfigManager manager = ServerUserPreferences.INSTANCE.getPlayerConfigManager(ctx.getSource().getPlayer()); modifier.accept(manager.get()); @@ -121,4 +152,33 @@ private static int setSlotSortingTrigger(CommandContext ctx) ctx.getSource().sendSuccess(() -> formatSlotSortingTrigger(ctx), false); return Command.SINGLE_SUCCESS; } + + private static int spawnBarrel(CommandContext ctx) { + int size = IntegerArgumentType.getInteger(ctx, "size"); + ServerPlayer player = ctx.getSource().getPlayer(); + Level level = player.level(); + level.setBlock(player.getOnPos(), Blocks.BARREL.defaultBlockState(), Block.UPDATE_ALL); + BarrelBlockEntity blockEntity = (BarrelBlockEntity) level.getBlockEntity(player.getOnPos()); + List items = BuiltInRegistries.ITEM.stream().filter((i) -> i != Items.AIR).toList(); + + while (size != 0) { + int slot = 0; + while (!blockEntity.getItem(slot).isEmpty()) { + slot = level.random.nextInt(27); + } + + Item item = items.get(level.random.nextInt(items.size())); + ItemStack stack = item.getDefaultInstance(); + + if (stack.getMaxStackSize() != 1) { + stack.setCount(level.random.nextInt(1, stack.getMaxStackSize())); + } + + blockEntity.setItem(slot, stack); + size -= 1; + } + + ctx.getSource().sendSuccess(() -> Component.literal("Spawned barrel"), false); + return Command.SINGLE_SUCCESS; + } } diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/preference/UserPreferences.java b/common/src/main/java/io/github/jamalam360/sort_it_out/preference/UserPreferences.java index dc60001..6986f28 100644 --- a/common/src/main/java/io/github/jamalam360/sort_it_out/preference/UserPreferences.java +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/preference/UserPreferences.java @@ -22,6 +22,7 @@ public Comparator createComparator() { for (SortingComparator sortingComparator : this.comparators) { Comparator 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; @@ -36,6 +37,7 @@ public Comparator 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 diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/util/Comparators.java b/common/src/main/java/io/github/jamalam360/sort_it_out/util/Comparators.java index c4cb6d2..76c153f 100644 --- a/common/src/main/java/io/github/jamalam360/sort_it_out/util/Comparators.java +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/util/Comparators.java @@ -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; @@ -14,4 +12,13 @@ public class Comparators { public static final Comparator DURABILITY = Comparator.comparingInt(ItemStack::getDamageValue).reversed(); public static final Comparator DISPLAY_NAME = Comparator.comparing((stack) -> stack.getDisplayName().getString()); public static final Comparator NAMESPACE = Comparator.comparing((stack) -> BuiltInRegistries.ITEM.getKey(stack.getItem()).getNamespace()); + public static final Comparator CREATIVE_TAB = Comparator.comparing((stack) -> { + CreativeModeTab tab = CreativeModeTabLookup.INSTANCE.lookup(stack); + + if (tab == null) { + return null; + } else { + return tab.getDisplayName().getString(); + } + }, Comparator.nullsLast(String::compareTo)); } diff --git a/common/src/main/java/io/github/jamalam360/sort_it_out/util/CreativeModeTabLookup.java b/common/src/main/java/io/github/jamalam360/sort_it_out/util/CreativeModeTabLookup.java new file mode 100644 index 0000000..ca1e21f --- /dev/null +++ b/common/src/main/java/io/github/jamalam360/sort_it_out/util/CreativeModeTabLookup.java @@ -0,0 +1,62 @@ +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.CreativeModeTabs; +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.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class CreativeModeTabLookup { + public static final CreativeModeTabLookup INSTANCE = new CreativeModeTabLookup(); + private final Map lookup; + + private CreativeModeTabLookup() { + this.lookup = new ConcurrentHashMap<>(); + } + + @Nullable + public CreativeModeTab lookup(ItemStack stack) { + return this.lookup.get(stack.getItem()); + } + + public void build(Level level) { + this.lookup.clear(); + + for (Map.Entry, CreativeModeTab> entry : BuiltInRegistries.CREATIVE_MODE_TAB.entrySet()) { + CreativeModeTab tab = entry.getValue(); + + if (tab.isAlignedRight() && entry.getKey() != CreativeModeTabs.OP_BLOCKS) { + continue; + } + + 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); + continue; + } + + this.associate(tab, tab.getDisplayItems()); + } + + SortItOut.LOGGER.info("Built creative tab lookup with {} entries", this.lookup.size()); + } + + private void associate(CreativeModeTab tab, Collection displayItems) { + for (ItemStack stack : displayItems) { + if (this.lookup.containsKey(stack.getItem())) { + continue; + } + + this.lookup.put(stack.getItem(), tab); + } + } +} diff --git a/common/src/main/resources/assets/sort_it_out/lang/en_us.json b/common/src/main/resources/assets/sort_it_out/lang/en_us.json index 647fbb7..d6b772e 100644 --- a/common/src/main/resources/assets/sort_it_out/lang/en_us.json +++ b/common/src/main/resources/assets/sort_it_out/lang/en_us.json @@ -21,6 +21,7 @@ "config.sort_it_out.client_preferences.comparators.namespace": "By Namespace", "config.sort_it_out.client_preferences.comparators.count": "By Quantity", "config.sort_it_out.client_preferences.comparators.durability": "By Durability", + "config.sort_it_out.client_preferences.comparators.creative_tab": "By Creative Tab", "config.sort_it_out.client_preferences.slotSortingTrigger": "Slot Sorting Trigger", "config.sort_it_out.client_preferences.slotSortingTrigger.tooltip": "The action to use to trigger sorting (along with the keybind and sort buttons, if the mod is installed clientside)", "config.sort_it_out.client_preferences.slotSortingTrigger.press_offhand_key": "Press Offhand Swap Key (replaces vanilla behaviour)",