diff --git a/src/main/java/vazkii/botania/api/mana/ManaItemHandler.java b/src/main/java/vazkii/botania/api/mana/ManaItemHandler.java index 66ad83043..dbe366880 100644 --- a/src/main/java/vazkii/botania/api/mana/ManaItemHandler.java +++ b/src/main/java/vazkii/botania/api/mana/ManaItemHandler.java @@ -10,6 +10,12 @@ */ package vazkii.botania.api.mana; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -18,6 +24,64 @@ import vazkii.botania.api.BotaniaAPI; public final class ManaItemHandler { + /** + * Gets a list containing all mana-holding items in a player's inventory. + * Also includes a call to ManaItemsEvent, so other mods can add items from + * their player-associated inventories. + * @return The list of items + */ + public static List getManaItems(EntityPlayer player) + { + if (player == null) + return new ArrayList(); + + IInventory mainInv = player.inventory; + + List toReturn = new ArrayList(); + int size = mainInv.getSizeInventory(); + + for(int slot = 0; slot < size; slot++) { + ItemStack stackInSlot = mainInv.getStackInSlot(slot); + + if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { + toReturn.add(stackInSlot); + } + } + + ManaItemsEvent event = new ManaItemsEvent(player, toReturn); + MinecraftForge.EVENT_BUS.post(event); + toReturn = event.getItems(); + return toReturn; + } + + /** + * Gets a list containing all mana-holding items in a player's baubles inventory. + * @return The list of items + */ + public static Map getManaBaubles(EntityPlayer player) + { + if (player == null) + return new HashMap(); + + IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player); + if (baublesInv == null) + return new HashMap(); + + + Map toReturn = new HashMap(); + int size = baublesInv.getSizeInventory(); + + for(int slot = 0; slot < size; slot++) { + ItemStack stackInSlot = baublesInv.getStackInSlot(slot); + + if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { + toReturn.put(slot, stackInSlot); + } + } + + return toReturn; + } + /** * Requests mana from items in a given player's inventory. * @param manaToGet How much mana is to be requested, if less mana exists than this amount, @@ -29,37 +93,44 @@ public final class ManaItemHandler { if(stack == null) return 0; - IInventory mainInv = player.inventory; - IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player); - - int invSize = mainInv.getSizeInventory(); - int size = invSize; - if(baublesInv != null) - size += baublesInv.getSizeInventory(); - - for(int i = 0; i < size; i++) { - boolean useBaubles = i >= invSize; - IInventory inv = useBaubles ? baublesInv : mainInv; - int slot = i - (useBaubles ? invSize : 0); - ItemStack stackInSlot = inv.getStackInSlot(slot); + List items = getManaItems(player); + for (ItemStack stackInSlot : items) + { if(stackInSlot == stack) continue; + IManaItem manaItem = (IManaItem) stackInSlot.getItem(); + if(manaItem.canExportManaToItem(stackInSlot, stack) && manaItem.getMana(stackInSlot) > 0) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) + continue; - if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { - IManaItem manaItem = (IManaItem) stackInSlot.getItem(); - if(manaItem.canExportManaToItem(stackInSlot, stack) && manaItem.getMana(stackInSlot) > 0) { - if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) - continue; + int mana = Math.min(manaToGet, manaItem.getMana(stackInSlot)); - int mana = Math.min(manaToGet, manaItem.getMana(stackInSlot)); + if(remove) + manaItem.addMana(stackInSlot, -mana); - if(remove) - manaItem.addMana(stackInSlot, -mana); - if(useBaubles) - BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, slot); + return mana; + } + } + + Map baubles = getManaBaubles(player); + for (Entry entry : baubles.entrySet()) + { + ItemStack stackInSlot = entry.getValue(); + if(stackInSlot == stack) + continue; + IManaItem manaItem = (IManaItem) stackInSlot.getItem(); + if(manaItem.canExportManaToItem(stackInSlot, stack) && manaItem.getMana(stackInSlot) > 0) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) + continue; - return mana; - } + int mana = Math.min(manaToGet, manaItem.getMana(stackInSlot)); + + if(remove) + manaItem.addMana(stackInSlot, -mana); + + BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, entry.getKey()); + + return mana; } } @@ -77,35 +148,39 @@ public final class ManaItemHandler { if(stack == null) return false; - IInventory mainInv = player.inventory; - IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player); - - int invSize = mainInv.getSizeInventory(); - int size = invSize; - if(baublesInv != null) - size += baublesInv.getSizeInventory(); - - for(int i = 0; i < size; i++) { - boolean useBaubles = i >= invSize; - IInventory inv = useBaubles ? baublesInv : mainInv; - int slot = i - (useBaubles ? invSize : 0); - ItemStack stackInSlot = inv.getStackInSlot(slot); + List items = getManaItems(player); + for (ItemStack stackInSlot : items) + { if(stackInSlot == stack) continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.canExportManaToItem(stackInSlot, stack) && manaItemSlot.getMana(stackInSlot) > manaToGet) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) + continue; - if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { - IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); - if(manaItemSlot.canExportManaToItem(stackInSlot, stack) && manaItemSlot.getMana(stackInSlot) > manaToGet) { - if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) - continue; + if(remove) + manaItemSlot.addMana(stackInSlot, -manaToGet); - if(remove) - manaItemSlot.addMana(stackInSlot, -manaToGet); - if(useBaubles) - BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, slot); + return true; + } + } + + Map baubles = getManaBaubles(player); + for (Entry entry : baubles.entrySet()) + { + ItemStack stackInSlot = entry.getValue(); + if(stackInSlot == stack) + continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.canExportManaToItem(stackInSlot, stack) && manaItemSlot.getMana(stackInSlot) > manaToGet) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canReceiveManaFromItem(stack, stackInSlot)) + continue; - return true; - } + if(remove) + manaItemSlot.addMana(stackInSlot, -manaToGet); + BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, entry.getKey()); + + return true; } } @@ -123,42 +198,51 @@ public final class ManaItemHandler { if(stack == null) return 0; - IInventory mainInv = player.inventory; - IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player); - - int invSize = mainInv.getSizeInventory(); - int size = invSize; - if(baublesInv != null) - size += baublesInv.getSizeInventory(); - - for(int i = 0; i < size; i++) { - boolean useBaubles = i >= invSize; - IInventory inv = useBaubles ? baublesInv : mainInv; - int slot = i - (useBaubles ? invSize : 0); - ItemStack stackInSlot = inv.getStackInSlot(slot); + List items = getManaItems(player); + for (ItemStack stackInSlot : items) + { if(stackInSlot == stack) continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) + continue; - if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { - IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); - - if(manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { - if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) - continue; - - int received; - if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot)) - received = manaToSend; - else received = manaToSend - (manaItemSlot.getMana(stackInSlot) + manaToSend - manaItemSlot.getMaxMana(stackInSlot)); + int received; + if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot)) + received = manaToSend; + else received = manaToSend - (manaItemSlot.getMana(stackInSlot) + manaToSend - manaItemSlot.getMaxMana(stackInSlot)); - if(add) - manaItemSlot.addMana(stackInSlot, manaToSend); - if(useBaubles) - BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, slot); + if(add) + manaItemSlot.addMana(stackInSlot, manaToSend); - return received; - } + return received; + } + } + + Map baubles = getManaBaubles(player); + for (Entry entry : baubles.entrySet()) + { + ItemStack stackInSlot = entry.getValue(); + if(stackInSlot == stack) + continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) + continue; + + int received; + if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot)) + received = manaToSend; + else received = manaToSend - (manaItemSlot.getMana(stackInSlot) + manaToSend - manaItemSlot.getMaxMana(stackInSlot)); + + + if(add) + manaItemSlot.addMana(stackInSlot, manaToSend); + BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, entry.getKey()); + + return received; } } @@ -176,35 +260,39 @@ public final class ManaItemHandler { if(stack == null) return false; - IInventory mainInv = player.inventory; - IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player); - - int invSize = mainInv.getSizeInventory(); - int size = invSize; - if(baublesInv != null) - size += baublesInv.getSizeInventory(); - - for(int i = 0; i < size; i++) { - boolean useBaubles = i >= invSize; - IInventory inv = useBaubles ? baublesInv : mainInv; - int slot = i - (useBaubles ? invSize : 0); - ItemStack stackInSlot = inv.getStackInSlot(slot); + List items = getManaItems(player); + for (ItemStack stackInSlot : items) + { if(stackInSlot == stack) continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot) && manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) + continue; - if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) { - IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); - if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot) && manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { - if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) - continue; + if(add) + manaItemSlot.addMana(stackInSlot, manaToSend); + + return true; + } + } + + Map baubles = getManaBaubles(player); + for (Entry entry : baubles.entrySet()) + { + ItemStack stackInSlot = entry.getValue(); + if(stackInSlot == stack) + continue; + IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem(); + if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot) && manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) { + if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot)) + continue; - if(add) - manaItemSlot.addMana(stackInSlot, manaToSend); - if(useBaubles) - BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, slot); + if(add) + manaItemSlot.addMana(stackInSlot, manaToSend); + BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, entry.getKey()); - return true; - } + return true; } } diff --git a/src/main/java/vazkii/botania/api/mana/ManaItemsEvent.java b/src/main/java/vazkii/botania/api/mana/ManaItemsEvent.java new file mode 100644 index 000000000..379256559 --- /dev/null +++ b/src/main/java/vazkii/botania/api/mana/ManaItemsEvent.java @@ -0,0 +1,40 @@ +/** + * 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 @ [Sep 23, 2016, 11:59:12 PM (GMT)] + */ +package vazkii.botania.api.mana; + +import java.util.List; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.common.eventhandler.Event; + +public class ManaItemsEvent extends Event { + + private final EntityPlayer entityPlayer; + private List items; + + public ManaItemsEvent(EntityPlayer entityPlayer, List items) { + this.entityPlayer = entityPlayer; + this.items = items; + } + + public EntityPlayer getEntityPlayer() { + return entityPlayer; + } + + public List getItems() { + return items; + } + + public void add(ItemStack item) { + items.add(item); + } +} diff --git a/src/main/java/vazkii/botania/client/core/handler/HUDHandler.java b/src/main/java/vazkii/botania/client/core/handler/HUDHandler.java index 6c692bce7..0b10b84df 100644 --- a/src/main/java/vazkii/botania/client/core/handler/HUDHandler.java +++ b/src/main/java/vazkii/botania/client/core/handler/HUDHandler.java @@ -10,7 +10,11 @@ */ package vazkii.botania.client.core.handler; -import baubles.common.lib.PlayerHandler; +import java.awt.Color; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import net.minecraft.block.Block; import net.minecraft.block.BlockLiquid; import net.minecraft.block.state.IBlockState; @@ -36,13 +40,16 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.relauncher.ReflectionHelper; + import org.lwjgl.opengl.GL11; + import vazkii.botania.api.lexicon.ILexicon; import vazkii.botania.api.lexicon.ILexiconable; import vazkii.botania.api.lexicon.LexiconEntry; import vazkii.botania.api.mana.ICreativeManaProvider; import vazkii.botania.api.mana.IManaItem; import vazkii.botania.api.mana.IManaUsingItem; +import vazkii.botania.api.mana.ManaItemHandler; import vazkii.botania.api.recipe.RecipeManaInfusion; import vazkii.botania.api.wand.IWandHUD; import vazkii.botania.api.wiki.IWikiProvider; @@ -66,8 +73,7 @@ import vazkii.botania.common.item.equipment.bauble.ItemDodgeRing; import vazkii.botania.common.item.equipment.bauble.ItemFlightTiara; import vazkii.botania.common.item.equipment.bauble.ItemMonocle; import vazkii.botania.common.lib.LibObfuscation; - -import java.awt.*; +import baubles.common.lib.PlayerHandler; public final class HUDHandler { @@ -213,18 +219,33 @@ public final class HUDHandler { Item item = stack.getItem(); if(item instanceof IManaUsingItem) anyRequest = anyRequest || ((IManaUsingItem) item).usesMana(stack); - - if(item instanceof IManaItem) { - if(!((IManaItem) item).isNoExport(stack)) { - totalMana += ((IManaItem) item).getMana(stack); - totalMaxMana += ((IManaItem) item).getMaxMana(stack); - } - } - - if(item instanceof ICreativeManaProvider && ((ICreativeManaProvider) item).isCreative(stack)) - creative = true; } } + + List items = ManaItemHandler.getManaItems(player); + for (ItemStack stack : items) + { + Item item = stack.getItem(); + if(!((IManaItem) item).isNoExport(stack)) { + totalMana += ((IManaItem) item).getMana(stack); + totalMaxMana += ((IManaItem) item).getMaxMana(stack); + } + if(item instanceof ICreativeManaProvider && ((ICreativeManaProvider) item).isCreative(stack)) + creative = true; + } + + Map baubles = ManaItemHandler.getManaBaubles(player); + for (Entry entry : baubles.entrySet()) + { + ItemStack stack = entry.getValue(); + Item item = stack.getItem(); + if(!((IManaItem) item).isNoExport(stack)) { + totalMana += ((IManaItem) item).getMana(stack); + totalMaxMana += ((IManaItem) item).getMaxMana(stack); + } + if(item instanceof ICreativeManaProvider && ((ICreativeManaProvider) item).isCreative(stack)) + creative = true; + } if(anyRequest) renderManaInvBar(event.getResolution(), creative, totalMana, totalMaxMana);