package com.yaohun.wheatwar.listener;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.weather.WeatherChangeEvent;

public class GamePotect implements Listener {
    @EventHandler // 取消生物燃烧
    public void onEntityCombust(EntityCombustEvent event) {
        if (event.getEntity() instanceof LivingEntity) {
            event.setCancelled(true);
        }
    }
    @EventHandler // 禁止火焰燃烧方块
    public void onBlockFire(BlockBurnEvent e) {
        e.setCancelled(true);
    }
    @EventHandler// 禁止火焰燃烧物品
    public void onBlockIgnite(BlockIgniteEvent e) {
        e.setCancelled(true);
    }
    @EventHandler(priority= EventPriority.HIGH)
    public void onWeatherChange(WeatherChangeEvent e) {
        e.setCancelled(true);
    }
    @EventHandler(priority= EventPriority.HIGH)
    public void onFoodChange(FoodLevelChangeEvent e) {
        e.setCancelled(true);
    }
    @EventHandler /*保护农作物免踩踏*/
    public void onJumpFarm(PlayerInteractEvent e){
        if (e.isCancelled()) {return;}
        if(e.getAction() == Action.PHYSICAL){
            Block block = e.getClickedBlock();
            if(block.getType() == Material.FARMLAND){
                e.setCancelled(true);
            }
        }
    }
    @EventHandler /*保护农作物免踩踏*/
    public void onMobsFarm(EntityInteractEvent e){
        if (e.isCancelled()) {return;}
        if(e.getEntityType() != EntityType.PLAYER){
            Block block = e.getBlock();
            if(block.getType() == Material.FARMLAND){
                e.setCancelled(true);
            }
        }
    }
}
