[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 01:51, 19. Sep 2023
Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Awesome
Mon, 05/31/2021 - 19:56

Awesome

Last seen on 00:47, 11. Apr 2022
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
im late to this post, but it…
Mon, 06/14/2021 - 21:51

im late to this post, but it's really well done

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
thank you both! and E12345E,…
Wed, 06/23/2021 - 12:46

thank you both!

and E12345E, you know what they say: "it's better late than never"! don't worry, it's not a problem at all for me if you came late. and you can always feel free to try this method next time in case you used a different one last time ;)

Last seen on 23:59, 19. Feb 2022
Joined May 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Does it work on other…
Sun, 06/27/2021 - 12:25

Does it work on other versions as well?

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
nope... firstly, this won't…
Sun, 06/27/2021 - 12:41

nope... firstly, this won't work in previous versions since the system of registering the bow's item model properties for 1.16 is different from that of the past versions if I'm right; secondly (and kinda unrelated), Forge still doesn't have a 1.17 version. if you're looking to do it on 1.15, check the link i put (or if you already know it, then refer to it instead of this), which leads to another user's tutorial (i think that one could also work on 1.14, haven't tested tho). but if you're doing it for 1.12, then sadly I can't help you as i normally don't mod it anymore

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
idk if it's just me but when…
Mon, 06/28/2021 - 03:50

idk if it's just me but when i try to do the whole onPlayerStoppedUsing thing i get build errors, any way to fix this? I've tried CTRL-W

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
one possible cause is that…
Mon, 06/28/2021 - 04:31

one possible cause is that you must've used different ammo for your bow but copy-pasted the code i provided without refactoring it to accept your ammo instead... but i could be wrong, as you didn't provide your bow's code (all of it). well, idk a fix for it if you don't provide the code, so kindly post it somewhere like Pastebin then link it here

 

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh, also post the build…
Mon, 06/28/2021 - 04:34

oh, also post the build error so that we'll know what type of error it is

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
at the beggining i left the…
Mon, 06/28/2021 - 04:49

at the beggining i left the bow with no ammo, maybe that was a detrement

 

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

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
probably. anyways, how does…
Mon, 06/28/2021 - 05:07

probably. anyways, how does it go now?

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
i deleted and remade the bow…
Mon, 06/28/2021 - 05:12

i deleted and remade the bow with arrow as ammo, just now im gonna do the whole process again with  the arrow ammo bow, ill check if it works now :D

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
it doesn't work still :/ im…
Mon, 06/28/2021 - 05:46

it doesn't work still :/ im having a hard time trying to find what's wrong and it's late tmr ill try to find an answer

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
https://pastebin.com…
Mon, 06/28/2021 - 16:26

https://pastebin.com/EXkk3uEc completely forgot to provide the code, sorry ;<; it's the .java

Last seen on 23:42, 22. Aug 2021
Joined Jun 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
finally got it to work,…
Tue, 06/29/2021 - 00:18

finally got it to work, thanks for the guide :D