package com.example.bazaar;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class BazaarPlugin extends JavaPlugin implements Listener, CommandExecutor {

    private static final int BASE_COAL_SELL_PRICE = 50;
    private static final int BASE_STONE_SELL_PRICE = 10;
    private static final double PRICE_CHANGE_PERCENTAGE = 0.03;
    private static final double BUY_PRICE_MULTIPLIER = 1.1;
    private static final double MAX_PRICE_CHANGE_PERCENTAGE = 0.4;

    private Inventory bazaarMenu;
    private Map<Player, Inventory> playerMenus;
    private Map<Material, Double> sellPrices;
    private Map<UUID, Long> shiftClickCooldowns;

    @Override
    public void onEnable() {
        getCommand(“bazaar”).setExecutor(this);
        getServer().getPluginManager().registerEvents(this, this);

        playerMenus = new HashMap<>();
        sellPrices = new HashMap<>();
        shiftClickCooldowns = new HashMap<>();

        // Установка изначальных цен продажи
        sellPrices.put(Material.COAL, (double) BASE_COAL_SELL_PRICE);
        sellPrices.put(Material.STONE, (double) BASE_STONE_SELL_PRICE);

        // Запустить задачу обновления цен каждые 30 секунд
        new BukkitRunnable() {
            @Override
            public void run() {
                for (Material material : sellPrices.keySet()) {
                    double currentPrice = sellPrices.get(material);
                    double priceChange = PRICE_CHANGE_PERCENTAGE * currentPrice;

                    if (Math.abs(priceChange) / currentPrice > MAX_PRICE_CHANGE_PERCENTAGE) {
                        sellPrices.put(material, (double) ((material == Material.COAL) ? BASE_COAL_SELL_PRICE : BASE_STONE_SELL_PRICE));
                    } else {
                        double newPrice = currentPrice + priceChange;
                        sellPrices.put(material, newPrice);
                    }
                }
            }
        }.runTaskTimer(this, 0L, 600L); // 20 тиков = 1 секунда, 30 секунд = 600 тиков

        // Создать базарное меню
        bazaarMenu = Bukkit.createInventory(null, 9, “Bazaar Menu”);
        ItemStack coalItem = createMenuItem(Material.COAL, “&cCoal”, “&7Sell Price: &a” + sellPrices.get(Material.COAL));
        ItemStack stoneItem = createMenuItem(Material.STONE, “&7Stone”, “&7Sell Price: &a” + sellPrices.get(Material.STONE));
        bazaarMenu.setItem(0, coalItem);
        bazaarMenu.setItem(1, stoneItem);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase(“bazaar”)) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                player.openInventory(bazaarMenu);
                playerMenus.put(player, bazaarMenu);
            }
            return true;
        }
        return false;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
        Inventory clickedInventory = event.getClickedInventory();
        ItemStack clickedItem = event.getCurrentItem();

        if (clickedInventory != null && playerMenus.containsKey(player) && clickedInventory.equals(playerMenus.get(player))) {
            event.setCancelled(true);

            if (clickedItem != null && clickedItem.getType() != Material.AIR) {
                if (clickedItem.getType() == Material.COAL || clickedItem.getType() == Material.STONE) {
                    openBuySellMenu(player, clickedItem.getType());
                }
            }
        } else if (clickedInventory != null && playerMenus.containsValue(clickedInventory)) {
            event.setCancelled(true);
            
            if (player.isSneaking() && shiftClickCooldowns.getOrDefault(player.getUniqueId(), 0L) < System.currentTimeMillis()) {
                shiftClickCooldowns.put(player.getUniqueId(), System.currentTimeMillis() + 1000L);

                if (clickedItem != null && clickedItem.getType() != Material.AIR && event.isRightClick()) {
                    handleSell(player, clickedItem.getType(), 64);
                } else if (clickedItem != null && clickedItem.getType() != Material.AIR && event.isLeftClick()) {
                    handleBuy(player, clickedItem.getType(), 64);
                }
            }
        }
    }

    private void openBuySellMenu(Player player, Material material) {
        Inventory buySellMenu = Bukkit.createInventory(null, 9, “Buy/Sell Menu”);

        // Установка цен продажи и покупки
        double sellPrice = sellPrices.get(material);
        double buyPrice = sellPrice * BUY_PRICE_MULTIPLIER;
        ItemStack sellItem = createMenuItem(material, “&6Sell”, “&7Sell Price: &a” + sellPrice);
        ItemStack buyItem = createMenuItem(material, “&aBuy”, “&7Buy Price: &e” + buyPrice);

        buySellMenu.setItem(2, sellItem);
        buySellMenu.setItem(6, buyItem);
        player.openInventory(buySellMenu);
        playerMenus.put(player, buySellMenu);
    }

    private void handleSell(Player player, Material material, int amount) {
        double sellPrice = sellPrices.get(material);
        int sellAmount = 0;

        for (ItemStack item : player.getInventory().getContents()) {
            if (item != null && item.getType() == material && sellAmount < amount) {
                if (item.getAmount() <= amount - sellAmount) {
                    sellAmount += item.getAmount();
                    player.getInventory().remove(item);
                } else {
                    item.setAmount(item.getAmount() - (amount - sellAmount));
                    sellAmount = amount;
                }
            }
        }

        double profit = sellAmount * sellPrice;
        player.sendMessage(“Sold " + sellAmount + " " + material.toString() + " for " + profit + " coins.”);
        // Добавьте код здесь для добавления монет в экономику вашего сервера
    }

    private void handleBuy(Player player, Material material, int amount) {
        double sellPrice = sellPrices.get(material);
        double buyPrice = sellPrice * BUY_PRICE_MULTIPLIER;
        double totalCost = amount * buyPrice;

        if (/* Проверка наличия достаточного количества монет у игрока */) {
            for (int i = 0; i < amount; i++) {
                ItemStack item = new ItemStack(material);
                player.getInventory().addItem(item);
            }

            player.sendMessage(“Bought " + amount + " " + material.toString() + " for " + totalCost + " coins.”);
            // Удалите код ниже и добавьте код здесь для вычета монет из эконоимки вашего сервера
        } else {
            player.sendMessage(“You don’t have enough coins to purchase " + amount + " " + material.toString() + “.”);
        }
    }

    private ItemStack createMenuItem(Material material, String name, String lore) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name.replace(”&“, “§”));
        meta.setLore(Collections.singletonList(lore.replace(”&", “§”)));
        item.setItemMeta(meta);
        return item;
    }
} переведи все сообщения в коде на русский язык и скинь итоговый код