Skip to content

Commit

Permalink
Expose slotless item handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jan 16, 2017
1 parent adafccc commit 4bf524e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod_version=1.4.2
minecraft_version=1.10.2
forge_version=12.18.2.2116
mcp_mappings_version=snapshot_20160701
cyclopscore_version=0.9.0-464
cyclopscore_version=0.9.0-470
release_type=release

ironchest_version=7.0.9.796
3 changes: 3 additions & 0 deletions src/main/java/org/cyclops/colossalchests/Capabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import org.cyclops.commoncapabilities.api.capability.inventorystate.IInventoryState;
import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler;

/**
* Used capabilities for this mod.
Expand All @@ -11,4 +12,6 @@
public class Capabilities {
@CapabilityInject(IInventoryState.class)
public static Capability<IInventoryState> INVENTORY_STATE = null;
@CapabilityInject(ISlotlessItemHandler.class)
public static Capability<ISlotlessItemHandler> SLOTLESS_ITEMHANDLER = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import gnu.trove.map.TIntObjectMap;
import lombok.experimental.Delegate;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
Expand All @@ -22,23 +23,25 @@
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.ArrayUtils;
import org.cyclops.colossalchests.Capabilities;
import org.cyclops.colossalchests.ColossalChests;
import org.cyclops.colossalchests.GeneralConfig;
import org.cyclops.colossalchests.block.*;
import org.cyclops.colossalchests.inventory.container.ContainerColossalChest;
import org.cyclops.cyclopscore.block.multi.*;
import org.cyclops.cyclopscore.datastructure.EnumFacingMap;
import org.cyclops.cyclopscore.helper.*;
import org.cyclops.cyclopscore.inventory.INBTInventory;
import org.cyclops.cyclopscore.inventory.LargeInventory;
import org.cyclops.cyclopscore.inventory.SimpleInventory;
import org.cyclops.cyclopscore.inventory.*;
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
import org.cyclops.cyclopscore.tileentity.CyclopsTileEntity;
import org.cyclops.cyclopscore.tileentity.InventoryTileEntityBase;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* A machine that can infuse things with blood.
Expand Down Expand Up @@ -104,6 +107,48 @@ public Vec3i getMaximumSize() {
private Block block = ColossalChest.getInstance();
private EnumFacingMap<int[]> facingSlots = EnumFacingMap.newMap();

public TileColossalChest() {
if (Capabilities.SLOTLESS_ITEMHANDLER != null) {
addSlotlessItemHandlerCapability();
}
}

protected void addSlotlessItemHandlerCapability() {
IItemHandler itemHandler = getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
addCapabilityInternal(Capabilities.SLOTLESS_ITEMHANDLER,
new IndexedSlotlessItemHandlerWrapper(itemHandler, new IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference() {
@Override
public int getInventoryStackLimit() {
return getInventory().getInventoryStackLimit();
}

@Override
public Map<Item, TIntObjectMap<ItemStack>> getIndex() {
return ((IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference) getInventory()).getIndex();
}

@Override
public int getFirstEmptySlot() {
return ((IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference) getInventory()).getFirstEmptySlot();
}

@Override
public int getLastEmptySlot() {
return ((IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference) getInventory()).getLastEmptySlot();
}

@Override
public int getFirstNonEmptySlot() {
return ((IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference) getInventory()).getFirstNonEmptySlot();
}

@Override
public int getLastNonEmptySlot() {
return ((IndexedSlotlessItemHandlerWrapper.IInventoryIndexReference) getInventory()).getLastNonEmptySlot();
}
}));
}

/**
* @return the size
*/
Expand Down Expand Up @@ -168,15 +213,15 @@ public int getSizeSingular() {
return getSize().getX() + 1;
}

protected LargeInventory constructInventory() {
protected IndexedInventory constructInventory() {
if (GeneralConfig.creativeChests) {
return constructInventoryDebug();
}
return new LargeInventory(calculateInventorySize(), ColossalChestConfig._instance.getNamedId(), 64);
return new IndexedInventory(calculateInventorySize(), ColossalChestConfig._instance.getNamedId(), 64);
}

protected LargeInventory constructInventoryDebug() {
LargeInventory inv = new LargeInventory(calculateInventorySize(), ColossalChestConfig._instance.getNamedId(), 64);
protected IndexedInventory constructInventoryDebug() {
IndexedInventory inv = new IndexedInventory(calculateInventorySize(), ColossalChestConfig._instance.getNamedId(), 64);
for (int i = 0; i < inv.getSizeInventory(); i++) {
inv.setInventorySlotContents(i, new ItemStack(Item.REGISTRY.getRandomObject(worldObj.rand)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import net.minecraftforge.items.wrapper.InvWrapper;
import org.cyclops.colossalchests.Capabilities;
import org.cyclops.commoncapabilities.api.capability.inventorystate.IInventoryState;
import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler;
import org.cyclops.cyclopscore.helper.TileHelpers;
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
import org.cyclops.cyclopscore.tileentity.CyclopsTileEntity;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;

/**
Expand All @@ -39,12 +41,19 @@ public TileInterface() {
if (Capabilities.INVENTORY_STATE != null) {
addInventoryStateCapability();
}
if (Capabilities.SLOTLESS_ITEMHANDLER != null) {
addSlotlessItemHandlerCapability();
}
}

protected void addInventoryStateCapability() {
addCapabilityInternal(Capabilities.INVENTORY_STATE, new TileInterfaceInventoryState(this));
}

protected void addSlotlessItemHandlerCapability() {
addCapabilityInternal(Capabilities.SLOTLESS_ITEMHANDLER, new TileInterfaceSlotlessItemHandler(this));
}

public void setCorePosition(Vec3i corePosition) {
this.corePosition = corePosition;
coreReference = new WeakReference<TileColossalChest>(null);
Expand Down Expand Up @@ -259,4 +268,43 @@ public int getHash() {
}
}

/**
* {@link IInventoryState} implementation for the {@link TileInterface} that proxies a {@link TileColossalChest}.
* @author rubensworks
*/
public static class TileInterfaceSlotlessItemHandler implements ISlotlessItemHandler {

private final TileInterface tile;

public TileInterfaceSlotlessItemHandler(TileInterface tile) {
this.tile = tile;
}

protected @Nullable ISlotlessItemHandler getHandler() {
TileColossalChest core = tile.getCore();
if (core != null) {
return core.hasCapability(Capabilities.SLOTLESS_ITEMHANDLER, null) ? core.getCapability(Capabilities.SLOTLESS_ITEMHANDLER, null) : null;
}
return null;
}

@Override
public ItemStack insertItem(ItemStack stack, boolean simulate) {
ISlotlessItemHandler handler = getHandler();
return handler != null ? handler.insertItem(stack, simulate) : stack;
}

@Override
public ItemStack extractItem(int amount, boolean simulate) {
ISlotlessItemHandler handler = getHandler();
return handler != null ? handler.extractItem(amount, simulate) : null;
}

@Override
public ItemStack extractItem(ItemStack matchStack, int matchFlags, boolean simulate) {
ISlotlessItemHandler handler = getHandler();
return handler != null ? handler.extractItem(matchStack, matchFlags, simulate) : null;
}
}

}

0 comments on commit 4bf524e

Please sign in to comment.