11package gregtech .api .items .toolitem ;
22
33import gregtech .api .GregTechAPI ;
4+ import gregtech .api .capability .GregtechCapabilities ;
45import gregtech .api .items .IDyeableItem ;
56import gregtech .api .items .gui .ItemUIFactory ;
67import gregtech .api .items .toolitem .behavior .IToolBehavior ;
@@ -141,8 +142,8 @@ public ModularPanel buildUI(HandGuiData guiData, PanelSyncManager guiSyncManager
141142 (newItem , onlyAmountChanged , client , init ) -> handler
142143 .onContentsChanged (index )))
143144 .background (GTGuiTextures .SLOT , GTGuiTextures .TOOL_SLOT_OVERLAY )
144- .debugName ("slot_" + index ))
145- .debugName ("toolbelt_inventory" ))
145+ .name ("slot_" + index ))
146+ .name ("toolbelt_inventory" ))
146147 .bindPlayerInventory ();
147148 }
148149
@@ -265,7 +266,7 @@ public boolean shouldCauseReequipAnimation(@NotNull ItemStack oldStack, @NotNull
265266 }
266267
267268 @ Override
268- public int getMetadata (ItemStack stack ) {
269+ public int getMetadata (@ NotNull ItemStack stack ) {
269270 ItemStack selected = getHandler (stack ).getSelectedStack ();
270271 if (!selected .isEmpty ()) {
271272 return selected .getItem ().getMetadata (selected );
@@ -420,7 +421,7 @@ public boolean supportsTool(ItemStack stack, ItemStack tool) {
420421 }
421422
422423 private ToolStackHandler getHandler (ItemStack stack ) {
423- IItemHandler handler = stack .getCapability (CapabilityItemHandler . ITEM_HANDLER_CAPABILITY , null );
424+ IItemHandler handler = stack .getCapability (GregtechCapabilities . CAPABILITY_TOOLBELT_HANDLER , null );
424425 if (handler instanceof ToolStackHandler h ) return h ;
425426 else return FALLBACK ;
426427 }
@@ -441,8 +442,7 @@ public void changeSelectedToolMousewheel(int direction, ItemStack stack) {
441442 @ SideOnly (Side .CLIENT )
442443 public void changeSelectedToolHotkey (int slot , ItemStack stack ) {
443444 ToolStackHandler handler = getHandler (stack );
444- if (slot < 0 || slot >= handler .getSlots ()) handler .selectedSlot = -1 ;
445- else handler .selectedSlot = slot ;
445+ handler .setSelectedSlot (slot );
446446 PacketToolbeltSelectionChange .toServer (handler .selectedSlot );
447447 }
448448
@@ -569,14 +569,25 @@ public ToolbeltCapabilityProvider(ItemStack stack) {
569569
570570 @ Override
571571 public boolean hasCapability (@ NotNull Capability <?> capability , EnumFacing facing ) {
572- return capability == CapabilityItemHandler .ITEM_HANDLER_CAPABILITY ;
572+ if (capability == GregtechCapabilities .CAPABILITY_TOOLBELT_HANDLER )
573+ return true ;
574+ ItemStack selected = getHandler ().getSelectedStack ();
575+ if (!selected .isEmpty ()) {
576+ return selected .hasCapability (capability , facing );
577+ } else return capability == CapabilityItemHandler .ITEM_HANDLER_CAPABILITY ;
573578 }
574579
575580 @ Override
576581 public <T > T getCapability (@ NotNull Capability <T > capability , EnumFacing facing ) {
577- if (capability == CapabilityItemHandler .ITEM_HANDLER_CAPABILITY )
582+ if (capability == GregtechCapabilities .CAPABILITY_TOOLBELT_HANDLER )
583+ return GregtechCapabilities .CAPABILITY_TOOLBELT_HANDLER .cast (this .getHandler ());
584+ ItemStack selected = getHandler ().getSelectedStack ();
585+ if (!selected .isEmpty ()) {
586+ return selected .getCapability (capability , facing );
587+ } else if (capability == CapabilityItemHandler .ITEM_HANDLER_CAPABILITY ) {
588+ // if nothing is selected, expose the handler under the item handler capability
578589 return CapabilityItemHandler .ITEM_HANDLER_CAPABILITY .cast (this .getHandler ());
579- else return null ;
590+ } else return null ;
580591 }
581592
582593 @ Override
@@ -616,7 +627,7 @@ public void readNBTShareTag(ItemStack stack, NBTTagCompound nbt) {
616627
617628 protected static final ToolStackHandler FALLBACK = new ToolStackHandler (0 );
618629
619- protected static class ToolStackHandler extends ItemStackHandler {
630+ public static class ToolStackHandler extends ItemStackHandler {
620631
621632 private static final Set <String > EMPTY = ImmutableSet .of ();
622633
@@ -646,7 +657,8 @@ public int getSelectedSlot() {
646657 }
647658
648659 public void setSelectedSlot (int selectedSlot ) {
649- this .selectedSlot = Math .min (getSlots () - 1 , Math .max (selectedSlot , -1 ));
660+ if (selectedSlot >= getSlots () || selectedSlot < 0 ) this .selectedSlot = -1 ;
661+ else this .selectedSlot = selectedSlot ;
650662 }
651663
652664 public void enablePassthrough () {
0 commit comments