[Tutorial] How to animate custom bow pulling (for 1.16)!

Started by Sajevius on

Topic category: User side tutorials

Last seen on 08:14, 28. Nov 2023
Joined Aug 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] How to animate custom bow pulling (for 1.16)!
Sun, 05/30/2021 - 11:47 (edited)

Custom bow pulling animations. Until now, they still aren't in MCreator. But just because MCreator's generator doesn't support them yet doesn't mean they're entirely impossible to make, coz this possibility can actually be unlocked through code. 

This tutorial is meant to be an extension of a tutorial made by our favorite fox XD TheLuxureSlime on how to do the same thing for 1.15 and below; however, this one’s for Forge 1.16 as said in the title. So if you're only interested in animating bows for 1.15 & below, make sure to check the mentioned tutorial instead (huge thanks to it for required JSON code that we'll be talking about!) There’s a handful of code involved now that we're modding 1.16, so this is going to be harder than how it used to be in prior versions. But hopefully, you can follow along and be able to successfully animate your bow!


PREPARATIONS:

  • Mod workspace for Forge 1.16.5
  • Bow mod element that uses any ammo
  • JSON knowledge, more specifically for item models
  • Java knowledge
  • 3 pulling textures for your bow (1st stage, 2nd stage, 3rd stage)
    • 1st stage example: "example_bow_pulling_0"
    • 2nd stage example: "example_bow_pulling_1"
    • 3rd stage example: "example_bow_pulling_2"
  • Memorization of the following:
    • your mod ID
    • your bow item class name

ADVANTAGES:

  • Bow pulling will now be animated, making it look better
  • Players can no longer spam right-click to use your bows instantly

DISADVANTAGES:

  • Slightly harder than on previous MC versions
  • Still plays the animation even if the bow is out of ammo for some weird reason (even if the pull isn't animated, MCreator ranged items still play their respective animation somehow when used without ammo, which is weird...)

STEP 1 of 3: Editing JSON Item Model

Make sure that you've already imported the 3 pulling textures for your bow. Remember what you named each of them, because you'll be referring to each one in the JSON code! 

Go to your mod's workspace folder, either through clicking "Workspace -> Open Workspace Folder" or browsing it yourself via your file explorer. In the folder, navigate to src/main/resources/assets/mod_id/models/item. You should see the JSON file of your bow there.

  • Example: example_bow.json

Duplicate the file and rename the duplicated file to something that has "pulling_0".

  • Example: example_bow_pulling_0.json

Open that file, and replace everything with these lines:

{
    "parent": "item/bow",
    "textures": 
    {
        "layer0": "mod_id:items/example_bow_pulling_0"
    }
}

Remember to always replace "example_bow_pulling" with the appropriate texture name of the pulling bow! For example, if your bow's "pulling_0" texture is named "cool_bow_pulling_0", then make sure that it should be exactly like that. Also replace "mod_id" with the appropriate mod ID that you specified when creating your workspace (you can always check workspace settings to view your mod ID.) And then save it! 

Keep the "pulling_0" file open, then replace "pulling_0" with "pulling_1" 

  • Example: from "example_bow_pulling_0" to "example_bow_pulling_1"

and save the file as "example_bow_pulling_1.json".

Lastly, change it to "pulling_2"

  • Example: from "example_bow_pulling_1" to "example_bow_pulling_2"

And save it as "example_bow_pulling_2", then close your text/code editor.

Go back to MCreator and lock the code of your bow. Then double-click it; you should see three file options pop out.
Choose the last option which ends with ".json". Once you open yours, you should see something that looks similar to this:
 

{
  "parent": "item/generated",
  "textures": {
    "layer0": "mod_id:items/example_bow"
  },
  "display": {
    "thirdperson_righthand": {
      "rotation": [
        -80,
        260,
        -40
      ],
      "translation": [
        -1,
        -2,
        2.5
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "thirdperson_lefthand": {
      "rotation": [
        -80,
        -280,
        40
      ],
      "translation": [
        -1,
        -2,
        2.5
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "firstperson_righthand": {
      "rotation": [
        0,
        -90,
        25
      ],
      "translation": [
        1.13,
        3.2,
        1.13
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "firstperson_lefthand": {
      "rotation": [
        0,
        90,
        -25
      ],
      "translation": [
        1.13,
        3.2,
        1.13
      ],
      "scale": [
        1,
        1,
        1
      ]
    }
  }
}

Find the second to the last closed curly bracket '}' in there, which is near the end of the file, then add these lines:

,
  "overrides": [
    {
      "predicate": {
        "pulling": 1
      },
      "model": "mod_id:item/example_bow_pulling_0"
    },
    {
      "predicate": {
        "pulling": 1,
        "pull": 0.65
      },
      "model": "mod_id:item/example_bow_pulling_1"
    },
    {
      "predicate": {
        "pulling": 1,
        "pull": 0.9
      },
      "model": "mod_id:item/example_bow_pulling_2"
    }
  ]

Final result should look like this:

{
  "parent": "item/generated",
  "textures": {
    "layer0": "mod_id:items/example_bow"
  },
  "display": {
    "thirdperson_righthand": {
      "rotation": [
        -80,
        260,
        -40
      ],
      "translation": [
        -1,
        -2,
        2.5
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "thirdperson_lefthand": {
      "rotation": [
        -80,
        -280,
        40
      ],
      "translation": [
        -1,
        -2,
        2.5
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "firstperson_righthand": {
      "rotation": [
        0,
        -90,
        25
      ],
      "translation": [
        1.13,
        3.2,
        1.13
      ],
      "scale": [
        1,
        1,
        1
      ]
    },
    "firstperson_lefthand": {
      "rotation": [
        0,
        90,
        -25
      ],
      "translation": [
        1.13,
        3.2,
        1.13
      ],
      "scale": [
        1,
        1,
        1
      ]
    }
  },
  "overrides": [
    {
      "predicate": {
        "pulling": 1
      },
      "model": "mod_id:item/example_bow_pulling_0"
    },
    {
      "predicate": {
        "pulling": 1,
        "pull": 0.65
      },
      "model": "mod_id:item/example_bow_pulling_1"
    },
    {
      "predicate": {
        "pulling": 1,
        "pull": 0.9
      },
      "model": "mod_id:item/example_bow_pulling_2"
    }
  ]
}

Then save and close it! That makes us done with step 1!

 

STEP 2 of 3: Modifying Bow Code

Remember how we locked the bow's code in the previous step? This time, go to the bow item's Java class. Again, double-click bow element, but select the first option, which you'll know because it has ".java" as a file extension.

  • Example: ExampleBowItem.java

First things first, look for this line:

public static class ItemRanged extends Item {

We don't want our bow to simply extend the Item class, because bows use a special method called getArrowVelocity() that's exclusive to them; it isn't available for a normal item, which is what MCreator's default ranged weapons extend from, so if we use that method when our bow extends Item, there will be errors. So let's change it to extend BowItem instead by replacing Item with BowItem (such that the code looks exactly like this):

public static class ItemRanged extends BowItem {

The following are the last things to modify about the bow. So search for this line:

@Override
public void onPlayerStoppedUsing(ItemStack itemstack, World world, LivingEntity entityLiving, int timeLeft) {

Under that are the checks and methods that determine the result after the player stops using the item (when player lets go of the right mouse button.) 

Let's assume that you made a bow which uses arrow as ammo. In that case, its code usually looks like this:

@Override
		public void onPlayerStoppedUsing(ItemStack itemstack, World world, LivingEntity entityLiving, int timeLeft) {
			if (!world.isRemote && entityLiving instanceof ServerPlayerEntity) {
				ServerPlayerEntity entity = (ServerPlayerEntity) entityLiving;
				double x = entity.getPosX();
				double y = entity.getPosY();
				double z = entity.getPosZ();
				if (true) {
					ItemStack stack = ShootableItem.getHeldAmmo(entity, e -> e.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem());
					if (stack == ItemStack.EMPTY) {
						for (int i = 0; i < entity.inventory.mainInventory.size(); i++) {
							ItemStack teststack = entity.inventory.mainInventory.get(i);
							if (teststack != null && teststack.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem()) {
								stack = teststack;
								break;
							}
						}
					}
					if (entity.abilities.isCreativeMode || stack != ItemStack.EMPTY) {
						ArrowCustomEntity entityarrow = shoot(world, entity, random, 1f, 4, 0);
						itemstack.damageItem(1, entity, e -> e.sendBreakAnimation(entity.getActiveHand()));
						if (entity.abilities.isCreativeMode) {
							entityarrow.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
						} else {
							if (new ItemStack(Items.ARROW, (int) (1)).isDamageable()) {
								if (stack.attemptDamageItem(1, random, entity)) {
									stack.shrink(1);
									stack.setDamage(0);
									if (stack.isEmpty())
										entity.inventory.deleteStack(stack);
								}
							} else {
								stack.shrink(1);
								if (stack.isEmpty())
									entity.inventory.deleteStack(stack);
							}
						}
					}
				}
			}
		}

This is not very organized and is also missing a few lines of code that react to the player pulling the bow, so we don't want to stay with this.
Instead, let's change it to:

@Override
		public void onPlayerStoppedUsing(ItemStack itemstack, World world, LivingEntity entityLiving, int timeLeft) {
			if (!world.isRemote && entityLiving instanceof ServerPlayerEntity) {
				ServerPlayerEntity entity = (ServerPlayerEntity) entityLiving;
				double x = entity.getPosX();
				double y = entity.getPosY();
				double z = entity.getPosZ();
				boolean flag = entity.abilities.isCreativeMode;
				ItemStack stack = ShootableItem.getHeldAmmo(entity, e -> e.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem());
				int h = this.getUseDuration(itemstack) - timeLeft;
				h = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(itemstack, world, entity, h, flag || stack != ItemStack.EMPTY);
				if (h < 0)
					return;
				if (stack == ItemStack.EMPTY) {
					for (int i = 0; i < entity.inventory.mainInventory.size(); i++) {
						ItemStack teststack = entity.inventory.mainInventory.get(i);
						if (teststack != null && teststack.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem()) {
							stack = teststack;
							break;
						}
					}
				}
				if (flag || stack != ItemStack.EMPTY) {
					float j = getArrowVelocity(h);
					if (!((double) j < 0.1D)) {
						ArrowCustomEntity entityarrow = shoot(world, entity, random, 1f, 4, 0);
						itemstack.damageItem(1, entity, e -> e.sendBreakAnimation(entity.getActiveHand()));
						if (entity.abilities.isCreativeMode) {
							entityarrow.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
						} else {
							if (new ItemStack(Items.ARROW, (int) (1)).isDamageable()) {
								if (stack.attemptDamageItem(1, random, entity)) {
									stack.shrink(1);
									stack.setDamage(0);
									if (stack.isEmpty())
										entity.inventory.deleteStack(stack);
								}
							} else {
								stack.shrink(1);
								if (stack.isEmpty())
									entity.inventory.deleteStack(stack);
							}
						}
					}
				}
			}
		}

EXPLANATION:

  • boolean flag = entity.abilities.isCreativeMode - this variable is either true or false, depending on the player's current gamemode. if player is in creative mode, this equals to true; otherwise, this equals to false in other gamemodes.
  • int h = this.getUseDuration(itemstack) - timeLeft - this variable is used to store the charge/pull of the bow, which is an integer (whole number).
  • h = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(itemstack, world, entity, h, flag || stack != ItemStack.EMPTY) - here, we are copying the value of the bow's charge to the variable "h".
  • float j = getArrowVelocity(h) - this variable is equal to the velocity of the arrow, which is determined by "h" or the bow's charge.
  • if (!((double) j < 0.1D)) - this checks if the variable "j" is not less than 0.1. If yes, it continues executing code; otherwise, if it's less than 0.1, the bow won't shoot the arrow. What's good about this is that it actually prevents the player from spamming right-click to instantly shoot!

Finally, use Ctrl + W, which most likely fixes the entire code by organizing it and adding required imports.

(If you're curious, the only additional import we need is:)

import net.minecraft.item.BowItem;

Everything good? No build errors? Then let's proceed to the 3rd and final step! :D

(If there were errors, feel free to consult me!)

 

STEP 3 of 3: Creating Mod Client Setup

This is the final step before going in-game to test if everything works. This is mandatory, as the animation technically won't exist without this, so take note!

Click the plus icon and create a new custom element. Let's use this format for naming the custom element:

Mod name (without spaces) + "Client"

  • Example: ExampleModClient.java

Erase everything in there, as we don't need them. Instead, code the following:

package net.mcreator.example_mod;

import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.util.ResourceLocation;
import net.minecraft.item.ItemModelsProperties;

import net.mcreator.example_mod.item.ExampleBowItem;

@Mod.EventBusSubscriber(modid = "example_mod", value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ExampleModClient {
	@SubscribeEvent
	public static void setModelProperties(FMLClientSetupEvent event) {
		ItemModelsProperties.registerProperty((ExampleBowItem.block), new ResourceLocation("pull"), (itemstack, world, entity) -> {
			if (entity == null) {
				return 0.0F;
			} else {
				return entity.getActiveItemStack() != itemstack ? 0.0F : (float) (itemstack.getUseDuration() - entity.getItemInUseCount()) / 20.0F;
			}
		});
		ItemModelsProperties.registerProperty((ExampleBowItem.block), new ResourceLocation("pulling"),
				(itemstack, world, entity) -> entity != null && entity.isHandActive() && entity.getActiveItemStack() == itemstack ? 1.0F : 0.0F);
	}
}

EXPLANATION:

  • ItemModelsProperties.registerProperty((ExampleBowItem.block), new ResourceLocation("pull"), (itemstack, world, entity) -> {//code here})}  - This is a lambda expression that takes the custom bow you specified (example here: ExampleBowItem.block) and registers a property for it by creating a new ResourceLocation with the name pull, which value is equal to the bow's charge/pull amount. Then, (itemstack, world, entity) are the three parameters used for the bow, the "->" arrow is the lambda operator, and the rest under it is the body that's necessary for determining the value of that property. 
  • ItemModelsProperties.registerProperty((ExampleBowItem.block), new ResourceLocation("pulling"), (itemstack, world, entity) -> // code here - This one, unlike the previous, registers a property with the name pulling, which is different because this acts like true/false, depending on whether the player is pulling the bow or not.

Here's a sample workspace to finish off & show the nitty-gritty parts of this tutorial!

And that's it for this one. I'm proud of you for making it this far!

If there were any errors, please let me know! This tutorial is open to changes in case I accidentally wrote bad code/forgot to include something... because I openly admit: even if this tutorial works, I still kinda suck at coding, coz maybe I miss including some things or perhaps write code that isn't very efficient. So feedback from someone with decent Java knowledge would be very much appreciated to avoid potential bad coding practices in the future, if any!


Special thanks to:

  • TheLuxureSlime, for the original tutorial which is for Forge 1.15 ( you're a legend :D )
  • akisephila, for inspiring me to find a solution for 1.16, the version most people are modding right now
  • Frostygames0, for making me aware that 1.16 property overrides are registered in client setup event, which helped complete this tutorial
  • Gobber (mod by kwpugh) source code, for greatly inspiring the code in the client setup event class we made
Edited by Sajevius on Sun, 05/30/2021 - 11:47
Last seen on 15:10, 24. Dec 2023
Joined Jun 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank you for the tutorial,…
Sun, 06/26/2022 - 00:56

Thank you for the tutorial, this was exactly what I was looking for, but can you update the tutorial for the 1.18.2 version please? I'm really struggling with the bow rn

Last seen on 08:14, 28. Nov 2023
Joined Aug 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm away from home for a…
Sun, 06/26/2022 - 14:07

I'm away from home for a couple of days so you may have to wait, but I'll look into it :D

Last seen on 17:24, 17. Oct 2022
Joined Jul 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
import net.minecraft.item…
Mon, 08/22/2022 - 12:29

import net.minecraft.item.BowItem;

where am i suposed to place this sorry for my english

Last seen on 17:24, 17. Oct 2022
Joined Jul 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you make Video tutorial…
Thu, 08/25/2022 - 20:08

Can you make Video tutorial please

 

Last seen on 13:56, 19. Dec 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Sisoniklstvan put that…
Wed, 09/07/2022 - 23:20

@Sisoniklstvan put that import right before the top.

Last seen on 16:04, 31. Oct 2022
Joined Oct 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
nvm i fixed it. I forgot to…
Sun, 10/30/2022 - 14:55

nvm i fixed it. I forgot to change 

    public static class ItemRanged extends Item {

to

    public static class ItemRanged extends BowItem {

im a little stupid lmao

Last seen on 05:04, 5. Feb 2023
Joined Jan 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey, im getting this error! …
Mon, 01/09/2023 - 04:26

Hey, im getting this error! 

Executing Gradle task: runClient
Build info: MCreator 2022.3.48217, forge-1.19.2, 64-bit, 16259 MB, Windows 11, JVM 17.0.3, JAVA_HOME: C:\Program Files\Pylo\MCreator\jdk, started on: 2023-01-08-23:24:24

> Configure project :
The code of this workspace uses official obfuscation mappings provided by Mojang. These mappings fall under their associated license you should be fully aware of.
(c) 2020 Microsoft Corporation. These mappings are provided "as-is" and you bear the risk of using them. You may copy and use the mappings for development purposes,
but you may not redistribute the mappings complete and unmodified. Microsoft makes no warranties, express or implied, with respect to the mappings provided here.
Use and modification of this document or the source code (in any form) of Minecraft: Java Edition is governed by the Minecraft End User License Agreement available
at https://account.mojang.com/documents/minecraft_eula.

> Task :compileJava FAILED
C:\Users\Owner\MCreatorWorkspaces\best_superhero_mod\src\main\java\net\mcreator\bestsuperheromod\item\CompoundBowItem.java:22: error: invalid method declaration; return type required public BowItem() {
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
1 actionable task: 1 executed

BUILD FAILED
Task completed in 4 seconds

 

Last seen on 04:12, 11. Dec 2023
Joined Jun 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It's perfect!But how to make…
Sun, 07/23/2023 - 12:23

It's perfect!But how to make it in 1.19?

Last seen on 18:52, 28. Feb 2024
Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I keep trying to run this…
Fri, 01/12/2024 - 21:54

I keep trying to run this mod that I am working on and it just won't run for some reason, any help would be appreciated.

 

> Task :compileJava FAILED
C:\Users\Owner\MCreatorWorkspaces\smithing_plus_mod\src\main\java\net\mcreator\irontools\item\IronBowItem.java:206: error: reached end of file while parsing }
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
1 actionable task: 1 executed

BUILD FAILED
Task completed in 4 seconds

Last seen on 18:52, 28. Feb 2024
Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I managed to fix the issue…
Fri, 01/12/2024 - 23:53

I managed to fix the issue but now I'm getting these errors, I will appreciate any help whatsoever, hope you all have a great day/night.

https://imgur.com/a/VAnHJfV

Last seen on 18:52, 28. Feb 2024
Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I finally fixed all the…
Sun, 01/14/2024 - 20:38

I finally fixed all the errors in the files but for some reason, the bow doesn't show up in the game, but the bow base item does.

Last seen on 18:52, 28. Feb 2024
Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Never mind, I finally got it…
Sun, 01/14/2024 - 22:45

Never mind, I finally got it all to work, currently trying to make it so the bow fires an arrow entity rather than a PNG of an arrow lol..