Files
.github
src
pom.xml
README.md
Code
37 2,059
src/main/java/me/itzmetechu/rebirthshop/RebirthShopPlugin.java
package me.itzmetechu.rebirthshop;
import me.itzmetechu.rebirthshop.command.RebirthShopCommand;
import me.itzmetechu.rebirthshop.hook.RebirthShopExpansion;
import me.itzmetechu.rebirthshop.listener.ChestListener;
import me.itzmetechu.rebirthshop.listener.ShopEditorListener;
import me.itzmetechu.rebirthshop.listener.ShopListener;
import me.itzmetechu.rebirthshop.listener.StockListener;
import me.itzmetechu.rebirthshop.service.HologramService;
import me.itzmetechu.rebirthshop.service.Messages;
import me.itzmetechu.rebirthshop.service.PurchaseLimitService;
import me.itzmetechu.rebirthshop.service.ShopService;
import me.itzmetechu.rebirthshop.service.TransactionService;
import me.itzmetechu.rebirthshop.service.WebhookService;
import me.itzmetechu.rebirthshop.storage.ShopRepository;
import me.itzmetechu.rebirthshop.storage.TransactionRepository;
import me.itzmetechu.rebirthshop.storage.YamlTransactionRepository;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class RebirthShopPlugin extends JavaPlugin {
private ExecutorService databaseExecutor;
private ShopRepository repository;
private TransactionRepository transactionRepository;
private ShopService shops;
private Messages messages;
private WebhookService webhooks;
private HologramService holograms;
private TransactionService transactions;
@Override
public void onEnable() {
saveDefaultResources();
Economy economy = resolveEconomy();
if (economy == null) {
getLogger().severe("Vault economy provider not found. RebirthShop has been disabled.");
getServer().getPluginManager().disablePlugin(this);
return;
}
databaseExecutor = Executors.newFixedThreadPool(2, runnable -> {
Thread thread = new Thread(runnable, "RebirthShop-Storage");
thread.setDaemon(true);
return thread;
});
YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
messages = new Messages(new File(getDataFolder(), "messages.yml"), getServer().getPluginManager().isPluginEnabled("PlaceholderAPI"));
webhooks = new WebhookService(new File(getDataFolder(), "webhooks.yml"), getLogger());
repository = new YamlShopRepository(new File(getDataFolder(), "shops.yml"), databaseExecutor);
transactionRepository = new YamlTransactionRepository(new File(getDataFolder(), "transactions.yml"), databaseExecutor);
transactions = new TransactionService(transactionRepository, getLogger());
holograms = new HologramService(this, getLogger(), new File(getDataFolder(), "holograms.yml"), config.getBoolean("holograms.enabled", true));
holograms.detect();
shops = new ShopService(repository, economy, new PurchaseLimitService(), webhooks, getLogger(), holograms, transactions, config.getInt("low-stock-threshold", 10));
getServer().getPluginManager().registerEvents(new ShopListener(shops, messages), this);
getServer().getPluginManager().registerEvents(new StockListener(shops), this);
getServer().getPluginManager().registerEvents(new ChestListener(shops), this);
getServer().getPluginManager().registerEvents(new ShopEditorListener(shops), this);
RebirthShopCommand command = new RebirthShopCommand(shops, transactions, messages, this::reloadPlugin);
for (String name : new String[]{"rebirthshop", "shop"}) {
PluginCommand registered = Objects.requireNonNull(getCommand(name), "Missing command " + name);
registered.setExecutor(command);
registered.setTabCompleter(command);
}
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new RebirthShopExpansion(shops).register();
}
transactionRepository.load().thenAccept(transactions::load);
repository.load().thenAccept(loaded -> getServer().getScheduler().runTask(this, () -> shops.load(loaded)))
.exceptionally(exception -> {
getLogger().severe("Unable to load shops: " + exception.getMessage());
return null;
});
getLogger().info("RebirthShop v1.0.0 enabled. Developer: ItzMeTechu");
}
@Override
public void onDisable() {
if (holograms != null) holograms.removeAll();
if (repository != null) repository.close();
if (transactionRepository != null) transactionRepository.close();
if (databaseExecutor != null) databaseExecutor.shutdown();
}
private void reloadPlugin() {
reloadConfig();
messages.reload();
webhooks.reload();
YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
shops.setLowStockThreshold(config.getInt("low-stock-threshold", 10));
repository.load().thenAccept(loaded -> getServer().getScheduler().runTask(this, () -> shops.load(loaded)));
}
private Economy resolveEconomy() {
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
return registration == null ? null : registration.getProvider();
}
private void saveDefaultResources() {
for (String resource : new String[]{"config.yml", "messages.yml", "shops.yml", "categories.yml", "webhooks.yml", "database.yml", "limits.yml", "schedule.yml", "holograms.yml", "lang.yml", "transactions.yml"}) {
File destination = new File(getDataFolder(), resource);
if (!destination.exists()) saveResource(resource, false);
}
}
}