-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from P3pp3rF1y/1.20.x-dev
Release merge
- Loading branch information
Showing
5 changed files
with
126 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package reliquary.util; | ||
|
||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraftforge.items.IItemHandler; | ||
import net.minecraftforge.items.wrapper.EmptyHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
// combines multiple IItemHandler into one interface | ||
public class CombinedItemHandler implements IItemHandler { | ||
|
||
protected final IItemHandler[] itemHandler; // the handlers | ||
protected final int[] baseIndex; // index-offsets of the different handlers | ||
protected final int slotCount; // number of total slots | ||
|
||
public CombinedItemHandler(IItemHandler... itemHandler) { | ||
this.itemHandler = itemHandler; | ||
baseIndex = new int[itemHandler.length]; | ||
int index = 0; | ||
for (int i = 0; i < itemHandler.length; i++) { | ||
index += itemHandler[i].getSlots(); | ||
baseIndex[i] = index; | ||
} | ||
slotCount = index; | ||
} | ||
|
||
// returns the handler index for the slot | ||
protected int getIndexForSlot(int slot) { | ||
if (slot < 0) {return -1;} | ||
|
||
for (int i = 0; i < baseIndex.length; i++) { | ||
if (slot - baseIndex[i] < 0) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
protected IItemHandler getHandlerFromIndex(int index) { | ||
if (index < 0 || index >= itemHandler.length) { | ||
return EmptyHandler.INSTANCE; | ||
} | ||
return itemHandler[index]; | ||
} | ||
|
||
protected int getSlotFromIndex(int slot, int index) { | ||
if (index <= 0 || index >= baseIndex.length) { | ||
return slot; | ||
} | ||
return slot - baseIndex[index - 1]; | ||
} | ||
|
||
@Override | ||
public int getSlots() { | ||
return slotCount; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public ItemStack getStackInSlot(int slot) { | ||
int index = getIndexForSlot(slot); | ||
IItemHandler handler = getHandlerFromIndex(index); | ||
slot = getSlotFromIndex(slot, index); | ||
return handler.getStackInSlot(slot); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) { | ||
int index = getIndexForSlot(slot); | ||
IItemHandler handler = getHandlerFromIndex(index); | ||
slot = getSlotFromIndex(slot, index); | ||
return handler.insertItem(slot, stack, simulate); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public ItemStack extractItem(int slot, int amount, boolean simulate) { | ||
int index = getIndexForSlot(slot); | ||
IItemHandler handler = getHandlerFromIndex(index); | ||
slot = getSlotFromIndex(slot, index); | ||
return handler.extractItem(slot, amount, simulate); | ||
} | ||
|
||
@Override | ||
public int getSlotLimit(int slot) { | ||
int index = getIndexForSlot(slot); | ||
IItemHandler handler = getHandlerFromIndex(index); | ||
int localSlot = getSlotFromIndex(slot, index); | ||
return handler.getSlotLimit(localSlot); | ||
} | ||
|
||
@Override | ||
public boolean isItemValid(int slot, @NotNull ItemStack stack) { | ||
int index = getIndexForSlot(slot); | ||
IItemHandler handler = getHandlerFromIndex(index); | ||
int localSlot = getSlotFromIndex(slot, index); | ||
return handler.isItemValid(localSlot, stack); | ||
} | ||
} |