Files
src
build.gradle
CHANGELOG.md
manifest.kod
settings.gradle
Code
12 3,125
src/main/java/com/dreamlands/DreamlandsPlugin.java
package com.dreamlands;
import com.cryptomorin.xseries.XMaterial;
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.command.TabExecutor;
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.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
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.logging.Level;
public final class DreamlandsPlugin extends JavaPlugin implements Listener, TabExecutor {
private StorageManager storage;
private EconomyService economy;
private DreamlandsItemPricingService pricing;
private YamlConfiguration shop;
private final Map<UUID, Long> sessionStarted = new ConcurrentHashMap<>();
private final Map<UUID, Long> sellAllConfirmations = new ConcurrentHashMap<>();
private final Map<UUID, Scoreboard> scoreboards = new HashMap<>();
private long lastLeaderboardRefresh;
private StorageManager.BalanceEntry richestCache;
private List<StorageManager.BalanceEntry> leaderboardCache = List.of();
private final Map<UUID, Integer> leaderboardRanks = new HashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
saveResource("shop.yml", false);
reloadShop();
try {
if (!getConfig().getBoolean("economy.enabled", true)) throw new IllegalStateException("Economy is disabled in config.yml");
storage = new StorageManager(this);
economy = new EconomyService(this, storage);
pricing = new DreamlandsItemPricingService(this);
getServer().getPluginManager().registerEvents(this, this);
for (String commandName : List.of("balance", "pay", "eco", "baltop", "market", "orders", "shop", "sell", "sellhistory", "shards", "shardshop", "bounty", "bounties")) {
if (getCommand(commandName) == null) throw new IllegalStateException("Command is missing from plugin.yml: " + commandName);
getCommand(commandName).setExecutor(this);
getCommand(commandName).setTabCompleter(this);
}
for (Player player : Bukkit.getOnlinePlayers()) beginSession(player);
int interval = Math.max(20, getConfig().getInt("scoreboard.update-interval", 20));
Bukkit.getScheduler().runTaskTimer(this, () -> {
for (Player player : Bukkit.getOnlinePlayers()) updateScoreboard(player);
}, interval, interval);
Bukkit.getScheduler().runTaskTimer(this, this::flushPlaytime, 1200L, 1200L);
Bukkit.getScheduler().runTaskTimer(this, this::returnExpiredListings, 1200L, 1200L);
Bukkit.getScheduler().runTaskTimer(this, this::expireOrders, 1200L, 1200L);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) new DreamlandsExpansion(this).register();
getLogger().info("Dreamlands economy, persistent storage, marketplace, shop, bounty, and playtime systems initialized.");
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Dreamlands initialization failed", exception);
Bukkit.getPluginManager().disablePlugin(this);
}
}
@Override
public void onDisable() {
if (storage == null) return;
for (Player player : Bukkit.getOnlinePlayers()) finishSession(player);
try {
storage.close();
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Failed to close Dreamlands storage", exception);
}
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
beginSession(event.getPlayer());
try {
storage.expiredListings(event.getPlayer().getUniqueId());
deliverPendingItems(event.getPlayer());
storage.ensureAccount(event.getPlayer().getUniqueId(), event.getPlayer().getName(), BigDecimal.valueOf(getConfig().getLong("economy.starting-balance", 1000)));
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Could not initialize account for " + event.getPlayer().getUniqueId(), exception);
}
Bukkit.getScheduler().runTask(this, () -> updateScoreboard(event.getPlayer()));
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
finishSession(event.getPlayer());
sellAllConfirmations.remove(event.getPlayer().getUniqueId());
scoreboards.remove(event.getPlayer().getUniqueId());
for (Player online : Bukkit.getOnlinePlayers()) updateScoreboard(online);
}
@EventHandler
public void onDeath(PlayerDeathEvent event) {
Player target = event.getEntity();
Player killer = target.getKiller();
if (killer == null || killer.getUniqueId().equals(target.getUniqueId()) || !getConfig().getBoolean("bounty.enabled", true)) return;
try {
BigDecimal claimed = storage.claimBounty(target.getUniqueId(), killer.getUniqueId(), killer.getName(), target.getName(), economy.maximum(), getConfig().getBoolean("bounty.clear-after-claim", true));
if (claimed.signum() <= 0) return;
String amount = format(claimed);
killer.sendMessage(prefix() + color(message("messages.bounty-claimed", "&6{killer} claimed a bounty of {amount} on {player}.")
.replace("{killer}", killer.getName()).replace("{amount}", currency(claimed)).replace("{player}", target.getName())));
target.sendMessage(prefix() + color(message("messages.bounty-claimed", "&6{killer} claimed a bounty of {amount} on {player}.")
.replace("{killer}", killer.getName()).replace("{amount}", currency(claimed)).replace("{player}", target.getName())));
updateAllScoreboards();
getLogger().info("Bounty payout " + amount + " paid to " + killer.getName() + " for killing " + target.getName());
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Failed to claim bounty for target " + target.getUniqueId(), exception);
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!(event.getView().getTopInventory().getHolder() instanceof MenuHolder holder)) return;
if (holder.type.equals("sell")) {
handleSellClick(event, holder);
return;
}
event.setCancelled(true);
if (!(event.getWhoClicked() instanceof Player player) || event.getClickedInventory() != event.getView().getTopInventory()) return;
String action = holder.actions.get(event.getSlot());
if (action == null) return;
try {
if (action.equals("close")) {
player.closeInventory();
} else if (action.equals("sell-confirm")) {
completeSell(player, event.getView().getTopInventory());
} else if (action.equals("sell-all-confirm")) {
completeSellAll(player);
player.closeInventory();
} else if (action.startsWith("order-fill:")) {
fulfillOrderFromHand(player, Long.parseLong(action.substring(11)));
openOrders(player, holder.page, false);
} else if (action.startsWith("order-cancel:")) {
cancelOrder(player, Long.parseLong(action.substring(13)));
openOrders(player, holder.page, true);
} else if (action.equals("orders-mine")) {
openOrders(player, 0, true);
} else if (action.equals("orders-all")) {
openOrders(player, 0, false);
} else if (action.startsWith("orders-page:")) {
openOrders(player, Integer.parseInt(action.substring(12)), holder.type.equals("my-orders"));
} else if (action.startsWith("shardbuy:")) {
buyShardItem(player, action.substring(9));
shardShopCommand(player);
} else if (action.startsWith("sell-history-page:")) {
player.performCommand("sellhistory " + action.substring(18));
} else if (action.startsWith("market:")) {
buyListing(player, Long.parseLong(action.substring(7)));
openMarket(player, 0);
} else if (action.startsWith("cancel:")) {
cancelListing(player, Long.parseLong(action.substring(7)));
openMyListings(player);
} else if (action.startsWith("shopbuy:")) {
shopTransaction(player, action.substring(8), true, 1);
openShop(player, holder.category, holder.page);
} else if (action.startsWith("shopsell:")) {
shopTransaction(player, action.substring(9), false, 1);
openShop(player, holder.category, holder.page);
} else if (action.equals("market-next")) openMarket(player, 1);
else if (action.equals("market-previous")) openMarket(player, 0);
else if (action.equals("my-listings")) openMyListings(player);
else if (action.equals("market-home")) openMarket(player, 0);
else if (action.equals("bounty-next")) openBounties(player, 1);
else if (action.equals("bounty-previous")) openBounties(player, 0);
else if (action.startsWith("market-next:")) openMarket(player, Integer.parseInt(action.substring(12)));
else if (action.startsWith("market-previous:")) openMarket(player, Integer.parseInt(action.substring(16)));
else if (action.startsWith("bounty-next:")) openBounties(player, Integer.parseInt(action.substring(12)));
else if (action.startsWith("bounty-previous:")) openBounties(player, Integer.parseInt(action.substring(16)));