Files
src
build.gradle
README.md
settings.gradle
Code
15 1,553
src/main/java/com/kodari/auction/AuctionPlugin.java
package com.kodari.auction;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
public final class AuctionPlugin extends JavaPlugin {
private HikariDataSource dataSource;
private AuctionRepository repository;
private AuctionService service;
private AuctionGuiListener guiListener;
private Economy economy;
private ExecutorService databaseExecutor;
@Override
public void onEnable() {
saveDefaultConfig();
if (!hookEconomy()) {
getLogger().severe("No Vault economy provider is registered; Auction is disabling.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
try {
HikariConfig hikari = new HikariConfig();
hikari.setJdbcUrl(getConfig().getString("database.jdbc-url"));
hikari.setUsername(getConfig().getString("database.username"));
hikari.setPassword(getConfig().getString("database.password"));
hikari.setMaximumPoolSize(Math.max(2, getConfig().getInt("database.pool-size", 10)));
hikari.setMinimumIdle(1);
hikari.setConnectionTimeout(10000);
hikari.setPoolName("Auction-MySQL");
dataSource = new HikariDataSource(hikari);
repository = new AuctionRepository(dataSource);
repository.initialize();
} catch (Exception exception) {
getLogger().severe("Could not initialize the Auction database: " + exception.getMessage());
if (dataSource != null) {
dataSource.close();
}
Bukkit.getPluginManager().disablePlugin(this);
return;
}
databaseExecutor = Executors.newFixedThreadPool(4, runnable -> {
Thread thread = new Thread(runnable, "Auction-Database");
thread.setDaemon(true);
return thread;
});
service = new AuctionService(this, repository);
guiListener = new AuctionGuiListener(this, service, repository);
Bukkit.getPluginManager().registerEvents(guiListener, this);
Bukkit.getPluginManager().registerEvents(new AuctionAliasListener(this), this);
getCommand("ah").setExecutor(new AuctionCommand(this, service));
getCommand("ah").setTabCompleter(new AuctionCommand(this, service));
getCommand("auctionadmin").setExecutor(new AuctionAdminCommand(this, service));
getCommand("auctionadmin").setTabCompleter(new AuctionAdminCommand(this, service));
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
try {
repository.expireListings(System.currentTimeMillis());
} catch (SQLException exception) {
getLogger().warning("Could not expire auction listings: " + exception.getMessage());
}
}, 20L, 1200L);
getLogger().info("Auction enabled. Shared MySQL storage is ready.");
}
private boolean hookEconomy() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null) {
return false;
}
economy = registration.getProvider();
return economy != null;
}
@Override
public void onDisable() {
if (databaseExecutor != null) {
databaseExecutor.shutdown();
}
if (dataSource != null) {
dataSource.close();
}
}
public <T> void submit(Callable<T> action, Consumer<T> success, Consumer<Throwable> failure) {
databaseExecutor.execute(() -> {
T value;
try {
value = action.call();
} catch (Throwable throwable) {
getLogger().warning("Auction database operation failed: " + throwable.getMessage());
dispatch(() -> failure.accept(throwable));
return;
}
dispatch(() -> success.accept(value));
});
}
private void dispatch(Runnable callback) {
if (!isEnabled()) {
return;
}
Bukkit.getScheduler().runTask(this, () -> {
if (isEnabled()) {
callback.run();
}
});
}
public void message(CommandSender recipient, String key) {
message(recipient, key, Map.of());
}
public void message(CommandSender recipient, String key, Map<String, String> replacements) {
String prefix = getConfig().getString("messages.prefix", "");
String text = getConfig().getString("messages." + key, "&cMissing message: " + key);
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
text = text.replace("{" + replacement.getKey() + "}", replacement.getValue());
}
recipient.sendMessage(color(prefix + text));
}
public String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
public String formatPrice(double price) {
return economy.format(price);
}
public ItemStack menuItem(String path, String fallbackMaterial, Map<String, String> replacements) {
String material = getConfig().getString("gui.items." + path + ".material", fallbackMaterial);
ItemStack item = XMaterial.matchXMaterial(material)
.map(XMaterial::parseItem)
.orElseGet(() -> XMaterial.matchXMaterial(fallbackMaterial).map(XMaterial::parseItem).orElse(null));
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta != null) {
String name = getConfig().getString("gui.items." + path + ".name", "&f" + path);
meta.setDisplayName(color(replace(name, replacements)));
List<String> lore = new ArrayList<>();
for (String line : getConfig().getStringList("gui.items." + path + ".lore")) {
lore.add(color(replace(line, replacements)));
}
meta.setLore(lore);
item.setItemMeta(meta);
}
return item;
}
public String replace(String text, Map<String, String> replacements) {
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
text = text.replace("{" + replacement.getKey() + "}", replacement.getValue());
}
return text;
}
public void playSound(Player player, String key) {
String sound = getConfig().getString("gui.sounds." + key, "UI_BUTTON_CLICK");
XSound.matchXSound(sound).ifPresent(xSound -> xSound.play(player));
}
public AuctionRepository repository() {
return repository;
}
public AuctionGuiListener guiListener() {
return guiListener;
}
public Economy economy() {
return economy;
}
public String serverName() {
return getConfig().getString("server-name", "server-1");