From ef39757229966d25eba7cd6188282da668f863f5 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Sun, 17 May 2020 00:04:38 +0100 Subject: [PATCH] Add right-click use for mana flasks - right-click while holding a mana flask to charge the emptiest item from the hotbar, offhand or armour slots --- .../wizardry/item/IManaStoringItem.java | 6 ++++ .../wizardry/item/ItemManaFlask.java | 35 +++++++++++++++++++ .../wizardry/registry/WizardrySounds.java | 4 +++ .../resources/assets/ebwizardry/sounds.json | 4 ++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/electroblob/wizardry/item/IManaStoringItem.java b/src/main/java/electroblob/wizardry/item/IManaStoringItem.java index 585f0752..26317e9e 100644 --- a/src/main/java/electroblob/wizardry/item/IManaStoringItem.java +++ b/src/main/java/electroblob/wizardry/item/IManaStoringItem.java @@ -71,4 +71,10 @@ public interface IManaStoringItem { default boolean isManaEmpty(ItemStack stack){ return getMana(stack) == 0; } + + /** Returns how full the given stack's mana is, as a fraction between 0 (empty) and 1 (full) */ + default float getFullness(ItemStack stack){ + return (float)getMana(stack) / getManaCapacity(stack); + } + } diff --git a/src/main/java/electroblob/wizardry/item/ItemManaFlask.java b/src/main/java/electroblob/wizardry/item/ItemManaFlask.java index 79f2105d..4b8ee67a 100644 --- a/src/main/java/electroblob/wizardry/item/ItemManaFlask.java +++ b/src/main/java/electroblob/wizardry/item/ItemManaFlask.java @@ -1,16 +1,23 @@ package electroblob.wizardry.item; import electroblob.wizardry.Wizardry; +import electroblob.wizardry.registry.WizardrySounds; import electroblob.wizardry.registry.WizardryTabs; +import electroblob.wizardry.util.WizardryUtilities; import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumRarity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nullable; +import java.util.Comparator; import java.util.List; public class ItemManaFlask extends Item { @@ -49,4 +56,32 @@ public class ItemManaFlask extends Item { Wizardry.proxy.addMultiLineDescription(tooltip, "item." + Wizardry.MODID + ":mana_flask.desc", size.capacity); } + @Override + public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand){ + + ItemStack flask = player.getHeldItem(hand); + + List stacks = WizardryUtilities.getPrioritisedHotbarAndOffhand(player); + stacks.addAll(player.inventory.armorInventory); // player#getArmorInventoryList() only returns an Iterable + + // Find the chargeable item with the least mana + ItemStack toCharge = stacks.stream() + .filter(s -> s.getItem() instanceof IManaStoringItem && !((IManaStoringItem)s.getItem()).isManaFull(s)) + .min(Comparator.comparingDouble(s -> ((IManaStoringItem)s.getItem()).getFullness(s))).orElse(null); + + if(toCharge != null){ + + ((IManaStoringItem)toCharge.getItem()).rechargeMana(toCharge, size.capacity); + + WizardryUtilities.playSoundAtPlayer(player, WizardrySounds.ITEM_MANA_FLASK_USE, 1, 1); + WizardryUtilities.playSoundAtPlayer(player, WizardrySounds.ITEM_MANA_FLASK_RECHARGE, 0.7f, 1.1f); + + if(!player.isCreative()) flask.shrink(1); + + return new ActionResult<>(EnumActionResult.SUCCESS, flask); + + }else{ + return new ActionResult<>(EnumActionResult.FAIL, flask); + } + } } diff --git a/src/main/java/electroblob/wizardry/registry/WizardrySounds.java b/src/main/java/electroblob/wizardry/registry/WizardrySounds.java index 6b732c7b..2e203b04 100644 --- a/src/main/java/electroblob/wizardry/registry/WizardrySounds.java +++ b/src/main/java/electroblob/wizardry/registry/WizardrySounds.java @@ -36,6 +36,8 @@ public final class WizardrySounds { public static final SoundEvent ITEM_WAND_MELEE = createSound("item.wand.melee"); public static final SoundEvent ITEM_ARMOUR_EQUIP_SILK = createSound("item.armour.equip_silk"); public static final SoundEvent ITEM_PURIFYING_ELIXIR_DRINK = createSound("item.purifying_elixir.drink"); + public static final SoundEvent ITEM_MANA_FLASK_USE = createSound("item.mana_flask.use"); + public static final SoundEvent ITEM_MANA_FLASK_RECHARGE = createSound("item.mana_flask.recharge"); public static final SoundEvent ENTITY_BLACK_HOLE_AMBIENT = createSound("entity.black_hole.ambient"); public static final SoundEvent ENTITY_BLACK_HOLE_VANISH = createSound("entity.black_hole.vanish"); @@ -164,6 +166,8 @@ public final class WizardrySounds { event.getRegistry().register(ITEM_WAND_MELEE); event.getRegistry().register(ITEM_ARMOUR_EQUIP_SILK); event.getRegistry().register(ITEM_PURIFYING_ELIXIR_DRINK); + event.getRegistry().register(ITEM_MANA_FLASK_USE); + event.getRegistry().register(ITEM_MANA_FLASK_RECHARGE); event.getRegistry().register(ENTITY_BLACK_HOLE_AMBIENT); event.getRegistry().register(ENTITY_BLACK_HOLE_VANISH); diff --git a/src/main/resources/assets/ebwizardry/sounds.json b/src/main/resources/assets/ebwizardry/sounds.json index 4ff79637..7686d7ba 100644 --- a/src/main/resources/assets/ebwizardry/sounds.json +++ b/src/main/resources/assets/ebwizardry/sounds.json @@ -10,7 +10,9 @@ "item.wand.melee": {"category": "player", "sounds": ["ebwizardry:effect1", "ebwizardry:effect2"]}, "item.armour.equip_silk": {"category": "player", "sounds": ["item/armor/equip_leather1", "item/armor/equip_leather2", "item/armor/equip_leather3", "item/armor/equip_leather4", "item/armor/equip_leather5", "item/armor/equip_leather6"]}, "item.purifying_elixir.drink": {"category": "player", "sounds": ["ebwizardry:buff_large"]}, - + "item.mana_flask.use": {"category": "player", "sounds": ["item/bottle/fill1", "item/bottle/fill2", "item/bottle/fill3", "item/bottle/fill4"]}, + "item.mana_flask.recharge": {"category": "player", "sounds": ["ebwizardry:conjure"]}, + "entity.black_hole.ambient": {"category": "spells", "sounds": ["portal/portal"]}, "entity.black_hole.vanish": {"category": "spells", "sounds": ["portal/trigger"]}, "entity.bubble.pop": {"category": "spells", "sounds": ["random/pop"]},