Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.18.1] How can I make my custom bow/item use my custom arrow/item/ammo?

Featured Replies

Posted

Here is my main class for it:

public class Musket extends BowItem
{

    public Musket(Properties p_40660_)
    {
        super(p_40660_);
    }

    @Override
    public int getDefaultProjectileRange() {
        return 15;
    }

    @Override
    public void releaseUsing(ItemStack stack, Level worldIn, LivingEntity entityLiving, int timeLeft) {
        if (entityLiving instanceof Player) {
            Player playerentity = (Player) entityLiving;
            if (stack.getDamageValue() < (stack.getMaxDamage() - 1)) {
                playerentity.getCooldowns().addCooldown(this, 45);
                Player player = (Player) entityLiving;
                ItemStack itemstack = player.getProjectile(stack);
                
                if (itemstack.isEmpty()) {
                    itemstack = new ItemStack(ModItems.MUSKET_BULLET.get());
                }
                
                    MusketBullet arrowitem = (MusketBullet) (itemstack.getItem() instanceof MusketBullet ? itemstack.getItem() : ModItems.MUSKET_BULLET.get());
                    MusketAmmo abstractarrow = (MusketAmmo) arrowitem.createMusket(worldIn, itemstack, player);
                    abstractarrow = customeArrow(abstractarrow);
                    boolean flag1 = player.getAbilities().instabuild || (itemstack.getItem() instanceof MusketBullet && ((MusketBullet) itemstack.getItem())
                            .isInfinite(itemstack, stack, player));

                if (!worldIn.isClientSide) {
                    MusketAmmo abstractarrowentity = createArrow(worldIn, stack, playerentity);
                    abstractarrowentity = customeArrow(abstractarrowentity);
                    abstractarrowentity.shootFromRotation(playerentity, playerentity.getXRot(), playerentity.getYRot(),
                            0.0F, 1.0F * 3.0F, 1.0F);

                    abstractarrowentity.setBaseDamage(2.5);
                    abstractarrowentity.tickCount = 35;
                    abstractarrowentity.isNoGravity();

                    stack.hurtAndBreak(1, entityLiving, p -> p.broadcastBreakEvent(entityLiving.getUsedItemHand()));
                    worldIn.addFreshEntity(abstractarrowentity);
                    worldIn.playSound((Player) null, playerentity.getX(), playerentity.getY(), playerentity.getZ(),
                            ModSoundEvents.MUSKET.get(), SoundSource.PLAYERS, 1.0F,
                            1.0F / (worldIn.random.nextFloat() * 0.4F + 1.2F) + 0.25F * 0.5F);

                    if (!flag1 && !player.getAbilities().instabuild) {
                        itemstack.shrink(1);
                        if (itemstack.isEmpty()) {
                            player.getInventory().removeItem(itemstack);
                        }
                    }
                }
            }
        }
    }
    

    public InteractionResultHolder<ItemStack> use(Level p_40672_, Player p_40673_, InteractionHand p_40674_) {
        ItemStack itemstack = p_40673_.getItemInHand(p_40674_);
        boolean flag = !p_40673_.getProjectile(itemstack).isEmpty();

        InteractionResultHolder<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, p_40672_, p_40673_, p_40674_, flag);
        if (ret != null) return ret;

        if (!p_40673_.getAbilities().instabuild && !flag) {
            return InteractionResultHolder.fail(itemstack);
        } else {
            p_40673_.startUsingItem(p_40674_);
            return InteractionResultHolder.consume(itemstack);
        }
    }

    public MusketAmmo createArrow(Level worldIn, ItemStack stack, LivingEntity shooter)
    {
        MusketAmmo arrowentity = new MusketAmmo(worldIn, shooter);
        return arrowentity;
    }

    @Override
    public UseAnim getUseAnimation(ItemStack stack)
    {
        return UseAnim.BOW;
    }

    public MusketAmmo customeArrow(MusketAmmo arrow)
    {
        return arrow;
    }
}

ย 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.