Files
src
pom.xml
Code
18 1,079
src/main/java/com/sehrawat/shop/SehrawatShopPlugin.java
package com.sehrawat.shop;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public final class SehrawatShopPlugin extends JavaPlugin {
private Economy economy;
private FileConfiguration guiConfig;
private Messages messages;
private ShopManager shopManager;
private ShopMenu shopMenu;
@Override
public void onEnable() {
saveDefaultConfig();
saveResource("messages.yml", false);
saveResource("gui.yml", false);
File shopsDirectory = new File(getDataFolder(), "shops");
if (!shopsDirectory.exists() && !shopsDirectory.mkdirs()) {
getLogger().severe("Could not create the shops configuration directory.");
}
for (String file : new String[]{"wood.yml", "stone.yml", "farming.yml", "ores.yml", "misc.yml"}) {
if (!new File(shopsDirectory, file).exists()) {
saveResource("shops/" + file, false);
}
}
guiConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "gui.yml"));
messages = new Messages(new File(getDataFolder(), "messages.yml"));
shopManager = new ShopManager(this);
hookEconomy();
ShopTransactions transactions = new ShopTransactions(this, economy);
shopMenu = new ShopMenu(this, transactions);
getServer().getPluginManager().registerEvents(shopMenu, this);
getCommand("shop").setExecutor(new ShopCommand(this));
}
private void hookEconomy() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
getLogger().warning("Vault is not installed. Shop buying and selling are disabled until Vault and an economy provider are available.");
return;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null || registration.getProvider() == null) {
getLogger().warning("Vault is installed, but no economy provider is registered. Shop buying and selling are disabled.");
return;
}
economy = registration.getProvider();
getLogger().info("Connected to Vault economy provider: " + economy.getName());
}
public FileConfiguration getGuiConfig() {
return guiConfig;
}
public Messages getMessages() {
return messages;
}
public ShopManager getShopManager() {
return shopManager;
}
public ShopMenu getShopMenu() {
return shopMenu;
}
public String formatPrice(double amount) {
return economy == null ? Double.toString(amount) : economy.format(amount);
}
}