Files
src
pom.xml
README.md
Code
13 1,494
src/main/java/com/auraversemc/aurashop/AuraShopPlugin.java
package com.auraversemc.aurashop;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public final class AuraShopPlugin extends JavaPlugin {
private static final List<String> CONFIG_FILES = List.of("config.yml", "categories.yml", "items.yml", "messages.yml");
private YamlConfiguration settings;
private YamlConfiguration categorySettings;
private YamlConfiguration itemSettings;
private YamlConfiguration messages;
private Map<String, ShopCategory> categories = Map.of();
private Map<String, ShopListing> listings = Map.of();
private Economy economy;
private ShopMenus menus;
private final Set<UUID> purchasing = ConcurrentHashMap.newKeySet();
@Override
public void onEnable() {
if (!loadCandidate(true)) {
getServer().getPluginManager().disablePlugin(this);
return;
}
if (getServer().getPluginManager().getPlugin("Vault") == null) {
getLogger().severe("Vault is required. Install Vault and an economy provider such as EssentialsX Economy.");
getServer().getPluginManager().disablePlugin(this);
return;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null || registration.getProvider() == null) {
getLogger().severe("Vault is present, but no economy provider is registered. Enable EssentialsX Economy or another Vault provider.");
getServer().getPluginManager().disablePlugin(this);
return;
}
economy = registration.getProvider();
menus = new ShopMenus(this);
ShopCommand command = new ShopCommand(this);
getCommand("sh").setExecutor(command);
getCommand("sh").setTabCompleter(command);
getCommand("acshop").setExecutor(command);
getCommand("acshop").setTabCompleter(command);
getCommand("aurashop").setExecutor(command);
getCommand("aurashop").setTabCompleter(command);
getServer().getPluginManager().registerEvents(new ShopListener(this, menus), this);
getLogger().info("AuraShop enabled with Vault economy provider: " + economy.getName());
}
private boolean loadCandidate(boolean startup) {
try {
for (String resource : CONFIG_FILES) {
File file = new File(getDataFolder(), resource);
if (!file.exists()) {
saveResource(resource, false);
}
}
YamlConfiguration nextSettings = loadYaml("config.yml");
YamlConfiguration nextCategories = loadYaml("categories.yml");
YamlConfiguration nextItems = loadYaml("items.yml");
YamlConfiguration nextMessages = loadYaml("messages.yml");
Map<String, ShopCategory> parsedCategories = new LinkedHashMap<>();
Map<String, ShopListing> parsedListings = new LinkedHashMap<>();
List<String> errors = validate(nextSettings, nextCategories, nextItems, parsedCategories, parsedListings);
if (!errors.isEmpty()) {
throw new IllegalArgumentException(String.join("; ", errors));
}
settings = nextSettings;
categorySettings = nextCategories;
itemSettings = nextItems;
messages = nextMessages;
categories = Collections.unmodifiableMap(new LinkedHashMap<>(parsedCategories));
listings = Collections.unmodifiableMap(new LinkedHashMap<>(parsedListings));
return true;
} catch (IOException | InvalidConfigurationException | IllegalArgumentException exception) {
getLogger().severe("Could not " + (startup ? "load" : "reload") + " configuration: " + exception.getMessage());
return false;
}
}
private YamlConfiguration loadYaml(String fileName) throws IOException, InvalidConfigurationException {
YamlConfiguration yaml = new YamlConfiguration();
yaml.load(new File(getDataFolder(), fileName));
return yaml;
}
private List<String> validate(YamlConfiguration nextSettings, YamlConfiguration nextCategories,
YamlConfiguration nextItems, Map<String, ShopCategory> parsedCategories,
Map<String, ShopListing> parsedListings) {
List<String> errors = new ArrayList<>();
int categorySize = nextSettings.getInt("gui.categories.size", -1);
int itemSize = nextSettings.getInt("gui.items.size", -1);
int quantitySize = nextSettings.getInt("gui.quantity.size", -1);
int confirmationSize = nextSettings.getInt("gui.confirmation.size", -1);
validateSize("gui.categories.size", categorySize, errors);
validateSize("gui.items.size", itemSize, errors);
validateSize("gui.quantity.size", quantitySize, errors);
validateSize("gui.confirmation.size", confirmationSize, errors);
ConfigurationSection categorySection = nextCategories.getConfigurationSection("categories");
Set<Integer> categorySlots = new HashSet<>();
if (categorySection == null || categorySection.getKeys(false).isEmpty()) {
errors.add("categories.yml must contain at least one category");
} else {
for (String id : categorySection.getKeys(false)) {
String path = "categories." + id + ".";
String name = nextCategories.getString(path + "name", "").trim();
Material icon = matchMaterial(nextCategories.getString(path + "icon", ""));
int slot = nextCategories.getInt(path + "slot", -1);
if (name.isEmpty()) errors.add(path + "name is required");
if (icon == null) errors.add(path + "icon is not a valid Paper material");
if (slot < 0 || slot >= categorySize) errors.add(path + "slot must be inside the categories menu");
else if (!categorySlots.add(slot)) errors.add(path + "slot duplicates another category slot");
if (icon != null && slot >= 0 && slot < categorySize) {
parsedCategories.put(id, new ShopCategory(id, name, icon, slot,
nextCategories.getString(path + "description", "")));
}
}
}
ConfigurationSection itemSection = nextItems.getConfigurationSection("items");
if (itemSection == null || itemSection.getKeys(false).isEmpty()) {
errors.add("items.yml must contain at least one item");
} else {
for (String id : itemSection.getKeys(false)) {
String path = "items." + id + ".";
String categoryId = nextItems.getString(path + "category", "");
String name = nextItems.getString(path + "name", "").trim();
Material material = matchMaterial(nextItems.getString(path + "material", ""));
int amount = nextItems.getInt(path + "quantity", -1);
double price = nextItems.getDouble(path + "buy-price", -1);
if (!parsedCategories.containsKey(categoryId)) errors.add(path + "category does not exist");
if (name.isEmpty()) errors.add(path + "name is required");
if (material == null || material.isAir()) errors.add(path + "material is not a valid buyable material");
if (amount < 1 || amount > 64 || material != null && amount > material.getMaxStackSize()) {
errors.add(path + "quantity must fit in one material stack");
}
if (!Double.isFinite(price) || price <= 0) errors.add(path + "buy-price must be finite and greater than zero");
if (material != null && !material.isAir() && amount >= 1 && amount <= material.getMaxStackSize()
&& Double.isFinite(price) && price > 0 && parsedCategories.containsKey(categoryId) && !name.isEmpty()) {
parsedListings.put(id, new ShopListing(id, categoryId, name, material, amount,
new BigDecimal(Double.toString(price))));
}
}
}
validateMaterial(nextSettings.getString("gui.border.material", ""), "gui.border.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.back.material", ""), "gui.buttons.back.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.close.material", ""), "gui.buttons.close.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.previous.material", ""), "gui.buttons.previous.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.next.material", ""), "gui.buttons.next.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.confirm.material", ""), "gui.buttons.confirm.material", errors);
validateMaterial(nextSettings.getString("gui.buttons.cancel.material", ""), "gui.buttons.cancel.material", errors);
int categoryClose = nextSettings.getInt("gui.buttons.close-slot", -1);
validateMenuSlots(categorySize, "gui.categories", List.of(categoryClose), errors);
if (categorySlots.contains(categoryClose)) errors.add("a category slot conflicts with the categories close button");
List<Integer> itemContentSlots = nextSettings.getIntegerList("gui.items.content-slots");
List<Integer> itemControls = List.of(nextSettings.getInt("gui.buttons.back-slot", -1),
nextSettings.getInt("gui.buttons.previous-slot", -1), categoryClose,
nextSettings.getInt("gui.buttons.next-slot", -1));
validateSlots(itemContentSlots, itemSize, "gui.items.content-slots", errors);
validateMenuSlots(itemSize, "gui.items", itemControls, errors);
if (itemContentSlots.stream().anyMatch(itemControls::contains)) errors.add("gui.items.content-slots overlap navigation buttons");
List<Integer> quantityOptions = nextSettings.getIntegerList("gui.quantity.option-slots");
List<Integer> quantityControls = List.of(nextSettings.getInt("gui.quantity.back-slot", -1),
nextSettings.getInt("gui.quantity.close-slot", -1));
validateSlots(quantityOptions, quantitySize, "gui.quantity.option-slots", errors);
validateMenuSlots(quantitySize, "gui.quantity", quantityControls, errors);
if (quantityOptions.stream().anyMatch(quantityControls::contains)) errors.add("gui.quantity.option-slots overlap navigation buttons");
List<Integer> confirmationControls = List.of(nextSettings.getInt("gui.confirmation.item-slot", -1),