sarxJava

SaddledTailSpearEntity.java

Jul 29th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. package net.msrandom.wings.entity.item;
  2.  
  3. import net.minecraft.enchantment.EnchantmentHelper;
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.entity.EntityType;
  6. import net.minecraft.entity.LivingEntity;
  7. import net.minecraft.entity.effect.LightningBoltEntity;
  8. import net.minecraft.entity.player.PlayerEntity;
  9. import net.minecraft.entity.player.ServerPlayerEntity;
  10. import net.minecraft.entity.projectile.AbstractArrowEntity;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.CompoundNBT;
  13. import net.minecraft.network.IPacket;
  14. import net.minecraft.network.datasync.DataParameter;
  15. import net.minecraft.network.datasync.DataSerializers;
  16. import net.minecraft.network.datasync.EntityDataManager;
  17. import net.minecraft.util.DamageSource;
  18. import net.minecraft.util.SoundEvent;
  19. import net.minecraft.util.SoundEvents;
  20. import net.minecraft.util.math.BlockPos;
  21. import net.minecraft.util.math.EntityRayTraceResult;
  22. import net.minecraft.util.math.Vec3d;
  23. import net.minecraft.world.World;
  24. import net.minecraft.world.server.ServerWorld;
  25. import net.minecraftforge.api.distmarker.Dist;
  26. import net.minecraftforge.api.distmarker.OnlyIn;
  27. import net.msrandom.wings.entity.WingsEntities;
  28. import net.msrandom.wings.item.WingsItems;
  29.  
  30. import javax.annotation.Nullable;
  31.  
  32. public class SaddledTailSpearEntity extends AbstractArrowEntity {
  33. private static final DataParameter<Byte> LOYALTY_LEVEL = EntityDataManager.createKey(SaddledTailSpearEntity.class, DataSerializers.BYTE);
  34. private static final DataParameter<Boolean> bool = EntityDataManager.createKey(SaddledTailSpearEntity.class, DataSerializers.BOOLEAN);
  35. private ItemStack thrownStack = new ItemStack(WingsItems.STT_SPEAR);
  36. private boolean dealtDamage;
  37. public int returningTicks;
  38.  
  39. public SaddledTailSpearEntity(EntityType<? extends SaddledTailSpearEntity> type, World worldIn){
  40. super(type, worldIn);
  41. }
  42.  
  43. @Override
  44. public IPacket<?> createSpawnPacket() {
  45. return super.createSpawnPacket();
  46. }
  47.  
  48. public SaddledTailSpearEntity(World worldIn, LivingEntity thrower, ItemStack thrownStackIn){
  49. super(WingsEntities.ST_SPEAR, thrower, worldIn);
  50. this.thrownStack = thrownStackIn.copy();
  51. this.dataManager.set(LOYALTY_LEVEL, (byte) EnchantmentHelper.getLoyaltyModifier(thrownStackIn));
  52. this.dataManager.set(bool,thrownStackIn.hasEffect());
  53. }
  54.  
  55.  
  56.  
  57. @OnlyIn(Dist.CLIENT)
  58. public SaddledTailSpearEntity(World worldIn, double x, double y, double z){
  59. super(WingsEntities.ST_SPEAR, x, y, z, worldIn);
  60. }
  61.  
  62. @Override
  63. protected void registerData() {
  64. super.registerData();
  65. this.dataManager.register(LOYALTY_LEVEL, (byte)0);
  66. this.dataManager.register(bool, false);
  67. }
  68.  
  69. public void tick() {
  70. if (this.timeInGround > 4) {
  71. this.dealtDamage = true;
  72. }
  73.  
  74. Entity entity = this.getShooter();
  75. if ((this.dealtDamage || this.getNoClip()) && entity != null) {
  76. int i = this.dataManager.get(LOYALTY_LEVEL);
  77. if (i > 0 && !this.shouldReturnToThrower()) {
  78. if (!this.world.isRemote && this.pickupStatus == AbstractArrowEntity.PickupStatus.ALLOWED) {
  79. this.entityDropItem(this.getArrowStack(), 0.1F);
  80. }
  81.  
  82. this.remove();
  83. } else if (i > 0) {
  84. this.setNoClip(true);
  85. Vec3d vec3d = new Vec3d(entity.getPosX() - this.getPosX(), entity.getPosYEye() - this.getPosY(), entity.getPosZ() - this.getPosZ());
  86. this.setRawPosition(this.getPosX(), this.getPosY() + vec3d.y * 0.015D * (double)i, this.getPosZ());
  87. if (this.world.isRemote) {
  88. this.lastTickPosY = this.getPosY();
  89. }
  90.  
  91. double d0 = 0.05D * (double)i;
  92. this.setMotion(this.getMotion().scale(0.95D).add(vec3d.normalize().scale(d0)));
  93. if (this.returningTicks == 0) {
  94. this.playSound(SoundEvents.ITEM_TRIDENT_RETURN, 10.0F, 1.0F);
  95. }
  96.  
  97. ++this.returningTicks;
  98. }
  99. }
  100.  
  101. super.tick();
  102. }
  103.  
  104. private boolean shouldReturnToThrower(){
  105. Entity entity = this.getShooter();
  106. if(entity != null && entity.isAlive()){
  107. return !(entity instanceof ServerPlayerEntity)|| !entity.isSpectator();
  108. } else{
  109. return false;
  110. }
  111. }
  112.  
  113. @Override
  114. protected ItemStack getArrowStack() {
  115. return this.thrownStack.copy();
  116. }
  117.  
  118. @OnlyIn(Dist.CLIENT)
  119. public boolean isDealtDamage (){
  120. return this.dataManager.get(bool);
  121. }
  122.  
  123. @Nullable
  124. @Override
  125. protected EntityRayTraceResult rayTraceEntities(Vec3d startVec, Vec3d endVec) {
  126. return this.dealtDamage ? null : super.rayTraceEntities(startVec, endVec);
  127. }
  128.  
  129. protected void onEntityHit(EntityRayTraceResult result) {
  130. Entity entity = result.getEntity();
  131. float f = 8.0F;
  132. if (entity instanceof LivingEntity) {
  133. LivingEntity livingentity = (LivingEntity)entity;
  134. f += EnchantmentHelper.getModifierForCreature(this.thrownStack, livingentity.getCreatureAttribute());
  135. }
  136.  
  137. Entity entity1 = this.getShooter();
  138. DamageSource damagesource = DamageSource.causeTridentDamage(this, (Entity)(entity1 == null ? this : entity1));
  139. this.dealtDamage = true;
  140. SoundEvent soundevent = SoundEvents.ITEM_TRIDENT_HIT;
  141. if (entity.attackEntityFrom(damagesource, f)) {
  142. if (entity.getType() == EntityType.ENDERMAN) {
  143. return;
  144. }
  145.  
  146. if (entity instanceof LivingEntity) {
  147. LivingEntity livingentity1 = (LivingEntity)entity;
  148. if (entity1 instanceof LivingEntity) {
  149. EnchantmentHelper.applyThornEnchantments(livingentity1, entity1);
  150. EnchantmentHelper.applyArthropodEnchantments((LivingEntity)entity1, livingentity1);
  151. }
  152.  
  153. this.arrowHit(livingentity1);
  154. }
  155. }
  156.  
  157. this.setMotion(this.getMotion().mul(-0.01D, -0.1D, -0.01D));
  158. float f1 = 1.0F;
  159. if (this.world instanceof ServerWorld && this.world.isThundering() && EnchantmentHelper.hasChanneling(this.thrownStack)) {
  160. BlockPos blockpos = entity.getPosition();
  161. if (this.world.canSeeSky(blockpos)) {
  162. LightningBoltEntity lightningboltentity = new LightningBoltEntity(this.world, (double)blockpos.getX() + 0.5D, (double)blockpos.getY(), (double)blockpos.getZ() + 0.5D, false);
  163. lightningboltentity.setCaster(entity1 instanceof ServerPlayerEntity ? (ServerPlayerEntity)entity1 : null);
  164. ((ServerWorld)this.world).addLightningBolt(lightningboltentity);
  165. soundevent = SoundEvents.ITEM_TRIDENT_THUNDER;
  166. f1 = 5.0F;
  167. }
  168. }
  169.  
  170. this.playSound(soundevent, f1, 1.0F);
  171. }
  172.  
  173. @Override
  174. protected SoundEvent getHitEntitySound() {
  175. return SoundEvents.ITEM_TRIDENT_HIT_GROUND;
  176. }
  177.  
  178. public void onCollideWithPlayer(PlayerEntity entityIn) {
  179. Entity entity = this.getShooter();
  180. if (entity == null || entity.getUniqueID() == entityIn.getUniqueID()) {
  181. super.onCollideWithPlayer(entityIn);
  182. }
  183. }
  184.  
  185. public void readAdditional(CompoundNBT compound) {
  186. super.readAdditional(compound);
  187. if (compound.contains("Spear", 10)) {
  188. this.thrownStack = ItemStack.read(compound.getCompound("Spear"));
  189. }
  190.  
  191. this.dealtDamage = compound.getBoolean("DealtDamage");
  192. this.dataManager.set(LOYALTY_LEVEL, (byte)EnchantmentHelper.getLoyaltyModifier(this.thrownStack));
  193. }
  194.  
  195. public void writeAdditional(CompoundNBT compound) {
  196. super.writeAdditional(compound);
  197. compound.put("Spear", this.thrownStack.write(new CompoundNBT()));
  198. compound.putBoolean("DealtDamage", this.dealtDamage);
  199. }
  200.  
  201. public void func_225516_i_() {
  202. int i = this.dataManager.get(LOYALTY_LEVEL);
  203. if (this.pickupStatus != AbstractArrowEntity.PickupStatus.ALLOWED || i <= 0) {
  204. super.func_225516_i_();
  205. }
  206.  
  207. }
  208.  
  209. protected float getWaterDrag(){
  210. return 0.99F;
  211. }
  212.  
  213. @OnlyIn(Dist.CLIENT)
  214. public boolean isInRangeToRender3d(double x, double y, double z) {
  215. return true;
  216. }
  217.  
  218. }
  219.  
Add Comment
Please, Sign In to add comment