EffectUtils.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package me.km.effects;
  2. import me.km.KajetansMod;
  3. import me.km.utils.Utils;
  4. import me.km.entities.EntityItemProjectile;
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.entity.EntityLivingBase;
  7. import net.minecraft.entity.item.EntityEnderPearl;
  8. import net.minecraft.entity.item.EntityExpBottle;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.entity.player.EntityPlayerMP;
  11. import net.minecraft.entity.projectile.*;
  12. import net.minecraft.init.Items;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.potion.Potion;
  15. import net.minecraft.potion.PotionEffect;
  16. import net.minecraft.potion.PotionType;
  17. import net.minecraft.potion.PotionUtils;
  18. import net.minecraft.util.EnumParticleTypes;
  19. import net.minecraft.util.SoundCategory;
  20. import net.minecraft.util.SoundEvent;
  21. import net.minecraft.util.math.Vec3d;
  22. import net.minecraft.world.World;
  23. import net.minecraft.world.WorldServer;
  24. public class EffectUtils
  25. {
  26. // -----------------------------------------------------------------------------------
  27. // Particles
  28. // -----------------------------------------------------------------------------------
  29. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, double offX, double offY, double offZ, int count, int... data)
  30. {
  31. w.spawnParticle(particle, false, x, y, z, count, offX, offY, offZ, 1, data);
  32. }
  33. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, double offX, double offY, double offZ, int count)
  34. {
  35. spawnParticle(w, particle, x, y, z, offX, offY, offZ, count, new int[0]);
  36. }
  37. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, int count)
  38. {
  39. spawnParticle(w, particle, x, y, z, 0, 0, 0, count);
  40. }
  41. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, Entity ent, int count)
  42. {
  43. spawnParticle(w, particle, ent.posX, ent.posY, ent.posZ, count);
  44. }
  45. public static void spawnSpell(EntityPlayerMP p, int level)
  46. {
  47. spawnParticle(p.getServerWorld(), EnumParticleTypes.SPELL, p.posX, p.posY + 1, p.posZ, 0.5f, 0.5f, 0.5f, 10 + level);
  48. }
  49. public static void spawnParticleCircle(WorldServer w, double x, double y, double z, EnumParticleTypes particle, double radius, int counter, int... data)
  50. {
  51. double angle = 2 * Math.PI / counter;
  52. for(int i = 0; i < counter; i++)
  53. {
  54. spawnParticle(w, particle, x + Math.cos(i * angle) * radius, y, z + Math.sin(i * angle) * radius, 0, 0, 0, 1, data);
  55. }
  56. }
  57. public static void spawnParticleCircle(WorldServer w, Entity ent, EnumParticleTypes particle, double radius, int counter, int... data)
  58. {
  59. spawnParticleCircle(w, ent.posX, ent.posY, ent.posZ, particle, radius, counter, data);
  60. }
  61. public static void spawnParticleCircle(WorldServer w, double x, double y, double z, EnumParticleTypes particle, double radius, int counter)
  62. {
  63. spawnParticleCircle(w, x, y, z, particle, radius, counter, new int[0]);
  64. }
  65. public static void spawnParticleCircle(WorldServer w, Entity ent, EnumParticleTypes particle, double radius, int counter)
  66. {
  67. spawnParticleCircle(w, ent.posX, ent.posY, ent.posZ, particle, radius, counter, new int[0]);
  68. }
  69. // -----------------------------------------------------------------------------------
  70. // Sounds
  71. // -----------------------------------------------------------------------------------
  72. public static void playSound(World w, SoundEvent se, SoundCategory sc, double x, double y, double z)
  73. {
  74. w.playSound(null, x, y, z, se, sc, 1, w.rand.nextFloat() * 0.1f + 0.9f);
  75. }
  76. public static void playSound(EntityPlayer p, SoundEvent se, SoundCategory sc)
  77. {
  78. playSound(p.world, se, sc, p.posX, p.posY, p.posZ);
  79. }
  80. public static void playSound(EntityPlayer p, SoundEvent se)
  81. {
  82. playSound(p.world, se, SoundCategory.PLAYERS, p.posX, p.posY, p.posZ);
  83. }
  84. // -----------------------------------------------------------------------------------
  85. // Potions
  86. // -----------------------------------------------------------------------------------
  87. public static void addPotionTo(EntityLivingBase ent, Potion potion, int duration, int amplifier)
  88. {
  89. if(ent.isPotionActive(potion))
  90. {
  91. ent.removePotionEffect(potion);
  92. }
  93. ent.addPotionEffect(new PotionEffect(potion, duration, amplifier));
  94. }
  95. // -----------------------------------------------------------------------------------
  96. // Projectiles
  97. // -----------------------------------------------------------------------------------
  98. public static <T> T launchProjectile(EntityPlayer p, Class<? extends T> projectile, double scale, Object data)
  99. {
  100. World w = p.world;
  101. Entity launch = null;
  102. if(EntityItemProjectile.class == projectile)
  103. {
  104. if(data == null)
  105. {
  106. throw new NullPointerException("Data musn't be null for EntityItemProjectile");
  107. }
  108. ItemStack stack = (ItemStack) data;
  109. if(stack.isEmpty())
  110. {
  111. throw new IllegalArgumentException("Empty ItemStack not allowed here");
  112. }
  113. launch = new EntityItemProjectile(p, stack.copy());
  114. ((EntityItemProjectile) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  115. }
  116. else if(EntitySnowball.class == projectile)
  117. {
  118. launch = new EntitySnowball(w, p);
  119. ((EntitySnowball) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  120. }
  121. else if(EntityEgg.class == projectile)
  122. {
  123. launch = new EntityEgg(w, p);
  124. ((EntityEgg) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  125. }
  126. else if(EntityEnderPearl.class == projectile)
  127. {
  128. launch = new EntityEnderPearl(w, p);
  129. ((EntityEnderPearl) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  130. }
  131. else if(EntityPotion.class == projectile)
  132. {
  133. launch = new EntityPotion(w, p, (ItemStack) data);
  134. ((EntityPotion) launch).shoot(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.5f, 1.0f);
  135. }
  136. else if(EntityExpBottle.class == projectile)
  137. {
  138. launch = new EntityExpBottle(w, p);
  139. ((EntityExpBottle) launch).shoot(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.7f, 1.0f);
  140. }
  141. else if(EntityArrow.class.isAssignableFrom(projectile))
  142. {
  143. if(EntityTippedArrow.class == projectile)
  144. {
  145. launch = new EntityTippedArrow(w, p);
  146. ((EntityTippedArrow) launch).setPotionEffect((ItemStack) data);
  147. }
  148. else if(EntitySpectralArrow.class == projectile)
  149. {
  150. launch = new EntitySpectralArrow(w, p);
  151. }
  152. else
  153. {
  154. launch = new EntityTippedArrow(w, p);
  155. }
  156. ((EntityArrow) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0F, 3.0F, 1.0F);
  157. }
  158. else if(EntityFireball.class.isAssignableFrom(projectile))
  159. {
  160. Vec3d v = p.getLookVec().scale(10);
  161. if (EntitySmallFireball.class == projectile)
  162. {
  163. launch = new EntitySmallFireball(w, p, v.x, v.y, v.z);
  164. }
  165. else if (EntityWitherSkull.class == projectile)
  166. {
  167. launch = new EntityWitherSkull(w, p, v.x, v.y, v.z);
  168. }
  169. else if (EntityDragonFireball.class == projectile)
  170. {
  171. launch = new EntityDragonFireball(w, p, v.x, v.y, v.z);
  172. }
  173. else
  174. {
  175. launch = new EntityLargeFireball(w, p, v.x, v.y, v.z);
  176. }
  177. }
  178. Utils.scaleVelocity(launch, scale);
  179. w.spawnEntity(launch);
  180. return (T) launch;
  181. }
  182. public static EntityTippedArrow launchTippedArrow(EntityPlayer p, double scale, Potion potion, int duration, int amplifier)
  183. {
  184. ItemStack stack = new ItemStack(Items.TIPPED_ARROW);
  185. PotionUtils.addPotionToItemStack(stack, new PotionType(new PotionEffect(potion, duration, amplifier)));
  186. EntityTippedArrow arrow = launchProjectile(p, EntityTippedArrow.class, scale, stack);
  187. return arrow;
  188. }
  189. public static void jumpTo(EntityLivingBase j, EntityLivingBase g)
  190. {
  191. Utils.setVelocity(j, (g.posX - g.posX) * 0.2, (g.posY - g.posY) * 0.2 + 0.9, (g.posZ - g.posZ) * 0.2);
  192. KajetansMod.scheduler.scheduleTask(() ->
  193. {
  194. Utils.setVelocity(j, (g.posX - j.posX) * 0.2, (g.posY - j.posY) * 0.2, (g.posZ - j.posZ) * 0.2);
  195. }, 12);
  196. }
  197. }