/** * Heals an entity by a specific amount. * * @param target the entity to heal * @param amount the amount of health to regain * @param reason the reason supplied to the {@link EntityRegainHealthEvent} */ public static void heal(LivingEntity target, double amount, EntityRegainHealthEvent.RegainReason reason) { if (target.isDead()) { return; // too late! } final double maxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); final double currentHealth = target.getHealth(); if (currentHealth >= maxHealth) { return; } EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason); EventFactory.getInstance().callEvent(event); if (!event.isCancelled()) { target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount())); } }
/** * This method exists for legacy reasons to provide backwards * compatibility. It will not exist at runtime and should not be used * under any circumstances. */ @Deprecated public void _INVALID_setAmount(int amount) { setAmount(amount); }
/** * This method is called when an entity's health goes up or down. * @param event The Event that was fired. */ @EventHandler public void entityRegainHealth(EntityRegainHealthEvent event) { if (event.isCancelled()) { return; } RegainReason reason = event.getRegainReason(); MultiverseWorld world = this.worldManager.getMVWorld(event.getEntity().getLocation().getWorld()); if (world != null && reason == RegainReason.REGEN && !world.getAutoHeal()) { event.setCancelled(true); } }
public void onEntityRegainHealth(EntityRegainHealthEvent event) { if (!arena.isRunning()) return; if (!(event.getEntity() instanceof Player) || !arena.inArena((Player) event.getEntity())) return; if (!foodRegen && event.getRegainReason() == RegainReason.SATIATED) { event.setCancelled(true); } }
@EventHandler(priority = EventPriority.HIGHEST) public void onRegen(EntityRegainHealthEvent e){ if(e.getEntityType() != EntityType.PLAYER || e.getRegainReason() != EntityRegainHealthEvent.RegainReason.SATIATED){ return; } final Player p = (Player) e.getEntity(); if(!isEnabled(p.getWorld())) return; e.setCancelled(true); long currentTime = System.currentTimeMillis() / 1000; long lastHealTime = getLastHealTime(p); if(currentTime - lastHealTime < module().getLong("frequency")) return; double maxHealth = p.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); if(p.getHealth() < maxHealth){ p.setHealth(MathHelper.clamp(p.getHealth() + module().getInt("amount"), 0.0, maxHealth)); healTimes.put(p.getUniqueId(), currentTime); disableSpartanRegenCheck(p); } final float previousExhaustion = p.getExhaustion(); final float exhaustionToApply = (float) module().getDouble("exhaustion"); Bukkit.getScheduler().runTaskLater(plugin, () -> { //This is because bukkit doesn't stop the exhaustion change when cancelling the event p.setExhaustion(previousExhaustion + exhaustionToApply); debug("Exhaustion before: " + previousExhaustion + " Now: " + p.getExhaustion() + " Saturation: " + p.getSaturation(), p); }, 1L); }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onEntityRegainHealth(EntityRegainHealthEvent event) { Entity ent = event.getEntity(); World world = ent.getWorld(); ConfigurationManager cfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager(); WorldConfiguration wcfg = cfg.get(BukkitAdapter.adapt(world)); if (wcfg.disableHealthRegain) { event.setCancelled(true); return; } }
@EventHandler(priority = EventPriority.HIGH) public void onRegainHealth(EntityRegainHealthEvent rhe) { if (rhe.getEntityType() != EntityType.PLAYER) { return; } Player player = (Player) rhe.getEntity(); Game game = BedwarsRel.getInstance().getGameManager().getGameOfPlayer(player); if (game == null) { return; } if (game.getState() != GameState.RUNNING) { return; } if (player.getHealth() >= player.getMaxHealth()) { game.setPlayerDamager(player, null); } } }
/** * This method exists for legacy reasons to provide backwards * compatibility. It will not exist at runtime and should not be used * under any circumstances. */ @Deprecated public int _INVALID_getAmount() { return NumberConversions.ceil(getAmount()); }
@Override public MCEntity getEntity() { return BukkitConvertor.BukkitGetCorrectEntity(e.getEntity()); }
@Override public MCRegainReason getRegainReason() { return BukkitMCRegainReason.getConvertor().getAbstractedEnum(e.getRegainReason()); } }
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onEntityRegainHealthLow(final EntityRegainHealthEvent event) { final Entity entity = event.getEntity(); if (!(entity instanceof Player)) { return; } final Player player = (Player) entity; if (player.isDead() && BridgeHealth.getHealth(player) <= 0.0) { // Heal after death. // TODO: Problematic. At least skip CUSTOM. event.setCancelled(true); counters.addPrimaryThread(idCancelDead, 1); return; } if (event.getRegainReason() != RegainReason.SATIATED) { return; } // TODO: EATING reason / peaceful difficulty / regen potion - byCaptain SpigotMC final IPlayerData pData = DataManager.getPlayerData(player); if (pData.isCheckActive(CheckType.FIGHT_FASTHEAL, player) && fastHeal.check(player, pData)) { // TODO: Can clients force events with 0-re-gain ? event.setCancelled(true); } }
@EventHandler(priority = EventPriority.HIGH) public void onRegainHealth(EntityRegainHealthEvent rhe) { if (rhe.getEntityType() != EntityType.PLAYER) { return; } Player player = (Player) rhe.getEntity(); Game game = BedwarsRel.getInstance().getGameManager().getGameOfPlayer(player); if (game == null) { return; } if (game.getState() != GameState.RUNNING) { return; } if (player.getHealth() >= player.getMaxHealth()) { game.setPlayerDamager(player, null); } } }
@Override public double getAmount() { return e.getAmount(); }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onEntityRegainHealth(final EntityRegainHealthEvent event) { final Entity entity = event.getEntity(); if (!(entity instanceof Player)) { return; } final Player player = (Player) entity; final FightData data = DataManager.getGenericInstance(player, FightData.class); // Adjust god mode data: // Remember the time. data.regainHealthTime = System.currentTimeMillis(); // Set god-mode health to maximum. // TODO: Mind that health regain might half the ndt. final double health = Math.min(BridgeHealth.getHealth(player) + BridgeHealth.getAmount(event), BridgeHealth.getMaxHealth(player)); data.godModeHealth = Math.max(data.godModeHealth, health); }
EntityRegainHealthEvent event = new EntityRegainHealthEvent(targetEntity, healAmount, RegainReason.CUSTOM); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { return SpellResult.CANCELLED; healAmount = event.getAmount(); if (healAmount == 0)
/** * This method exists for legacy reasons to provide backwards * compatibility. It will not exist at runtime and should not be used * under any circumstances. */ @Deprecated public int _INVALID_getAmount() { return NumberConversions.ceil(getAmount()); }
@Override public void setAmount(double amount) { e.setAmount(amount); }
/** * Get the amount of health added with the event. * * @param event * @return * @throws RuntimeException, * in case of an IncompatibleClassChangeError without success on * recovery attempts. */ public static double getAmount(final EntityRegainHealthEvent event) { try{ return event.getAmount(); } catch(IncompatibleClassChangeError e) { return getDoubleOrInt(event, "getAmount", e); } }
/** * This method exists for legacy reasons to provide backwards * compatibility. It will not exist at runtime and should not be used * under any circumstances. */ @Deprecated public void _INVALID_setAmount(int amount) { setAmount(amount); }