Jump to content

Recommended Posts

Posted

You can register it like this:

	public static final RegistryObject<Item> BOWTEST = ITEMS.register("bowtest", () -> new BowItem(new Item.Properties().maxDamage(500).group(AnItemGroup)));

Instead of the vanilla BowItem you can also use your own class.

 

But seeing your other posts, you already did this with other items, so i don't understand your problem.

Posted (edited)

I want to add my custom bow class because it makes it so that the ammo is bullets, and the speed of the throwing is velocity 100 and that it shoots constantly and not like a real bow but like a ak47 I was able to do this with mcreator but i cant seem to do it with java.

Edited by Somonestolemyusername
typo
Posted

here is the code for the gun i made and registered

 

public static final RegistryObject<Item> AK47 = ITEMS.register("ak47", () -> new Ak47(new Item.Properties().group(ModItemGroup.instance).maxDamage(10000)));

 

Ak47 class:

package com.vicken.mod3.items;

import com.vicken.mod3.entities.projectiles.Ak47ArrowEntity;
import com.vicken.mod3.util.RegistryHandler;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.world.World;

import java.util.function.Predicate;

public class Ak47 extends ModBow {
    public Ak47(Properties builder) {
        super(builder);
    }
    @Override
    protected AbstractArrowEntity createArrow(World worldIn, ItemStack ammoStack, PlayerEntity playerentity) {
        return new Ak47ArrowEntity(worldIn, playerentity);
    }

    @Override
    protected double getArrowDamage(ItemStack bowStack, AbstractArrowEntity arrowEntity) {
        int powerLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, bowStack);

        return (double)powerLevel * 100.5D + 100.5D;
    }

    @Override
    public Predicate<ItemStack> getInventoryAmmoPredicate() {
        return (ammoStack) -> {
            return ammoStack.getItem() == RegistryHandler.BULLET.get();
        };
    }
}

modBow class:

package com.vicken.mod3.items;

import com.vicken.mod3.util.RegistryHandler;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.BowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.stats.Stats;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;

import java.util.function.Predicate;

public class ModBow extends BowItem {
    public ModBow(Properties builder) {
        super(builder);
    }

    public void onPlayerStoppedUsing(ItemStack bowStack, World worldIn, LivingEntity entityLiving, int timeLeft) {
        if (entityLiving instanceof PlayerEntity) {
            PlayerEntity playerentity = (PlayerEntity)entityLiving;
            boolean hasInfinity = playerentity.abilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, bowStack) > 0;
            ItemStack ammoStack = playerentity.findAmmo(bowStack);

            int timeDrawn = this.getUseDuration(bowStack) - timeLeft;
            timeDrawn = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(bowStack, worldIn, playerentity, timeDrawn, !ammoStack.isEmpty() || hasInfinity);
            if (timeDrawn < 0) return;

            if (!ammoStack.isEmpty() || hasInfinity) {
                boolean isTippedArrow = ammoStack.getItem() == RegistryHandler.BULLET.get() || ammoStack.getItem() == RegistryHandler.BULLET.get();

                if (ammoStack.isEmpty()) {
                    ammoStack = new ItemStack(RegistryHandler.BULLET.get());
                }

                float velocity = getArrowVelocity(timeDrawn);
                if (!((double)velocity < 100.1D)) {
                    if (!worldIn.isRemote) {
                        AbstractArrowEntity arrowEntity = createArrow(worldIn, ammoStack, playerentity);

                        arrowEntity.shoot(playerentity, playerentity.rotationPitch, playerentity.rotationYaw, 0.0F, velocity * 100.0F, 1.0F);

                        if (velocity == 1.0F) arrowEntity.setIsCritical(true);

                        double damage = getArrowDamage(bowStack, arrowEntity);
                        arrowEntity.setDamage(damage);

                        int knockback = getArrowKnockback(bowStack, arrowEntity);
                        arrowEntity.setKnockbackStrength(knockback);

                        // apply flame enchant
                        if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, bowStack) > 0) {
                            arrowEntity.setFire(100);
                        }

                        // reduce bow durability
                        bowStack.damageItem(1, playerentity, (p_220009_1_) -> {
                            p_220009_1_.sendBreakAnimation(playerentity.getActiveHand());
                        });

                        // set if arrow can be picked up from ground
                        if (hasInfinity && !isTippedArrow) {
                            arrowEntity.pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
                        }

                        // actually make the arrow entity exist in the world
                        worldIn.addEntity(arrowEntity);
                    }

                    // sound
                    worldIn.playSound((PlayerEntity)null, playerentity.getPosX(), playerentity.getPosY(), playerentity.getPosZ(), SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (random.nextFloat() * 0.4F + 1.2F) + velocity * 100.5F);

                    // use an arrow
                    boolean shouldConsumeArrow = !hasInfinity || isTippedArrow;
                    if (shouldConsumeArrow) {
                        ammoStack.shrink(1);
                        if (ammoStack.isEmpty()) {
                            playerentity.inventory.deleteStack(ammoStack);
                        }
                    }

                    playerentity.addStat(Stats.ITEM_USED.get(this));
                }
            }
        }
    }

    // override to use a custom arrow entity
    protected AbstractArrowEntity createArrow(World worldIn, ItemStack ammoStack, PlayerEntity playerentity) {
        ArrowItem arrowitem = (ArrowItem)(ammoStack.getItem() instanceof ArrowItem ? ammoStack.getItem() : RegistryHandler.BULLET.get());
        return arrowitem.createArrow(worldIn, ammoStack, playerentity);
    }

    protected double getArrowDamage(ItemStack bowStack, AbstractArrowEntity arrowEntity) {
        int powerLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, bowStack);

        if (powerLevel > 0) return arrowEntity.getDamage() + (double)powerLevel * 100.5D + 0.5D;
        else return arrowEntity.getDamage();
    }

    protected int getArrowKnockback(ItemStack bowStack, AbstractArrowEntity arrowEntity) {
        return EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, bowStack);
    }

    // Override to change what it uses as ammo
    public Predicate<ItemStack> getInventoryAmmoPredicate() {
        return (ammoStack) -> {
            return ammoStack.getItem().isIn(ItemTags.ARROWS);
        };
    }
    public static float getArrowVelocity(int charge) {
        float f = (float)charge / 100.0F;
        f = (f * f + f * 100.0F) / 100.0F;
        if (f > 100.0F) {
            f = 100.0F;
        }

        return f;
    }

}

but for some reason the bullet is not shooting and also idk how to make it shoot constantly so i don't have to wait for it to load like a real bow and so it can shoot like a real ak47 does

Posted

I think you should look at the vanilla SnowballItem and change it so that it uses ammo instead of itself. In summary, you should use onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn)

Posted

Also, giving the arrow a way too high velocity might cause problems because of engine limitations, so alternatively you could create new bullet entity and change its damage there.

Posted (edited)

It could look like this:

package com.thoriuslight.professionsmod.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.SnowballEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;

public class BOWTEST extends Item{

	public BOWTEST(Properties builder) {
		super(builder);
	}
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
		playerIn.setActiveHand(handIn);
		return ActionResult.resultPass(playerIn.getHeldItem(handIn));
	}
	@Override
	public int getUseDuration(ItemStack stack) {
		return 20;
	}
	@Override
	public void onUsingTick(ItemStack stack, LivingEntity player, int count) {
		if (!(player instanceof PlayerEntity)) {
			return;
		}
		World worldIn = player.world;
		worldIn.playSound((PlayerEntity)null, player.getPosX(), player.getPosY(), player.getPosZ(), SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F));
		if (!worldIn.isRemote) {
			SnowballEntity snowballentity = new SnowballEntity(worldIn, player);
			snowballentity.setItem(new ItemStack(Items.DIAMOND));
			snowballentity.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 1.F, 1.0F);
			worldIn.addEntity(snowballentity);
		}
	}
}

 

After getUseDuration ticks there will be a small pause but it's not that noticeable.

You have to set the active hand to be the item.

Edited by Thorius
Posted

The snowball entity class is actually very good as a template for bullets. You just have to add damage and change its skin. I don't think you have any advantages if you use the arrow class.

Posted (edited)

but if I use the snowball class it will be like im shooting snowballs not bullets. you know mr crayfishes gun mod? i want my gun to be somewhat like that also i figured out how to connect my registered object to the class

Edited by Somonestolemyusername
Posted

You have to create a new bullet entity class but you can use the snowball as a template. In kinematics there's not much difference between a snowball and a bullet. If it looks and hurts like a bullet then it's a bullet. But you have to register it as a new entity.

Posted

all i has in it is 

package com.vicken.mod3.entities.projectiles;

import com.vicken.mod3.util.RegistryHandler;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.WallTorchBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class Ak47ArrowEntity extends ArrowEntity {
    public Ak47ArrowEntity(World worldIn, LivingEntity shooter) {
        super(worldIn, shooter);
        RegistryHandler.BULLET.get();
    }



    @Override
    protected void arrowHit(LivingEntity living) {
        super.arrowHit(living);
        living.setFire(15);
    }

}


Posted

To spawn arrows, something like this would work:

	@Override
	public void onUsingTick(ItemStack stack, LivingEntity player, int count) {
		if (!(player instanceof PlayerEntity)) {
			return;
		}
		PlayerEntity playerEntity = (PlayerEntity)player;
		World worldIn = player.world;
            if (!worldIn.isRemote) {
               AbstractArrowEntity abstractarrowentity = new ArrowEntity(worldIn, playerEntity);
               abstractarrowentity.shoot(playerEntity, playerEntity.rotationPitch, playerEntity.rotationYaw, 0.0F, 10.0F, 1.0F);
               abstractarrowentity.setIsCritical(true);
               abstractarrowentity.setDamage(10.D);
               abstractarrowentity.setKnockbackStrength(1);
               abstractarrowentity.setFire(100);

               stack.damageItem(1, playerEntity, (p_220009_1_) -> {
                  p_220009_1_.sendBreakAnimation(playerEntity.getActiveHand());
               });
               abstractarrowentity.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
               worldIn.addEntity(abstractarrowentity);
            }

You dont even need your own arrow class for this.

 

But creating a custom bullet would also have its benefits and would look better with less unused functionality.

Posted (edited)

I have something like that here is the similar code I wrote 

package com.vicken.mod3.items;

import com.vicken.mod3.util.RegistryHandler;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.BowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.stats.Stats;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;

import java.util.function.Predicate;

public class ModBow extends BowItem {
    public ModBow(Properties builder) {
        super(builder);
    }

    public void onPlayerStoppedUsing(ItemStack bowStack, World worldIn, LivingEntity entityLiving, int timeLeft) {
        if (entityLiving instanceof PlayerEntity) {
            PlayerEntity playerentity = (PlayerEntity)entityLiving;
            boolean hasInfinity = playerentity.abilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, bowStack) > 0;
            ItemStack ammoStack = playerentity.findAmmo(bowStack);

            int timeDrawn = this.getUseDuration(bowStack) - timeLeft;
            timeDrawn = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(bowStack, worldIn, playerentity, timeDrawn, !ammoStack.isEmpty() || hasInfinity);
            if (timeDrawn < 0) return;

            if (!ammoStack.isEmpty() || hasInfinity) {
                boolean isTippedArrow = ammoStack.getItem() == RegistryHandler.BULLET.get() || ammoStack.getItem() == RegistryHandler.BULLET.get();

                if (ammoStack.isEmpty()) {
                    ammoStack = new ItemStack(RegistryHandler.BULLET.get());
                }

                float velocity = getArrowVelocity(timeDrawn);
                if (!((double)velocity < 100.1D)) {
                    if (!worldIn.isRemote) {
                        AbstractArrowEntity arrowEntity = createArrow(worldIn, ammoStack, playerentity);

                        arrowEntity.shoot(playerentity, playerentity.rotationPitch, playerentity.rotationYaw, 0.0F, velocity * 100.0F, 0.0F);

                        if (velocity == 1000.0F) arrowEntity.setIsCritical(true);

                        double damage = getArrowDamage(bowStack, arrowEntity);
                        arrowEntity.setDamage(damage);

                        int knockback = getArrowKnockback(bowStack, arrowEntity);
                        arrowEntity.setKnockbackStrength(knockback);

                        // apply flame enchant
                        if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, bowStack) > 0) {
                            arrowEntity.setFire(100);
                        }

                        // reduce bow durability
                        bowStack.damageItem(1, playerentity, (p_220009_1_) -> {
                            p_220009_1_.sendBreakAnimation(playerentity.getActiveHand());
                        });

                        // set if arrow can be picked up from ground
                        if (hasInfinity && !isTippedArrow) {
                            arrowEntity.pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
                        }

                        // actually make the arrow entity exist in the world
                        worldIn.addEntity(arrowEntity);
                    }

                    // sound
                    worldIn.playSound((PlayerEntity)null, playerentity.getPosX(), playerentity.getPosY(), playerentity.getPosZ(), SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (random.nextFloat() * 0.4F + 1.2F) + velocity * 100.5F);

                    // use an arrow
                    boolean shouldConsumeArrow = !hasInfinity || isTippedArrow;
                    if (shouldConsumeArrow) {
                        ammoStack.shrink(1);
                        if (ammoStack.isEmpty()) {
                            playerentity.inventory.deleteStack(ammoStack);
                        }
                    }

                    playerentity.addStat(Stats.ITEM_USED.get(this));
                }
            }
        }
    }

    // override to use a custom arrow entity
    protected AbstractArrowEntity createArrow(World worldIn, ItemStack ammoStack, PlayerEntity playerentity) {
        ArrowItem arrowitem = (ArrowItem)(ammoStack.getItem() instanceof ArrowItem ? ammoStack.getItem() : RegistryHandler.BULLET.get());
        return arrowitem.createArrow(worldIn, ammoStack, playerentity);
    }

    protected double getArrowDamage(ItemStack bowStack, AbstractArrowEntity arrowEntity) {
        int powerLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, bowStack);

        if (powerLevel > 0) return arrowEntity.getDamage() + (double)powerLevel * 100.5D + 0.5D;
        else return arrowEntity.getDamage();
    }

    protected int getArrowKnockback(ItemStack bowStack, AbstractArrowEntity arrowEntity) {
        return EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, bowStack);
    }

    // Override to change what it uses as ammo
    public Predicate<ItemStack> getInventoryAmmoPredicate() {
        return (ammoStack) -> {
            return ammoStack.getItem() == RegistryHandler.BULLET.get();
        };
    }
    @Override
    public Predicate<ItemStack> getAmmoPredicate() {
        return (ammoStack) -> {
            return ammoStack.getItem() == RegistryHandler.BULLET.get();
        };
    }
    public static float getArrowVelocity(int charge) {
        float f = (float)charge / 100.0F;
        f = (f * f + f * 100.0F) / 100.0F;
        if (f > 100.0F) {
            f = 100.0F;
        }

        return f;
    }


}


but for some reason its not shooting the bullets

Edited by Somonestolemyusername

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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