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, “Меню базара”);
ItemStack coalItem = createMenuItem(Material.COAL, “§cУголь”, “§7Цена продажи: §a” + sellPrices.get(Material.COAL));
ItemStack stoneItem = createMenuItem(Material.STONE, “§7Камень”, “§7Цена продажи: §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, “Меню покупки/продажи”);

// Установка цен продажи и покупки
double sellPrice = sellPrices.get(material);
double buyPrice = sellPrice * BUY_PRICE_MULTIPLIER;
ItemStack sellItem = createMenuItem(material, “§6Продажа”, “§7Цена продажи: §a” + sellPrice);
ItemStack buyItem = createMenuItem(material, “§aПокупка”, “§7Цена покупки: §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(“Продано " + sellAmount + " " + material.toString() + " за " + profit + " монет.”);
// Добавьте код здесь для добавления монет в экономику вашего сервера
}

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(“Куплено " + amount + " " + material.toString() + " за " + totalCost + " монет.”);
// Удалите код ниже и добавьте код здесь для вычета монет из эконоимки вашего сервера
} else {
player.sendMessage(“У вас недостаточно монет для покупки " + 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;
}
}
добавь в этот код валюту с плагина essentials и сделай чтобы все работало через эту валют