/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.zone.global.theforge;

import com.skelril.nitro.combat.PlayerCombatParser;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.projectile.Projectile;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.CollideEntityEvent;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.entity.DestructEntityEvent;
import org.spongepowered.api.event.filter.Getter;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.text.Text;

import java.util.Optional;

public class TheForgeListener {
  private final TheForgeManager manager;

  public TheForgeListener(TheForgeManager manager) {
    this.manager = manager;
  }

  // Poor man's flight check
  private boolean isFlying(Player player) {
    BlockType blockBelow = player.getLocation().add(0, -1, 0).getBlockType();
    BlockType blockBelowBelow = player.getLocation().add(0, -2, 0).getBlockType();
    return blockBelow == BlockTypes.AIR && blockBelowBelow == BlockTypes.AIR;
  }

  @Listener
  public void onPlayerCombat(CollideEntityEvent.Impact event, @First Projectile projectile) {
    Optional<TheForgeInstance> optInst = manager.getApplicableZone(projectile);
    if (!optInst.isPresent()) {
      return;
    }

    new PlayerCombatParser() {
      @Override
      public void processMonsterAttack(Living attacker, Player defender) {
        if (!(event instanceof DamageEntityEvent)) {
          return;
        }

        DamageEntityEvent dEvent = (DamageEntityEvent) event;
        if (isFlying(defender)) {
          dEvent.setBaseDamage(dEvent.getBaseDamage() * 3);
        }
      }
    }.parse(event);
  }

  @Listener
  public void onPlayerDeath(DestructEntityEvent.Death event, @Getter("getTargetEntity") Player player) {
    Optional<TheForgeInstance> optInst = manager.getApplicableZone(player);
    if (optInst.isPresent()) {
      if (event.getMessage().toPlain().contains("died")) {
        event.setMessage(Text.of(player.getName(), " was incinerated at The Forge"));
      }
    }
  }
}

@@ -323,92 +323,49 @@
 ort 
-org.spongepowered.api.block.BlockType;%0Aimport org.spongepowered.api.block.BlockTypes
+com.skelril.nitro.probability.Probability
 ;%0Aim
@@ -571,24 +571,95 @@
 t.Listener;%0A
+import org.spongepowered.api.event.cause.entity.damage.DamageModifier;%0A
 import org.s
@@ -981,34 +981,111 @@
 xt;%0A
-%0Aimport java.util.Optional
+import org.spongepowered.api.util.Tuple;%0A%0Aimport java.util.Optional;%0Aimport java.util.function.Function
 ;%0A%0Ap
@@ -1249,83 +1249,255 @@
 %0A%0A  
-// Poor man's flight check%0A  private boolean isFlying(Player play
+@Listener%0A  public void onPlayerCombat(DamageEntityEvent event) %7B%0A    Optional%3CTheForgeInstance%3E optInst = manager.getApplicableZone(event.getTargetEntity());%0A    if (!optInst.isPresent()) %7B%0A      return;%0A    %7D%0A%0A    new PlayerCombatPars
 er
+(
 ) %7B%0A    
 Bloc
@@ -1496,241 +1496,301 @@
     
-BlockType blockBelow = player.getLocation().add(0, -1, 0).getBlockType();%0A    BlockType blockBelowBelow = player.getLocation().add(0, -2, 0).getBlockType();%0A    return blockBelow == BlockTypes.AIR && blockBelowBelow == BlockTypes.AIR
+  @Override%0A      public void processPlayerAttack(Player attacker, Living defender) %7B%0A        if (attacker.getLocation().getY() != attacker.getLocation().getBlockY()) %7B%0A          event.setBaseDamage(Probability.getCompoundRandom(event.getBaseDamage(), 4));%0A        %7D%0A      %7D%0A    %7D.parse(event)
 ;%0A  
@@ -2309,29 +2309,100 @@
     
-if (isFlying(defender
+for (Tuple%3CDamageModifier, Function%3C? super Double, Double%3E%3E modifier : dEvent.getModifiers(
 )) %7B
@@ -2426,45 +2426,61 @@
 .set
-Base
 Damage(
-dEvent.getBaseDamage() * 3
+modifier.getFirst(), (a) -%3E Math.floor(a * .5)
 );%0A 

Change how the forge deals with flying players