131 lines
4.1 KiB
Java
131 lines
4.1 KiB
Java
package com.alttd.playershops;
|
|
|
|
import com.alttd.playershops.commands.PlayerShopCommand;
|
|
import com.alttd.playershops.config.Config;
|
|
import com.alttd.playershops.config.DatabaseConfig;
|
|
import com.alttd.playershops.config.MessageConfig;
|
|
import com.alttd.playershops.handler.ShopHandler;
|
|
import com.alttd.playershops.listener.*;
|
|
import com.alttd.playershops.shop.ShopType;
|
|
import com.alttd.playershops.storage.database.DatabaseManager;
|
|
import com.alttd.playershops.storage.database.DatabaseHelper;
|
|
import io.papermc.paper.command.brigadier.Commands;
|
|
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
|
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
|
import lombok.Getter;
|
|
import net.milkbowl.vault.economy.Economy;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.util.List;
|
|
|
|
public class PlayerShops extends JavaPlugin {
|
|
|
|
@Getter
|
|
private static PlayerShops instance;
|
|
@Getter
|
|
private Economy econ = null;
|
|
@Getter
|
|
private ShopHandler shopHandler;
|
|
@Getter
|
|
private DatabaseManager databaseManager;
|
|
@Getter
|
|
private DatabaseHelper databaseHelper;
|
|
private ShopListener shopListener;
|
|
private PlayerListener playerListener;
|
|
private TransactionListener transactionListener;
|
|
private GriefPreventionListener griefPreventionListener;
|
|
|
|
@Getter
|
|
private DatabaseConfig databaseConfig;
|
|
|
|
public void onEnable() {
|
|
instance = this;
|
|
if(!setupEconomy()) {
|
|
getLogger().warning("Error loading Vault and economy.\n Disabling plugin");
|
|
this.setEnabled(false);
|
|
return;
|
|
}
|
|
getLogger().info("Hooked into Vault economy provided by " + econ.getName());
|
|
reloadConfigs();
|
|
if(!setupDatabase()) {
|
|
getLogger().warning("Error setting up database connection.\n Disabling plugin");
|
|
this.setEnabled(false);
|
|
return;
|
|
}
|
|
|
|
shopHandler = new ShopHandler(instance);
|
|
|
|
registerListeners();
|
|
registerCommands();
|
|
}
|
|
|
|
public void onDisable() {
|
|
shopHandler.unloadShops();
|
|
unRegisterListeners();
|
|
Bukkit.getScheduler().cancelTasks(this);
|
|
|
|
databaseManager.unload();
|
|
}
|
|
|
|
private boolean setupEconomy() {
|
|
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
|
return false;
|
|
}
|
|
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
|
if (rsp == null) {
|
|
return false;
|
|
}
|
|
econ = rsp.getProvider();
|
|
return true;
|
|
}
|
|
|
|
private boolean setupDatabase() {
|
|
this.databaseManager = new DatabaseManager(this);
|
|
this.databaseHelper = new DatabaseHelper(this, this.databaseManager);
|
|
this.databaseHelper.init();
|
|
return true;
|
|
}
|
|
|
|
public Economy getEconomy() {
|
|
if(econ == null)
|
|
setupEconomy();
|
|
return econ;
|
|
}
|
|
|
|
private void registerListeners() {
|
|
shopListener = new ShopListener(this);
|
|
playerListener = new PlayerListener(this);
|
|
transactionListener = new TransactionListener(this);
|
|
griefPreventionListener = new GriefPreventionListener(this); // TODO hook into GP
|
|
}
|
|
|
|
private void unRegisterListeners() {
|
|
shopListener.unregister();
|
|
playerListener.unregister();
|
|
transactionListener.unregister();
|
|
}
|
|
|
|
private void registerCommands() {
|
|
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
|
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
|
final Commands commands = event.registrar();
|
|
commands.register(new PlayerShopCommand().command(), null, List.of("shop", "shops"));
|
|
});
|
|
}
|
|
|
|
public void reloadConfigs() {
|
|
Config.reload();
|
|
MessageConfig.reload();
|
|
databaseConfig = new DatabaseConfig(Bukkit.getServerName());
|
|
databaseConfig.reload();
|
|
|
|
for (ShopType shopType : ShopType.values()) {
|
|
// preload ShopType to get the configs active
|
|
}
|
|
}
|
|
|
|
}
|