mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-19 03:39:17 +00:00
Added support for StorageDrawers and Deep Storage API
This commit is contained in:
1 parent
275729d54e
commit
f44d846d32
16 files changed
+764
-56
No files matched your search
@@ -25,6 +25,7 @@ import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import vazkii.botania.api.BotaniaAPI;
|
||||
|
||||
public final class CorporeaHelper {
|
||||
|
||||
@@ -123,22 +124,15 @@ 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();
|
||||
|
||||
for(IInventory inv : inventories) {
|
||||
int count = 0;
|
||||
for(int i = 0; i < inv.getSizeInventory(); i++) {
|
||||
if(!isValidSlot(inv, i))
|
||||
continue;
|
||||
|
||||
ItemStack stackAt = inv.getStackInSlot(i);
|
||||
if(stacksMatch(stack, stackAt, checkNBT))
|
||||
count += stackAt.stackSize;
|
||||
public static Map<IInventory, Integer> getInventoriesWithItemInNetwork(ItemStack stack,List<IInventory> inventories, boolean checkNBT) {
|
||||
Map<IInventory, Integer> countMap = new HashMap<IInventory, Integer>();
|
||||
List<IWrappedInventory> wrappedInventories = BotaniaAPI.internalHandler.wrapInventory(inventories);
|
||||
for (IWrappedInventory inv : wrappedInventories) {
|
||||
CorporeaRequest request = new CorporeaRequest(stack, checkNBT, -1);
|
||||
inv.countItems(request);
|
||||
if (request.foundItems > 0) {
|
||||
countMap.put(inv.getWrappedObject(), request.foundItems);
|
||||
}
|
||||
|
||||
if(count > 0)
|
||||
countMap.put(inv, count);
|
||||
}
|
||||
|
||||
return countMap;
|
||||
@@ -167,62 +161,41 @@ public final class CorporeaHelper {
|
||||
* The "matcher" parameter has to be an ItemStack or a String, if the first it'll check if the
|
||||
* two stacks are similar using the "checkNBT" parameter, else it'll check if the name of the item
|
||||
* equals or matches (case a regex is passed in) the matcher string.
|
||||
* <br><br>
|
||||
* When requesting counting of items, individual stacks may exceed maxStackSize for
|
||||
* purposes of counting huge amounts.
|
||||
*/
|
||||
public static List<ItemStack> requestItem(Object matcher, int itemCount, ICorporeaSpark spark, boolean checkNBT, boolean doit) {
|
||||
List<ItemStack> stacks = new ArrayList();
|
||||
List<ItemStack> stacks = new ArrayList<ItemStack>();
|
||||
CorporeaRequestEvent event = new CorporeaRequestEvent(matcher, itemCount, spark, checkNBT, doit);
|
||||
if(MinecraftForge.EVENT_BUS.post(event))
|
||||
return stacks;
|
||||
|
||||
List<IInventory> inventories = getInventoriesOnNetwork(spark);
|
||||
Map<ICorporeaInterceptor, ICorporeaSpark> interceptors = new HashMap();
|
||||
List<IWrappedInventory> inventoriesW = BotaniaAPI.internalHandler.wrapInventory(inventories);
|
||||
Map<ICorporeaInterceptor, ICorporeaSpark> interceptors = new HashMap<ICorporeaInterceptor, ICorporeaSpark>();
|
||||
|
||||
lastRequestMatches = 0;
|
||||
lastRequestExtractions = 0;
|
||||
CorporeaRequest request = new CorporeaRequest(matcher, checkNBT, itemCount);
|
||||
for(IWrappedInventory inv : inventoriesW) {
|
||||
ICorporeaSpark invSpark = inv.getSpark();
|
||||
|
||||
int count = itemCount;
|
||||
for(IInventory inv : inventories) {
|
||||
boolean removedAny = false;
|
||||
ICorporeaSpark invSpark = getSparkForInventory(inv);
|
||||
|
||||
if(inv instanceof ICorporeaInterceptor) {
|
||||
ICorporeaInterceptor interceptor = (ICorporeaInterceptor) inv;
|
||||
Object originalInventory = inv.getWrappedObject();
|
||||
if(originalInventory instanceof ICorporeaInterceptor) {
|
||||
ICorporeaInterceptor interceptor = (ICorporeaInterceptor) originalInventory;
|
||||
interceptor.interceptRequest(matcher, itemCount, invSpark, spark, stacks, inventories, doit);
|
||||
interceptors.put(interceptor, invSpark);
|
||||
}
|
||||
|
||||
for(int i = inv.getSizeInventory() - 1; i >= 0; i--) {
|
||||
if(!isValidSlot(inv, i))
|
||||
continue;
|
||||
|
||||
ItemStack stackAt = inv.getStackInSlot(i);
|
||||
if(matcher instanceof ItemStack ? stacksMatch((ItemStack) matcher, stackAt, checkNBT) : matcher instanceof String ? stacksMatch(stackAt, (String) matcher) : false) {
|
||||
int rem = Math.min(stackAt.stackSize, count == -1 ? stackAt.stackSize : count);
|
||||
|
||||
if(rem > 0) {
|
||||
ItemStack copy = stackAt.copy();
|
||||
if(rem < copy.stackSize)
|
||||
copy.stackSize = rem;
|
||||
stacks.add(copy);
|
||||
}
|
||||
|
||||
lastRequestMatches += stackAt.stackSize;
|
||||
lastRequestExtractions += rem;
|
||||
if(doit && rem > 0) {
|
||||
inv.decrStackSize(i, rem);
|
||||
removedAny = true;
|
||||
if(invSpark != null)
|
||||
invSpark.onItemExtracted(stackAt);
|
||||
}
|
||||
if(count != -1)
|
||||
count -= rem;
|
||||
}
|
||||
if(doit) {
|
||||
stacks.addAll(inv.extractItems(request));
|
||||
} else {
|
||||
stacks.addAll(inv.countItems(request));
|
||||
}
|
||||
|
||||
if(removedAny)
|
||||
inv.markDirty();
|
||||
}
|
||||
|
||||
lastRequestMatches = request.foundItems;
|
||||
lastRequestExtractions = request.extractedItems;
|
||||
|
||||
for(ICorporeaInterceptor interceptor : interceptors.keySet())
|
||||
interceptor.interceptRequestLast(matcher, itemCount, interceptors.get(interceptor), spark, stacks, inventories, doit);
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.api.corporea;
|
||||
|
||||
public class CorporeaRequest {
|
||||
public Object matcher;
|
||||
public boolean checkNBT;
|
||||
public int count;
|
||||
public int foundItems = 0;
|
||||
public int extractedItems = 0;
|
||||
|
||||
public CorporeaRequest(Object matcher, boolean checkNBT, int count) {
|
||||
super();
|
||||
this.matcher = matcher;
|
||||
this.checkNBT = checkNBT;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.api.corporea;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* This interface wraps IInventory corporea works with in order to provide
|
||||
* compatibility with different storage mods.
|
||||
*
|
||||
*/
|
||||
public interface IWrappedInventory {
|
||||
|
||||
/**
|
||||
* Break encapsulation and exposes original inventory.
|
||||
*/
|
||||
IInventory getWrappedObject();
|
||||
|
||||
/**
|
||||
* Counts items in the inventory matching the request
|
||||
*
|
||||
* @param request
|
||||
* - specifies what should be found
|
||||
* @return List of ItemStack, individual stacks may exceed maxStackSize for
|
||||
* purposes of counting huge amounts. To get final count requestor
|
||||
* should sum stackSize of all stacks.
|
||||
*/
|
||||
List<ItemStack> countItems(CorporeaRequest request);
|
||||
|
||||
/**
|
||||
* Convenience method for accessing spark over inventory
|
||||
*/
|
||||
ICorporeaSpark getSpark();
|
||||
|
||||
/**
|
||||
* Extracts items matching request from the inventory.<br/>
|
||||
* {@link CorporeaRequest#count} is updated to reflect how many items are
|
||||
* yet to be extracted.<br/>
|
||||
* {@link CorporeaRequest#foundItems} and
|
||||
* {@link CorporeaRequest#extractedItems} are updated to reflect how many
|
||||
* items were found and extracted.
|
||||
*
|
||||
* @param request
|
||||
* @return List of ItemStacks to be delivered to the destination.
|
||||
*/
|
||||
List<ItemStack> extractItems(CorporeaRequest request);
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import net.minecraft.util.IIcon;
|
||||
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.lexicon.LexiconPage;
|
||||
import vazkii.botania.api.lexicon.multiblock.MultiblockSet;
|
||||
import vazkii.botania.api.recipe.RecipeBrew;
|
||||
@@ -226,4 +227,9 @@ public class DummyMethodHandler implements IInternalMethodHandler {
|
||||
// NO-OP
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IWrappedInventory> wrapInventory(List<IInventory> inventories) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import net.minecraft.util.IIcon;
|
||||
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.lexicon.LexiconPage;
|
||||
import vazkii.botania.api.lexicon.multiblock.MultiblockSet;
|
||||
import vazkii.botania.api.recipe.RecipeBrew;
|
||||
@@ -125,4 +126,9 @@ public interface IInternalMethodHandler {
|
||||
|
||||
public void sendBaubleUpdatePacket(EntityPlayer player, int slot);
|
||||
|
||||
/**
|
||||
* Wrap inventories in the network into wrappers providing compatibility for storage mods.
|
||||
*/
|
||||
List<IWrappedInventory> wrapInventory(List<IInventory> inventories);
|
||||
|
||||
}
|
||||
@@ -41,6 +41,7 @@ public class Botania {
|
||||
public static boolean bloodMagicLoaded = false;
|
||||
public static boolean coloredLightsLoaded = false;
|
||||
public static boolean etFuturumLoaded = false;
|
||||
public static boolean storageDrawersLoaded = false;
|
||||
|
||||
public static ILightHelper lightHelper;
|
||||
|
||||
@@ -59,7 +60,8 @@ public class Botania {
|
||||
bloodMagicLoaded = Loader.isModLoaded("AWWayofTime"); // Psh, noob
|
||||
coloredLightsLoaded = Loader.isModLoaded("easycoloredlights");
|
||||
etFuturumLoaded = Loader.isModLoaded("etfuturum");
|
||||
|
||||
storageDrawersLoaded = Loader.isModLoaded("StorageDrawers");
|
||||
|
||||
lightHelper = coloredLightsLoaded ? new LightHelperColored() : new LightHelperVanilla();
|
||||
|
||||
proxy.preInit(event);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
package vazkii.botania.common.core.handler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
@@ -28,6 +29,9 @@ import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
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.internal.DummyMethodHandler;
|
||||
import vazkii.botania.api.internal.IManaNetwork;
|
||||
import vazkii.botania.api.lexicon.LexiconPage;
|
||||
@@ -48,6 +52,9 @@ import vazkii.botania.common.block.BlockModFlower;
|
||||
import vazkii.botania.common.block.BlockSpecialFlower;
|
||||
import vazkii.botania.common.block.ModBlocks;
|
||||
import vazkii.botania.common.block.subtile.functional.SubTileSolegnolia;
|
||||
import vazkii.botania.common.integration.corporea.WrappedDeepStorage;
|
||||
import vazkii.botania.common.integration.corporea.WrappedIInventory;
|
||||
import vazkii.botania.common.integration.corporea.WrappedStorageDrawers;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
import vazkii.botania.common.item.block.ItemBlockSpecialFlower;
|
||||
import vazkii.botania.common.item.relic.ItemLokiRing;
|
||||
@@ -260,4 +267,28 @@ public class InternalMethodHandler extends DummyMethodHandler {
|
||||
if(player instanceof EntityPlayerMP)
|
||||
PacketHandler.INSTANCE.sendTo(new PacketSyncBauble(player, slot), (EntityPlayerMP) player);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<IWrappedInventory> wrapInventory(List<IInventory> inventories) {
|
||||
ArrayList<IWrappedInventory> arrayList = new ArrayList<IWrappedInventory>();
|
||||
for(IInventory inv : inventories) {
|
||||
ICorporeaSpark spark = CorporeaHelper.getSparkForInventory(inv);
|
||||
IWrappedInventory wrapped = null;
|
||||
// try StorageDrawers integration
|
||||
if(Botania.storageDrawersLoaded) {
|
||||
wrapped = WrappedStorageDrawers.wrap(inv, spark);
|
||||
}
|
||||
// try DeepStorageUnit
|
||||
if(wrapped == null) {
|
||||
wrapped = WrappedDeepStorage.wrap(inv, spark);
|
||||
}
|
||||
// last chance - this will always work
|
||||
if(wrapped == null) {
|
||||
wrapped = WrappedIInventory.wrap(inv, spark);
|
||||
}
|
||||
arrayList.add(wrapped);
|
||||
}
|
||||
return arrayList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.common.integration.corporea;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import cpw.mods.fml.common.FMLLog;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
|
||||
import vazkii.botania.api.corporea.CorporeaRequest;
|
||||
import vazkii.botania.api.corporea.ICorporeaSpark;
|
||||
import vazkii.botania.api.corporea.IWrappedInventory;
|
||||
|
||||
/**
|
||||
* Wrapper for StorageDrawers compatibility.
|
||||
*
|
||||
*/
|
||||
public class WrappedDeepStorage extends WrappedInventoryBase {
|
||||
|
||||
private static boolean checkedInterface = false;
|
||||
private static boolean deepStoragePresent = false;
|
||||
|
||||
private IDeepStorageUnit inv;
|
||||
|
||||
private WrappedDeepStorage(IDeepStorageUnit inv, ICorporeaSpark spark) {
|
||||
this.inv = inv;
|
||||
this.spark = spark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IInventory getWrappedObject() {
|
||||
return (IInventory) inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> countItems(CorporeaRequest request) {
|
||||
return iterateOverStacks(request, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> extractItems(CorporeaRequest request) {
|
||||
return iterateOverStacks(request, true);
|
||||
}
|
||||
|
||||
private List<ItemStack> iterateOverStacks(CorporeaRequest request, boolean doit) {
|
||||
List<ItemStack> stacks = new ArrayList<ItemStack>();
|
||||
boolean removedAny = false;
|
||||
|
||||
ItemStack prototype = inv.getStoredItemType();
|
||||
if(prototype == null) {
|
||||
// for the case of barrel without contents set
|
||||
return stacks;
|
||||
}
|
||||
int storedCount = prototype.stackSize;
|
||||
|
||||
// WARNING: this code is very similar in all implementations of
|
||||
// IWrappedInventory - keep it synch
|
||||
// this code is also near duplicate to WrappedStorageDrawers - keep it
|
||||
// synchronized
|
||||
if(isMatchingItemStack(request.matcher, request.checkNBT, prototype)) {
|
||||
int rem = Math.min(storedCount, request.count == -1 ? storedCount : request.count);
|
||||
|
||||
if(rem > 0) {
|
||||
ItemStack copy = prototype.copy();
|
||||
copy.stackSize = rem;
|
||||
if(doit) {
|
||||
stacks.addAll(breakDownBigStack(copy));
|
||||
} else {
|
||||
stacks.add(copy);
|
||||
}
|
||||
}
|
||||
|
||||
request.foundItems += storedCount;
|
||||
request.extractedItems += rem;
|
||||
|
||||
if(doit && rem > 0) {
|
||||
decreaseStoredCount(inv, rem);
|
||||
|
||||
removedAny = true;
|
||||
if(spark != null)
|
||||
spark.onItemExtracted(prototype);
|
||||
}
|
||||
if(request.count != -1)
|
||||
request.count -= rem;
|
||||
}
|
||||
if(removedAny) {
|
||||
// inv.markDirtyIfNeeded();
|
||||
}
|
||||
return stacks;
|
||||
}
|
||||
|
||||
private void decreaseStoredCount(IDeepStorageUnit inventory, int rem) {
|
||||
inventory.setStoredItemCount(inventory.getStoredItemType().stackSize - rem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link WrappedDeepStorage} if specified inv can be wrapped.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks for presence of Deep Storage API in the pack - if some
|
||||
* other mod provides IDeepStorageUnit, support for Deep Storage will be
|
||||
* used. This way we don't have to ship IDeepStorageUnit in Botania jar.
|
||||
*/
|
||||
private static boolean isDeepStorageNeeded() {
|
||||
if(!checkedInterface) {
|
||||
try {
|
||||
deepStoragePresent = (Class.forName("powercrystals.minefactoryreloaded.api.IDeepStorageUnit") != null);
|
||||
} catch (ClassNotFoundException e) {
|
||||
deepStoragePresent = false;
|
||||
}
|
||||
checkedInterface = true;
|
||||
FMLLog.log(Level.INFO, "[Botania] Corporea support for Deep Storage: %b", deepStoragePresent);
|
||||
}
|
||||
return deepStoragePresent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.common.integration.corporea;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.api.corporea.CorporeaHelper;
|
||||
import vazkii.botania.api.corporea.CorporeaRequest;
|
||||
import vazkii.botania.api.corporea.ICorporeaSpark;
|
||||
import vazkii.botania.api.corporea.IWrappedInventory;
|
||||
|
||||
public class WrappedIInventory extends WrappedInventoryBase{
|
||||
|
||||
private IInventory inv;
|
||||
|
||||
private WrappedIInventory(IInventory inv, ICorporeaSpark spark) {
|
||||
this.inv = inv;
|
||||
this.spark = spark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IInventory getWrappedObject() {
|
||||
return inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> countItems(CorporeaRequest request) {
|
||||
return iterateOverSlots(request, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> extractItems(CorporeaRequest request) {
|
||||
return iterateOverSlots(request, true);
|
||||
}
|
||||
|
||||
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);
|
||||
// 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);
|
||||
}
|
||||
|
||||
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) {
|
||||
return new WrappedIInventory(inv, spark);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.common.integration.corporea;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.api.corporea.CorporeaHelper;
|
||||
import vazkii.botania.api.corporea.ICorporeaSpark;
|
||||
import vazkii.botania.api.corporea.IWrappedInventory;
|
||||
|
||||
public abstract class WrappedInventoryBase implements IWrappedInventory {
|
||||
|
||||
protected ICorporeaSpark spark;
|
||||
|
||||
@Override
|
||||
public ICorporeaSpark getSpark() {
|
||||
return spark;
|
||||
}
|
||||
|
||||
protected boolean isMatchingItemStack(Object matcher, boolean checkNBT, ItemStack stackAt) {
|
||||
return matcher instanceof ItemStack ? CorporeaHelper.stacksMatch((ItemStack) matcher, stackAt, checkNBT)
|
||||
: matcher instanceof String ? CorporeaHelper.stacksMatch(stackAt, (String) matcher) : false;
|
||||
}
|
||||
|
||||
protected Collection<? extends ItemStack> breakDownBigStack(ItemStack stack) {
|
||||
List<ItemStack> stacks = new ArrayList<ItemStack>();
|
||||
int additionalStacks = stack.stackSize / stack.getMaxStackSize();
|
||||
int lastStackSize = stack.stackSize % stack.getMaxStackSize();
|
||||
if(additionalStacks > 0) {
|
||||
ItemStack fullStack = stack.copy();
|
||||
fullStack.stackSize = stack.getMaxStackSize();
|
||||
for (int i = 0; i < additionalStacks; i++) {
|
||||
stacks.add(fullStack.copy());
|
||||
}
|
||||
}
|
||||
ItemStack lastStack = stack.copy();
|
||||
lastStack.stackSize = lastStackSize;
|
||||
stacks.add(lastStack);
|
||||
|
||||
return stacks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* This class was created by <Vindex>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
*/
|
||||
package vazkii.botania.common.integration.corporea;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.api.corporea.CorporeaRequest;
|
||||
import vazkii.botania.api.corporea.ICorporeaSpark;
|
||||
import vazkii.botania.api.corporea.IWrappedInventory;
|
||||
|
||||
/**
|
||||
* Wrapper for StorageDrawers compatibility.
|
||||
*
|
||||
*/
|
||||
public class WrappedStorageDrawers extends WrappedInventoryBase {
|
||||
|
||||
private IDrawerGroup inv;
|
||||
|
||||
private WrappedStorageDrawers(IDrawerGroup inv, ICorporeaSpark spark) {
|
||||
this.inv = inv;
|
||||
this.spark = spark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IInventory getWrappedObject() {
|
||||
return (IInventory) inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> countItems(CorporeaRequest request) {
|
||||
return iterateOverStacks(request, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> extractItems(CorporeaRequest request) {
|
||||
return iterateOverStacks(request, true);
|
||||
}
|
||||
|
||||
private List<ItemStack> iterateOverStacks(CorporeaRequest request, boolean doit) {
|
||||
List<ItemStack> stacks = new ArrayList<ItemStack>();
|
||||
boolean removedAny = false;
|
||||
|
||||
for(int i = 0; i < inv.getDrawerCount(); i++) {
|
||||
IDrawer drawer = inv.getDrawer(i);
|
||||
if(drawer == null) {
|
||||
continue;
|
||||
}
|
||||
ItemStack prototype = drawer.getStoredItemPrototype();
|
||||
int storedCount = drawer.getStoredItemCount();
|
||||
|
||||
// WARNING: this code is very similar in all implementations of
|
||||
// IWrappedInventory - keep it synch
|
||||
// also this code is near duplicate to WrappedDeepStorage - keep it
|
||||
// synchronized
|
||||
if(isMatchingItemStack(request.matcher, request.checkNBT, prototype)) {
|
||||
int rem = Math.min(storedCount, request.count == -1 ? storedCount : request.count);
|
||||
|
||||
if(rem > 0) {
|
||||
ItemStack copy = prototype.copy();
|
||||
copy.stackSize = rem;
|
||||
if(doit) {
|
||||
stacks.addAll(breakDownBigStack(copy));
|
||||
} else {
|
||||
stacks.add(copy);
|
||||
}
|
||||
}
|
||||
|
||||
request.foundItems += storedCount;
|
||||
request.extractedItems += rem;
|
||||
|
||||
if(doit && rem > 0) {
|
||||
decreaseStoredCount(drawer, rem);
|
||||
|
||||
removedAny = true;
|
||||
if(spark != null)
|
||||
spark.onItemExtracted(prototype);
|
||||
}
|
||||
if(request.count != -1)
|
||||
request.count -= rem;
|
||||
}
|
||||
}
|
||||
if(removedAny) {
|
||||
inv.markDirtyIfNeeded();
|
||||
}
|
||||
return stacks;
|
||||
}
|
||||
|
||||
private void decreaseStoredCount(IDrawer drawer, int rem) {
|
||||
drawer.setStoredItemCount(drawer.getStoredItemCount() - rem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link WrappedStorageDrawers} if specified inv can be wrapped.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user