diff --git a/src/main/java/baubles/api/expanded/BaubleItemHelper.java b/src/main/java/baubles/api/expanded/BaubleItemHelper.java index 2c9a6b9..ba3d8f2 100644 --- a/src/main/java/baubles/api/expanded/BaubleItemHelper.java +++ b/src/main/java/baubles/api/expanded/BaubleItemHelper.java @@ -1,6 +1,7 @@ package baubles.api.expanded; import baubles.api.BaublesApi; +import baubles.common.BaublesConfig; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.gui.GuiScreen; @@ -26,9 +27,10 @@ public class BaubleItemHelper { boolean shiftHeld = GuiScreen.isShiftKeyDown(); if(shiftHeld) { tooltip.add(StatCollector.translateToLocal("tooltip.compatibleslots")); - for(int i = 0; i < types.length; i++) { - String type = StatCollector.translateToLocal("slot." + types[i]); - if(i < types.length - 1) type += ","; + String[] displayTypes = BaublesConfig.getDisplayTypesWithUniversalOverrides(types); + for(int i = 0; i < displayTypes.length; i++) { + String type = StatCollector.translateToLocal("slot." + displayTypes[i]); + if(i < displayTypes.length - 1) type += ","; tooltip.add(type); } } else { diff --git a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java index 8f9a7a0..a08b5ed 100644 --- a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java +++ b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java @@ -333,7 +333,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { tooltipIndexCache = slotIndex; - String slotType = BaubleExpandedSlots.getSlotType(slotIndex); + String slotType = slot instanceof SlotBauble ? ((SlotBauble) slot).getSlotType() : BaubleExpandedSlots.unknownType; tooltipCache.clear(); @@ -348,7 +348,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { if (heldItem.getItem() instanceof IBaubleExpanded baubleExpandedItem) { String[] itemBaubleTypes = baubleExpandedItem.getBaubleTypes(heldItem); for(String itemBaubleType : itemBaubleTypes) { - if (itemBaubleType.equals(BaubleExpandedSlots.universalType) || slotType.equals(itemBaubleType)) { + if (BaublesConfig.canTypeFitSlot(itemBaubleType, slotType)) { fitsInSlot = true; break; } @@ -356,7 +356,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { } else if(heldItem.getItem() instanceof IBauble baubleItem) { String itemBaubleType = BaubleExpandedSlots.getTypeFromBaubleType(baubleItem.getBaubleType(heldItem)); - if (itemBaubleType.equals(BaubleExpandedSlots.universalType) || slotType.equals(itemBaubleType)) { + if (BaublesConfig.canTypeFitSlot(itemBaubleType, slotType)) { fitsInSlot = true; } } diff --git a/src/main/java/baubles/common/BaublesConfig.java b/src/main/java/baubles/common/BaublesConfig.java index 32c44d8..b230495 100644 --- a/src/main/java/baubles/common/BaublesConfig.java +++ b/src/main/java/baubles/common/BaublesConfig.java @@ -2,6 +2,7 @@ package baubles.common; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.Map; import baubles.api.expanded.BaubleExpandedSlots; @@ -20,7 +21,9 @@ public class BaublesConfig { public static boolean manualSlotSelection = false; public static boolean displayTooltipOnHover = true; public static String[] slotStackLimitOverrides = new String[] {}; + public static String[] slotUniversalOverrides = new String[] {}; private static final Map stackLimitsByType = new HashMap<>(); + private static final ArrayList universalOverrideTypes = new ArrayList<>(); public static String[] overrideSlotTypes = new String[] { BaubleExpandedSlots.amuletType, @@ -80,8 +83,31 @@ public class BaublesConfig { + "Unspecified types default to a stack limit of 1.\n", currentlyRegisteredTypes.stream().map(type -> type + "=1").toArray(String[]::new) ); + slotUniversalOverrides = config.getStringList("slotUniversalOverrides", categoryOverride, slotUniversalOverrides, + "Bauble types listed here are also allowed in " + BaubleExpandedSlots.universalType + " slots.\n" + + "Use registered slot type names like " + BaubleExpandedSlots.ringType + ", " + BaubleExpandedSlots.amuletType + + ", " + BaubleExpandedSlots.beltType + ".\n", + currentlyRegisteredTypes.toArray(new String[0]) + ); stackLimitsByType.clear(); + universalOverrideTypes.clear(); + for (String type : slotUniversalOverrides) { + if (type == null) continue; + String trimmedType = type.trim(); + if (trimmedType.isEmpty()) continue; + if (!BaubleExpandedSlots.isTypeRegistered(trimmedType)) { + Baubles.log.warn("Ignoring slotUniversalOverrides entry '{}': unknown slot type '{}'", type, trimmedType); + continue; + } + if (trimmedType.equals(BaubleExpandedSlots.universalType) || trimmedType.equals(BaubleExpandedSlots.unknownType)) { + continue; + } + if (!universalOverrideTypes.contains(trimmedType)) { + universalOverrideTypes.add(trimmedType); + } + } + for (String entry : slotStackLimitOverrides) { if (entry == null) continue; String[] split = entry.split("=", 2); @@ -119,4 +145,31 @@ public class BaublesConfig { return stackLimitsByType.getOrDefault(type, 1); } + public static boolean canTypeFitSlot(String itemType, String slotType) { + if (itemType == null || slotType == null) { + return false; + } + if (itemType.equals(BaubleExpandedSlots.universalType) || itemType.equals(slotType)) { + return true; + } + return slotType.equals(BaubleExpandedSlots.universalType) && universalOverrideTypes.contains(itemType); + } + + public static String[] getDisplayTypesWithUniversalOverrides(String[] types) { + if (types == null || types.length == 0) { + return new String[0]; + } + LinkedHashSet displayTypes = new LinkedHashSet<>(); + for (String type : types) { + if (type == null) continue; + String trimmed = type.trim(); + if (trimmed.isEmpty()) continue; + displayTypes.add(trimmed); + if (universalOverrideTypes.contains(trimmed)) { + displayTypes.add(BaubleExpandedSlots.universalType); + } + } + return displayTypes.toArray(new String[0]); + } + } diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index ac2ffb9..fc053a7 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -247,26 +247,16 @@ public class ContainerPlayerExpanded extends Container { returnStack = null; } } else if (item instanceof IBauble bauble && bauble.canEquip(returnStack, thePlayer)) { - for (int baubleSlot = baubleStart; baubleSlot < baubleEnd; baubleSlot++) { - if (returnStack == null) { - break; - } - if (((Slot) inventorySlots.get(baubleSlot)).getHasStack()) { - continue; - } - String[] types; - if (item instanceof IBaubleExpanded) { - types = ((IBaubleExpanded) item).getBaubleTypes(returnStack); - } else { - types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(bauble.getBaubleType(returnStack))}; - } - for (String type : types) { - if ((type.equals(BaubleExpandedSlots.universalType) - || type.equals(BaubleExpandedSlots.getSlotType(baubleSlot - baubleStart))) - && !mergeItemStack(originalStack, baubleSlot, baubleSlot + 1, false)) { - returnStack = null; - } - } + String[] types = item instanceof IBaubleExpanded + ? ((IBaubleExpanded) item).getBaubleTypes(returnStack) + : new String[] {BaubleExpandedSlots.getTypeFromBaubleType(bauble.getBaubleType(returnStack))}; + + // Priority 2: direct slot matches (or universal items) + tryMergeIntoBaubleSlotsByPriority(originalStack, baubleStart, baubleEnd, types, 2); + + // Priority 1: override matches (e.g. amulet/ring/belt into universal) + if (originalStack.stackSize > 0) { + tryMergeIntoBaubleSlotsByPriority(originalStack, baubleStart, baubleEnd, types, 1); } } else if (slotIndex >= baubleEnd && slotIndex < invEnd) { if (!mergeItemStack(originalStack, invEnd, hotbarEnd, false)) { @@ -307,6 +297,38 @@ public class ContainerPlayerExpanded extends Container { super.putStacksInSlots(p_75131_1_); } + private void tryMergeIntoBaubleSlotsByPriority(ItemStack sourceStack, int baubleStart, int baubleEnd, String[] itemTypes, int priority) { + for (int baubleSlot = baubleStart; baubleSlot < baubleEnd && sourceStack.stackSize > 0; baubleSlot++) { + Slot targetInventorySlot = (Slot) inventorySlots.get(baubleSlot); + if (targetInventorySlot.getHasStack()) { + continue; + } + String targetSlotType = targetInventorySlot instanceof SlotBauble + ? ((SlotBauble) targetInventorySlot).getSlotType() + : BaubleExpandedSlots.unknownType; + if (getBestMatchPriority(itemTypes, targetSlotType) != priority) { + continue; + } + mergeItemStack(sourceStack, baubleSlot, baubleSlot + 1, false); + } + } + + private int getBestMatchPriority(String[] itemTypes, String slotType) { + int best = 0; + for (String type : itemTypes) { + if (type == null) { + continue; + } + if (type.equals(BaubleExpandedSlots.universalType) || type.equals(slotType)) { + return 2; + } + if (BaublesConfig.canTypeFitSlot(type, slotType)) { + best = 1; + } + } + return best; + } + protected boolean mergeItemStack(ItemStack sourceStack, int startIndex, int endIndex, boolean reverse, Slot sourceSlot) { boolean merged = false; int index = reverse ? endIndex - 1 : startIndex; diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 9f6c86f..92678c5 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -7,6 +7,7 @@ import baubles.api.BaubleType; import baubles.api.IBauble; import baubles.api.expanded.BaubleExpandedSlots; import baubles.api.expanded.IBaubleExpanded; +import baubles.common.BaublesConfig; import baubles.common.lib.ItemStackHelper; import baubles.common.network.PacketHandler; import baubles.common.network.PacketSyncBauble; @@ -209,7 +210,7 @@ public class InventoryBaubles implements IInventory { } for(String type : types) { - if(type.equals(BaubleExpandedSlots.universalType) || type.equals(slotType)) { + if (BaublesConfig.canTypeFitSlot(type, slotType)) { return true; } } diff --git a/src/main/java/baubles/common/container/SlotBauble.java b/src/main/java/baubles/common/container/SlotBauble.java index 0f0bf15..94dd117 100644 --- a/src/main/java/baubles/common/container/SlotBauble.java +++ b/src/main/java/baubles/common/container/SlotBauble.java @@ -36,6 +36,10 @@ public class SlotBauble extends Slot { } } + public String getSlotType() { + return slotType; + } + /** * Check if the stack is a valid item for this slot. Always true beside for the armor slots. */ @@ -59,7 +63,7 @@ public class SlotBauble extends Slot { } for(String type : types) { - if(type.equals(BaubleExpandedSlots.universalType) || type.equals(slotType)) { + if (BaublesConfig.canTypeFitSlot(type, slotType)) { return true; } }