Skip to content

Commit 76bee8d

Browse files
authored
Toolbelt capability passthrough (#2901)
1 parent c214f00 commit 76bee8d

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/main/java/gregtech/api/capability/GregtechCapabilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gregtech.api.GTValues;
44
import gregtech.api.capability.impl.EUToFEProvider;
5+
import gregtech.api.items.toolitem.ItemGTToolbelt;
56
import gregtech.api.util.GTUtility;
67
import gregtech.common.metatileentities.converter.ConverterTrait;
78

@@ -22,6 +23,9 @@ public class GregtechCapabilities {
2223
@CapabilityInject(IElectricItem.class)
2324
public static Capability<IElectricItem> CAPABILITY_ELECTRIC_ITEM = null;
2425

26+
@CapabilityInject(ItemGTToolbelt.ToolStackHandler.class)
27+
public static Capability<ItemGTToolbelt.ToolStackHandler> CAPABILITY_TOOLBELT_HANDLER = null;
28+
2529
@CapabilityInject(IMultiblockController.class)
2630
public static Capability<IMultiblockController> CAPABILITY_MULTIBLOCK_CONTROLLER = null;
2731

src/main/java/gregtech/api/capability/SimpleCapabilityManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gregtech.api.capability.impl.AbstractRecipeLogic;
44
import gregtech.api.cover.CoverHolder;
5+
import gregtech.api.items.toolitem.ItemGTToolbelt;
56
import gregtech.api.metatileentity.multiblock.IMaintenance;
67
import gregtech.api.worldgen.generator.GTWorldGenCapability;
78
import gregtech.common.metatileentities.converter.ConverterTrait;
@@ -37,6 +38,7 @@ public void readNBT(Capability<T> capability, T instance, EnumFacing side, NBTBa
3738
public static void init() {
3839
registerCapabilityWithNoDefault(IEnergyContainer.class);
3940
registerCapabilityWithNoDefault(IElectricItem.class);
41+
registerCapabilityWithNoDefault(ItemGTToolbelt.ToolStackHandler.class);
4042
registerCapabilityWithNoDefault(IWorkable.class);
4143
registerCapabilityWithNoDefault(CoverHolder.class);
4244
registerCapabilityWithNoDefault(IControllable.class);

src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gregtech.api.items.toolitem;
22

33
import gregtech.api.GregTechAPI;
4+
import gregtech.api.capability.GregtechCapabilities;
45
import gregtech.api.items.IDyeableItem;
56
import gregtech.api.items.gui.ItemUIFactory;
67
import 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

Comments
 (0)