How to kill a player and call playerdeathevent?

Discussion in 'Plugin Development' started by AGuyWhoSkis, Jul 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    AGuyWhoSkis

    Hey guys, so I've done some experimenting with code, as well as a fair share of googling, and nothing helpful has turned up.

    What I need to do is kill a player, and call a playerdeathevent. This is my code right now (No attempt at the death event because I'm not sure how to do it):
    Code:java
    1. double healthLeft = ((Damageable) p).getHealth();
    2. hit.damage(healthLeft, p); //p is the killer (a Player), and hit is the player who is being killed


    This is (I think) causing a very weird glitch... It sets your health to 0, but you don't die. Instead, you are stuck in an invincible state (with no hearts), and every 1/10 of a second (roughly), the affected player will flinch, as if they were taking damage. It's not visible to other players. It also doesn't happen consistently, which is why I'm not sure this is the cause of it.

    Any help would be greatly appreciated.
     
  2. Offline

    fireblast709

    AGuyWhoSkis full code please. If A kills B, then B is killed by A in the death event of A. This would trigger a death event for B, where it should be killing A because he killed B.
     
  3. Offline

    AGuyWhoSkis

    Here is the whole event:
    Code:
    @EventHandler
        public void onShoot(PlayerInteractEvent e) {
     
            if (!e.getAction().equals(Action.RIGHT_CLICK_AIR) && !e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
     
            Player p = e.getPlayer();
            if (p.getItemInHand() == null) return;
            if (p.getItemInHand().getType() == null) return;
     
            if (p.getItemInHand().equals(Inv.gun)) {
                PlayerInventory inventory = p.getInventory();
     
                boolean containsBullet = false;
     
                for (ItemStack i : inventory.getContents()) {
                    if (i == null || i.getType() == null) {
                        continue;
                    }
     
                    if (i.getType().equals(Inv.bullet.getType())) {
                        containsBullet = true;
                 
                        if (i.getAmount() == 1) {
                            inventory.remove(i);
                        } else {
                            i.setAmount(i.getAmount() -1);
                        }
                 
                        p.updateInventory();
                        break;
                    }
                }
     
                if (!containsBullet) {
                    //Volume of 0.66F means it can be heard a max of 10 blocks away
                    p.getWorld().playSound(p.getLocation(), Sound.CLICK, 0.66F, 2F);
                    return;
                }
     
                Location observerPos = p.getEyeLocation();
                Vector3D observerDir = new Vector3D(observerPos.getDirection());
     
                Vector3D observerStart = new Vector3D(observerPos);
                Vector3D observerEnd = observerStart.add(observerDir.multiply(ATTACK_REACH));
     
                //Check if player has line of sight to any entity
                LivingEntity hit = null;
         
                for (LivingEntity target : p.getWorld().getLivingEntities()) {
                    // Bounding box of the shooter
                    Vector3D targetPos = new Vector3D(target.getLocation());
                    Vector3D minimum = targetPos.add(-0.5, 0, -0.5);
                    Vector3D maximum = targetPos.add(0.5, 1.67, 0.5);
     
                    if (target != p && hasIntersection(observerStart, observerEnd, minimum, maximum)) {
                 
                        BlockIterator bit = new BlockIterator(p, 200);
                        Block firstSolid = null;
                        while(bit.hasNext()) {
                            Block next = bit.next();
                     
                            if (!a.contains(next.getType())) {
                            //'a' is an arraylist which has a list of all nonsolid blocks
                                firstSolid = next;
                                break;
                         
                            } else continue;
                        }
                 
                        if (hit == null || hit.getLocation().distanceSquared(observerPos) > target.getLocation().distanceSquared(observerPos)) {
                            if (firstSolid != null) {
                                if (p.getLocation().distance(target.getLocation()) > p.getLocation().distance(firstSolid.getLocation())) {
                                    firstSolid.getWorld().playEffect(firstSolid.getLocation(), Effect.STEP_SOUND, Material.STONE.getId(), 2);
                                    break;
                                }
                            }
                            hit = target;
                        }
                    }
                }
     
                // Hit the closest player
                if (hit != null) {
                    //Death event should be triggered here
                    double healthLeft = ((Damageable) p).getHealth();
                    Utils.logInfo(""+healthLeft);
                    hit.damage(healthLeft, p);
             
                    Float pitch = (float) Utils.getRandDouble(1.4, 1.7, 3);
                    p.getWorld().playSound(hit.getLocation(), Sound.HURT_FLESH, 0.66F, pitch);
                }
         
                Location particleLocation = p.getEyeLocation().clone();
     
                org.bukkit.util.Vector vec = p.getLocation().getDirection();
                particleLocation.setX(particleLocation.getX() + vec.getX());
                particleLocation.setY(particleLocation.getY() + vec.getY());
                particleLocation.setZ(particleLocation.getZ() + vec.getZ());
     
                p.getWorld().playEffect(particleLocation, Effect.STEP_SOUND, Material.STONE.getId(), 2);
         
                Float pitch = (float) Utils.getRandDouble(0.7, 1.5, 2);
                p.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST, 6.66F, pitch);
     
            }
        }
     
        @EventHandler
        public void onPVP(EntityDamageByEntityEvent e) {
     
            Entity damagerEnt = e.getDamager();
     
            Entity damagedEnt = e.getEntity();
     
            if (damagerEnt instanceof Player && damagedEnt instanceof Player) {
     
                Player damager = (Player) damagerEnt;
                Player damaged = (Player) damagedEnt;
     
                Game g = GameManager.getGame(damaged.getName());
     
                if (g == null) {
                    e.setCancelled(true);
                    return;
                }
     
                if (!g.isPvpEnabled()) {
                    e.setCancelled(true);
                    return;
                }
     
                if (g.equals(GameManager.getGame(damaged.getName()))) {
                    //In the same game
                    if (g.getTeam(damager.getName()) == g.getTeam(damaged.getName())) {
                        //On the same team
                        damager.sendMessage(RED + "Don't hurt your teammates!");
                        e.setCancelled(true);
                    }
                }
            }
        }
     
  4. Offline

    teej107

    You can modify the damage in the damage event. Just have damage dealt equal the the player's health. That will kill the player, calling the death event
     
  5. Offline

    fireblast709

    AGuyWhoSkis ok to start of with a very simple solution, try setting the health to 0.
     
  6. Offline

    AGuyWhoSkis

    Normally this would work, but there needs to be a killer, specifically the person who shot the gun.
     
  7. Offline

    teej107

  8. Offline

    fireblast709

    teej107 he does that already
    AGuyWhoSkis try damaging with an absurd amount (like Short.MAX_VALUE)
     
  9. Offline

    AGuyWhoSkis

    I've tried Double.MAX_VALUE, it didn't fix it
     
  10. Offline

    fireblast709

    AGuyWhoSkis my next bet is that you heal them somewhere else during the same tick. It causes that the entity is flagged dead, but the health to be > 0.0. That way their death will never be fully processed iirc.
     
    AGuyWhoSkis likes this.
  11. Offline

    rippin

    EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(final Entity damager, final Entity damagee, final DamageCause cause, final double damage);
    Bukkit.getServer().getPluginManager().callEvent(e);
    Put that wherever you do damage to a player by another player.
     
  12. Offline

    AGuyWhoSkis

    You were right. I had a duplicate interactevent listener which was messing with it. Thanks :)

    Edit: Ok, apparantly not a 100% fix. This does make the playerdeathevent work, though. I still have the damage glitch unfortunately.

    Edit2: So I changed the 'force-gamemode' setting to false. That fixed it for some reason. Weird.
     
Thread Status:
Not open for further replies.

Share This Page