<commit_before>package de.epiceric.shopchest.listeners;

import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;

public class ShopItemListener implements Listener {

    private ShopUtils shopUtils;
    private ShopChest plugin;

    public ShopItemListener(ShopChest plugin) {
        this.plugin = plugin;
        this.shopUtils = plugin.getShopUtils();
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent e) {

        if (e.getFrom().getBlockX() == e.getTo().getBlockX()
                && e.getFrom().getBlockZ() == e.getTo().getBlockZ()
                && e.getFrom().getBlockY() == e.getTo().getBlockY()) {
            return;
        }

        updateShopItemVisibility(e.getPlayer(), true);
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onPlayerTeleport(PlayerTeleportEvent e) {
        updateShopItemVisibility(e.getPlayer(), true, e.getTo());
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        updateShopItemVisibility(e.getPlayer(), false);
    }

    @EventHandler
    public void onPlayerLeave(PlayerQuitEvent e) {
        for (Shop shop : plugin.getShopUtils().getShops()) {
            shop.getItem().setVisible(e.getPlayer(), false);
        }
    }

    private void updateShopItemVisibility(Player p, boolean hideIfAway) {
        updateShopItemVisibility(p, hideIfAway, p.getLocation());
    }

    private void updateShopItemVisibility(Player p, boolean hideIfAway, Location playerLocation) {

        double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance;
        itemDistanceSquared *= itemDistanceSquared;
        World w = p.getWorld();

        for (Shop shop : shopUtils.getShops()) {
            Location shopLocation = shop.getLocation();
            if (w.equals(shopLocation.getWorld()) && shopLocation.distanceSquared(playerLocation) <= itemDistanceSquared) {
                shop.getItem().setVisible(p, true);
            } else if (hideIfAway) {
                shop.getItem().setVisible(p, false);
            }
        }

    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onBlockPlace(BlockPlaceEvent e) {
        Block b = e.getBlockPlaced();
        Block below = b.getRelative(BlockFace.DOWN);

        if (shopUtils.isShop(below.getLocation())) {
            shopUtils.getShop(below.getLocation()).getItem().resetForPlayer(e.getPlayer());
            e.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.HIGH)<commit_msg>Fixed error message in console after teleporting<commit_after>package de.epiceric.shopchest.listeners;

import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;

public class ShopItemListener implements Listener {

    private ShopUtils shopUtils;
    private ShopChest plugin;

    public ShopItemListener(ShopChest plugin) {
        this.plugin = plugin;
        this.shopUtils = plugin.getShopUtils();
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent e) {

        if (e.getFrom().getBlockX() == e.getTo().getBlockX()
                && e.getFrom().getBlockZ() == e.getTo().getBlockZ()
                && e.getFrom().getBlockY() == e.getTo().getBlockY()) {
            return;
        }

        updateShopItemVisibility(e.getPlayer(), true);
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onPlayerTeleport(PlayerTeleportEvent e) {
        updateShopItemVisibility(e.getPlayer(), true, e.getTo());
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        updateShopItemVisibility(e.getPlayer(), false);
    }

    @EventHandler
    public void onPlayerLeave(PlayerQuitEvent e) {
        for (Shop shop : plugin.getShopUtils().getShops()) {
            shop.getItem().setVisible(e.getPlayer(), false);
        }
    }

    private void updateShopItemVisibility(Player p, boolean hideIfAway) {
        updateShopItemVisibility(p, hideIfAway, p.getLocation());
    }

    private void updateShopItemVisibility(Player p, boolean hideIfAway, Location playerLocation) {

        double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance;
        itemDistanceSquared *= itemDistanceSquared;
        World w = playerLocation.getWorld();

        for (Shop shop : shopUtils.getShops()) {
            Location shopLocation = shop.getLocation();
            if (w.equals(shopLocation.getWorld()) && shopLocation.distanceSquared(playerLocation) <= itemDistanceSquared) {
                shop.getItem().setVisible(p, true);
            } else if (hideIfAway) {
                shop.getItem().setVisible(p, false);
            }
        }

    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onBlockPlace(BlockPlaceEvent e) {
        Block b = e.getBlockPlaced();
        Block below = b.getRelative(BlockFace.DOWN);

        if (shopUtils.isShop(below.getLocation())) {
            shopUtils.getShop(below.getLocation()).getItem().resetForPlayer(e.getPlayer());
            e.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.HIGH)
{'fraction_non_alphanumeric': 0.11486486486486487, 'fraction_numerical': 0.0, 'mean_word_length': 3.180901143241426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6988003', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1814, 'n_words': 325}