EffectUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package me.km.effects;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.stream.Collectors;
  5. import me.km.KajetansMod;
  6. import me.km.api.Module;
  7. import me.km.api.Utils;
  8. import me.km.entities.EntityItemProjectile;
  9. import me.km.jobsystem.JobAPI;
  10. import me.km.plots.ProtectionBank;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.item.EntityEnderPearl;
  14. import net.minecraft.entity.item.EntityExpBottle;
  15. import net.minecraft.entity.passive.EntityAnimal;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraft.entity.player.EntityPlayerMP;
  18. import net.minecraft.entity.projectile.*;
  19. import net.minecraft.init.Items;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.potion.Potion;
  22. import net.minecraft.potion.PotionEffect;
  23. import net.minecraft.potion.PotionType;
  24. import net.minecraft.potion.PotionUtils;
  25. import net.minecraft.util.EnumParticleTypes;
  26. import net.minecraft.util.SoundCategory;
  27. import net.minecraft.util.SoundEvent;
  28. import net.minecraft.util.math.BlockPos;
  29. import net.minecraft.util.math.Vec3d;
  30. import net.minecraft.util.text.TextFormatting;
  31. import net.minecraft.world.World;
  32. import net.minecraft.world.WorldServer;
  33. public class EffectUtils extends Module
  34. {
  35. private final HashMap<Integer, EntityData> entityData;
  36. public EffectUtils(String mname, String prefix, TextFormatting color)
  37. {
  38. super(mname, prefix, color);
  39. entityData = new HashMap<>();
  40. }
  41. // -----------------------------------------------------------------------------------
  42. // Entity-Data
  43. // -----------------------------------------------------------------------------------
  44. private class EntityData
  45. {
  46. private final String s;
  47. private final Object data;
  48. public EntityData(String s, Object data)
  49. {
  50. this.s = s;
  51. this.data = data;
  52. }
  53. }
  54. public void addEntityData(Entity ent, String key, Object data)
  55. {
  56. entityData.put(ent.getEntityId(), new EntityData(key, data));
  57. removeInvalidData(ent);
  58. }
  59. public void addEntityData(Entity ent, String key)
  60. {
  61. addEntityData(ent, key, true);
  62. }
  63. private void removeInvalidData(Entity ent)
  64. {
  65. if(ent.isDead)
  66. {
  67. entityData.remove(ent.getEntityId());
  68. return;
  69. }
  70. KajetansMod.scheduler.scheduleTask(() -> removeInvalidData(ent), 100);
  71. }
  72. public Object getEntityData(Entity ent, String key)
  73. {
  74. EntityData data = entityData.get(ent.getEntityId());
  75. if(data == null)
  76. {
  77. return null;
  78. }
  79. if(data.s.equals(key))
  80. {
  81. return data.data;
  82. }
  83. return null;
  84. }
  85. // -----------------------------------------------------------------------------------
  86. // Entity-Level
  87. // -----------------------------------------------------------------------------------
  88. /** Returns the level of a players effect, this is used for passive effects
  89. *
  90. * @param p a player
  91. * @param eff an effect
  92. * @return the level of the effect, 0 on non existance for the player
  93. */
  94. public static int getEffectLevel(EntityPlayer p, Effect eff)
  95. {
  96. JobAPI job = KajetansMod.jobs;
  97. return eff.getSkills().stream().mapToInt(s -> job.getSkillLevel(p, s)).max().orElse(0);
  98. }
  99. // -----------------------------------------------------------------------------------
  100. // Entity-Collections
  101. // -----------------------------------------------------------------------------------
  102. public static Collection<EntityPlayer> getPlayersOfGuild(EntityPlayer p, World w, double x, double y, double z, double radius)
  103. {
  104. Collection<EntityPlayer> col = Utils.getPlayers(w, x, y, z, radius);
  105. if(col.isEmpty())
  106. {
  107. return col;
  108. }
  109. int id = KajetansMod.playerbank.getGuildId(p);
  110. col.removeIf(pl -> KajetansMod.playerbank.getGuildId(pl) != id);
  111. return col;
  112. }
  113. public static Collection<EntityPlayer> getPlayersOfGuild(EntityPlayer p, double radius)
  114. {
  115. return getPlayersOfGuild(p, p.world, p.posX, p.posY, p.posZ, radius);
  116. }
  117. public static Collection<EntityLivingBase> getEntsOfNotGuild(EntityPlayer p, World w, double x, double y, double z, double radius)
  118. {
  119. boolean animals = KajetansMod.plots.getDataBank(ProtectionBank.class).canBuild(w, new BlockPos(x, y, z), p);
  120. boolean pvp = !KajetansMod.worldManager.getWorldPreferences(w).pvpProtection;
  121. boolean ppvp = KajetansMod.playerbank.getPvpStatus(p);
  122. int id = KajetansMod.playerbank.getGuildId(p);
  123. return Utils.getEntities(w, x, y, z, radius, EntityLivingBase.class).stream()
  124. .filter(ent -> animals || !(ent instanceof EntityAnimal))
  125. .filter(ent -> pvp || !(ent instanceof EntityPlayer) || (ppvp &&
  126. KajetansMod.playerbank.getPvpStatus((EntityPlayer) ent)))
  127. .filter(ent -> !(ent instanceof EntityPlayer) ||
  128. (id != KajetansMod.playerbank.getGuildId((EntityPlayer) ent)))
  129. .filter(ent -> ent != p)
  130. .collect(Collectors.toList());
  131. }
  132. public static Collection<EntityLivingBase> getEntsOfNotGuild(EntityPlayer p, double radius)
  133. {
  134. return getEntsOfNotGuild(p, p.world, p.posX, p.posY, p.posZ, radius);
  135. }
  136. // -----------------------------------------------------------------------------------
  137. // Particles
  138. // -----------------------------------------------------------------------------------
  139. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, double offX, double offY, double offZ, int count, int... data)
  140. {
  141. w.spawnParticle(particle, false, x, y, z, count, offX, offY, offZ, 1, data);
  142. }
  143. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, double offX, double offY, double offZ, int count)
  144. {
  145. spawnParticle(w, particle, x, y, z, offX, offY, offZ, count, new int[0]);
  146. }
  147. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, double x, double y, double z, int count)
  148. {
  149. spawnParticle(w, particle, x, y, z, 0, 0, 0, count);
  150. }
  151. public static void spawnParticle(WorldServer w, EnumParticleTypes particle, Entity ent, int count)
  152. {
  153. spawnParticle(w, particle, ent.posX, ent.posY, ent.posZ, count);
  154. }
  155. public static void spawnSpell(EntityPlayerMP p, int level)
  156. {
  157. spawnParticle(p.getServerWorld(), EnumParticleTypes.SPELL, p.posX, p.posY + 1, p.posZ, 0.5f, 0.5f, 0.5f, 10 + level);
  158. }
  159. public static void spawnParticleCircle(WorldServer w, double x, double y, double z, EnumParticleTypes particle, double radius, int counter, int... data)
  160. {
  161. double angle = 2 * Math.PI / counter;
  162. for(int i = 0; i < counter; i++)
  163. {
  164. spawnParticle(w, particle, x + Math.cos(i * angle) * radius, y, z + Math.sin(i * angle) * radius, 0, 0, 0, 1, data);
  165. }
  166. }
  167. public static void spawnParticleCircle(WorldServer w, Entity ent, EnumParticleTypes particle, double radius, int counter, int... data)
  168. {
  169. spawnParticleCircle(w, ent.posX, ent.posY, ent.posZ, particle, radius, counter, data);
  170. }
  171. public static void spawnParticleCircle(WorldServer w, double x, double y, double z, EnumParticleTypes particle, double radius, int counter)
  172. {
  173. spawnParticleCircle(w, x, y, z, particle, radius, counter, new int[0]);
  174. }
  175. public static void spawnParticleCircle(WorldServer w, Entity ent, EnumParticleTypes particle, double radius, int counter)
  176. {
  177. spawnParticleCircle(w, ent.posX, ent.posY, ent.posZ, particle, radius, counter, new int[0]);
  178. }
  179. // -----------------------------------------------------------------------------------
  180. // Sounds
  181. // -----------------------------------------------------------------------------------
  182. public static void playSound(World w, SoundEvent se, SoundCategory sc, double x, double y, double z)
  183. {
  184. w.playSound(null, x, y, z, se, sc, 1, w.rand.nextFloat() * 0.1f + 0.9f);
  185. }
  186. public static void playSound(EntityPlayer p, SoundEvent se, SoundCategory sc)
  187. {
  188. playSound(p.world, se, sc, p.posX, p.posY, p.posZ);
  189. }
  190. public static void playSound(EntityPlayer p, SoundEvent se)
  191. {
  192. playSound(p.world, se, SoundCategory.PLAYERS, p.posX, p.posY, p.posZ);
  193. }
  194. // -----------------------------------------------------------------------------------
  195. // Potions
  196. // -----------------------------------------------------------------------------------
  197. public static void addPotionTo(EntityLivingBase ent, Potion potion, int duration, int amplifier)
  198. {
  199. if(ent.isPotionActive(potion))
  200. {
  201. ent.removePotionEffect(potion);
  202. }
  203. ent.addPotionEffect(new PotionEffect(potion, duration, amplifier));
  204. }
  205. // -----------------------------------------------------------------------------------
  206. // Projectiles
  207. // -----------------------------------------------------------------------------------
  208. public static <T> T launchProjectile(EntityPlayer p, Class<? extends T> projectile, double scale, Object data)
  209. {
  210. World w = p.world;
  211. Entity launch = null;
  212. if(EntityItemProjectile.class == projectile)
  213. {
  214. if(data == null)
  215. {
  216. throw new NullPointerException("Data musn't be null for EntityItemProjectile");
  217. }
  218. ItemStack stack = (ItemStack) data;
  219. if(stack.isEmpty())
  220. {
  221. throw new IllegalArgumentException("Empty ItemStack not allowed here");
  222. }
  223. launch = new EntityItemProjectile(p, stack.copy());
  224. ((EntityItemProjectile) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  225. }
  226. else if(EntitySnowball.class == projectile)
  227. {
  228. launch = new EntitySnowball(w, p);
  229. ((EntitySnowball) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  230. }
  231. else if(EntityEgg.class == projectile)
  232. {
  233. launch = new EntityEgg(w, p);
  234. ((EntityEgg) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  235. }
  236. else if(EntityEnderPearl.class == projectile)
  237. {
  238. launch = new EntityEnderPearl(w, p);
  239. ((EntityEnderPearl) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  240. }
  241. else if(EntityPotion.class == projectile)
  242. {
  243. launch = new EntityPotion(w, p, (ItemStack) data);
  244. ((EntityPotion) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.5f, 1.0f);
  245. }
  246. else if(EntityExpBottle.class == projectile)
  247. {
  248. launch = new EntityExpBottle(w, p);
  249. ((EntityExpBottle) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.7f, 1.0f);
  250. }
  251. else if(EntityArrow.class.isAssignableFrom(projectile))
  252. {
  253. if(EntityTippedArrow.class == projectile)
  254. {
  255. launch = new EntityTippedArrow(w, p);
  256. ((EntityTippedArrow) launch).setPotionEffect((ItemStack) data);
  257. }
  258. else if(EntitySpectralArrow.class == projectile)
  259. {
  260. launch = new EntitySpectralArrow(w, p);
  261. }
  262. else
  263. {
  264. launch = new EntityTippedArrow(w, p);
  265. }
  266. ((EntityArrow) launch).setAim(p, p.rotationPitch, p.rotationYaw, 0.0F, 3.0F, 1.0F);
  267. }
  268. else if(EntityFireball.class.isAssignableFrom(projectile))
  269. {
  270. Vec3d v = p.getLookVec().scale(10);
  271. if (EntitySmallFireball.class == projectile)
  272. {
  273. launch = new EntitySmallFireball(w, p, v.x, v.y, v.z);
  274. }
  275. else if (EntityWitherSkull.class == projectile)
  276. {
  277. launch = new EntityWitherSkull(w, p, v.x, v.y, v.z);
  278. }
  279. else if (EntityDragonFireball.class == projectile)
  280. {
  281. launch = new EntityDragonFireball(w, p, v.x, v.y, v.z);
  282. }
  283. else
  284. {
  285. launch = new EntityLargeFireball(w, p, v.x, v.y, v.z);
  286. }
  287. }
  288. Utils.scaleVelocity(launch, scale);
  289. w.spawnEntity(launch);
  290. return (T) launch;
  291. }
  292. public static EntityTippedArrow launchTippedArrow(EntityPlayer p, double scale, Potion potion, int duration, int amplifier)
  293. {
  294. ItemStack stack = new ItemStack(Items.TIPPED_ARROW);
  295. PotionUtils.addPotionToItemStack(stack, new PotionType(new PotionEffect(potion, duration, amplifier)));
  296. EntityTippedArrow arrow = launchProjectile(p, EntityTippedArrow.class, scale, stack);
  297. return arrow;
  298. }
  299. public static void jumpTo(EntityLivingBase j, EntityLivingBase g)
  300. {
  301. Utils.setVelocity(j, (g.posX - g.posX) * 0.2, (g.posY - g.posY) * 0.2 + 0.9, (g.posZ - g.posZ) * 0.2);
  302. KajetansMod.scheduler.scheduleTask(() ->
  303. {
  304. Utils.setVelocity(j, (g.posX - j.posX) * 0.2, (g.posY - j.posY) * 0.2, (g.posZ - j.posZ) * 0.2);
  305. }, 12);
  306. }
  307. }