onDamage event and player.getHealth() delay?

Discussion in 'Plugin Development' started by blah900, May 3, 2011.

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

    blah900

    So I have 2 spells on my plugin.

    1 is shell which absorbs entity_explosion, fire, fire_tick, block_explosion dmg.

    The other is protect which absorbs entity_damage

    So currently for both spells I have something like this
    Code:
    if (e.getCause() == DamageCause.ENTITY_ATTACK) {
                for (Player p : plist) {
                    if (player.getEntityId() == p.getEntityId()) {
                        k = p.getName() + "protectSpell";
                        if (plugin.Grimoire.containsKey(k) && plugin.Grimoire.get(k)) {
                            int dmg = e.getDamage();
                            int shield = plugin.Protect.get(p);
                            if (shield>dmg){ //Shield is greater than dmg. Restore all hp
                                plugin.Protect.put(p, shield-dmg);
                                p.sendMessage("Protect(Half Hearts): "+(shield-dmg));
                                p.setHealth(p.getHealth()+dmg);
                            } else{ //Shield is equal to or lesser than dmg. Restore only shield amount of hp.
                            plugin.setGrimoire(k, false);
                            plugin.Protect.put(p, 0);
                            p.sendMessage("You no longer feel protected");
                            p.setHealth(ceiling(p.getHealth()+shield,20));
                            }
                        }
                    }
                }
            }
    
            else if(e.getCause() == DamageCause.BLOCK_EXPLOSION || e.getCause() == DamageCause.ENTITY_EXPLOSION || e.getCause() == DamageCause.FIRE || e.getCause() == DamageCause.LAVA || e.getCause() == DamageCause.FIRE_TICK) {
                for (Player p : plist) {
                    if (player.getEntityId() == p.getEntityId()) {
                        k = p.getName() + "shellSpell";
                        if (plugin.Grimoire.containsKey(k) && plugin.Grimoire.get(k)) {
                            int dmg = e.getDamage();
                            int shield = plugin.Shell.get(p);
                            if (shield>dmg){ //Shield is greater than dmg. Restore all hp
                                plugin.Shell.put(p, shield-dmg);
                                p.sendMessage("Shell(Half Hearts): "+(shield-dmg));
                                p.setHealth(p.getHealth()+dmg);
                            } else{ //Shield is equal to or lesser than dmg. Restore only shield amount of hp.
                            plugin.setGrimoire(k, false);
                            plugin.Shell.put(p, 0);
                            p.sendMessage("You no longer feel protected");
                            p.setHealth(ceiling(p.getHealth()+shield,20));
                            }
                        }
                    }
                }
            }
    However sometimes, the spell actually ends up healing the player, rather than making it stay at same leve. Is there something I'm doing wrong?
     
  2. Offline

    Afforess

    the damage from the event has not yet been applied to the player. This is the entire point of the event, since it is to see whether the damage should be applied, or altered, or cancelled. If you need to do something after the event, create a sync delayed task with the scheduler.
     
  3. Offline

    DreadKyller

    if you want to prevent health from going down, all you need to do is add this line:

    e.setCancelled(true);

    then the player will not even take damage, so no need to revive it.
     
Thread Status:
Not open for further replies.

Share This Page