ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/util/compat/CommonCapsUtil.java

93 lines
4.4 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.util.compat;
2019-05-02 09:10:29 +02:00
import org.cyclops.commoncapabilities.api.capability.itemhandler.DefaultSlotlessItemHandlerWrapper;
import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler;
import de.ellpeck.actuallyadditions.common.tile.TileEntityItemViewer;
import de.ellpeck.actuallyadditions.common.tile.TileEntityItemViewer.SlotlessItemHandlerInfo;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
2019-05-02 09:10:29 +02:00
public final class CommonCapsUtil {
2019-05-02 09:10:29 +02:00
public static ISlotlessItemHandler createSlotlessItemViewerHandler(final TileEntityItemViewer tile, IItemHandler normalHandler) {
return new DefaultSlotlessItemHandlerWrapper(normalHandler) {
@Override
2019-05-02 09:10:29 +02:00
public ItemStack insertItem(ItemStack stack, boolean simulate) {
ItemStack remain = stack.copy();
2019-05-02 09:10:29 +02:00
for (SlotlessItemHandlerInfo handler : tile.slotlessInfos) {
if (handler.isLoaded() && tile.isWhitelisted(handler, stack, false)) {
if (handler.handler instanceof ISlotlessItemHandler) {
remain = ((ISlotlessItemHandler) handler.handler).insertItem(stack, simulate);
2019-05-02 09:10:29 +02:00
if (!ItemStack.areItemStacksEqual(remain, stack) && !simulate) {
tile.markDirty();
tile.doItemParticle(stack, handler.relayInQuestion.getPos(), tile.connectedRelay.getPos());
}
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(remain)) { return StackUtil.getEmpty(); }
}
}
}
return super.insertItem(remain, simulate);
}
@Override
2019-05-02 09:10:29 +02:00
public ItemStack extractItem(int amount, boolean simulate) {
for (SlotlessItemHandlerInfo handler : tile.slotlessInfos) {
if (handler.isLoaded()) {
if (handler.handler instanceof ISlotlessItemHandler) {
ISlotlessItemHandler slotless = (ISlotlessItemHandler) handler.handler;
ItemStack would = slotless.extractItem(amount, true);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(would)) {
if (tile.isWhitelisted(handler, would, true)) {
ItemStack has;
2019-05-02 09:10:29 +02:00
if (simulate) {
has = would;
2019-05-02 09:10:29 +02:00
} else {
has = slotless.extractItem(amount, false);
}
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(has) && !simulate) {
tile.markDirty();
tile.doItemParticle(has, tile.connectedRelay.getPos(), handler.relayInQuestion.getPos());
}
return has;
}
}
}
}
}
return super.extractItem(amount, simulate);
}
@Override
2019-05-02 09:10:29 +02:00
public ItemStack extractItem(ItemStack matchStack, int matchFlags, boolean simulate) {
for (SlotlessItemHandlerInfo handler : tile.slotlessInfos) {
if (handler.isLoaded()) {
if (handler.handler instanceof ISlotlessItemHandler) {
ISlotlessItemHandler slotless = (ISlotlessItemHandler) handler.handler;
ItemStack would = slotless.extractItem(matchStack, matchFlags, true);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(would)) {
if (tile.isWhitelisted(handler, would, true)) {
if (simulate) {
return would;
2019-05-02 09:10:29 +02:00
} else {
return slotless.extractItem(matchStack, matchFlags, false);
}
}
}
}
}
}
return super.extractItem(matchStack, matchFlags, simulate);
}
};
}
}