From 73d973783dbb57aa89b240477fbec8c69b08966d Mon Sep 17 00:00:00 2001 From: Electroblob <35599699+Electroblob77@users.noreply.github.com> Date: Sun, 24 Jun 2018 15:48:48 +0100 Subject: [PATCH] Add continuous spell casting with scrolls and increase scroll and spell book stack sizes to 16 --- .../electroblob/wizardry/CommonProxy.java | 7 -- .../wizardry/client/ClientProxy.java | 2 - .../electroblob/wizardry/item/ItemScroll.java | 78 +++++++++++++++++-- .../wizardry/item/ItemSpellBook.java | 2 +- .../wizardry/registry/WizardryItems.java | 5 +- 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/main/java/electroblob/wizardry/CommonProxy.java b/src/main/java/electroblob/wizardry/CommonProxy.java index b8ef9d67..cb2e1441 100644 --- a/src/main/java/electroblob/wizardry/CommonProxy.java +++ b/src/main/java/electroblob/wizardry/CommonProxy.java @@ -87,14 +87,7 @@ public class CommonProxy { */ public String getScrollDisplayName(ItemStack scroll){ - // I have now learnt that the server side I18n always translates to the default en_US, so I could just return - // a hardcoded name in English instead. - Wizardry.logger.info("A mod has called ItemScroll#getItemStackDisplayName from the server side. Using the" - + "deprecated server-side translation methods as a fallback."); - - // Displays [Empty slot] if spell is continuous. Spell spell = Spell.get(scroll.getItemDamage()); - if(spell.isContinuous) spell = Spells.none; return I18n.translateToLocalFormatted("item." + Wizardry.MODID + ":scroll.name", I18n.translateToLocal("spell." + spell.getUnlocalisedName())).trim(); diff --git a/src/main/java/electroblob/wizardry/client/ClientProxy.java b/src/main/java/electroblob/wizardry/client/ClientProxy.java index cd68ad7c..93deb66d 100644 --- a/src/main/java/electroblob/wizardry/client/ClientProxy.java +++ b/src/main/java/electroblob/wizardry/client/ClientProxy.java @@ -241,9 +241,7 @@ public class ClientProxy extends CommonProxy { @Override public String getScrollDisplayName(ItemStack scroll){ - // Displays [Empty slot] if spell is continuous. Spell spell = Spell.get(scroll.getItemDamage()); - if(spell.isContinuous) spell = Spells.none; EntityPlayer player = Minecraft.getMinecraft().player; diff --git a/src/main/java/electroblob/wizardry/item/ItemScroll.java b/src/main/java/electroblob/wizardry/item/ItemScroll.java index e70f730e..cd27e026 100644 --- a/src/main/java/electroblob/wizardry/item/ItemScroll.java +++ b/src/main/java/electroblob/wizardry/item/ItemScroll.java @@ -10,6 +10,7 @@ import electroblob.wizardry.spell.Spell; import electroblob.wizardry.util.SpellModifiers; import net.minecraft.client.gui.FontRenderer; import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -24,19 +25,27 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class ItemScroll extends Item { + + /** The maximum number of ticks a continuous spell scroll can be cast for (by holding the use item button). */ + // TODO: Make this configurable + public static final int CASTING_TIME = 120; public ItemScroll(){ super(); setHasSubtypes(true); - setMaxStackSize(1); + setMaxStackSize(16); setCreativeTab(WizardryTabs.SPELLS); } @Override public void getSubItems(CreativeTabs tab, NonNullList list){ - if (isInCreativeTab(tab)) { - for(Spell spell : Spell.getSpells(Spell.nonContinuousSpells)){ - list.add(new ItemStack(this, 1, spell.id())); + if(isInCreativeTab(tab)){ + // In this particular case, getTotalSpellCount() is a more efficient way of doing this since the spell instance + // is not required, only the id. + for(int i = 0; i < Spell.getTotalSpellCount(); i++){ + // i+1 is used so that the metadata ties up with the id() method. In other words, the none spell has id + // 0 and since this is not used as a spell book the metadata starts at 1. + list.add(new ItemStack(this, 1, i + 1)); } } } @@ -63,6 +72,11 @@ public class ItemScroll extends Item { return Wizardry.proxy.getScrollDisplayName(stack); } + + @Override + public int getMaxItemUseDuration(ItemStack stack){ + return CASTING_TIME; + } @Override public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand){ @@ -78,7 +92,13 @@ public class ItemScroll extends Item { return new ActionResult(EnumActionResult.FAIL, stack); } - if(!spell.isContinuous){ + // Now we can cast continuous spells with scrolls! + if(spell.isContinuous){ + if(!player.isHandActive()){ + player.setActiveHand(hand); + return new ActionResult(EnumActionResult.SUCCESS, stack); + } + }else{ if(!world.isRemote){ @@ -112,6 +132,54 @@ public class ItemScroll extends Item { return new ActionResult(EnumActionResult.FAIL, stack); } + + // For continuous spells. The count argument actually decrements by 1 each tick. + @Override + public void onUsingTick(ItemStack stack, EntityLivingBase user, int count){ + + if(user instanceof EntityPlayer){ + + EntityPlayer player = (EntityPlayer)user; + + Spell spell = Spell.get(stack.getItemDamage()); + // By default, scrolls have no modifiers - but with the event system, they could be added. + SpellModifiers modifiers = new SpellModifiers(); + int castingTick = stack.getMaxItemUseDuration() - count; + + if(MinecraftForge.EVENT_BUS.post(new SpellCastEvent.Tick(Source.SCROLL, spell, player, modifiers, castingTick))) + return; + + // Continuous spells (these must check if they can be cast each tick since the mana changes) + if(spell.isContinuous){ + + if(spell.cast(player.world, player, player.getActiveHand(), castingTick, modifiers)){ + + if(castingTick == 0) + MinecraftForge.EVENT_BUS.post(new SpellCastEvent.Post(Source.SCROLL, spell, player, modifiers)); + } + } + } + } + + @Override + public void onPlayerStoppedUsing(ItemStack stack, World world, EntityLivingBase user, int timeLeft){ + // Consumes a continuous spell scroll when a player in survival mode stops using it. + if(Spell.get(stack.getItemDamage()).isContinuous + && (!(user instanceof EntityPlayer) || !((EntityPlayer)user).capabilities.isCreativeMode)){ + stack.shrink(1); + } + } + + @Override + public ItemStack onItemUseFinish(ItemStack stack, World world, EntityLivingBase user){ + // Consumes a continuous spell scroll when the casting elapses whilst in use by a player in survival mode. + if(Spell.get(stack.getItemDamage()).isContinuous + && (!(user instanceof EntityPlayer) || !((EntityPlayer)user).capabilities.isCreativeMode)){ + stack.shrink(1); + } + + return stack; + } @Override @SideOnly(Side.CLIENT) diff --git a/src/main/java/electroblob/wizardry/item/ItemSpellBook.java b/src/main/java/electroblob/wizardry/item/ItemSpellBook.java index 8e7cc4c3..eb785366 100644 --- a/src/main/java/electroblob/wizardry/item/ItemSpellBook.java +++ b/src/main/java/electroblob/wizardry/item/ItemSpellBook.java @@ -30,7 +30,7 @@ public class ItemSpellBook extends Item { public ItemSpellBook(){ super(); setHasSubtypes(true); - setMaxStackSize(1); + setMaxStackSize(16); setCreativeTab(WizardryTabs.SPELLS); } diff --git a/src/main/java/electroblob/wizardry/registry/WizardryItems.java b/src/main/java/electroblob/wizardry/registry/WizardryItems.java index e7bf125a..a6549f63 100644 --- a/src/main/java/electroblob/wizardry/registry/WizardryItems.java +++ b/src/main/java/electroblob/wizardry/registry/WizardryItems.java @@ -13,6 +13,7 @@ import electroblob.wizardry.constants.Element; import electroblob.wizardry.constants.Tier; import electroblob.wizardry.item.ItemArcaneTome; import electroblob.wizardry.item.ItemArmourUpgrade; +import electroblob.wizardry.item.ItemBlankScroll; import electroblob.wizardry.item.ItemFirebomb; import electroblob.wizardry.item.ItemFlamingAxe; import electroblob.wizardry.item.ItemFrostAxe; @@ -119,8 +120,8 @@ public final class WizardryItems { public static final Item firebomb = new ItemFirebomb(); public static final Item poison_bomb = new ItemPoisonBomb(); - public static final Item blank_scroll = new Item().setCreativeTab(WizardryTabs.WIZARDRY); - public static final Item scroll = new ItemScroll().setCreativeTab(WizardryTabs.SPELLS); + public static final Item blank_scroll = new ItemBlankScroll(); + public static final Item scroll = new ItemScroll(); // The only way to get these is in dungeon chests (They are legendary, after all. Wizards don't just have them. // Also if they were sold you could buy four from the same wizard - and that's no fun at all!)