[Tutorial] [1.12.2] How to make a "vanilla-like", pullable bow.

Started by mega1134227 on

Topic category: User side tutorials

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] [1.12.2] How to make a "vanilla-like", pullable bow.
Thu, 04/09/2020 - 00:29 (edited)

For this tutorial, I'll show you guys how I make my ranged items pullable like the vanilla bows.
First of all, you'll have to add in an extra import around line 7 since the reformat imports function doesn't add it in

So the import is:

import javax.annotation.Nullable;

Next, you have to add in a property in the RangedItem class right under SetCreativeTab(somerandomtab);.
This is the property :

this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter() {
                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    if (entityIn == null) {
                        return 0.0F;
                    } else {
                        return entityIn.getActiveItemStack().getItem() != Items.BOW ? 0.0F : (float) (stack.getMaxItemUseDuration() - entityIn
                                .getItemInUseCount()) / 20.0F;
                    }
                }
            });
            this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter() {
                @SideOnly(Side.CLIENT)
                public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                    return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
                }
            });

Both of these combined give the bow it's pullback property, but that doesn't mean it works yet. We have two other parts to add in that calculate the range of the bullet depending on how long the bow was being pulled.
This bit of code is probably the hardest one to put in since it's hard to describe where you're supposed to put it.

You have to find this line of code if (entity.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, itemstack) > 0 || slotID != -1) {, put right under it the bit of code that gives the bullet it's velocity and change two values in the code.

The bit of code that you have to add in looks like this:
int i = this.getMaxItemUseDuration(itemstack) - timeLeft;
                    i = i;
                    if (i < 0)
                        return;
                    float f = getArrowVelocity(i);
                    EntityArrowCustom entityarrow = new EntityArrowCustom(world, entity);

And the bits of code you have to change look like this:
entityarrow.shoot(entity.getLookVec().x, entity.getLookVec().y, entity.getLookVec().z, power * 2, 0);
SoundCategory.NEUTRAL, 1, 1f / (itemRand.nextFloat() * 0.5f + 1f) + (power / 2));

You simply have to change the power part for i.
If you did everything right and your bow uses ammo, then your final bit of code should look like this:
@Override
        public void onPlayerStoppedUsing(ItemStack itemstack, World world, EntityLivingBase entityLivingBase, int timeLeft) {
            if (!world.isRemote && entityLivingBase instanceof EntityPlayerMP) {
                EntityPlayerMP entity = (EntityPlayerMP) entityLivingBase;
                int slotID = -1;
                for (int i = 0; i < entity.inventory.mainInventory.size(); i++) {
                    ItemStack stack = entity.inventory.mainInventory.get(i);
                    if (stack != null && stack.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem()
                            && stack.getMetadata() == new ItemStack(Items.ARROW, (int) (1)).getMetadata()) {
                        slotID = i;
                        break;
                    }
                }
                if (entity.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, itemstack) > 0 || slotID != -1) {
                    int i = this.getMaxItemUseDuration(itemstack) - timeLeft;
                    i = i;
                    if (i < 0)
                        return;
                    float f = getArrowVelocity(i);

                    EntityArrowCustom entityarrow = new EntityArrowCustom(world, entity);
                    entityarrow.shoot(entity.getLookVec().x, entity.getLookVec().y, entity.getLookVec().z, i * 2, 0);
                    entityarrow.setSilent(true);
                    entityarrow.setIsCritical(true);
                    entityarrow.setDamage(6.1);
                    entityarrow.setKnockbackStrength(2);
                    entityarrow.setFire(100);
                    itemstack.damageItem(1, entity);
                    int x = (int) entity.posX;
                    int y = (int) entity.posY;
                    int z = (int) entity.posZ;
                    world.playSound((EntityPlayer) null, (double) x, (double) y, (double) z,
                            (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation(
                                    ("entity.arrow.shoot"))), SoundCategory.NEUTRAL, 1, 1f / (itemRand.nextFloat() * 0.5f + 1f) + (i / 2));
                    if (entity.capabilities.isCreativeMode) {
                        entityarrow.pickupStatus = EntityArrow.PickupStatus.CREATIVE_ONLY;
                    } else {
                        if (new ItemStack(Items.ARROW, (int) (1)).isItemStackDamageable()) {
                            ItemStack stack = entity.inventory.getStackInSlot(slotID);
                            if (stack.attemptDamageItem(1, new Random(), entity)) {
                                stack.shrink(1);
                                stack.setItemDamage(0);
                            }
                        } else {
                            entity.inventory.clearMatchingItems(new ItemStack(Items.ARROW, (int) (1)).getItem(), -1, 1, null);
                        }
                    }
                    if (!world.isRemote)
                        world.spawnEntity(entityarrow);
                }
            }
        }

Then you have to add in this bit of code right under this part.
The bit looks like this:
public static float getArrowVelocity(int charge) {
            float f = (float) charge / 20.0F;
            f = (f * f + f * 2.0F) / 3.0F;
            if (f > 1.0F) {
                f = 1.0F;
            }
            return f;
        }

Just so you know, this is the part where you can manipulate the numbers to make it so its velocity goes up or down. The larger you make the division numbers, the less far your bullet will go, the smaller they are, the farther will your bullet go.
Finaly, you just have to change maxitemduration to 72000 and you should be done. You should find it around line 172 if you did everything right.
That's all, you just have to reformat the code and everything should work as long as you followed the instructions correctly.

Here's a workspace file for those of you it would help. I would like to thank NatePlays95 for providing us with this workspace.
https://mcreator.net/forum/58742/template-worspace-112-shield-bow-and-more

Edited by mega1134227 on Thu, 04/09/2020 - 00:29
Joined Apr 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can you Specify for what…
Thu, 03/05/2020 - 15:32

Can you Specify for what version this is?
Thank you

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yeah, sure. It's for 1.12.2…
Sat, 03/07/2020 - 01:46

Yeah, sure. It's for 1.12.2 and I'm working on the 1.14.4 version of it.

Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
So that's for a "drawing"…
Sat, 03/07/2020 - 03:34

So that's for a "drawing" animation or "drawing" damage

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It's for drawing "damage,"…
Sat, 03/07/2020 - 21:26

It's for drawing "damage," it affects the range of the bow and the power in the bullet.

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I've been busy updating my…
Thu, 04/09/2020 - 00:24

I've been busy updating my mod so I couldn't get around to making it for 1.14.4 yet. Sorry for the delay :(
But I don't think you should wait for me to make the tutorial, go ahead and find out for yourself!

Joined Jun 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hello, thanks for your…
Fri, 06/26/2020 - 02:57

Hello, thanks for your contribution, it helped me a lot, but I have a question. Do you know how to make a crossbow that holds the arrow or X projectile? You would help me a lot, thanks.

Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Did you ever find out how to…
Thu, 03/04/2021 - 23:03

Did you ever find out how to do that crossbow thing? I've been looking for several days and have found nothing to help with that yet.