Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/main/java/cpw/mods/inventorysorter/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Config {
final ForgeConfigSpec.ConfigValue<List<? extends String>> containerBlacklist;
final ForgeConfigSpec.ConfigValue<List<? extends String>> slotBlacklist;

final ForgeConfigSpec.ConfigValue<ItemOrdering> itemOrdering;

private Config(ForgeConfigSpec.Builder builder) {
builder.comment("Inventory sorter blacklists");
builder.push("blacklists");
Expand All @@ -31,5 +33,10 @@ private Config(ForgeConfigSpec.Builder builder) {
.translation("inventorysorter.config.slotblacklist")
.defineList("slotBlacklist", new ArrayList<>(), t -> true);
builder.pop();

itemOrdering = builder
.comment("Item ordering method")
.translation("inventorysorter.config.itemordering")
.defineEnum("itemOrdering", ItemOrdering.BY_COUNT);
}
}
12 changes: 12 additions & 0 deletions src/main/java/cpw/mods/inventorysorter/InventorySorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class InventorySorter
final Set<String> imcSlotBlacklist = new HashSet<>();
final Set<ResourceLocation> imcContainerBlacklist = new HashSet<>();

ItemOrdering itemOrdering = ItemOrdering.BY_COUNT;

public InventorySorter() {
INSTANCE = this;
final IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
Expand Down Expand Up @@ -140,11 +142,13 @@ private void updateBlacklists() {
private void updateConfig() {
Config.CONFIG.containerBlacklist.set(containerblacklist.stream().filter(e -> !imcContainerBlacklist.contains(e)).map(Objects::toString).collect(Collectors.toList()));
Config.CONFIG.slotBlacklist.set(slotblacklist.stream().filter(e -> !imcSlotBlacklist.contains(e)).collect(Collectors.toList()));
Config.CONFIG.itemOrdering.set(itemOrdering);

updateBlacklists();
}

void onConfigLoad(ModConfigEvent configEvent) {
itemOrdering = Config.CONFIG.itemOrdering.get();
updateBlacklists();
}

Expand Down Expand Up @@ -205,6 +209,14 @@ static int showBlacklist(final CommandContext<CommandSourceStack> context) {
return 0;
}

static int setItemOrdering(final CommandContext<CommandSourceStack> context) {
final ItemOrdering newOrdering = InventorySorterCommand.Arguments.ORDERING.get(context);
INSTANCE.itemOrdering = newOrdering;
INSTANCE.updateConfig();
context.getSource().sendSuccess(new TranslatableComponent("inventorysorter.commands.inventorysorter.setorder.ok", newOrdering.toString()), true);
return 0;
}

static Stream<String> listContainers() {
return ForgeRegistries.CONTAINERS.getEntries().stream().map(e -> e.getKey().location().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraftforge.server.command.EnumArgument;

public class InventorySorterCommand {
public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
Expand All @@ -43,7 +44,8 @@ private enum CommandAction {
BLADD(InventorySorter::blackListAdd, 1, Arguments.CONTAINER),
BLREMOVE(InventorySorter::blackListRemove, 4, Arguments.BLACKLISTED),
SHOWLAST(InventorySorter::showLast, 1),
LIST(InventorySorter::showBlacklist, 1);
LIST(InventorySorter::showBlacklist, 1),
SETORDERING(InventorySorter::setItemOrdering, 1, Arguments.ORDERING);

private final int permissionLevel;
private final ToIntFunction<CommandContext<CommandSourceStack>> action;
Expand Down Expand Up @@ -74,6 +76,7 @@ public LiteralArgumentBuilder<CommandSourceStack> getCommand() {
public static class Arguments {
static final TypedArgumentHandler<ResourceLocation> CONTAINER = new TypedArgumentHandler<>("container", () -> new ContainerClassArgument(InventorySorter::listContainers));
static final TypedArgumentHandler<ResourceLocation> BLACKLISTED = new TypedArgumentHandler<>("blacklisted", () -> new ContainerClassArgument(InventorySorter::listBlacklist));
static final TypedArgumentHandler<ItemOrdering> ORDERING = new TypedArgumentHandler<>("ordering", ItemOrdering.class, () -> EnumArgument.enumArgument(ItemOrdering.class));
}

public static class TypedArgumentHandler<T> {
Expand All @@ -83,10 +86,12 @@ public static class TypedArgumentHandler<T> {

@SuppressWarnings("unchecked")
TypedArgumentHandler(final String argName, final Supplier<? extends ArgumentType<T>> argumentType) {
this(argName, (Class<T>) TypeResolver.resolveRawArguments(ArgumentType.class, argumentType.get().getClass())[0], argumentType);
}
TypedArgumentHandler(final String argName, Class<T> clazz, final Supplier<? extends ArgumentType<T>> argumentType) {
this.argName = argName;
this.argumentType = argumentType.get();
final Class<?>[] classes = TypeResolver.resolveRawArguments(ArgumentType.class, this.argumentType.getClass());
this.clazz = (Class<T>) classes[0];
this.clazz = clazz;
}

public static <A> TypedArgumentHandler<A> of(final String argumentName, final Supplier<? extends ArgumentType<A>> supplier) {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/cpw/mods/inventorysorter/ItemOrdering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright © 2022 David Koňařík
* This file is part of Inventorysorter.
*
* Inventorysorter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Inventorysorter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Inventorysorter. If not, see <http://www.gnu.org/licenses/>.
*/

package cpw.mods.inventorysorter;

import java.util.Comparator;
import java.util.stream.Stream;

import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;

/**
* Enum deciding how to order items during sorting
*/
public enum ItemOrdering {
BY_COUNT,
BY_MOD_AND_NAME,
BY_NAME;

public Stream<Entry<ItemStackHolder>> ordered(Multiset<ItemStackHolder> itemCounts) {
if (this == BY_COUNT)
return orderedByCount(itemCounts);
if (this == BY_MOD_AND_NAME)
return orderedByModAndName(itemCounts);
if (this == BY_NAME)
return orderedByName(itemCounts);
throw new IllegalStateException();
}

private static Stream<Entry<ItemStackHolder>> orderedByCount(Multiset<ItemStackHolder> itemCounts) {
return itemCounts.entrySet().stream()
.sorted((a, b) -> {
int countComp = Integer.compare(a.getCount(), b.getCount());
if (countComp != 0)
return -countComp; // Descending
return a.getElement().is.getItem().getRegistryName().toString()
.compareTo(b.getElement().is.getItem().getRegistryName().toString());
});
}

private static Stream<Entry<ItemStackHolder>> orderedByModAndName(Multiset<ItemStackHolder> itemCounts) {
return itemCounts.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getElement().is.getItem().getRegistryName().toString()));
}

private static Stream<Entry<ItemStackHolder>> orderedByName(Multiset<ItemStackHolder> itemCounts) {
return itemCounts.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getElement().is.getItem().getRegistryName().getPath()));
}
}
9 changes: 7 additions & 2 deletions src/main/java/cpw/mods/inventorysorter/SortingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

import com.google.common.base.*;
import com.google.common.collect.*;
import com.google.common.collect.Multiset.Entry;

import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.*;

import javax.annotation.*;

import java.util.Comparator;
import java.util.Iterator;
import java.util.function.*;

import net.minecraft.world.inventory.CraftingContainer;
Expand Down Expand Up @@ -123,10 +128,10 @@ private void compactInventory(final ContainerContext context, final Multiset<Ite
}

InventorySorter.INSTANCE.debugLog("Container \"{}\" being sorted", ()->new String[] {containerTypeName.toString()});
final UnmodifiableIterator<Multiset.Entry<ItemStackHolder>> itemsIterator;
final Iterator<Multiset.Entry<ItemStackHolder>> itemsIterator;
try
{
itemsIterator = Multisets.copyHighestCountFirst(itemcounts).entrySet().iterator();
itemsIterator = InventorySorter.INSTANCE.itemOrdering.ordered(itemcounts).iterator();
}
catch (Exception e)
{
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/inventorysorter/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"inventorysorter.commands.inventorysorter.showlast.nosort": "You have not sorted a container yet. Try sorting one!",
"inventorysorter.commands.inventorysorter.showblacklist.message": "Current blacklist §e{0}§f",
"inventorysorter.commands.inventorysorter.showblacklist.empty": "The blacklist is empty",
"inventorysorter.commands.inventorysorter.setorder.ok": "Set ordering method to §e{0}§f",
"inventorysorter.config.containerblacklist": "Container blacklist",
"inventorysorter.config.slotblacklist": "Slot type blacklist"
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/inventorysorter/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ inventorysorter.commands.inventorysorter.blremove.message=Removed %1$s§f from c
inventorysorter.commands.inventorysorter.show.message=Last container sorted %1$s
inventorysorter.commands.inventorysorter.list.message=Current blacklist %1$s
inventorysorter.commands.inventorysorter.list.empty=The blacklist is empty
inventorysorter.commands.inventorysorter.noop=§cThe last command had no effect (maybe try sorting a container?)
inventorysorter.commands.inventorysorter.noop=§cThe last command had no effect (maybe try sorting a container?)
inventorysorter.commands.inventorysorter.setorder.ok=Set ordering method to §e{0}§f