Files
src
build.gradle
Code
4 428
src/main/java/com/kodari/auction/DonutAuction.java
package com.kodari.auction;
import com.cryptomorin.xseries.XMaterial;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public final class DonutAuction extends JavaPlugin implements Listener {
private final Map<UUID, Listing> listings = new HashMap<>();
private final Map<UUID, Boolean> searching = new ConcurrentHashMap<>();
private Economy economy;
private File dataFile;
private static final class Listing {
final UUID id;
final UUID seller;
final String sellerName;
final ItemStack item;
final double price;
final long created;
Listing(UUID id, UUID seller, String sellerName, ItemStack item, double price, long created) {
this.id = id;
this.seller = seller;
this.sellerName = sellerName;
this.item = item;
this.price = price;
this.created = created;
}
}
private static final class Menu implements InventoryHolder {
final String mode;
final String query;
final int sort;
final int page;
final Map<Integer, UUID> entries = new HashMap<>();
Inventory inventory;
Menu(String mode, String query, int sort, int page) {
this.mode = mode;
this.query = query;
this.sort = sort;
this.page = page;
}
@Override
public Inventory getInventory() {
return inventory;
}
}
@Override
public void onEnable() {
saveDefaultConfig();
RegisteredServiceProvider<Economy> provider = getServer().getServicesManager().getRegistration(Economy.class);
if (provider == null || provider.getProvider() == null) {
getLogger().severe("Vault requires an economy provider; disabling auction.");
getServer().getPluginManager().disablePlugin(this);
return;
}
economy = provider.getProvider();
dataFile = new File(getDataFolder(), "listings.yml");
loadListings();
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
if (dataFile != null) saveListings();
}
private void loadListings() {
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(dataFile);
ConfigurationSection section = yaml.getConfigurationSection("listings");
if (section == null) return;
for (String key : section.getKeys(false)) {
try {
String path = "listings." + key + ".";
UUID id = UUID.fromString(key);
UUID seller = UUID.fromString(yaml.getString(path + "seller", ""));
ItemStack item = yaml.getItemStack(path + "item");
double price = yaml.getDouble(path + "price");
if (item != null && Double.isFinite(price) && price > 0) {
listings.put(id, new Listing(id, seller, yaml.getString(path + "seller-name", "Unknown"), item, price, yaml.getLong(path + "created")));
}
} catch (IllegalArgumentException ex) {
getLogger().warning("Skipping invalid listing " + key);
}
}
}
private boolean saveListings() {
YamlConfiguration yaml = new YamlConfiguration();
for (Listing listing : listings.values()) {
String path = "listings." + listing.id + ".";
yaml.set(path + "seller", listing.seller.toString());
yaml.set(path + "seller-name", listing.sellerName);
yaml.set(path + "item", listing.item);
yaml.set(path + "price", listing.price);
yaml.set(path + "created", listing.created);
}
try {
yaml.save(dataFile);
return true;
} catch (IOException ex) {
getLogger().severe("Could not save auction listings: " + ex.getMessage());
return false;
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use the auction.");
return true;
}
Player player = (Player) sender;
if (command.getName().equalsIgnoreCase("pause")) {
openPause(player);
return true;
}
if (args.length == 0) {
open(player, "auction", "", 0, 0);
} else if (args[0].equalsIgnoreCase("sell")) {
if (args.length != 2) {
player.sendMessage("§cUsage: /" + label + " sell <price>");
return true;
}
double price;
try {
price = Double.parseDouble(args[1]);
} catch (NumberFormatException ex) {
player.sendMessage("§cEnter a valid price.");
return true;
}
if (!Double.isFinite(price) || price <= 0 || price > 1000000000000D) {
player.sendMessage("§cPrice must be greater than zero and at most 1 trillion.");
return true;
}
ItemStack held = player.getInventory().getItemInMainHand();
if (held.getType().isAir() || held.getAmount() <= 0) {
player.sendMessage("§cHold the item you want to sell.");
return true;
}
long count = listings.values().stream().filter(l -> l.seller.equals(player.getUniqueId())).count();
if (count >= Math.max(1, getConfig().getInt("max-listings-per-player", 18))) {
player.sendMessage("§cYou have reached your listing limit.");
return true;
}
Listing listing = new Listing(UUID.randomUUID(), player.getUniqueId(), player.getName(), held.clone(), price, System.currentTimeMillis());
listings.put(listing.id, listing);
if (!saveListings()) {
listings.remove(listing.id);
player.sendMessage("§cCould not save your listing. Your item was not taken.");
return true;
}
player.getInventory().setItemInMainHand(null);
player.sendMessage("§aListed your item for " + economy.format(price) + ".");
} else if (args[0].equalsIgnoreCase("search")) {
String query = args.length > 1 ? String.join(" ", Arrays.copyOfRange(args, 1, args.length)) : "";
if (query.isEmpty()) beginSearch(player);
else open(player, "auction", query, 0, 0);
} else {
player.sendMessage("§e/auction §7- Browse listings");
player.sendMessage("§e/auction sell <price> §7- List the stack in your hand");
player.sendMessage("§e/auction search <item> §7- Search listings");
}


