package me.km.effects; import java.util.Collection; import java.util.stream.Collectors; import me.km.KajetansMod; import me.km.api.Module; import me.km.api.Utils; import net.minecraft.entity.EntityAreaEffectCloud; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.passive.EntityAnimal; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.MobEffects; import net.minecraft.init.SoundEvents; import net.minecraft.network.play.server.SPacketParticles; import net.minecraft.network.play.server.SPacketSoundEffect; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; import net.minecraft.potion.PotionType; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import scala.remote; public class EffectUtils extends Module { public EffectUtils(String mname, String prefix, TextFormatting color) { super(mname, prefix, color); } /** Returns the level of a players effect * * @param p a player * @param eff an effect * @return the level of the effect, 0 on non existance for the player */ public static int getEffectLevel(EntityPlayer p, Effect eff) { // TODO return 0; /*JobAPI job = KajetansMod.jobs; return eff.getSkills().stream().mapToInt(s -> job.getSkillLevel(p, s)).max().orElse(0);*/ } @SuppressWarnings(value = "unchecked") public static Class getEffectClass(String s) { Class c; try { c = (Class) Class.forName("me.km.effects.active." + s); } catch(ClassNotFoundException | ClassCastException ex) { return null; } return c; } // ----------------------------------------------------------------------------------- // Entity-Collections // ----------------------------------------------------------------------------------- public static Collection getPlayersOfGuild(EntityPlayer p, World w, Vec3d v, double radius) { Collection col = Utils.getNearbyPlayers(w, v, radius); if(col.isEmpty()) { return col; } int id = KajetansMod.playerbank.getGuildId(p); col.removeIf(pl -> KajetansMod.playerbank.getGuildId(pl) != id); return col; } public static Collection getPlayersOfGuild(EntityPlayer p, double radius) { return getPlayersOfGuild(p, p.world, p.getPositionVector(), radius); } public static Collection getEntsOfNotGuild(EntityPlayer p, World w, Vec3d v, double radius) { // TODO boolean animals = false; //KajetansMod.plots.getDataBank(ProtectionBank.class).canBuild(l, p); boolean pvp = !KajetansMod.worldManager.getWorldPreferences(w).pvpProtection; boolean ppvp = KajetansMod.playerbank.getDataBank().getTag(p, "pvp") >= 1; int id = KajetansMod.playerbank.getGuildId(p); return Utils.getNearbyEntities(w, v, radius, EntityLivingBase.class).stream() .filter(ent -> animals || !(ent instanceof EntityAnimal)) .filter(ent -> pvp || !(ent instanceof EntityPlayer) || (ppvp && KajetansMod.playerbank.getDataBank().getTag((EntityPlayer) ent, "pvp") >= 1)) .filter(ent -> !(ent instanceof EntityPlayer) || (id != KajetansMod.playerbank.getGuildId((EntityPlayer) ent))) .filter(ent -> !(ent.equals(p))) .collect(Collectors.toList()); } public static Collection getEntsOfNotGuild(EntityPlayer p, double radius) { return getEntsOfNotGuild(p, p.world, p.getPositionVector(), radius); } // ----------------------------------------------------------------------------------- // Target Selector // ----------------------------------------------------------------------------------- public static BlockPos getPlayerTarget(EntityPlayer p, int range) { return Utils.getPlayerTarget(p, range + 7).up(); } // ----------------------------------------------------------------------------------- // Particles // ----------------------------------------------------------------------------------- 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) { SPacketParticles packet = new SPacketParticles(particle, false, x, y, z, offX, offY, offZ, speed, count, data); Utils.getNearbyPlayers(w, x, y, z, 512).forEach(p -> ((EntityPlayerMP) p).connection.sendPacket(packet)); } public static void spawnParticle(WorldServer w, EnumParticleTypes particle, float x, float y, float z, float offX, float offY, float offZ, int count) { spawnParticle(w, particle, x, y, z, offX, offY, offZ, 1, count, new int[0]); } public static void spawnParticle(WorldServer w, EnumParticleTypes particle, float x, float y, float z, int count) { spawnParticle(w, particle, x, y, z, 0, 0, 0, count); } public static void spawnParticle(WorldServer w, EnumParticleTypes particle, Vec3d v, int count) { spawnParticle(w, particle, (float) v.xCoord, (float) v.yCoord, (float) v.zCoord, 0, 0, 0, count); } public static void playSpell(EntityPlayerMP p, int level) { 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]); } public static void playEffectCircleWithData(WorldServer w, Vec3d v, EnumParticleTypes particle, double radius, int counter) { double x = v.xCoord; float y = (float) v.yCoord; double z = v.yCoord; double angle = 2 * Math.PI / counter; for(int i = 0; i < counter; i++) { spawnParticle(w, particle, (float) (x + Math.cos(i * angle) * radius), y, (float) (z + Math.sin(i * angle) * radius), 1); } } // ----------------------------------------------------------------------------------- // Sounds // ----------------------------------------------------------------------------------- public static void playSound(WorldServer w, SoundEvent se, SoundCategory sc, double x, double y, double z) { SPacketSoundEffect packet = new SPacketSoundEffect(se, sc, x, y, z, 1, 1); Utils.getNearbyPlayers(w, x, y, z, 128).forEach(p -> ((EntityPlayerMP) p).connection.sendPacket(packet)); } public static void playSound(EntityPlayerMP p, SoundEvent se, SoundCategory sc) { playSound(p.getServerWorld(), se, sc, p.posX, p.posY, p.posZ); } // ----------------------------------------------------------------------------------- // Potions // ----------------------------------------------------------------------------------- public static void addPotionTo(EntityLivingBase ent, Potion potion, int duration, int amplifier) { if(ent.isPotionActive(potion)) { ent.removePotionEffect(potion); } ent.addPotionEffect(new PotionEffect(potion, duration, amplifier)); } public static void spawnPotionCloud(Vec3d v, EntityPlayerMP p, PotionType potion, int duration, float radius) { EntityAreaEffectCloud cloud = new EntityAreaEffectCloud(p.getServerWorld(), v.xCoord, v.yCoord, v.zCoord); cloud.setDuration(duration); cloud.setRadius(radius); cloud.setOwner(p); cloud.setWaitTime(5); cloud.setPotion(potion); p.getServerWorld().spawnEntity(cloud); } public static void spawnPotionCloud(Vec3d v, EntityPlayerMP p, PotionType potion, int level) { BlockPos pos = getPlayerTarget(p, level); spawnPotionCloud(new Vec3d(pos.getX(), pos.getY(), pos.getZ()), p, potion, 40 * level, 0.5f + level / 4f); playSpell(p, level); playSound(p, SoundEvents.ENTITY_FIREWORK_BLAST, SoundCategory.PLAYERS); } }