11package stackabletools
22
33import net.minecraft.item.ArmorItem
4- import net.minecraft.item.ElytraItem
5- import net.minecraft.item.EnchantedBookItem
4+ import net.minecraft.item.Item
65import net.minecraft.item.ItemStack
76import net.minecraft.item.PotionItem
87import net.minecraft.item.SwordItem
9- import net.minecraft.item.ToolItem
108import net.minecraft.item.TridentItem
119import net.minecraft.registry.Registries
1210import java.util.concurrent.ConcurrentHashMap
@@ -17,7 +15,7 @@ import net.minecraft.item.Items
1715
1816object StackableToolsUtils {
1917
20- private val stackableCache = ConcurrentHashMap <String , Boolean >()
18+ private val stackableCache = ConcurrentHashMap <Item , Boolean >()
2119 private var lastConfigHash = 0
2220
2321 /* *
@@ -50,7 +48,7 @@ object StackableToolsUtils {
5048 if (a.item != = b.item) return false
5149
5250 // RULE: We only stack FRESH items (damage == 0).
53- if (a.item is ToolItem || a.item is ArmorItem || a.item is SwordItem || a.item is TridentItem || a.item is ElytraItem ) {
51+ if (a.item is ArmorItem || a.item is SwordItem || a.item is TridentItem || a.item == = Items . ELYTRA ) {
5452 if (a.damage > 0 || b.damage > 0 ) return false
5553 }
5654
@@ -83,16 +81,31 @@ object StackableToolsUtils {
8381 lastConfigHash = configHash
8482 }
8583
86- val itemId = Registries . ITEM .getId( stack.item).toString()
87- return stackableCache.getOrPut(itemId ) {
88- computeIsStackable(stack, config, itemId )
84+ val item = stack.item
85+ return stackableCache.getOrPut(item ) {
86+ computeIsStackable(stack, config, item )
8987 }
9088 }
9189
9290 /* *
9391 * Internal logic to compute if an item is stackable.
9492 */
95- private fun computeIsStackable (stack : ItemStack , config : stackabletools.config.StackableToolsConfig , itemId : String ): Boolean {
93+ fun maxStackFor (stack : ItemStack , cfg : stackabletools.config.StackableToolsConfig .StackingConfig ): Int {
94+ val hasToolComponent = stack.get(DataComponentTypes .TOOL ) != null
95+
96+ return when {
97+ stack.item is SwordItem || stack.item is TridentItem -> cfg.maxWeaponsStackSize
98+ hasToolComponent && stack.item !is SwordItem && stack.item !is TridentItem -> cfg.maxToolStackSize
99+ stack.item is ArmorItem -> cfg.maxArmorPieceStackSize
100+ stack.item is PotionItem -> cfg.maxPotionStackSize
101+ stack.item == = Items .ENCHANTED_BOOK -> cfg.maxEnchantedBooksStackSize
102+ stack.item == = Items .ELYTRA -> cfg.maxElytraStackSize
103+ else -> cfg.maxStackSize
104+ }.toInt().coerceAtLeast(1 )
105+ }
106+
107+ private fun computeIsStackable (stack : ItemStack , config : stackabletools.config.StackableToolsConfig , item : Item ): Boolean {
108+ val itemId = Registries .ITEM .getId(item).toString()
96109 val shortItemId = itemId.substringAfter(' :' )
97110
98111 val excluded = config.stacking.excludedItemIds
@@ -101,13 +114,15 @@ object StackableToolsUtils {
101114 val active = config.stacking.activeCategories
102115 val isAll = StackingCategory .ALL in active
103116
104- when (stack.item) {
105- is SwordItem , is TridentItem -> if (isAll || StackingCategory .WEAPONS in active) return true
106- is ToolItem -> if (isAll || StackingCategory .TOOLS in active) return true
107- is ArmorItem -> if (isAll || StackingCategory .ARMORS in active) return true
108- is PotionItem -> if (isAll || StackingCategory .POTIONS in active) return true
109- is EnchantedBookItem -> if (isAll || StackingCategory .ENCHANTED_BOOKS in active) return true
110- is ElytraItem -> if (isAll || StackingCategory .ELYTRA in active) return true
117+ val hasToolComponent = stack.get(DataComponentTypes .TOOL ) != null
118+
119+ when {
120+ stack.item is SwordItem || stack.item is TridentItem -> if (isAll || StackingCategory .WEAPONS in active) return true
121+ hasToolComponent && stack.item !is SwordItem && stack.item !is TridentItem -> if (isAll || StackingCategory .TOOLS in active) return true
122+ stack.item is ArmorItem -> if (isAll || StackingCategory .ARMORS in active) return true
123+ stack.item is PotionItem -> if (isAll || StackingCategory .POTIONS in active) return true
124+ stack.item == = Items .ENCHANTED_BOOK -> if (isAll || StackingCategory .ENCHANTED_BOOKS in active) return true
125+ stack.item == = Items .ELYTRA -> if (isAll || StackingCategory .ELYTRA in active) return true
111126 }
112127
113128 val manual = config.stacking.manualStackableItemIds
0 commit comments