package in.twizmwaz.cardinal.module.modules.playable;

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.chat.ChatConstant;
import in.twizmwaz.cardinal.chat.LocalizedChatMessage;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.regions.RegionModule;
import in.twizmwaz.cardinal.util.ChatUtils;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;

public class Playable implements Module {

    private final RegionModule region;

    public Playable(RegionModule region) {
        this.region = region;
    }

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (GameHandler.getGameHandler().getMatch().isRunning() && !TeamUtils.getTeamByPlayer(event.getPlayer()).isObserver()) {
            if (region != null) {
                if (region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector())) {
                    ChatUtils.sendWarningMessage(event.getPlayer(), new LocalizedChatMessage(ChatConstant.ERROR_PLAYABLE_LEAVE));
                    event.setCancelled(true);
                }
            }
        }
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        if (GameHandler.getGameHandler().getMatch().isRunning() && !TeamUtils.getTeamByPlayer(event.getPlayer()).isObserver()) {
            if (region != null) {
                if (region.contains(event.getBlock().getLocation())) {
                    ChatUtils.sendWarningMessage(event.getPlayer(), new LocalizedChatMessage(ChatConstant.ERROR_PLAYABLE_INTERACT));
                    event.setCancelled(true);
                }
            }
        }
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        if (GameHandler.getGameHandler().getMatch().isRunning() && !TeamUtils.getTeamByPlayer(event.getPlayer()).isObserver()) {
            if (region != null) {
                if (region.contains(event.getBlock().getLocation())) {
                    ChatUtils.sendWarningMessage(event.getPlayer(), new LocalizedChatMessage(ChatConstant.ERROR_PLAYABLE_INTERACT));
                    event.setCancelled(true);
                }
            }
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (GameHandler.getGameHandler().getMatch().isRunning() && !TeamUtils.getTeamByPlayer(event.getPlayer()).isObserver()) {
            if (region != null && event.getClickedBlock() != null) {
                if (region.contains(event.getClickedBlock().getLocation())) {
                    ChatUtils.sendWarningMessage(event.getPlayer(), new LocalizedChatMessage(ChatConstant.ERROR_PLAYABLE_INTERACT));
                    event.setCancelled(true);
                }
            }
        }
    }


}

@@ -1039,32 +1039,88 @@
 ).isRunning() &&
+ TeamUtils.getTeamByPlayer(event.getPlayer()) != null &&
  !TeamUtils.getT
@@ -1158,32 +1158,32 @@
 isObserver()) %7B%0A
-
             if (
@@ -1668,32 +1668,88 @@
 ).isRunning() &&
+ TeamUtils.getTeamByPlayer(event.getPlayer()) != null &&
  !TeamUtils.getT
@@ -2258,32 +2258,88 @@
 ).isRunning() &&
+ TeamUtils.getTeamByPlayer(event.getPlayer()) != null &&
  !TeamUtils.getT
@@ -2790,32 +2790,32 @@
 tEvent event) %7B%0A
-
         if (Game
@@ -2864,16 +2864,72 @@
 ing() &&
+ TeamUtils.getTeamByPlayer(event.getPlayer()) != null &&
  !TeamUt

Check if the player's team is null in Playable