Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mcreator.yourmodid.item;
- import net.minecraftforge.registries.ObjectHolder;
- import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
- import net.minecraft.world.World;
- import net.minecraft.util.SoundEvents;
- import net.minecraft.util.SoundCategory;
- import net.minecraft.util.Hand;
- import net.minecraft.util.ActionResultType;
- import net.minecraft.util.ActionResult;
- import net.minecraft.stats.Stats;
- import net.minecraft.item.UseAction;
- import net.minecraft.item.Items;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.ItemGroup;
- import net.minecraft.item.Item;
- import net.minecraft.item.ArrowItem;
- import net.minecraft.entity.projectile.ArrowEntity;
- import net.minecraft.entity.projectile.AbstractArrowEntity;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.LivingEntity;
- import net.minecraft.entity.EntityType;
- import net.minecraft.entity.EntityClassification;
- import net.minecraft.enchantment.Enchantments;
- import net.minecraft.enchantment.EnchantmentHelper;
- import net.mcreator.yourmodid.entity.renderer.YourBowRenderer;
- import net.mcreator.yourmodid.YourModIDModElements;
- @YourModIDModElements.ModElement.Tag
- public class YourBowItem extends YourModIDModElements.ModElement {
- @ObjectHolder("your_mod_id:your_bow")
- public static final Item block = null;
- public static final EntityType arrow = (EntityType.Builder.<ArrowEntity>create(ArrowEntity::new, EntityClassification.MISC).size(0.5f, 0.5f))
- .build("projectile_your_bow").setRegistryName("projectile_your_bow");
- public YourBowItem(YourModIDModElements instance) {
- super(instance, 87);
- FMLJavaModLoadingContext.get().getModEventBus().register(new YourBowRenderer.ModelRegisterHandler());
- }
- @Override
- public void initElements() {
- elements.items.add(() -> new ItemRanged());
- elements.entities.add(() -> arrow);
- }
- public static class ItemRanged extends Item {
- public ItemRanged() {
- super(new Item.Properties().group(ItemGroup.COMBAT).maxDamage("bow durability"));
- setRegistryName("your_bow");
- }
- @Override
- public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity entity, Hand hand) {
- entity.setActiveHand(hand);
- return new ActionResult(ActionResultType.SUCCESS, entity.getHeldItem(hand));
- }
- @Override
- public UseAction getUseAction(ItemStack itemstack) {
- return UseAction.BOW;
- }
- @Override
- public int getUseDuration(ItemStack itemstack) {
- return 72000;
- }
- @Override
- public void onPlayerStoppedUsing(ItemStack stack, World worldIn, LivingEntity entityLiving, int timeLeft) {
- if (entityLiving instanceof PlayerEntity) {
- PlayerEntity playerentity = (PlayerEntity) entityLiving;
- boolean flag = playerentity.abilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
- ItemStack itemstack = playerentity.findAmmo(stack);
- int i = this.getUseDuration(stack) - timeLeft;
- i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, playerentity, i, !itemstack.isEmpty() || flag);
- if (i < 0)
- return;
- if (!itemstack.isEmpty() || flag) {
- if (itemstack.isEmpty()) {
- itemstack = new ItemStack(Items.ARROW);
- }
- float f = getArrowVelocity(i);
- if (!((double) f < 0.1D)) {
- boolean flag1 = playerentity.abilities.isCreativeMode || (itemstack.getItem() instanceof ArrowItem
- && ((ArrowItem) itemstack.getItem()).isInfinite(itemstack, stack, playerentity));
- if (!worldIn.isRemote) {
- ArrowItem arrowitem = (ArrowItem) (itemstack.getItem() instanceof ArrowItem ? itemstack.getItem() : Items.ARROW);
- AbstractArrowEntity abstractarrowentity = arrowitem.createArrow(worldIn, itemstack, playerentity);
- abstractarrowentity = customArrow(abstractarrowentity);
- abstractarrowentity.func_234612_a_(playerentity, playerentity.rotationPitch, playerentity.rotationYaw, 0.0F, f * 3.0F,
- 1.0F);
- if (f == 1.0F) {
- abstractarrowentity.setIsCritical(true);
- }
- int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack);
- if (j > 0) {
- abstractarrowentity.setDamage(abstractarrowentity.getDamage() + (double) j * 0.5D + 0.5D);
- }
- int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack);
- if (k > 0) {
- abstractarrowentity.setKnockbackStrength(k);
- }
- if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, stack) > 0) {
- abstractarrowentity.setFire(100);
- }
- stack.damageItem(1, playerentity, (p_220009_1_) -> {
- p_220009_1_.sendBreakAnimation(playerentity.getActiveHand());
- });
- if (flag1 || playerentity.abilities.isCreativeMode
- && (itemstack.getItem() == Items.SPECTRAL_ARROW || itemstack.getItem() == Items.TIPPED_ARROW)) {
- abstractarrowentity.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
- }
- worldIn.addEntity(abstractarrowentity);
- }
- worldIn.playSound((PlayerEntity) null, playerentity.getPosX(), playerentity.getPosY(), playerentity.getPosZ(),
- SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (random.nextFloat() * 2F + 3F) + 2 * 12.0F);
- if (!flag1 && !playerentity.abilities.isCreativeMode) {
- itemstack.shrink(1);
- if (itemstack.isEmpty()) {
- playerentity.inventory.deleteStack(itemstack);
- }
- }
- playerentity.addStat(Stats.ITEM_USED.get(this));
- }
- }
- }
- }
- /**
- * Gets the velocity of the arrow entity from the bow's charge
- */
- 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;
- }
- /**
- * Get the predicate to match ammunition when searching the player's inventory, not their main/offhand
- */
- public AbstractArrowEntity customArrow(AbstractArrowEntity arrow) {
- return arrow;
- }
- public int func_230305_d_() {
- return 15;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement