/*
 * Copyright 2016 Ali Moghnieh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.blurengine.blur;

import com.blurengine.blur.events.players.PlayerDamagePlayerEvent;
import com.blurengine.blur.events.players.PlayerMoveBlockEvent;
import com.blurengine.blur.session.BlurPlayer;
import com.supaham.commons.bukkit.utils.EventUtils;
import com.supaham.commons.bukkit.utils.LocationUtils;

import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerMoveEvent;

/*
 * All events extend LOW to call Blur events at an early enough point in time that other code will be able to cancel these Bukkit events if necessary.
 */
class BlurListener implements Listener {

    private final BlurPlugin plugin;

    public BlurListener(BlurPlugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
    public void callPlayerMoveBlockEvent(PlayerMoveEvent event) {
        if (!LocationUtils.isSameBlock(event.getFrom(), event.getTo())) {
            BlurPlayer blurPlayer = plugin.getBlur().getPlayer(event.getPlayer());
            if (blurPlayer.getSession() != null) {
                EventUtils.callEvent(new PlayerMoveBlockEvent(event, blurPlayer));
            }
        }
    }

    @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
    public void callPlayerDamagePlayerEvent(EntityDamageByEntityEvent event) {
        LivingEntity damager = EventUtils.getLivingEntityDamager(event);
        if (event.getEntity() instanceof Player && damager instanceof Player) {
            BlurPlayer blurDamager = plugin.getBlur().getPlayer((Player) damager);
            BlurPlayer blurVictim = plugin.getBlur().getPlayer((Player) event.getEntity());
            
            PlayerDamagePlayerEvent damageEvent = new PlayerDamagePlayerEvent(blurDamager, blurVictim, event);
            blurDamager.getSession().callEvent(damageEvent);
            if (damageEvent.isCancelled()) {
                event.setCancelled(true);
            }
        }
    }
}

@@ -854,42 +854,27 @@
 ls;%0A
+%0A
 import 
-com.supaham.commons
+org
 .bukkit.
 util
@@ -873,22 +873,16 @@
 kit.
-utils.
 Location
 Util
@@ -881,47 +881,8 @@
 tion
-Utils;%0A%0Aimport org.bukkit.entity.Entity
 ;%0Aim
@@ -1639,21 +1639,16 @@
     
-if (!
 Location
 Util
@@ -1647,54 +1647,117 @@
 tion
-Utils.isSameBlock(event.getFrom(), event.getTo
+ from = event.getFrom();%0A        Location to = event.getTo();%0A        if (!from.toVector().equals(to.toVector
 ()))
@@ -2486,28 +2486,16 @@
 ity());%0A
-            
 %0A       

Make PlayerMoveBlockEvent fire more often.