DERIYS

LeviathanAxeEntity

Apr 6th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.69 KB | None | 0 0
  1. // Because I did not start to write the logic of the entity, I just copied the content of TridentEntity
  2.  
  3.  
  4. public class LeviathanAxeEntity extends AbstractArrowEntity {
  5.     private static final DataParameter<Byte> LOYALTY_LEVEL = EntityDataManager.createKey(LeviathanAxeEntity.class, DataSerializers.BYTE);
  6.     private static final DataParameter<Boolean> field_226571_aq_ = EntityDataManager.createKey(LeviathanAxeEntity.class, DataSerializers.BOOLEAN);
  7.     private ItemStack thrownStack = new ItemStack(ModItems.LEVIATHAN_AXE.get());
  8.     private boolean dealtDamage;
  9.     public int returningTicks;
  10.  
  11.     public LeviathanAxeEntity(EntityType<? extends LeviathanAxeEntity> type, World worldIn) {
  12.         super(type, worldIn);
  13.     }
  14.  
  15.     public LeviathanAxeEntity(World worldIn, LivingEntity thrower, ItemStack thrownStackIn) {
  16.         super(ModEntityTypes.LEVIATHAN_AXE.get(), thrower, worldIn);
  17.         this.thrownStack = thrownStackIn.copy();
  18.         this.dataManager.set(LOYALTY_LEVEL, (byte) EnchantmentHelper.getLoyaltyModifier(thrownStackIn));
  19.         this.dataManager.set(field_226571_aq_, thrownStackIn.hasEffect());
  20.     }
  21.  
  22.     @OnlyIn(Dist.CLIENT)
  23.     public LeviathanAxeEntity(World worldIn, double x, double y, double z) {
  24.         super(ModEntityTypes.LEVIATHAN_AXE.get(), x, y, z, worldIn);
  25.     }
  26.  
  27.     protected void registerData() {
  28.         super.registerData();
  29.         this.dataManager.register(LOYALTY_LEVEL, (byte)0);
  30.         this.dataManager.register(field_226571_aq_, false);
  31.     }
  32.  
  33.     /**
  34.      * Called to update the entity's position/logic.
  35.      */
  36.     public void tick() {
  37.         if (this.timeInGround > 4) {
  38.             this.dealtDamage = true;
  39.         }
  40.  
  41.         Entity entity = this.getShooter();
  42.         if ((this.dealtDamage || this.getNoClip()) && entity != null) {
  43.             int i = this.dataManager.get(LOYALTY_LEVEL);
  44.             System.out.println(i);
  45.             if (i > 0 && !this.shouldReturnToThrower()) {
  46.                 if (!this.world.isRemote && this.pickupStatus == AbstractArrowEntity.PickupStatus.ALLOWED) {
  47.                     this.entityDropItem(this.getArrowStack(), 0.1F);
  48.                 }
  49.  
  50.                 this.remove();
  51.             } else if (i > 0) {
  52.                 this.setNoClip(true);
  53.                 Vector3d vector3d = new Vector3d(entity.getPosX() - this.getPosX(), entity.getPosYEye() - this.getPosY(), entity.getPosZ() - this.getPosZ());
  54.                 this.setRawPosition(this.getPosX(), this.getPosY() + vector3d.y * 0.015D * (double)i, this.getPosZ());
  55.                 if (this.world.isRemote) {
  56.                     this.lastTickPosY = this.getPosY();
  57.                 }
  58.  
  59.                 double d0 = 0.05D * (double)i;
  60.                 this.setMotion(this.getMotion().scale(0.95D).add(vector3d.normalize().scale(d0)));
  61.                 if (this.returningTicks == 0) {
  62.                     this.playSound(SoundEvents.ITEM_TRIDENT_RETURN, 10.0F, 1.0F);
  63.                 }
  64.  
  65.                 ++this.returningTicks;
  66.             }
  67.         }
  68.  
  69.         super.tick();
  70.     }
  71.  
  72.     private boolean shouldReturnToThrower() {
  73.         Entity entity = this.getShooter();
  74.         if (entity != null && entity.isAlive()) {
  75.             return !(entity instanceof ServerPlayerEntity) || !entity.isSpectator();
  76.         } else {
  77.             return false;
  78.         }
  79.     }
  80.  
  81.     protected ItemStack getArrowStack() {
  82.         return this.thrownStack.copy();
  83.     }
  84.  
  85.     @OnlyIn(Dist.CLIENT)
  86.     public boolean func_226572_w_() {
  87.         return this.dataManager.get(field_226571_aq_);
  88.     }
  89.  
  90.     /**
  91.      * Gets the EntityRayTraceResult representing the entity hit
  92.      */
  93.     @Nullable
  94.     protected EntityRayTraceResult rayTraceEntities(Vector3d startVec, Vector3d endVec) {
  95.         return this.dealtDamage ? null : super.rayTraceEntities(startVec, endVec);
  96.     }
  97.  
  98.     /**
  99.      * Called when the arrow hits an entity
  100.      */
  101.     protected void onEntityHit(EntityRayTraceResult result) {
  102.         Entity entity = result.getEntity();
  103.         float f = 8.0F;
  104.         if (entity instanceof LivingEntity) {
  105.             LivingEntity livingentity = (LivingEntity)entity;
  106.             f += EnchantmentHelper.getModifierForCreature(this.thrownStack, livingentity.getCreatureAttribute());
  107.         }
  108.  
  109.         Entity entity1 = this.getShooter();
  110.         DamageSource damagesource = DamageSource.causeTridentDamage(this, (Entity)(entity1 == null ? this : entity1));
  111.         this.dealtDamage = true;
  112.         SoundEvent soundevent = SoundEvents.ITEM_TRIDENT_HIT;
  113.         if (entity.attackEntityFrom(damagesource, f)) {
  114.             if (entity.getType() == EntityType.ENDERMAN) {
  115.                 return;
  116.             }
  117.  
  118.             if (entity instanceof LivingEntity) {
  119.                 LivingEntity livingentity1 = (LivingEntity)entity;
  120.                 if (entity1 instanceof LivingEntity) {
  121.                     EnchantmentHelper.applyThornEnchantments(livingentity1, entity1);
  122.                     EnchantmentHelper.applyArthropodEnchantments((LivingEntity)entity1, livingentity1);
  123.                 }
  124.  
  125.                 this.arrowHit(livingentity1);
  126.             }
  127.         }
  128.  
  129.         this.setMotion(this.getMotion().mul(-0.01D, -0.1D, -0.01D));
  130.         float f1 = 1.0F;
  131.         if (this.world instanceof ServerWorld && this.world.isThundering() && EnchantmentHelper.hasChanneling(this.thrownStack)) {
  132.             BlockPos blockpos = entity.getPosition();
  133.             if (this.world.canSeeSky(blockpos)) {
  134.                 LightningBoltEntity lightningboltentity = EntityType.LIGHTNING_BOLT.create(this.world);
  135.                 lightningboltentity.moveForced(Vector3d.copyCenteredHorizontally(blockpos));
  136.                 lightningboltentity.setCaster(entity1 instanceof ServerPlayerEntity ? (ServerPlayerEntity)entity1 : null);
  137.                 this.world.addEntity(lightningboltentity);
  138.                 soundevent = SoundEvents.ITEM_TRIDENT_THUNDER;
  139.                 f1 = 5.0F;
  140.             }
  141.         }
  142.  
  143.         this.playSound(soundevent, f1, 1.0F);
  144.     }
  145.  
  146.     /**
  147.      * The sound made when an entity is hit by this projectile
  148.      */
  149.     protected SoundEvent getHitEntitySound() {
  150.         return SoundEvents.ITEM_TRIDENT_HIT_GROUND;
  151.     }
  152.  
  153.     /**
  154.      * Called by a player entity when they collide with an entity
  155.      */
  156.     public void onCollideWithPlayer(PlayerEntity entityIn) {
  157.         Entity entity = this.getShooter();
  158.         if (entity == null || entity.getUniqueID() == entityIn.getUniqueID()) {
  159.             super.onCollideWithPlayer(entityIn);
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * (abstract) Protected helper method to read subclass entity data from NBT.
  165.      */
  166.     public void readAdditional(CompoundNBT compound) {
  167.         super.readAdditional(compound);
  168.         if (compound.contains("Trident", 10)) {
  169.             this.thrownStack = ItemStack.read(compound.getCompound("Trident"));
  170.         }
  171.  
  172.         this.dealtDamage = compound.getBoolean("DealtDamage");
  173.         this.dataManager.set(LOYALTY_LEVEL, (byte)EnchantmentHelper.getLoyaltyModifier(this.thrownStack));
  174.     }
  175.  
  176.     public void writeAdditional(CompoundNBT compound) {
  177.         super.writeAdditional(compound);
  178.         compound.put("Trident", this.thrownStack.write(new CompoundNBT()));
  179.         compound.putBoolean("DealtDamage", this.dealtDamage);
  180.     }
  181.  
  182.     public void func_225516_i_() {
  183.         int i = this.dataManager.get(LOYALTY_LEVEL);
  184.         if (this.pickupStatus != AbstractArrowEntity.PickupStatus.ALLOWED || i <= 0) {
  185.             super.func_225516_i_();
  186.         }
  187.  
  188.     }
  189.  
  190.     protected float getWaterDrag() {
  191.         return 0.99F;
  192.     }
  193.  
  194.     @OnlyIn(Dist.CLIENT)
  195.     public boolean isInRangeToRender3d(double x, double y, double z) {
  196.         return true;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment