package com.github.games647.flexiblelogin.listener;

import com.flowpowered.math.vector.Vector3d;
import com.github.games647.flexiblelogin.FlexibleLogin;
import com.google.common.base.Optional;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.BreakBlockEvent;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.block.PlaceBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.command.MessageSinkEvent;
import org.spongepowered.api.event.command.SendCommandEvent;
import org.spongepowered.api.event.entity.DisplaceEntityEvent;
import org.spongepowered.api.event.entity.InteractEntityEvent;
import org.spongepowered.api.event.inventory.DropItemStackEvent;
import org.spongepowered.api.event.inventory.PickUpItemEvent;
import org.spongepowered.api.event.inventory.UseItemStackEvent;

public class PreventListener {

    private final FlexibleLogin plugin;

    public PreventListener(FlexibleLogin plugin) {
        this.plugin = plugin;
    }

    @Listener(ignoreCancelled = true)
    public void onPlayerMove(DisplaceEntityEvent.TargetPlayer playerMoveEvent) {
        Vector3d oldLocation = playerMoveEvent.getFromTransform().getPosition();
        Vector3d newLocation = playerMoveEvent.getToTransform().getPosition();
        if ((oldLocation.getFloorX()!= newLocation.getFloorX()
                || oldLocation.getFloorZ()!= newLocation.getFloorZ())) {
            checkLoginStatus(playerMoveEvent, playerMoveEvent.getTargetEntity());
        }
    }

    @Listener(ignoreCancelled = true)
    public void onChat(MessageSinkEvent.Chat chatEvent) {
        checkLoginStatus(chatEvent, chatEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onCommand(SendCommandEvent commandEvent) {
        Optional<Player> playerOptional = commandEvent.getCause().first(Player.class);
        if (playerOptional.isPresent()) {
            String command = commandEvent.getCommand();
            //do not blacklist our own commands
            if ("register".equals(command) || "login".equals(command)
                    || "forgotpassword".equals(command)) {
                return;
            }

            checkLoginStatus(commandEvent, playerOptional.get());
        }
    }

    @Listener(ignoreCancelled = true)
    public void onPlayerItemDrop(DropItemStackEvent.Drop dropItemEvent) {
        Optional<Player> playerOptional = dropItemEvent.getCause().first(Player.class);
        if (playerOptional.isPresent()) {
            checkLoginStatus(dropItemEvent, playerOptional.get());
        }
    }

    @Listener(ignoreCancelled = true)
    public void onPlayerItemPickup(PickUpItemEvent pickUpItemEvent) {
        checkLoginStatus(pickUpItemEvent, pickUpItemEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onItemConsume(UseItemStackEvent itemConsumeEvent) {
        checkLoginStatus(itemConsumeEvent, itemConsumeEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onBlockBreak(BreakBlockEvent breakBlockEvent) {
        checkLoginStatus(breakBlockEvent, breakBlockEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onBlockBreak(PlaceBlockEvent blockPlaceEvent) {
        checkLoginStatus(blockPlaceEvent, blockPlaceEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onBlockChange(ChangeBlockEvent changeBlockEvent) {
        checkLoginStatus(changeBlockEvent, changeBlockEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onBlockInteract(InteractBlockEvent interactBlockEvent) {
        checkLoginStatus(interactBlockEvent, interactBlockEvent.getCause());
    }

    @Listener(ignoreCancelled = true)
    public void onEntityInteract(InteractEntityEvent interactEntityEvent) {
        checkLoginStatus(interactEntityEvent, interactEntityEvent.getCause());
    }

    private void checkLoginStatus(Cancellable event, Cause cause) {
        Optional<Player> playerOptional = cause.first(Player.class);
        if (playerOptional.isPresent()) {
            checkLoginStatus(event, playerOptional.get());
        }
    }

    private void checkLoginStatus(Cancellable event, Player player) {
        if (!plugin.getDatabase().isLoggedin(player)) {
            event.setCancelled(true);
        }
    }
}

@@ -151,37 +151,25 @@
 in;%0A
+%0A
 import 
-com.google.common.base
+java.util
 .Opt
@@ -331,66 +331,8 @@
 er;%0A
-import org.spongepowered.api.event.block.BreakBlockEvent;%0A
 impo
@@ -451,66 +451,8 @@
 nt;%0A
-import org.spongepowered.api.event.block.PlaceBlockEvent;%0A
 impo
@@ -493,16 +493,23 @@
 se.Cause
+Tracked
 ;%0Aimport
@@ -799,29 +799,24 @@
 ory.DropItem
-Stack
 Event;%0Aimpor
@@ -1749,35 +1749,24 @@
 t, chatEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -2427,23 +2427,13 @@
 Item
-StackEvent.Drop
+Event
  dro
@@ -2458,206 +2458,55 @@
     
-Optional%3CPlayer%3E playerOptional = dropItemEvent.getCause().first(Player.class);%0A        if (playerOptional.isPresent()) %7B%0A            checkLoginStatus(dropItemEvent, playerOptional.get());%0A        %7D
+checkLoginStatus(dropItemEvent, dropItemEvent);
 %0A   
@@ -2670,35 +2670,24 @@
 kUpItemEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -2845,35 +2845,24 @@
 ConsumeEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -2926,21 +2926,22 @@
 ckBreak(
-Break
+Change
 BlockEve
@@ -2942,16 +2942,22 @@
 ockEvent
+.Break
  breakBl
@@ -3021,35 +3021,24 @@
 akBlockEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -3102,20 +3102,21 @@
 ckBreak(
-Plac
+Chang
 eBlockEv
@@ -3118,16 +3118,22 @@
 ockEvent
+.Place
  blockPl
@@ -3197,35 +3197,24 @@
 ckPlaceEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -3371,35 +3371,24 @@
 geBlockEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -3555,35 +3555,24 @@
 ctBlockEvent
-.getCause()
 );%0A    %7D%0A%0A  
@@ -3748,27 +3748,16 @@
 ityEvent
-.getCause()
 );%0A    %7D
@@ -3816,22 +3816,34 @@
 t, Cause
+Tracked
  cause
+Event
 ) %7B%0A    
@@ -3885,16 +3885,32 @@
  = cause
+Event.getCause()
 .first(P

Update to the newest Sponge 699+