Retrofit corporea to work with capabilities

This commit is contained in:
Vincent Lee committed 2016-03-07 13:09:59 -06:00
1 parent cda20175c8
commit 9f1a2b8a7d
17 files changed
+132 -117

No files matched your search

@@ -32,8 +32,8 @@ import vazkii.botania.api.BotaniaAPI;
public final class CorporeaHelper {
private static final List<IInventory> empty = ImmutableList.of();
private static final WeakHashMap<List<ICorporeaSpark>, List<IInventory>> cachedNetworks = new WeakHashMap<>();
private static final List<InvWithLocation> empty = ImmutableList.of();
private static final WeakHashMap<List<ICorporeaSpark>, List<InvWithLocation>> cachedNetworks = new WeakHashMap<>();
private static final List<ICorporeaAutoCompleteController> autoCompleteControllers = new ArrayList<>();
private static final Pattern patternControlCode = Pattern.compile("(?i)\\u00A7[0-9A-FK-OR]");
@@ -55,23 +55,23 @@ public final class CorporeaHelper {
* Gets a list of all the inventories on this spark network. This list is cached for use once every tick,
* and if something changes during that tick it'll still have the first result.
*/
public static List<IInventory> getInventoriesOnNetwork(ICorporeaSpark spark) {
public static List<InvWithLocation> getInventoriesOnNetwork(ICorporeaSpark spark) {
ICorporeaSpark master = spark.getMaster();
if(master == null)
return empty;
List<ICorporeaSpark> network = master.getConnections();
if(cachedNetworks.containsKey(network)) {
List<IInventory> cache = cachedNetworks.get(network);
List<InvWithLocation> cache = cachedNetworks.get(network);
if(cache != null)
return cache;
}
List<IInventory> inventories = new ArrayList<>();
List<InvWithLocation> inventories = new ArrayList<>();
if(network != null)
for(ICorporeaSpark otherSpark : network)
if(otherSpark != null) {
IInventory inv = otherSpark.getSparkInventory();
InvWithLocation inv = otherSpark.getSparkInventory();
if(inv != null)
inventories.add(inv);
}
@@ -86,7 +86,7 @@ public final class CorporeaHelper {
* called instead if the context for those exists to avoid having to get the values again.
*/
public static int getCountInNetwork(ItemStack stack, ICorporeaSpark spark, boolean checkNBT) {
List<IInventory> inventories = getInventoriesOnNetwork(spark);
List<InvWithLocation> inventories = getInventoriesOnNetwork(spark);
return getCountInNetwork(stack, inventories, checkNBT);
}
@@ -95,18 +95,18 @@ public final class CorporeaHelper {
* The higher level function that use a Map< IInventory, Integer > should be
* called instead if the context for this exists to avoid having to get the value again.
*/
public static int getCountInNetwork(ItemStack stack, List<IInventory> inventories, boolean checkNBT) {
Map<IInventory, Integer> map = getInventoriesWithItemInNetwork(stack, inventories, checkNBT);
public static int getCountInNetwork(ItemStack stack, List<InvWithLocation> inventories, boolean checkNBT) {
Map<InvWithLocation, Integer> map = getInventoriesWithItemInNetwork(stack, inventories, checkNBT);
return getCountInNetwork(stack, map, checkNBT);
}
/**
* Gets the amount of available items in the network of the type passed in, checking NBT or not.
*/
public static int getCountInNetwork(ItemStack stack, Map<IInventory, Integer> inventories, boolean checkNBT) {
public static int getCountInNetwork(ItemStack stack, Map<InvWithLocation, Integer> inventories, boolean checkNBT) {
int count = 0;
for(IInventory inv : inventories.keySet())
for(InvWithLocation inv : inventories.keySet())
count += inventories.get(inv);
return count;
@@ -117,8 +117,8 @@ public final class CorporeaHelper {
* The higher level function that use a List< IInventory > should be
* called instead if the context for this exists to avoid having to get the value again.
*/
public static Map<IInventory, Integer> getInventoriesWithItemInNetwork(ItemStack stack, ICorporeaSpark spark, boolean checkNBT) {
List<IInventory> inventories = getInventoriesOnNetwork(spark);
public static Map<InvWithLocation, Integer> getInventoriesWithItemInNetwork(ItemStack stack, ICorporeaSpark spark, boolean checkNBT) {
List<InvWithLocation> inventories = getInventoriesOnNetwork(spark);
return getInventoriesWithItemInNetwork(stack, inventories, checkNBT);
}
@@ -127,8 +127,8 @@ public final class CorporeaHelper {
* The deeper level function that use a List< IInventory > should be
* called instead if the context for this exists to avoid having to get the value again.
*/
public static Map<IInventory, Integer> getInventoriesWithItemInNetwork(ItemStack stack,List<IInventory> inventories, boolean checkNBT) {
Map<IInventory, Integer> countMap = new HashMap<IInventory, Integer>();
public static Map<InvWithLocation, Integer> getInventoriesWithItemInNetwork(ItemStack stack, List<InvWithLocation> inventories, boolean checkNBT) {
Map<InvWithLocation, Integer> countMap = new HashMap<>();
List<IWrappedInventory> wrappedInventories = BotaniaAPI.internalHandler.wrapInventory(inventories);
for (IWrappedInventory inv : wrappedInventories) {
CorporeaRequest request = new CorporeaRequest(stack, checkNBT, -1);
@@ -174,7 +174,7 @@ public final class CorporeaHelper {
if(MinecraftForge.EVENT_BUS.post(event))
return stacks;
List<IInventory> inventories = getInventoriesOnNetwork(spark);
List<InvWithLocation> inventories = getInventoriesOnNetwork(spark);
List<IWrappedInventory> inventoriesW = BotaniaAPI.internalHandler.wrapInventory(inventories);
Map<ICorporeaInterceptor, ICorporeaSpark> interceptors = new HashMap<ICorporeaInterceptor, ICorporeaSpark>();
@@ -183,9 +183,9 @@ public final class CorporeaHelper {
for(IWrappedInventory inv : inventoriesW) {
ICorporeaSpark invSpark = inv.getSpark();
Object originalInventory = inv.getWrappedObject();
if(originalInventory instanceof ICorporeaInterceptor) {
ICorporeaInterceptor interceptor = (ICorporeaInterceptor) originalInventory;
InvWithLocation originalInventory = inv.getWrappedObject();
if(originalInventory.world.getTileEntity(originalInventory.pos) instanceof ICorporeaInterceptor) {
ICorporeaInterceptor interceptor = (ICorporeaInterceptor) originalInventory.world.getTileEntity(originalInventory.pos);
interceptor.interceptRequest(matcher, itemCount, invSpark, spark, stacks, inventories, doit);
interceptors.put(interceptor, invSpark);
}
@@ -209,11 +209,8 @@ public final class CorporeaHelper {
/**
* Gets the spark attached to the inventory passed case it's a TileEntity.
*/
public static ICorporeaSpark getSparkForInventory(IInventory inv) {
if(!(inv instanceof TileEntity))
return null;
TileEntity tile = (TileEntity) inv;
public static ICorporeaSpark getSparkForInventory(InvWithLocation inv) {
TileEntity tile = inv.world.getTileEntity(inv.pos);
return getSparkForBlock(tile.getWorld(), tile.getPos());
}
@@ -234,13 +231,6 @@ public final class CorporeaHelper {
return getSparkForBlock(world, pos) != null;
}
/**
* Gets if the slot passed in can be extracted from by a spark.
*/
public static boolean isValidSlot(IInventory inv, int slot) {
return !(inv instanceof ISidedInventory) || arrayHas(((ISidedInventory) inv).getSlotsForFace(EnumFacing.UP), slot) && ((ISidedInventory) inv).canExtractItem(slot, inv.getStackInSlot(slot), EnumFacing.UP);
}
/**
* Gets if two stacks match.
*/
@@ -283,17 +273,6 @@ public final class CorporeaHelper {
cachedNetworks.clear();
}
/**
* Helper method to check if an int array contains an int.
*/
public static boolean arrayHas(int[] arr, int val) {
for (int element : arr)
if(element == val)
return true;
return false;
}
/**
* Helper method to make stacksMatch() less messy.
*/
@@ -26,13 +26,13 @@ public interface ICorporeaInterceptor {
* at this point, but the list of stacks is not complete. The request parameter can
* be either a String or ItemStack.
*/
public void interceptRequest(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<IInventory> inventories, boolean doit);
public void interceptRequest(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<InvWithLocation> inventories, boolean doit);
/**
* Intercepts a request after all the stacks have been found and processed. Both the
* list of inventories and stacks is complete at this point. The request parameter can
* be either a String or ItemStack.
*/
public void interceptRequestLast(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<IInventory> inventories, boolean doit);
public void interceptRequestLast(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<InvWithLocation> inventories, boolean doit);
}
@@ -12,7 +12,6 @@ package vazkii.botania.api.corporea;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
/**
@@ -38,7 +37,7 @@ public interface ICorporeaSpark {
/**
* Gets the inventory this spark is bound to, generally the one right below it.
*/
public IInventory getSparkInventory();
public InvWithLocation getSparkInventory();
/**
* Gets the list of sparks this spark is connected to, see registerConnections(). This list
@@ -24,7 +24,7 @@ public interface IWrappedInventory {
/**
* Break encapsulation and exposes original inventory.
*/
IInventory getWrappedObject();
InvWithLocation getWrappedObject();
/**
* Counts items in the inventory matching the request
@@ -0,0 +1,32 @@
package vazkii.botania.api.corporea;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
public class InvWithLocation {
public final IItemHandler handler;
public final World world;
public final BlockPos pos;
public InvWithLocation(IItemHandler itemHandler, World world, BlockPos pos) {
this.handler = itemHandler;
this.world = world;
this.pos = pos;
}
@Override
public int hashCode() {
return 31 * handler.hashCode() ^ world.hashCode() ^ pos.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof InvWithLocation
&& handler.equals(((InvWithLocation) o).handler)
&& world == ((InvWithLocation) o).world
&& pos.equals(((InvWithLocation) o).pos);
}
}
@@ -28,6 +28,7 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import vazkii.botania.api.boss.IBotaniaBoss;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.api.lexicon.LexiconPage;
import vazkii.botania.api.lexicon.multiblock.MultiblockSet;
import vazkii.botania.api.recipe.RecipeBrew;
@@ -229,7 +230,7 @@ public class DummyMethodHandler implements IInternalMethodHandler {
}
@Override
public List<IWrappedInventory> wrapInventory(List<IInventory> inventories) {
public List<IWrappedInventory> wrapInventory(List<InvWithLocation> inventories) {
return null;
}
@@ -27,6 +27,7 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import vazkii.botania.api.boss.IBotaniaBoss;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.api.lexicon.LexiconPage;
import vazkii.botania.api.lexicon.multiblock.MultiblockSet;
import vazkii.botania.api.recipe.RecipeBrew;
@@ -128,6 +129,6 @@ public interface IInternalMethodHandler {
/**
* Wrap inventories in the network into wrappers providing compatibility for storage mods.
*/
List<IWrappedInventory> wrapInventory(List<IInventory> inventories);
List<IWrappedInventory> wrapInventory(List<InvWithLocation> inventories);
}
@@ -38,6 +38,7 @@ import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import org.lwjgl.opengl.GL11;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.mana.IManaItem;
import vazkii.botania.api.subtile.RadiusDescriptor;
@@ -96,12 +97,12 @@ public class SubTileHopperhock extends SubTileFunctional {
for(EnumFacing dir : EnumFacing.VALUES) {
BlockPos pos_ = pos.offset(dir);
IItemHandler inv = InventoryHelper2.getInventory(supertile.getWorld(), pos_, dir);
InvWithLocation inv = InventoryHelper2.getInventory(supertile.getWorld(), pos_, dir);
if(inv != null) {
List<ItemStack> filter = getFilterForInventory(pos_, true);
boolean canAccept = canAcceptItem(stack, filter, filterType);
ItemStack simulate = ItemHandlerHelper.insertItem(inv, stack.copy(), true);
ItemStack simulate = ItemHandlerHelper.insertItem(inv.handler, stack.copy(), true);
int availablePut = supertile.getWorld().isRemote ? 1 : stack.stackSize - (simulate == null ? 0 : simulate.stackSize);
canAccept &= availablePut > 0;
@@ -113,7 +114,7 @@ public class SubTileHopperhock extends SubTileFunctional {
if(priorityInv && !priority)
break setInv;
invToPutItemIn = inv;
invToPutItemIn = inv.handler;
priorityInv = priority;
sideToPutItemIn = dir.getOpposite();
amountToPutIn = availablePut;
@@ -22,6 +22,7 @@ import net.minecraft.util.EnumFacing;
import vazkii.botania.api.corporea.CorporeaHelper;
import vazkii.botania.api.corporea.ICorporeaInterceptor;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.api.state.BotaniaStateProps;
import vazkii.botania.common.lib.LibMisc;
@@ -38,12 +39,12 @@ public class TileCorporeaInterceptor extends TileCorporeaBase implements ICorpor
}
@Override
public void interceptRequest(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<IInventory> inventories, boolean doit) {
public void interceptRequest(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<InvWithLocation> inventories, boolean doit) {
// NO-OP
}
@Override
public void interceptRequestLast(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<IInventory> inventories, boolean doit) {
public void interceptRequestLast(Object request, int count, ICorporeaSpark spark, ICorporeaSpark source, List<ItemStack> stacks, List<InvWithLocation> inventories, boolean doit) {
List<ItemStack> filter = getFilter();
for(ItemStack stack : filter)
if(requestMatches(request, stack)) {
@@ -55,7 +56,7 @@ public class TileCorporeaInterceptor extends TileCorporeaBase implements ICorpor
worldObj.setBlockState(getPos(), worldObj.getBlockState(getPos()).withProperty(BotaniaStateProps.POWERED, true), 1 | 2);
worldObj.scheduleUpdate(getPos(), getBlockType(), 2);
TileEntity requestor = (TileEntity) source.getSparkInventory();
TileEntity requestor = source.getSparkInventory().world.getTileEntity(source.getSparkInventory().pos);
for(EnumFacing dir : LibMisc.CARDINAL_DIRECTIONS) {
TileEntity tile = worldObj.getTileEntity(pos.offset(dir));
if(tile != null && tile instanceof TileCorporeaRetainer)
@@ -17,6 +17,7 @@ import net.minecraft.util.BlockPos;
import vazkii.botania.api.corporea.CorporeaHelper;
import vazkii.botania.api.corporea.ICorporeaRequestor;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.common.block.tile.TileMod;
public class TileCorporeaRetainer extends TileMod {
@@ -60,9 +61,9 @@ public class TileCorporeaRetainer extends TileMod {
ICorporeaSpark spark = CorporeaHelper.getSparkForBlock(worldObj, requestPos);
if(spark != null) {
IInventory inv = spark.getSparkInventory();
if(inv != null && inv instanceof ICorporeaRequestor) {
ICorporeaRequestor requestor = (ICorporeaRequestor) inv;
InvWithLocation inv = spark.getSparkInventory();
if(inv != null && inv.world.getTileEntity(inv.pos) instanceof ICorporeaRequestor) {
ICorporeaRequestor requestor = (ICorporeaRequestor) inv.world.getTileEntity(inv.pos);
requestor.doCorporeaRequest(request, requestCount, spark);
pendingRequest = false;
worldObj.updateComparatorOutputLevel(pos, worldObj.getBlockState(pos).getBlock());
@@ -33,6 +33,7 @@ import vazkii.botania.api.boss.IBotaniaBoss;
import vazkii.botania.api.corporea.CorporeaHelper;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.api.internal.DummyMethodHandler;
import vazkii.botania.api.internal.IManaNetwork;
import vazkii.botania.api.lexicon.LexiconPage;
@@ -264,9 +265,9 @@ public class InternalMethodHandler extends DummyMethodHandler {
@Override
public List<IWrappedInventory> wrapInventory(List<IInventory> inventories) {
ArrayList<IWrappedInventory> arrayList = new ArrayList<IWrappedInventory>();
for(IInventory inv : inventories) {
public List<IWrappedInventory> wrapInventory(List<InvWithLocation> inventories) {
List<IWrappedInventory> arrayList = new ArrayList<IWrappedInventory>();
for(InvWithLocation inv : inventories) {
ICorporeaSpark spark = CorporeaHelper.getSparkForInventory(inv);
IWrappedInventory wrapped = null;
// try StorageDrawers integration
@@ -14,28 +14,30 @@ import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import vazkii.botania.api.corporea.InvWithLocation;
public final class InventoryHelper2 {
public static IItemHandler getInventory(World world, BlockPos pos, EnumFacing side) {
public static InvWithLocation getInventory(World world, BlockPos pos, EnumFacing side) {
TileEntity te = world.getTileEntity(pos);
if(te == null)
return null;
IItemHandler ret = te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side) ?
te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side) : null;
if(te instanceof TileEntityChest) {
Block chestBlock = world.getBlockState(pos).getBlock();
if(world.getBlockState(pos.west()).getBlock() == chestBlock)
return new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) world.getTileEntity(pos.west()), (ILockableContainer) te));
ret = new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) world.getTileEntity(pos.west()), (ILockableContainer) te));
if(world.getBlockState(pos.east()).getBlock() == chestBlock)
return new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) te, (ILockableContainer) world.getTileEntity(pos.east())));
ret = new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) te, (ILockableContainer) world.getTileEntity(pos.east())));
if(world.getBlockState(pos.north()).getBlock() == chestBlock)
return new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) world.getTileEntity(pos.north()), (ILockableContainer) te));
ret = new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) world.getTileEntity(pos.north()), (ILockableContainer) te));
if(world.getBlockState(pos.south()).getBlock() == chestBlock)
return new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) te, (ILockableContainer) world.getTileEntity(pos.south())));
ret = new InvWrapper(new InventoryLargeChest("Large chest", (ILockableContainer) te, (ILockableContainer) world.getTileEntity(pos.south())));
}
return te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side) ?
te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side) : null;
return ret == null ? null : new InvWithLocation(ret, world, pos);
}
private InventoryHelper2() {}
@@ -25,7 +25,9 @@ import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.common.core.helper.InventoryHelper;
import vazkii.botania.common.core.helper.InventoryHelper2;
import vazkii.botania.common.item.ModItems;
public class EntityCorporeaSpark extends Entity implements ICorporeaSpark {
@@ -72,7 +74,7 @@ public class EntityCorporeaSpark extends Entity implements ICorporeaSpark {
@Override
public void onUpdate() {
super.onUpdate();
IInventory inv = getSparkInventory();
InvWithLocation inv = getSparkInventory();
if(inv == null) {
if(!worldObj.isRemote)
setDead();
@@ -183,11 +185,11 @@ public class EntityCorporeaSpark extends Entity implements ICorporeaSpark {
}
@Override
public IInventory getSparkInventory() {
public InvWithLocation getSparkInventory() {
int x = MathHelper.floor_double(posX);
int y = MathHelper.floor_double(posY - 1);
int z = MathHelper.floor_double(posZ);
return InventoryHelper.getInventory(worldObj, new BlockPos(x, y, z));
return InventoryHelper2.getInventory(worldObj, new BlockPos(x, y, z), null);
}
@Override
@@ -12,7 +12,9 @@ package vazkii.botania.common.integration.corporea;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.items.wrapper.InvWrapper;
import org.apache.logging.log4j.Level;
import net.minecraft.inventory.IInventory;
@@ -21,6 +23,7 @@ import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
import vazkii.botania.api.corporea.CorporeaRequest;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
/**
* Wrapper for StorageDrawers compatibility.
@@ -31,16 +34,16 @@ public class WrappedDeepStorage extends WrappedInventoryBase {
private static boolean checkedInterface = false;
private static boolean deepStoragePresent = false;
private IDeepStorageUnit inv;
private final IDeepStorageUnit invRaw;
private WrappedDeepStorage(IDeepStorageUnit inv, ICorporeaSpark spark) {
this.inv = inv;
this.invRaw = inv;
this.spark = spark;
}
@Override
public IInventory getWrappedObject() {
return (IInventory) inv;
public InvWithLocation getWrappedObject() {
return new InvWithLocation(new InvWrapper(((IInventory) invRaw)), spark.getSparkInventory().world, spark.getSparkInventory().pos);
}
@Override
@@ -57,7 +60,7 @@ public class WrappedDeepStorage extends WrappedInventoryBase {
List<ItemStack> stacks = new ArrayList<ItemStack>();
boolean removedAny = false;
ItemStack prototype = inv.getStoredItemType();
ItemStack prototype = invRaw.getStoredItemType();
if(prototype == null) {
// for the case of barrel without contents set
return stacks;
@@ -85,7 +88,7 @@ public class WrappedDeepStorage extends WrappedInventoryBase {
request.extractedItems += rem;
if(doit && rem > 0) {
decreaseStoredCount(inv, rem);
decreaseStoredCount(invRaw, rem);
removedAny = true;
if(spark != null)
@@ -109,11 +112,10 @@ public class WrappedDeepStorage extends WrappedInventoryBase {
*
* @return wrapped inventory or null if it has incompatible type.
*/
public static IWrappedInventory wrap(IInventory inv, ICorporeaSpark spark) {
if(!isDeepStorageNeeded()) {
return null;
}
return inv instanceof IDeepStorageUnit ? new WrappedDeepStorage((IDeepStorageUnit) inv, spark) : null;
public static IWrappedInventory wrap(InvWithLocation inv, ICorporeaSpark spark) {
if(isDeepStorageNeeded() && inv.handler instanceof InvWrapper && ((InvWrapper) inv.handler).inv instanceof IDeepStorageUnit) {
return new WrappedDeepStorage(((IDeepStorageUnit) ((InvWrapper) inv.handler).inv), spark);
} else return null;
}
/**
@@ -18,18 +18,19 @@ import vazkii.botania.api.corporea.CorporeaHelper;
import vazkii.botania.api.corporea.CorporeaRequest;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
public class WrappedIInventory extends WrappedInventoryBase{
private IInventory inv;
private InvWithLocation inv;
private WrappedIInventory(IInventory inv, ICorporeaSpark spark) {
private WrappedIInventory(InvWithLocation inv, ICorporeaSpark spark) {
this.inv = inv;
this.spark = spark;
}
@Override
public IInventory getWrappedObject() {
public InvWithLocation getWrappedObject() {
return inv;
}
@@ -46,46 +47,31 @@ public class WrappedIInventory extends WrappedInventoryBase{
private List<ItemStack> iterateOverSlots(CorporeaRequest request, boolean doit) {
List<ItemStack> stacks = new ArrayList<ItemStack>();
boolean removedAny = false;
for (int i = inv.getSizeInventory() - 1; i >= 0; i--) {
if(!CorporeaHelper.isValidSlot(inv, i))
continue;
ItemStack stackAt = inv.getStackInSlot(i);
for (int i = inv.handler.getSlots() - 1; i >= 0; i--) {
ItemStack stackAt = inv.handler.getStackInSlot(i);
// WARNING: this code is very similar in all implementations of
// IWrappedInventory - keep it synch
if(isMatchingItemStack(request.matcher, request.checkNBT, stackAt)) {
int rem = Math.min(stackAt.stackSize, request.count == -1 ? stackAt.stackSize : request.count);
if(rem > 0) {
ItemStack copy = stackAt.copy();
if(rem < copy.stackSize)
copy.stackSize = rem;
stacks.add(copy);
stacks.add(inv.handler.extractItem(i, rem, !doit));
if(doit && spark != null)
spark.onItemExtracted(stackAt);
}
request.foundItems += stackAt.stackSize;
request.extractedItems += rem;
if(doit && rem > 0) {
inv.decrStackSize(i, rem);
removedAny = true;
if(spark != null)
spark.onItemExtracted(stackAt);
}
if(request.count != -1)
request.count -= rem;
}
}
if(removedAny) {
inv.markDirty();
}
return stacks;
}
public static IWrappedInventory wrap(IInventory inv, ICorporeaSpark spark) {
public static IWrappedInventory wrap(InvWithLocation inv, ICorporeaSpark spark) {
return new WrappedIInventory(inv, spark);
}
@@ -17,9 +17,13 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraftforge.items.wrapper.InvWrapper;
import vazkii.botania.api.corporea.CorporeaRequest;
import vazkii.botania.api.corporea.ICorporeaSpark;
import vazkii.botania.api.corporea.IWrappedInventory;
import vazkii.botania.api.corporea.InvWithLocation;
import vazkii.botania.common.entity.EntityCorporeaSpark;
/**
* Wrapper for StorageDrawers compatibility.
@@ -27,16 +31,16 @@ import vazkii.botania.api.corporea.IWrappedInventory;
*/
public class WrappedStorageDrawers extends WrappedInventoryBase {
private IDrawerGroup inv;
private IDrawerGroup invRaw;
private WrappedStorageDrawers(IDrawerGroup inv, ICorporeaSpark spark) {
this.inv = inv;
this.invRaw = inv;
this.spark = spark;
}
@Override
public IInventory getWrappedObject() {
return (IInventory) inv;
public InvWithLocation getWrappedObject() {
return new InvWithLocation(new InvWrapper((IInventory) invRaw), spark.getSparkInventory().world, spark.getSparkInventory().pos);
}
@Override
@@ -53,8 +57,8 @@ public class WrappedStorageDrawers extends WrappedInventoryBase {
List<ItemStack> stacks = new ArrayList<ItemStack>();
boolean removedAny = false;
for(int i = 0; i < inv.getDrawerCount(); i++) {
IDrawer drawer = inv.getDrawer(i);
for(int i = 0; i < invRaw.getDrawerCount(); i++) {
IDrawer drawer = invRaw.getDrawer(i);
if(drawer == null) {
continue;
}
@@ -93,7 +97,7 @@ public class WrappedStorageDrawers extends WrappedInventoryBase {
}
}
if(removedAny) {
inv.markDirtyIfNeeded();
invRaw.markDirtyIfNeeded();
}
return stacks;
}
@@ -107,7 +111,9 @@ public class WrappedStorageDrawers extends WrappedInventoryBase {
*
* @return wrapped inventory or null if it has incompatible type.
*/
public static IWrappedInventory wrap(IInventory inv, ICorporeaSpark spark) {
return inv instanceof IDrawerGroup ? new WrappedStorageDrawers((IDrawerGroup) inv, spark) : null;
public static IWrappedInventory wrap(InvWithLocation inv, ICorporeaSpark spark) {
if(inv.handler instanceof InvWrapper && ((InvWrapper) inv.handler).inv instanceof IDrawerGroup) {
return new WrappedStorageDrawers((IDrawerGroup) ((InvWrapper) inv.handler).inv, spark);
} else return null;
}
}
@@ -21,6 +21,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import vazkii.botania.api.corporea.CorporeaHelper;
import vazkii.botania.common.entity.EntityCorporeaSpark;
import vazkii.botania.common.lib.LibItemNames;
@@ -41,7 +42,7 @@ public class ItemCorporeaSpark extends ItemMod {
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float xv, float yv, float zv) {
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof IInventory && !CorporeaHelper.doesBlockHaveSpark(world, pos)) {
if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null) && !CorporeaHelper.doesBlockHaveSpark(world, pos)) {
stack.stackSize--;
if(!world.isRemote) {
EntityCorporeaSpark spark = new EntityCorporeaSpark(world);