Guest User

Untitled

a guest
Jul 16th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.mcreator.supermod.entity;
  2.  
  3. import net.neoforged.api.distmarker.OnlyIn;
  4. import net.neoforged.api.distmarker.Dist;
  5.  
  6. import net.minecraft.world.phys.Vec3;
  7. import net.minecraft.world.level.Level;
  8. import net.minecraft.world.item.enchantment.Enchantments;
  9. import net.minecraft.world.item.enchantment.EnchantmentHelper;
  10. import net.minecraft.world.item.ItemStack;
  11. import net.minecraft.world.entity.projectile.ItemSupplier;
  12. import net.minecraft.world.entity.projectile.AbstractArrow;
  13. import net.minecraft.world.entity.ai.attributes.Attributes;
  14. import net.minecraft.world.entity.LivingEntity;
  15. import net.minecraft.world.entity.EntityType;
  16. import net.minecraft.world.damagesource.DamageSource;
  17. import net.minecraft.util.RandomSource;
  18. import net.minecraft.sounds.SoundSource;
  19. import net.minecraft.resources.ResourceLocation;
  20. import net.minecraft.core.registries.Registries;
  21. import net.minecraft.core.registries.BuiltInRegistries;
  22.  
  23. import net.mcreator.supermod.init.SuperModModItems;
  24. import net.mcreator.supermod.init.SuperModModEntities;
  25.  
  26. import javax.annotation.Nullable;
  27.  
  28. @OnlyIn(value = Dist.CLIENT, _interface = ItemSupplier.class)
  29. public class CustomProjectileEntity extends AbstractArrow implements ItemSupplier {
  30.     public static final ItemStack PROJECTILE_ITEM = new ItemStack(SuperModModItems.CUSTOM_ARROW.get());
  31.     private int knockback = 0;
  32.  
  33.     public CustomProjectileEntity(EntityType<? extends CustomProjectileEntity> type, Level world) {
  34.         super(type, world);
  35.     }
  36.  
  37.     public CustomProjectileEntity(EntityType<? extends CustomProjectileEntity> type, double x, double y, double z, Level world, @Nullable ItemStack firedFromWeapon) {
  38.         super(type, x, y, z, world, PROJECTILE_ITEM, firedFromWeapon);
  39.         if (firedFromWeapon != null)
  40.             setKnockback(EnchantmentHelper.getItemEnchantmentLevel(world.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).getOrThrow(Enchantments.KNOCKBACK), firedFromWeapon));
  41.     }
  42.  
  43.     public CustomProjectileEntity(EntityType<? extends CustomProjectileEntity> type, LivingEntity entity, Level world, @Nullable ItemStack firedFromWeapon) {
  44.         super(type, entity, world, PROJECTILE_ITEM, firedFromWeapon);
  45.         if (firedFromWeapon != null)
  46.             setKnockback(EnchantmentHelper.getItemEnchantmentLevel(world.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).getOrThrow(Enchantments.KNOCKBACK), firedFromWeapon));
  47.     }
  48.  
  49.     @Override
  50.     @OnlyIn(Dist.CLIENT)
  51.     public ItemStack getItem() {
  52.         return PROJECTILE_ITEM;
  53.     }
  54.  
  55.     @Override
  56.     protected ItemStack getDefaultPickupItem() {
  57.         return new ItemStack(SuperModModItems.CUSTOM_ARROW.get());
  58.     }
  59.  
  60.     @Override
  61.     protected void doPostHurtEffects(LivingEntity entity) {
  62.         super.doPostHurtEffects(entity);
  63.         entity.setArrowCount(entity.getArrowCount() - 1);
  64.     }
  65.  
  66.     public void setKnockback(int knockback) {
  67.         this.knockback = knockback;
  68.     }
  69.  
  70.     @Override
  71.     protected void doKnockback(LivingEntity livingEntity, DamageSource damageSource) {
  72.         if (knockback > 0.0) {
  73.             double d1 = Math.max(0.0, 1.0 - livingEntity.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE));
  74.             Vec3 vec3 = this.getDeltaMovement().multiply(1.0, 0.0, 1.0).normalize().scale(knockback * 0.6 * d1);
  75.             if (vec3.lengthSqr() > 0.0) {
  76.                 livingEntity.push(vec3.x, 0.1, vec3.z);
  77.             }
  78.         } else { // knockback might be set by firedFromWeapon passed into constructor
  79.             super.doKnockback(livingEntity, damageSource);
  80.         }
  81.     }
  82.  
  83.     @Override
  84.     public void tick() {
  85.         super.tick();
  86.     }
  87.  
  88.     public static CustomProjectileEntity shoot(Level world, LivingEntity entity, RandomSource source) {
  89.         return shoot(world, entity, source, 2f, 20, 10);
  90.     }
  91.  
  92.     public static CustomProjectileEntity shoot(Level world, LivingEntity entity, RandomSource source, float pullingPower) {
  93.         return shoot(world, entity, source, pullingPower * 2f, 20, 10);
  94.     }
  95.  
  96.     public static CustomProjectileEntity shoot(Level world, LivingEntity entity, RandomSource random, float power, double damage, int knockback) {
  97.         CustomProjectileEntity entityarrow = new CustomProjectileEntity(SuperModModEntities.CUSTOM_PROJECTILE.get(), entity, world, null);
  98.         entityarrow.shoot(entity.getViewVector(1).x, entity.getViewVector(1).y, entity.getViewVector(1).z, power * 2, 0);
  99.         entityarrow.setSilent(true);
  100.         entityarrow.setCritArrow(false);
  101.         entityarrow.setBaseDamage(damage);
  102.         entityarrow.setKnockback(knockback);
  103.         world.addFreshEntity(entityarrow);
  104.         world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.parse("entity.arrow.shoot")), SoundSource.PLAYERS, 1, 1f / (random.nextFloat() * 0.5f + 1) + (power / 2));
  105.         return entityarrow;
  106.     }
  107.  
  108.     public static CustomProjectileEntity shoot(LivingEntity entity, LivingEntity target) {
  109.         CustomProjectileEntity entityarrow = new CustomProjectileEntity(SuperModModEntities.CUSTOM_PROJECTILE.get(), entity, entity.level(), null);
  110.         double dx = target.getX() - entity.getX();
  111.         double dy = target.getY() + target.getEyeHeight() - 1.1;
  112.         double dz = target.getZ() - entity.getZ();
  113.         entityarrow.shoot(dx, dy - entityarrow.getY() + Math.hypot(dx, dz) * 0.2F, dz, 2f * 2, 12.0F);
  114.         entityarrow.setSilent(true);
  115.         entityarrow.setBaseDamage(99999);
  116.         entityarrow.setKnockback(1);
  117.         entityarrow.setCritArrow(true);
  118.         entity.level().addFreshEntity(entityarrow);
  119.         entity.level().playSound(null, entity.getX(), entity.getY(), entity.getZ(), BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.parse("entity.arrow.shoot")), SoundSource.PLAYERS, 1, 1f / (RandomSource.create().nextFloat() * 0.5f + 1));
  120.         return entityarrow;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment