Search in sources :

Example 1 with QAHeadShotEvent

use of me.zombie_striker.qg.api.QAHeadShotEvent in project QualityArmory by ZombieStriker.

the class GunUtil method shootInstantVector.

@SuppressWarnings("deprecation")
public static void shootInstantVector(Gun g, Player p, double sway, double damage, int shots, int range) {
    boolean timingsReport = false;
    long time1 = System.currentTimeMillis();
    long time2 = 0;
    long time3 = 0;
    long time4point5 = 0;
    long time4 = 0;
    for (int i = 0; i < shots; i++) {
        Location start = p.getEyeLocation().clone();
        start.add(p.getVelocity());
        Vector normalizedDirection = p.getLocation().getDirection().normalize();
        normalizedDirection.add(new Vector((Math.random() * 2 * sway) - sway, (Math.random() * 2 * sway) - sway, (Math.random() * 2 * sway) - sway));
        normalizedDirection = normalizedDirection.normalize();
        Vector step = normalizedDirection.clone().multiply(QAMain.bulletStep);
        Entity hitTarget = null;
        AbstractBoundingBox hitBox = null;
        Location bulletHitLoc = null;
        double maxDistance = getTargetedSolidMaxDistance(step, start, range);
        double maxEntityDistance = maxDistance;
        double maxEntityDistanceSquared = maxDistance * maxDistance;
        List<Location> blocksThatWillPLAYBreak = new ArrayList<>();
        List<Block> blocksThatWillBreak = new ArrayList<>();
        Location centerTest = start.clone().add(normalizedDirection.clone().multiply(maxDistance / 2));
        for (Entity e : centerTest.getWorld().getNearbyEntities(centerTest, maxDistance / 2, maxDistance / 2, maxDistance / 2)) {
            if (e instanceof Damageable) {
                if (QAMain.avoidTypes.contains(e.getType()))
                    continue;
                if (e != p && e != p.getVehicle() && e != p.getPassenger()) {
                    double entityDistanceSquared = e.getLocation().distanceSquared(start);
                    if (entityDistanceSquared >= maxEntityDistanceSquared)
                        continue;
                    double entityDistance = e.getLocation().distance(start);
                    AbstractBoundingBox box = BoundingBoxManager.getBoundingBox(e);
                    Location bulletLocationTest = start.clone();
                    // If the entity is close to the line of fire.
                    if (e instanceof Player) {
                        Player player = (Player) e;
                        if (player.getGameMode() == GameMode.SPECTATOR) {
                            continue;
                        }
                        if (QAMain.hasParties && (!QAMain.friendlyFire)) {
                            try {
                                PartiesAPI api = Parties.getApi();
                                PartyPlayer pp1 = api.getPartyPlayer(p.getUniqueId());
                                PartyPlayer pp2 = api.getPartyPlayer(e.getUniqueId());
                                if (pp1.getPartyId() != null && pp1.getPartyId().equals(pp2.getPartyId())) {
                                    Party party = api.getParty(pp1.getPartyId());
                                    if (party != null && party.isFriendlyFireProtected()) {
                                        continue;
                                    }
                                }
                            } catch (Error | Exception e43) {
                            }
                        }
                    }
                    // Clear this to make sure
                    double checkDistanceMax = box.maximumCheckingDistance(e);
                    double startDistance = Math.max(entityDistance - (checkDistanceMax), 0);
                    // Get it somewhere close to the entity
                    if (startDistance > 0)
                        bulletLocationTest.add(normalizedDirection.clone().multiply(startDistance));
                    for (double testDistance = startDistance; testDistance < entityDistance + (checkDistanceMax); testDistance += step.length()) {
                        bulletLocationTest.add(step);
                        if (box.intersects(p, bulletLocationTest, e)) {
                            bulletHitLoc = bulletLocationTest;
                            maxEntityDistance = entityDistance;
                            maxEntityDistanceSquared = entityDistanceSquared;
                            hitTarget = e;
                            hitBox = box;
                            // headShot = box.allowsHeadshots() ? box.intersectsHead(bulletLocationTest, e) : false;
                            break;
                        }
                    }
                }
            }
        }
        time2 = System.currentTimeMillis();
        if (hitTarget != null) {
            if (!(hitTarget instanceof Player) || QualityArmory.allowGunsInRegion(hitTarget.getLocation())) {
                boolean headshot = hitBox.allowsHeadshots() && hitBox.intersectsHead(bulletHitLoc, hitTarget);
                if (headshot) {
                    QAMain.DEBUG("Headshot!");
                    if (QAMain.headshotPling) {
                        try {
                            p.playSound(p.getLocation(), QAMain.headshot_sound, 2, 1);
                            if (!QAMain.isVersionHigherThan(1, 9))
                                try {
                                    p.playSound(p.getLocation(), Sound.valueOf("LAVA_POP"), 6, 1);
                                } catch (Error | Exception h4) {
                                }
                        } catch (Error | Exception h4) {
                            p.playSound(p.getLocation(), Sound.valueOf("LAVA_POP"), 1, 1);
                        }
                    }
                }
                boolean negateHeadshot = false;
                boolean bulletProtection = false;
                if (hitTarget instanceof Player) {
                    bulletProtection = BulletProtectionUtil.stoppedBullet(p, bulletHitLoc, normalizedDirection);
                    if (headshot) {
                        negateHeadshot = BulletProtectionUtil.negatesHeadshot(p);
                    }
                }
                double damageMAX = damage * (bulletProtection ? 0.1 : 1) * ((headshot && !negateHeadshot) ? (QAMain.HeadshotOneHit ? 50 * g.getHeadshotMultiplier() : g.getHeadshotMultiplier()) : 1);
                QAWeaponDamageEntityEvent shootevent = new QAWeaponDamageEntityEvent(p, g, hitTarget, headshot, damage, bulletProtection);
                Bukkit.getPluginManager().callEvent(shootevent);
                if (!shootevent.isCancelled()) {
                    if (headshot) {
                        QAHeadShotEvent headshotevent = new QAHeadShotEvent(hitTarget, p, g);
                        Bukkit.getPluginManager().callEvent(headshotevent);
                        headshot = !headshotevent.isCancelled();
                    }
                    if (hitTarget instanceof Player) {
                        Player player = (Player) hitTarget;
                        if (!QAMain.enableArmorIgnore) {
                            try {
                                // damage = damage * ( 1 - min( 20, max( defensePoints / 5, defensePoints -
                                // damage / ( toughness / 4 + 2 ) ) ) / 25 )
                                double defensePoints = 0;
                                double toughness = 0;
                                for (ItemStack is : new ItemStack[] { player.getInventory().getHelmet(), player.getInventory().getChestplate(), player.getInventory().getLeggings(), player.getInventory().getBoots() }) {
                                    if (is != null) {
                                        Collection<AttributeModifier> attributes = is.getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR);
                                        Collection<AttributeModifier> toughnessAttributes = is.getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR_TOUGHNESS);
                                        if (attributes != null && !attributes.isEmpty())
                                            for (AttributeModifier a : attributes) defensePoints += a.getAmount();
                                        if (toughnessAttributes != null && !toughnessAttributes.isEmpty())
                                            for (AttributeModifier a : toughnessAttributes) toughness += a.getAmount();
                                    }
                                }
                                QAMain.DEBUG("Applied armor protection: " + defensePoints);
                                damageMAX = damageMAX / (1 - Math.min(20, Math.max(defensePoints / 5, defensePoints - damageMAX / (toughness / 4 + 2))) / 25);
                            } catch (Error | Exception e5) {
                                QAMain.DEBUG("An error has occurred: " + e5.getMessage());
                            }
                        }
                        if (!bulletProtection) {
                            BulletWoundHandler.bulletHit((Player) hitTarget, g.getAmmoType() == null ? 1 : g.getAmmoType().getPiercingDamage());
                        } else {
                            hitTarget.sendMessage(QAMain.S_BULLETPROOFSTOPPEDBLEEDING);
                        }
                    }
                    if (hitTarget instanceof LivingEntity) {
                        ((LivingEntity) hitTarget).setNoDamageTicks(0);
                        QAMain.DEBUG("Damaging entity " + hitTarget.getName() + " ( " + ((LivingEntity) hitTarget).getHealth() + "/" + ((LivingEntity) hitTarget).getMaxHealth() + " :" + damageMAX + " DAM)");
                    }
                    if (hitTarget instanceof Damageable) {
                        ((Damageable) hitTarget).damage(damageMAX, p);
                    } else if (hitTarget instanceof EnderDragon) {
                        ((EnderDragon) hitTarget).damage(damageMAX, p);
                    } else if (hitTarget instanceof EnderDragonPart) {
                        ((EnderDragonPart) hitTarget).damage(damageMAX, p);
                    }
                    if (hitTarget.getPassenger() instanceof Damageable) {
                        QAMain.DEBUG("Found a passenger (" + hitTarget.getPassenger().getName() + "). Damaging it.");
                        QAWeaponDamageEntityEvent passengerShoot = new QAWeaponDamageEntityEvent(p, g, hitTarget.getPassenger(), false, damage, bulletProtection);
                        Bukkit.getPluginManager().callEvent(passengerShoot);
                        if (!passengerShoot.isCancelled()) {
                            ((Damageable) hitTarget.getPassenger()).damage(damageMAX, p);
                        }
                    }
                } else {
                    if (hitTarget instanceof LivingEntity) {
                        QAMain.DEBUG("Damaging entity CANCELED " + hitTarget.getName() + " ( " + ((LivingEntity) hitTarget).getHealth() + "/" + ((LivingEntity) hitTarget).getMaxHealth() + " :" + damageMAX + " DAM)");
                    } else {
                        QAMain.DEBUG("Damaging entity CANCELED " + hitTarget.getType() + ".");
                    }
                }
            }
        } else {
            QAMain.DEBUG("No enities hit.");
        }
        time3 = System.currentTimeMillis();
        if (QAMain.enableBulletTrails) {
            List<Player> nonheard = start.getWorld().getPlayers();
            nonheard.remove(p);
            if (g.useMuzzleSmoke())
                ParticleHandlers.spawnMuzzleSmoke(p, start.clone().add(step.clone().multiply(7)));
            double distSqrt = maxEntityDistance;
            Vector stepSmoke = normalizedDirection.clone().multiply(QAMain.smokeSpacing);
            for (double dist = 0; dist < distSqrt; dist += QAMain.smokeSpacing) {
                start.add(stepSmoke);
                if (start.getBlock().getType() != Material.AIR) {
                    boolean solid = isSolid(start.getBlock(), start);
                    QAWeaponDamageBlockEvent blockevent = new QAWeaponDamageBlockEvent(p, g, start.getBlock());
                    Bukkit.getPluginManager().callEvent(blockevent);
                    if (!blockevent.isCancelled()) {
                        if ((solid || isBreakable(start.getBlock(), start)) && !blocksThatWillPLAYBreak.contains(new Location(start.getWorld(), start.getBlockX(), start.getBlockY(), start.getBlockZ()))) {
                            blocksThatWillPLAYBreak.add(new Location(start.getWorld(), start.getBlockX(), start.getBlockY(), start.getBlockZ()));
                        }
                        final Block block = start.getBlock();
                        final Material type = block.getType();
                        if ((QAMain.destructableBlocks.contains(type) || g.getBreakableMaterials().contains(type)) && ProtectionHandler.canBreak(start)) {
                            blocksThatWillBreak.add(block);
                        }
                    }
                    if (!solid) {
                        continue;
                    }
                }
                /*try {
						int control = 3;
						if (dist % control == 0) {
							List<Player> heard = new ArrayList<>();
							for (Player p2 : nonheard) {
								if (p2.getLocation().distanceSquared(start) <= (control * control * 4)) {
									try {
										start.getWorld().playSound(start, Sound.BLOCK_DISPENSER_LAUNCH, 2, 3);
									} catch (Error e) {
										start.getWorld().playSound(start, Sound.valueOf("SHOOT_ARROW"), 2, 2);
									}
									heard.add(p2);
								}
							}
							for (Player p3 : heard) {
								nonheard.remove(p3);
							}
						}

					} catch (Error | Exception e53) {
						if (dist % 30 == 0) {
							try {
								start.getWorld().playSound(start, Sound.BLOCK_DISPENSER_LAUNCH, 2, 2);
								start.getWorld().playSound(start, Sound.BLOCK_FIRE_EXTINGUISH, 2, 2);
							} catch (Error e) {
								start.getWorld().playSound(start, Sound.valueOf("SHOOT_ARROW"), 2, 2);
								start.getWorld().playSound(start, Sound.valueOf("FIRE_IGNITE"), 2, 2);
							}
						}
					}*/
                ParticleHandlers.spawnGunParticles(g, start);
            }
            for (Block l : blocksThatWillBreak) {
                QAMain.DEBUG("Breaking " + l.getX() + " " + l.getY() + " " + l.getZ() + ": " + l.getType());
                QAWeaponDamageBlockEvent event = new QAWeaponDamageBlockEvent(p, g, l);
                Bukkit.getPluginManager().callEvent(event);
                if (!event.isCancelled()) {
                    l.breakNaturally();
                    CoreProtectHook.logBreak(l, p);
                }
            }
        }
        time4point5 = System.currentTimeMillis();
        // TODO: Do lights n stuff
        try {
            if (Bukkit.getPluginManager().getPlugin("LightAPI") != null) {
                if (p.getEyeLocation().getBlock().getLightLevel() < g.getLightOnShoot()) {
                    final Location loc = p.getEyeLocation().clone();
                    LightAPI.get().setLightLevel(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), g.getLightOnShoot());
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            LightAPI.get().setLightLevel(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 0);
                        }
                    }.runTaskLater(QAMain.getInstance(), 3);
                }
            }
        } catch (Error | Exception e5) {
        }
        // Breaking texture
        if (QAMain.blockBreakTexture)
            for (@SuppressWarnings("unused") Location l : blocksThatWillPLAYBreak) {
                start.getWorld().playSound(start, SoundHandler.getSoundWhenShot(start.getBlock()), 2, 1);
                try {
                /*
					 * for (Player p2 : l.getWorld().getPlayers()) {
					 * com.comphenix.protocol.events.PacketContainer packet = new
					 * com.comphenix.protocol.events.PacketContainer(
					 * com.comphenix.protocol.Packets.Server.BLOCK_BREAK_ANIMATION);
					 * packet.getIntegers().write(0, p2.getEntityId());
					 * packet.getBlockPositionModifier().write(1, new
					 * com.comphenix.protocol.wrappers.BlockPosition(l.getBlockX(), l.getBlockY(),
					 * l.getBlockZ())); packet.getBytes().write(2, (byte) 4);
					 * com.comphenix.protocol.ProtocolLibrary.getProtocolManager().sendServerPacket(
					 * p2, packet); }
					 */
                } catch (Error | Exception e4) {
                }
            }
        time4 = System.currentTimeMillis();
    }
    if (timingsReport) {
        System.out.println("time1 = " + time1);
        System.out.println("time2 = " + time2);
        System.out.println("time3 = " + time3);
        System.out.println("time3.5 = " + time4point5);
        System.out.println("time4 = " + time4);
    }
}
Also used : PartyPlayer(com.alessiodp.parties.api.interfaces.PartyPlayer) ArrayList(java.util.ArrayList) Party(com.alessiodp.parties.api.interfaces.Party) Vector(org.bukkit.util.Vector) PartyPlayer(com.alessiodp.parties.api.interfaces.PartyPlayer) QAHeadShotEvent(me.zombie_striker.qg.api.QAHeadShotEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) AttributeModifier(org.bukkit.attribute.AttributeModifier) QAWeaponDamageBlockEvent(me.zombie_striker.qg.api.QAWeaponDamageBlockEvent) Block(org.bukkit.block.Block) QAWeaponDamageEntityEvent(me.zombie_striker.qg.api.QAWeaponDamageEntityEvent) ItemStack(org.bukkit.inventory.ItemStack) PartiesAPI(com.alessiodp.parties.api.interfaces.PartiesAPI) AbstractBoundingBox(me.zombie_striker.qg.boundingbox.AbstractBoundingBox)

Aggregations

PartiesAPI (com.alessiodp.parties.api.interfaces.PartiesAPI)1 Party (com.alessiodp.parties.api.interfaces.Party)1 PartyPlayer (com.alessiodp.parties.api.interfaces.PartyPlayer)1 ArrayList (java.util.ArrayList)1 QAHeadShotEvent (me.zombie_striker.qg.api.QAHeadShotEvent)1 QAWeaponDamageBlockEvent (me.zombie_striker.qg.api.QAWeaponDamageBlockEvent)1 QAWeaponDamageEntityEvent (me.zombie_striker.qg.api.QAWeaponDamageEntityEvent)1 AbstractBoundingBox (me.zombie_striker.qg.boundingbox.AbstractBoundingBox)1 AttributeModifier (org.bukkit.attribute.AttributeModifier)1 Block (org.bukkit.block.Block)1 ItemStack (org.bukkit.inventory.ItemStack)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1 Vector (org.bukkit.util.Vector)1