/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

package pw.yumc.Residence.listeners;

import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;

import com.bekvon.bukkit.residence.protection.FlagPermissions;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class ResidencePlayerListener_1_8 implements Listener {
    ResidenceMain plugin;

    public ResidencePlayerListener_1_8(final ResidenceMain plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerInteractAtEntity(final PlayerInteractAtEntityEvent event) {
        final Player player = event.getPlayer();
        if (plugin.isResAdminOn(player)) {
            return;
        }

        final Entity ent = event.getRightClicked();
        if (ent.getType() != EntityType.ARMOR_STAND) {
            return;
        }

        final FlagPermissions perms = plugin.getPermsByLocForPlayer(ent.getLocation(), player);
        final String world = player.getWorld().getName();

        if (!perms.playerHas(player.getName(), world, "container", perms.playerHas(player.getName(), world, "use", true))) {
            event.setCancelled(true);
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "container"));
        }
    }

}
