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
How can I solve it?
And also someone would help me, have I done this well? I think this is the problem, but I don't know how to solve it? help
I have no clue what I've done wrong here :/
I followed the tutorial, and it dind't worked,the log says this:
Executing Gradle task: runClient
Build info: MCreator 2021.2.36710, forge-1.16.5, 64-bit, 8127 MB, Windows 10, JVM 11.0.11, JAVA_HOME: D:\Program Files (x86)\MCreator\jdk
> Task :compileJava FAILED
D:\Documentos\mymods\src\main\java\net\mcreator\projetolk\item\CrystalExterminatorItem.java:48: error: invalid method declaration; return type required public CrystalExterminatorItem(ProjetolkModElements instance) {
^
D:\Documentos\mymods\src\main\java\net\mcreator\projetolk\item\CrystalExterminatorItem.java:204: error: reached end of file while parsing }
^
2 errors
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
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.1.1/userguide/command_line_interface.html#sec…
BUILD FAILED in 2s
1 actionable task: 1 executed
BUILD FAILED
Task completed in 3 seconds
hm, for the first error it looks like there's something wrong with the CrystalExterminatorItem(ProjetolkModElements instance) { constructor (it needs to be public, kindly recheck it) and for the 2nd one you must've added one too many closed curly bracket '}'. may i see your code?
P.S. this goes out to anyone asking for support: pls share your code somewhere (the ones that have the errors), be it on Pastebin or just here. otherwise it'd be hard to help you guys
Here it is
https://pastebin.com/NexhsWfA
alright LkS_, it seems you missed a single '}' after line 122:
other than that, your constructor:
looks correct to me. try fixing that closed curly bracket error first and see if it somehow fixes this error:
Jose4T5Y, in case you haven't solved the error yet (and if you're still updated w/ this topic), I've finally found the problem in your code after reviewing it very, very carefully. turns out, it was a typo in the package name:
change "com" to "net"
your mod is using MCreator's default package name of net.mcreator.(modname). so you need to follow it strictly or else Java won't be able to find the package name you typed in
oh, and also change the "com" over here (the package name declaration) to "net":
It worked, thank you so much!
I'm using arrow as my ammo but i cant' seem to find the onPlayerStoppedUsing piece of code
Hi,
I have a problem where no texture is loading on my bow, only shows that purple and black block texture.
I done everything from your tutorial, I even compared every code with your tutorial.
And I have all 3 pulling textures and the normal textures.
Please help!
Hello, thank Sajevius about this guide, but what i need do if mcreator not load texture for bow? Could you me pls help?
Could someone update this to mcreator 2022.1?
Can you make a tutorial for crossbows please?