mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-27 15:33:08 +00:00
Retrofit corporea to work with capabilities
This commit is contained in:
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);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user