Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
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 @@ -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)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@
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;
import io.github.jamalam360.sort_it_out.preference.UserPreferences;
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;
Expand All @@ -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<CommandSourceStack> dispatcher, CommandBuildContext register, Commands.CommandSelection selection) {
Expand All @@ -52,6 +69,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 All @@ -67,6 +85,19 @@ private static void registerCommands(CommandDispatcher<CommandSourceStack> dispa
);
}

private static void registerDevCommands(CommandDispatcher<CommandSourceStack> 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<CommandSourceStack> ctx, Consumer<UserPreferences> modifier) {
ConfigManager<UserPreferences> manager = ServerUserPreferences.INSTANCE.getPlayerConfigManager(ctx.getSource().getPlayer());
modifier.accept(manager.get());
Expand Down Expand Up @@ -121,4 +152,33 @@ private static int setSlotSortingTrigger(CommandContext<CommandSourceStack> ctx)
ctx.getSource().sendSuccess(() -> formatSlotSortingTrigger(ctx), false);
return Command.SINGLE_SUCCESS;
}

private static int spawnBarrel(CommandContext<CommandSourceStack> 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<Item> 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;
}
}
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 null;
} else {
return tab.getDisplayName().getString();
}
}, Comparator.nullsLast(String::compareTo));
}
Original file line number Diff line number Diff line change
@@ -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<Item, CreativeModeTab> 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<ResourceKey<CreativeModeTab>, 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<ItemStack> displayItems) {
for (ItemStack stack : displayItems) {
if (this.lookup.containsKey(stack.getItem())) {
continue;
}

this.lookup.put(stack.getItem(), tab);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down