EffectUtils.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package me.km.effects;
  2. import java.util.Collection;
  3. import java.util.stream.Collectors;
  4. import me.km.KajetansMod;
  5. import me.km.api.Module;
  6. import me.km.api.Utils;
  7. import net.minecraft.entity.EntityAreaEffectCloud;
  8. import net.minecraft.entity.EntityLiving;
  9. import net.minecraft.entity.EntityLivingBase;
  10. import net.minecraft.entity.passive.EntityAnimal;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.entity.player.EntityPlayerMP;
  13. import net.minecraft.init.MobEffects;
  14. import net.minecraft.init.SoundEvents;
  15. import net.minecraft.network.play.server.SPacketParticles;
  16. import net.minecraft.network.play.server.SPacketSoundEffect;
  17. import net.minecraft.potion.Potion;
  18. import net.minecraft.potion.PotionEffect;
  19. import net.minecraft.potion.PotionType;
  20. import net.minecraft.util.EnumParticleTypes;
  21. import net.minecraft.util.SoundCategory;
  22. import net.minecraft.util.SoundEvent;
  23. import net.minecraft.util.math.BlockPos;
  24. import net.minecraft.util.math.Vec3d;
  25. import net.minecraft.util.text.TextFormatting;
  26. import net.minecraft.world.World;
  27. import net.minecraft.world.WorldServer;
  28. import scala.remote;
  29. public class EffectUtils extends Module
  30. {
  31. public EffectUtils(String mname, String prefix, TextFormatting color)
  32. {
  33. super(mname, prefix, color);
  34. }
  35. /** Returns the level of a players effect
  36. *
  37. * @param p a player
  38. * @param eff an effect
  39. * @return the level of the effect, 0 on non existance for the player
  40. */
  41. public static int getEffectLevel(EntityPlayer p, Effect eff)
  42. {
  43. // TODO
  44. return 0;
  45. /*JobAPI job = KajetansMod.jobs;
  46. return eff.getSkills().stream().mapToInt(s -> job.getSkillLevel(p, s)).max().orElse(0);*/
  47. }
  48. @SuppressWarnings(value = "unchecked")
  49. public static Class<? extends ActiveEffectBase> getEffectClass(String s)
  50. {
  51. Class<? extends ActiveEffectBase> c;
  52. try
  53. {
  54. c = (Class<? extends ActiveEffectBase>) Class.forName("me.km.effects.active." + s);
  55. }
  56. catch(ClassNotFoundException | ClassCastException ex)
  57. {
  58. return null;
  59. }
  60. return c;
  61. }
  62. // -----------------------------------------------------------------------------------
  63. // Entity-Collections
  64. // -----------------------------------------------------------------------------------
  65. public static Collection<EntityPlayer> getPlayersOfGuild(EntityPlayer p, World w, Vec3d v, double radius)
  66. {
  67. Collection<EntityPlayer> col = Utils.getNearbyPlayers(w, v, radius);
  68. if(col.isEmpty())
  69. {
  70. return col;
  71. }
  72. int id = KajetansMod.playerbank.getGuildId(p);
  73. col.removeIf(pl -> KajetansMod.playerbank.getGuildId(pl) != id);
  74. return col;
  75. }
  76. public static Collection<EntityPlayer> getPlayersOfGuild(EntityPlayer p, double radius)
  77. {
  78. return getPlayersOfGuild(p, p.world, p.getPositionVector(), radius);
  79. }
  80. public static Collection<EntityLivingBase> getEntsOfNotGuild(EntityPlayer p, World w, Vec3d v, double radius)
  81. {
  82. // TODO
  83. boolean animals = false; //KajetansMod.plots.getDataBank(ProtectionBank.class).canBuild(l, p);
  84. boolean pvp = !KajetansMod.worldManager.getWorldPreferences(w).pvpProtection;
  85. boolean ppvp = KajetansMod.playerbank.getDataBank().getTag(p, "pvp") >= 1;
  86. int id = KajetansMod.playerbank.getGuildId(p);
  87. return Utils.getNearbyEntities(w, v, radius, EntityLivingBase.class).stream()
  88. .filter(ent -> animals || !(ent instanceof EntityAnimal))
  89. .filter(ent -> pvp || !(ent instanceof EntityPlayer) || (ppvp &&
  90. KajetansMod.playerbank.getDataBank().getTag((EntityPlayer) ent, "pvp") >= 1))
  91. .filter(ent -> !(ent instanceof EntityPlayer) ||
  92. (id != KajetansMod.playerbank.getGuildId((EntityPlayer) ent)))
  93. .filter(ent -> !(ent.equals(p)))
  94. .collect(Collectors.toList());
  95. }
  96. public static Collection<EntityLivingBase> getEntsOfNotGuild(EntityPlayer p, double radius)
  97. {
  98. return getEntsOfNotGuild(p, p.world, p.getPositionVector(), radius);
  99. }
  100. // -----------------------------------------------------------------------------------
  101. // Target Selector
  102. // -----------------------------------------------------------------------------------
  103. public static BlockPos getPlayerTarget(EntityPlayer p, int range)
  104. {
  105. return Utils.getPlayerTarget(p, range + 7).up();
  106. }
  107. // -----------------------------------------------------------------------------------
  108. // Particles
  109. // -----------------------------------------------------------------------------------
  110. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, float x, float y, float z, float offX, float offY, float offZ, float speed, int count, int... data)
  111. {
  112. SPacketParticles packet = new SPacketParticles(particle, false, x, y, z, offX, offY, offZ, speed, count, data);
  113. Utils.getNearbyPlayers(w, x, y, z, 512).forEach(p -> ((EntityPlayerMP) p).connection.sendPacket(packet));
  114. }
  115. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, float x, float y, float z, float offX, float offY, float offZ, int count)
  116. {
  117. spawnParticle(w, particle, x, y, z, offX, offY, offZ, 1, count, new int[0]);
  118. }
  119. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, float x, float y, float z, int count)
  120. {
  121. spawnParticle(w, particle, x, y, z, 0, 0, 0, count);
  122. }
  123. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, Vec3d v, int count)
  124. {
  125. spawnParticle(w, particle, (float) v.xCoord, (float) v.yCoord, (float) v.zCoord, 0, 0, 0, count);
  126. }
  127. public static void playSpell(EntityPlayerMP p, int level)
  128. {
  129. spawnParticle(p.getServerWorld(), EnumParticleTypes.SPELL, (float) p.posX, (float) p.posY + 1, (float) p.posZ, 0.5f, 0.5f, 0.5f, 1, 10 + level, new int[0]);
  130. }
  131. public static <T> void playEffectCircleWithData(WorldServer w, Vec3d v, EnumParticleTypes particle, double radius, int counter)
  132. {
  133. double x = v.xCoord;
  134. float y = (float) v.yCoord;
  135. double z = v.yCoord;
  136. double angle = 2 * Math.PI / counter;
  137. for(int i = 0; i < counter; i++)
  138. {
  139. spawnParticle(w, particle, (float) (x + Math.cos(i * angle) * radius), y, (float) (z + Math.sin(i * angle) * radius), 1);
  140. }
  141. }
  142. // -----------------------------------------------------------------------------------
  143. // Sounds
  144. // -----------------------------------------------------------------------------------
  145. public static void playSound(WorldServer w, SoundEvent se, SoundCategory sc, double x, double y, double z)
  146. {
  147. SPacketSoundEffect packet = new SPacketSoundEffect(se, sc, x, y, z, 1, 1);
  148. Utils.getNearbyPlayers(w, x, y, z, 128).forEach(p -> ((EntityPlayerMP) p).connection.sendPacket(packet));
  149. }
  150. public static void playSound(EntityPlayerMP p, SoundEvent se, SoundCategory sc)
  151. {
  152. playSound(p.getServerWorld(), se, sc, p.posX, p.posY, p.posZ);
  153. }
  154. // -----------------------------------------------------------------------------------
  155. // Potions
  156. // -----------------------------------------------------------------------------------
  157. public static void addPotionTo(EntityLivingBase ent, Potion potion, int duration, int amplifier)
  158. {
  159. if(ent.isPotionActive(potion))
  160. {
  161. ent.removePotionEffect(potion);
  162. }
  163. ent.addPotionEffect(new PotionEffect(potion, duration, amplifier));
  164. }
  165. public static void spawnPotionCloud(Vec3d v, EntityPlayerMP p, PotionType potion, int duration, float radius)
  166. {
  167. EntityAreaEffectCloud cloud = new EntityAreaEffectCloud(p.getServerWorld(), v.xCoord, v.yCoord, v.zCoord);
  168. cloud.setDuration(duration);
  169. cloud.setRadius(radius);
  170. cloud.setOwner(p);
  171. cloud.setWaitTime(5);
  172. cloud.setPotion(potion);
  173. p.getServerWorld().spawnEntity(cloud);
  174. }
  175. public static void spawnPotionCloud(Vec3d v, EntityPlayerMP p, PotionType potion, int level)
  176. {
  177. BlockPos pos = getPlayerTarget(p, level);
  178. spawnPotionCloud(new Vec3d(pos.getX(), pos.getY(), pos.getZ()), p, potion, 40 * level, 0.5f + level / 4f);
  179. playSpell(p, level);
  180. playSound(p, SoundEvents.ENTITY_FIREWORK_BLAST, SoundCategory.PLAYERS);
  181. }
  182. }