From ad823286f1a856c971993f2ce28a9b87d1828221 Mon Sep 17 00:00:00 2001 From: Lizbeth Rika Date: Sat, 14 Feb 2015 16:39:25 +0000 Subject: [PATCH] Actual corporea handling, requesting works fine. --- .../botania/api/corporea/CorporeaHelper.java | 184 ++++++++++++++++++ .../core/handler/CommonTickHandler.java | 16 +- .../common/core/proxy/CommonProxy.java | 8 +- .../common/entity/EntityCorporeaSpark.java | 10 +- 4 files changed, 208 insertions(+), 10 deletions(-) create mode 100644 src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java diff --git a/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java b/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java new file mode 100644 index 000000000..813f84967 --- /dev/null +++ b/src/main/java/vazkii/botania/api/corporea/CorporeaHelper.java @@ -0,0 +1,184 @@ +/** + * This class was created by . 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 + * + * File Created @ [Feb 14, 2015, 3:28:54 PM (GMT)] + */ +package vazkii.botania.api.corporea; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public final class CorporeaHelper { + + private static final List empty = Collections.unmodifiableList(new ArrayList()); + private static final WeakHashMap, List> cachedNetworks = new WeakHashMap(); + + /** + * 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 getInventoriesOnNetwork(ICorporeaSpark spark) { + ICorporeaSpark master = spark.getMaster(); + if(master == null) + return empty; + List network = master.getConnections(); + + if(cachedNetworks.containsKey(network)) + return cachedNetworks.get(network); + + List inventories = new ArrayList(); + if(network != null) + for(ICorporeaSpark otherSpark : network) + if(otherSpark != null) { + IInventory inv = otherSpark.getInventory(); + if(inv != null) + inventories.add(inv); + } + + cachedNetworks.put(network, inventories); + return inventories; + } + + /** + * Gets the amount of available items in the network of the type passed in, checking NBT or not. + * The higher level functions that use a List< IInventory > or a Map< IInventory, Integer > should be + * 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 inventories = getInventoriesOnNetwork(spark); + return getCountInNetwork(stack, inventories, checkNBT); + } + + /** + * Gets the amount of available items in the network of the type passed in, checking NBT or not. + * 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 inventories, boolean checkNBT) { + Map 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 inventories, boolean checkNBT) { + int count = 0; + + for(IInventory inv : inventories.keySet()) + count += inventories.get(inv); + + return count; + } + + /** + * Gets a Map mapping IInventories to the amount of items of the type passed in that exist + * 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 getInventoriesWithItemInNetwork(ItemStack stack, ICorporeaSpark spark, boolean checkNBT) { + List inventories = getInventoriesOnNetwork(spark); + return getInventoriesWithItemInNetwork(stack, inventories, checkNBT); + } + + /** + * Gets a Map mapping IInventories to the amount of items of the type passed in that exist + * 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 getInventoriesWithItemInNetwork(ItemStack stack, List inventories, boolean checkNBT) { + Map 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; + } + + if(count > 0) + countMap.put(inv, count); + } + + return countMap; + } + + /** + * Requests an ItemStack of the type passed in from the network, or tries to, checking NBT or not. + * This will remove the items from the adequate inventories unless the "doit" parameter is false. + * Returns a new ItemStack of the item acquired or null if none was found. + */ + public static ItemStack requestItem(ItemStack stack, ICorporeaSpark spark, boolean checkNBT, boolean doit) { + Map inventories = getInventoriesWithItemInNetwork(stack, spark, checkNBT); + int count = getCountInNetwork(stack, inventories, checkNBT); + int size = Math.min(count, stack.stackSize); + if(size == 0) + return null; + + ItemStack retStack = stack.copy(); + retStack.stackSize = size; + if(!doit) + return retStack; + + int remove = size; + checkInvs : { + for(IInventory inv : inventories.keySet()) { + for(int i = 0; i < inv.getSizeInventory(); i++) { + if(!isValidSlot(inv, i)) + continue; + + ItemStack stackAt = inv.getStackInSlot(i); + if(stacksMatch(stack, stackAt, checkNBT)) { + int rem = Math.min(stackAt.stackSize, remove); + stackAt.stackSize -= rem; + remove -= rem; + + if(stackAt.stackSize == 0) + inv.setInventorySlotContents(i, null); + if(remove <= 0) + break checkInvs; + } + } + } + } + + return retStack; + } + + /** + * Gets if the slot passed in can be extracted from by a spark. + */ + public static boolean isValidSlot(IInventory inv, int slot) { + return true; + } + + /** + * Gets if two stacks match. + */ + public static boolean stacksMatch(ItemStack stack1, ItemStack stack2, boolean checkNBT) { + return stack1 != null && stack2 != null && stack1.isItemEqual(stack2) && (!checkNBT || ItemStack.areItemStackTagsEqual(stack1, stack2)); + } + + /** + * Clears the cached networks, called once per tick, should not be called outside + * of the botania code. + */ + public static void clearCache() { + cachedNetworks.clear(); + } +} diff --git a/src/main/java/vazkii/botania/common/core/handler/CommonTickHandler.java b/src/main/java/vazkii/botania/common/core/handler/CommonTickHandler.java index 2211c5d10..0d97dac58 100644 --- a/src/main/java/vazkii/botania/common/core/handler/CommonTickHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/CommonTickHandler.java @@ -13,6 +13,7 @@ package vazkii.botania.common.core.handler; import java.util.ArrayList; import java.util.List; +import vazkii.botania.api.corporea.CorporeaHelper; import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; @@ -24,17 +25,17 @@ import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -// This is legacy code, it used to be used for the old terrasteel crafting -// mechanic. For the new one check TileTerraPlate public final class CommonTickHandler { @SubscribeEvent public void onTick(WorldTickEvent event) { if(event.phase == Phase.END) { - List entities = new ArrayList(event.world.loadedEntityList); + /*List entities = new ArrayList(event.world.loadedEntityList); for(Entity entity : entities) if(entity instanceof EntityItem) - TerrasteelCraftingHandler.onEntityUpdate((EntityItem) entity); + TerrasteelCraftingHandler.onEntityUpdate((EntityItem) entity);*/ + + CorporeaHelper.clearCache(); } } @@ -42,14 +43,15 @@ public final class CommonTickHandler { @SubscribeEvent public void onTick(ClientTickEvent event) { if(event.phase == Phase.END) { - World world = Minecraft.getMinecraft().theWorld; + /*World world = Minecraft.getMinecraft().theWorld; if(world != null) { List entities = new ArrayList(world.loadedEntityList); for(Entity entity : entities) if(entity instanceof EntityItem) TerrasteelCraftingHandler.onEntityUpdate((EntityItem) entity); - } - + }*/ + + CorporeaHelper.clearCache(); } } diff --git a/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java b/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java index 82372ad14..88b849f23 100644 --- a/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java +++ b/src/main/java/vazkii/botania/common/core/proxy/CommonProxy.java @@ -31,6 +31,7 @@ import vazkii.botania.common.core.command.CommandOpen; import vazkii.botania.common.core.command.CommandShare; import vazkii.botania.common.core.handler.BiomeDecorationHandler; import vazkii.botania.common.core.handler.ChestGenHandler; +import vazkii.botania.common.core.handler.CommonTickHandler; import vazkii.botania.common.core.handler.ConfigHandler; import vazkii.botania.common.core.handler.InternalMethodHandler; import vazkii.botania.common.core.handler.ManaNetworkHandler; @@ -52,6 +53,7 @@ import vazkii.botania.common.integration.buildcraft.StatementAPIPlugin; import vazkii.botania.common.item.ModItems; import vazkii.botania.common.lexicon.LexiconData; import vazkii.botania.common.network.GuiHandler; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLPostInitializationEvent; @@ -96,9 +98,11 @@ public class CommonProxy { MinecraftForge.EVENT_BUS.register(new SheddingHandler()); MinecraftForge.EVENT_BUS.register(new SpawnerChangingHandler()); MinecraftForge.EVENT_BUS.register(new SubTileNarslimmus.SpawnIntercepter()); - + + FMLCommonHandler.instance().bus().register(new CommonTickHandler()); + FMLInterModComms.sendMessage("ProjectE", "interdictionblacklist", EntityManaBurst.class.getCanonicalName()); - + if(Botania.bcTriggersLoaded) new StatementAPIPlugin(); } diff --git a/src/main/java/vazkii/botania/common/entity/EntityCorporeaSpark.java b/src/main/java/vazkii/botania/common/entity/EntityCorporeaSpark.java index 367c44bda..36a5a2def 100644 --- a/src/main/java/vazkii/botania/common/entity/EntityCorporeaSpark.java +++ b/src/main/java/vazkii/botania/common/entity/EntityCorporeaSpark.java @@ -17,12 +17,15 @@ import java.util.List; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.MathHelper; import net.minecraft.world.World; +import vazkii.botania.api.corporea.CorporeaHelper; import vazkii.botania.api.corporea.ICorporeaSpark; import vazkii.botania.common.core.helper.InventoryHelper; import vazkii.botania.common.item.ModItems; @@ -70,8 +73,13 @@ public class EntityCorporeaSpark extends Entity implements ICorporeaSpark { return; } - if(isMaster()) + if(isMaster()) { master = this; + System.out.println(CorporeaHelper.getCountInNetwork(new ItemStack(Items.apple), this, false)); + ItemStack request = CorporeaHelper.requestItem(new ItemStack(Items.apple), this, false, true); + if(request != null) + InventoryHelper.insertItemIntoInventory(getInventory(), request); + } if(firstUpdate) { if(isMaster())