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

Started by mega1134227 on Mon, 03/02/2020 - 14:54

Topic category: User side tutorials

Last seen on 14:05, 19. May 2020
Joined Feb 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • 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
Last seen on 07:33, 26. May 2020
Joined Apr 2014
User points:

User statistics:

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

Can you Specify for what version this is?
Thank you

Last seen on 14:05, 19. May 2020
Joined Feb 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • 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.

Last seen on 10:27, 26. May 2020
Joined Jun 2019
User points:

User statistics:

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

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

Last seen on 14:05, 19. May 2020
Joined Feb 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • 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.

Last seen on 10:27, 26. May 2020
Joined Jun 2019
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • Comments:
please make 1.14 soon :(
Fri, 04/03/2020 - 10:11

please make 1.14 soon :(

Last seen on 14:05, 19. May 2020
Joined Feb 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • 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!

Last seen on 10:27, 26. May 2020
Joined Jun 2019
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • Comments:
yeah that's a good idea :D
Thu, 04/09/2020 - 00:45

yeah that's a good idea :D