package com.yutian.minerwar.listener;

import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.data.RepairGiftGui;
import com.yutian.minerwar.game.Game;
import org.bukkit.Effect;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.scheduler.BukkitRunnable;

public class PlayerListener implements Listener {
    private Game game;
    public PlayerListener(){
        game = MinerWar.getGame();
    }

    @EventHandler
    public void onPick(PlayerDropItemEvent e) {
        Player p = e.getPlayer();
        if (game.isStarted()) {
            if (p.isSneaking()) {
                // p.setFoodLevel(4);
                // p.setHealth(6);
                RepairGiftGui.OpenGui(p);
            }
        }
    }

    /*
    * 主播可进行二段跳
    * */
    @EventHandler
    public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
        Player player = event.getPlayer();
        if (player.getGameMode() == GameMode.CREATIVE) {
            return;
        }
        event.setCancelled(true);
        player.setAllowFlight(false);
        player.setFlying(false);
        player.setVelocity(player.getLocation().getDirection().multiply(0.6).setY(0.6));
        player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 10);
        player.playSound(player.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.2F, 1.0F);
        new BukkitRunnable() {
            @Override
            public void run() {
                if(!player.getAllowFlight()){
                    player.setAllowFlight(true);
                }
            }
        }.runTaskLater(MinerWar.inst(),20L);
    }
}
