Topic category: User side tutorials
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 namepull
, 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 namepulling
, 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
yo, good job! and you're welcome :D
Hello, I have a lot of error on the step 3, i don't know what I'm suppose to change in the line of code to make it work. I name my Bow: test .
do you know the name of your bow's class? (you can check it by opening your bow's Java code and looking for something like "public class YourBowName", but obviously YourBowName would be different, depending on what you named your bow, so memorize yours) did you make sure you changed "(ExampleBowItem.block)" to whatever your bow's class is named and didn't simply copy-paste the code?
also perhaps share the source code of your bow (.java) and the setup on step 3, i might've been able to figure out the error's cause and it might make solving this problem quicker
... what
explain
:>
greetings
even tho im not getting any errors, the bow is stuck on one frame "still one", pull0-2 are not in action. any ideas what could cause it? also could i send you the codes or whole mod for checking it out if i missed something from the tutorial or messed something up in it.
sure! simply provide the json files of all the bow models (normal & pull0-2) as well as console logs (since the console usually logs json errors, so that could be helpful to pinpoint the exact location & reason of the error)
have send invite on discord. will provide files there.
You are a legend. Is there any way to make the player able to pick the arrows up?
Never mind I just made a custom model and all works fine, but how can I go about changing the velocity/range of the arrow? Or alter its damage? Don't want to mess with the wrong values.
I REQUIRE EXPLANATION!!!!!
I followed the tutorial and it didn't work sadly, it's probably something I did wrong but I can't really find where. Everything seemed to be fine until I created the custom mod element (step 3) because my workspace console says the problem is in that mod element. I have downloaded and checked your example (Arcane Bow Workspace) and I have tried to replicate that too but it didn't work either. Here is the console error:
More details:
Mod id: world_of_hazard
Bow id: acidious_bow
Bow name: Acidious Bow
huh, problem was quite easy to identify this time. if you look carefully: your code's class declaration "public class ExampleModClient" doesn't match the file name which is "WorldOfHazardClient". in all cases, class declaration for public classes in code SHOULD match the class file name, otherwise Java throws the error:
(e.g. in your case, if the class name is "WorldOfHazardClient" then the declaration in code should be "public class WorldOfHazardClient")
after fixing that, Ctrl+W to potentially fix code formatting and add required imports that might've been missed
I changed the classname to "WorldOfHazardClient" but I'm still getting the same error 😮