ICanDoodle

Untitled

Jun 16th, 2023
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. package entity;
  2.  
  3. import item.moditems;
  4. import net.minecraft.nbt.CompoundTag;
  5. import net.minecraft.network.syncher.EntityDataAccessor;
  6. import net.minecraft.network.syncher.EntityDataSerializers;
  7. import net.minecraft.network.syncher.SynchedEntityData;
  8. import net.minecraft.server.level.ServerPlayer;
  9. import net.minecraft.sounds.SoundEvent;
  10. import net.minecraft.sounds.SoundEvents;
  11. import net.minecraft.world.damagesource.DamageSource;
  12. import net.minecraft.world.entity.Entity;
  13. import net.minecraft.world.entity.EntityType;
  14. import net.minecraft.world.entity.LivingEntity;
  15. import net.minecraft.world.entity.player.Player;
  16. import net.minecraft.world.entity.projectile.AbstractArrow;
  17. import net.minecraft.world.item.ItemStack;
  18. import net.minecraft.world.item.enchantment.EnchantmentHelper;
  19. import net.minecraft.world.level.Level;
  20. import net.minecraft.world.phys.EntityHitResult;
  21. import net.minecraft.world.phys.Vec3;
  22.  
  23. import javax.annotation.Nullable;
  24.  
  25. public class ThrownNetheriteSpear extends AbstractArrow {
  26.     private static final EntityDataAccessor<Byte> ID_LOYALTY = SynchedEntityData.defineId(ThrownNetheriteSpear.class, EntityDataSerializers.BYTE);
  27.     private static final EntityDataAccessor<Boolean> ID_FOIL = SynchedEntityData.defineId(ThrownNetheriteSpear.class, EntityDataSerializers.BOOLEAN);
  28.  
  29.     private ItemStack NetheriteSpearItem = new ItemStack(moditems.NETHERITE_SPEAR.get());
  30.  
  31.     private boolean dealtDamage;
  32.  
  33.     public int clientSideReturnNetheriteSpearTickCount;
  34.  
  35.     public ThrownNetheriteSpear(EntityType<? extends ThrownNetheriteSpear> type, Level level) {
  36.         super(type, level);
  37.     }
  38.  
  39.     public ThrownNetheriteSpear(EntityType<? extends AbstractArrow> type, Level world, LivingEntity entity, ItemStack stack) {
  40.         super(type, entity, world);
  41.         this.NetheriteSpearItem = stack.copy();
  42.         this.entityData.set(ID_LOYALTY, (byte)EnchantmentHelper.getLoyalty(stack));
  43.         this.entityData.set(ID_FOIL, stack.hasFoil());
  44.     }
  45.  
  46.     public void tick() {
  47.         if (this.inGroundTime > 4) {
  48.             this.dealtDamage = true;
  49.         }
  50.  
  51.         Entity entity = this.getOwner();
  52.         int i = this.entityData.get(ID_LOYALTY);
  53.         if (i > 0 && (this.dealtDamage || this.isNoPhysics()) && entity != null) {
  54.             if (!this.isAcceptibleReturnOwner()) {
  55.                 if (!this.level.isClientSide && this.pickup == AbstractArrow.Pickup.ALLOWED) {
  56.                     this.spawnAtLocation(this.getPickupItem(), 0.1F);
  57.                 }
  58.  
  59.                 this.discard();
  60.             } else {
  61.                 this.setNoPhysics(true);
  62.                 Vec3 vec3 = entity.getEyePosition().subtract(this.position());
  63.                 this.setPosRaw(this.getX(), this.getY() + vec3.y * 0.015D * (double)i, this.getZ());
  64.                 if (this.level.isClientSide) {
  65.                     this.yOld = this.getY();
  66.                 }
  67.  
  68.                 double d0 = 0.05D * (double)i;
  69.                 this.setDeltaMovement(this.getDeltaMovement().scale(0.95D).add(vec3.normalize().scale(d0)));
  70.                 if (this.clientSideReturnNetheriteSpearTickCount == 0) {
  71.                     this.playSound(SoundEvents.TRIDENT_RETURN, 10.0F, 1.0F);
  72.                 }
  73.  
  74.                 ++this.clientSideReturnNetheriteSpearTickCount;
  75.             }
  76.         }
  77.  
  78.         super.tick();
  79.     }
  80.  
  81.     private boolean isAcceptibleReturnOwner() {
  82.         Entity entity = this.getOwner();
  83.         if (entity != null && entity.isAlive()) {
  84.             return !(entity instanceof ServerPlayer) || !entity.isSpectator();
  85.         } else {
  86.             return false;
  87.         }
  88.     }
  89.  
  90.     public boolean isFoil() {
  91.         return this.entityData.get(ID_FOIL);
  92.     }
  93.  
  94.  
  95.     @Nullable
  96.     protected EntityHitResult findHitEntity(Vec3 p_37575_, Vec3 p_37576_) {
  97.         return this.dealtDamage ? null : super.findHitEntity(p_37575_, p_37576_);
  98.     }
  99.  
  100.     protected void onHitEntity(EntityHitResult p_37573_) {
  101.         Entity entity = p_37573_.getEntity();
  102.         float f = 8.0F;
  103.         if (entity instanceof LivingEntity livingentity) {
  104.             f += EnchantmentHelper.getDamageBonus(this.NetheriteSpearItem, livingentity.getMobType());
  105.         }
  106.  
  107.         Entity entity1 = this.getOwner();
  108.         DamageSource damagesource = this.damageSources().trident(this, (Entity)(entity1 == null ? this : entity1));
  109.         this.dealtDamage = true;
  110.         SoundEvent soundevent = SoundEvents.TRIDENT_HIT;
  111.         if (entity.hurt(damagesource, f)) {
  112.             if (entity.getType() == EntityType.ENDERMAN) {
  113.                 return;
  114.             }
  115.  
  116.             if (entity instanceof LivingEntity) {
  117.                 LivingEntity livingentity1 = (LivingEntity)entity;
  118.                 if (entity1 instanceof LivingEntity) {
  119.                     EnchantmentHelper.doPostHurtEffects(livingentity1, entity1);
  120.                     EnchantmentHelper.doPostDamageEffects((LivingEntity)entity1, livingentity1);
  121.                 }
  122.  
  123.                 this.doPostHurtEffects(livingentity1);
  124.             }
  125.         }
  126.         this.setDeltaMovement(this.getDeltaMovement().multiply(-0.01D, -0.1D, -0.01D));
  127.         this.playSound(soundevent, 1.0F,  1.0F);
  128.     }
  129.  
  130.     protected boolean tryPickup(Player player) {
  131.         return super.tryPickup(player) || this.isNoPhysics() && this.ownedBy(player) && player.getInventory().add(this.getPickupItem());
  132.     }
  133.  
  134.  
  135.     protected void defineSynchedData() {
  136.         super.defineSynchedData();
  137.         this.entityData.define(ID_LOYALTY, (byte)0);
  138.         this.entityData.define(ID_FOIL, false);
  139.     }
  140.  
  141.  
  142.  
  143.     protected ItemStack getPickupItem() {
  144.         return this.NetheriteSpearItem.copy();
  145.     }
  146.  
  147.  
  148.  
  149.     protected SoundEvent getDefaultHitGroundSoundEvent() {
  150.         return SoundEvents.TRIDENT_HIT_GROUND;
  151.     }
  152.  
  153.     public void playerTouch(Player p_37580_) {
  154.         if (this.ownedBy(p_37580_) || this.getOwner() == null) {
  155.             super.playerTouch(p_37580_);
  156.         }
  157.  
  158.     }
  159.  
  160.     public void readAdditionalSaveData(CompoundTag p_37578_) {
  161.         super.readAdditionalSaveData(p_37578_);
  162.         if (p_37578_.contains("Netherite Spear", 10)) {
  163.             this.NetheriteSpearItem = ItemStack.of(p_37578_.getCompound("Netherite Spear"));
  164.         }
  165.  
  166.         this.dealtDamage = p_37578_.getBoolean("DealtDamage");
  167.         this.entityData.set(ID_LOYALTY, (byte)EnchantmentHelper.getLoyalty(this.NetheriteSpearItem));
  168.     }
  169.  
  170.     public void addAdditionalSaveData(CompoundTag p_37582_) {
  171.         super.addAdditionalSaveData(p_37582_);
  172.         p_37582_.put("Netherite Spear", this.NetheriteSpearItem.save(new CompoundTag()));
  173.         p_37582_.putBoolean("DealtDamage", this.dealtDamage);
  174.     }
  175.  
  176.     protected float getWaterInertia() {
  177.         return 0.99F;
  178.     }
  179.  
  180.  
  181.     public boolean shouldRender(double x, double y, double z) {
  182.         return true;
  183.     }
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment