diff --git a/src/main/java/gregtech/api/capability/GregtechCapabilities.java b/src/main/java/gregtech/api/capability/GregtechCapabilities.java index df163402aeb..8d6bc2c799f 100644 --- a/src/main/java/gregtech/api/capability/GregtechCapabilities.java +++ b/src/main/java/gregtech/api/capability/GregtechCapabilities.java @@ -2,6 +2,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.impl.EUToFEProvider; +import gregtech.api.items.toolitem.ItemGTToolbelt; import gregtech.api.util.GTUtility; import gregtech.common.metatileentities.converter.ConverterTrait; @@ -22,6 +23,9 @@ public class GregtechCapabilities { @CapabilityInject(IElectricItem.class) public static Capability CAPABILITY_ELECTRIC_ITEM = null; + @CapabilityInject(ItemGTToolbelt.ToolStackHandler.class) + public static Capability CAPABILITY_TOOLBELT_HANDLER = null; + @CapabilityInject(IMultiblockController.class) public static Capability CAPABILITY_MULTIBLOCK_CONTROLLER = null; diff --git a/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java b/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java index f517b473bab..9532b4e361c 100644 --- a/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java +++ b/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java @@ -2,6 +2,7 @@ import gregtech.api.capability.impl.AbstractRecipeLogic; import gregtech.api.cover.CoverHolder; +import gregtech.api.items.toolitem.ItemGTToolbelt; import gregtech.api.metatileentity.multiblock.IMaintenance; import gregtech.api.worldgen.generator.GTWorldGenCapability; import gregtech.common.metatileentities.converter.ConverterTrait; @@ -37,6 +38,7 @@ public void readNBT(Capability capability, T instance, EnumFacing side, NBTBa public static void init() { registerCapabilityWithNoDefault(IEnergyContainer.class); registerCapabilityWithNoDefault(IElectricItem.class); + registerCapabilityWithNoDefault(ItemGTToolbelt.ToolStackHandler.class); registerCapabilityWithNoDefault(IWorkable.class); registerCapabilityWithNoDefault(CoverHolder.class); registerCapabilityWithNoDefault(IControllable.class); diff --git a/src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java b/src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java index b8fd6434292..ca4fb88843c 100644 --- a/src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java +++ b/src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java @@ -1,6 +1,7 @@ package gregtech.api.items.toolitem; import gregtech.api.GregTechAPI; +import gregtech.api.capability.GregtechCapabilities; import gregtech.api.items.IDyeableItem; import gregtech.api.items.gui.ItemUIFactory; import gregtech.api.items.toolitem.behavior.IToolBehavior; @@ -141,8 +142,8 @@ public ModularPanel buildUI(HandGuiData guiData, PanelSyncManager guiSyncManager (newItem, onlyAmountChanged, client, init) -> handler .onContentsChanged(index))) .background(GTGuiTextures.SLOT, GTGuiTextures.TOOL_SLOT_OVERLAY) - .debugName("slot_" + index)) - .debugName("toolbelt_inventory")) + .name("slot_" + index)) + .name("toolbelt_inventory")) .bindPlayerInventory(); } @@ -265,7 +266,7 @@ public boolean shouldCauseReequipAnimation(@NotNull ItemStack oldStack, @NotNull } @Override - public int getMetadata(ItemStack stack) { + public int getMetadata(@NotNull ItemStack stack) { ItemStack selected = getHandler(stack).getSelectedStack(); if (!selected.isEmpty()) { return selected.getItem().getMetadata(selected); @@ -420,7 +421,7 @@ public boolean supportsTool(ItemStack stack, ItemStack tool) { } private ToolStackHandler getHandler(ItemStack stack) { - IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); + IItemHandler handler = stack.getCapability(GregtechCapabilities.CAPABILITY_TOOLBELT_HANDLER, null); if (handler instanceof ToolStackHandler h) return h; else return FALLBACK; } @@ -441,8 +442,7 @@ public void changeSelectedToolMousewheel(int direction, ItemStack stack) { @SideOnly(Side.CLIENT) public void changeSelectedToolHotkey(int slot, ItemStack stack) { ToolStackHandler handler = getHandler(stack); - if (slot < 0 || slot >= handler.getSlots()) handler.selectedSlot = -1; - else handler.selectedSlot = slot; + handler.setSelectedSlot(slot); PacketToolbeltSelectionChange.toServer(handler.selectedSlot); } @@ -569,14 +569,25 @@ public ToolbeltCapabilityProvider(ItemStack stack) { @Override public boolean hasCapability(@NotNull Capability capability, EnumFacing facing) { - return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; + if (capability == GregtechCapabilities.CAPABILITY_TOOLBELT_HANDLER) + return true; + ItemStack selected = getHandler().getSelectedStack(); + if (!selected.isEmpty()) { + return selected.hasCapability(capability, facing); + } else return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; } @Override public T getCapability(@NotNull Capability capability, EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + if (capability == GregtechCapabilities.CAPABILITY_TOOLBELT_HANDLER) + return GregtechCapabilities.CAPABILITY_TOOLBELT_HANDLER.cast(this.getHandler()); + ItemStack selected = getHandler().getSelectedStack(); + if (!selected.isEmpty()) { + return selected.getCapability(capability, facing); + } else if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { + // if nothing is selected, expose the handler under the item handler capability return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(this.getHandler()); - else return null; + } else return null; } @Override @@ -616,7 +627,7 @@ public void readNBTShareTag(ItemStack stack, NBTTagCompound nbt) { protected static final ToolStackHandler FALLBACK = new ToolStackHandler(0); - protected static class ToolStackHandler extends ItemStackHandler { + public static class ToolStackHandler extends ItemStackHandler { private static final Set EMPTY = ImmutableSet.of(); @@ -646,7 +657,8 @@ public int getSelectedSlot() { } public void setSelectedSlot(int selectedSlot) { - this.selectedSlot = Math.min(getSlots() - 1, Math.max(selectedSlot, -1)); + if (selectedSlot >= getSlots() || selectedSlot < 0) this.selectedSlot = -1; + else this.selectedSlot = selectedSlot; } public void enablePassthrough() {