PlayerShops/src/main/java/com/alttd/playershops/PlayerShops.java

124 lines
3.8 KiB
Java
Raw Normal View History

package com.alttd.playershops;
2022-11-12 23:59:53 +00:00
import com.alttd.playershops.commands.PlayerShopCommands;
2022-08-07 15:28:38 +00:00
import com.alttd.playershops.config.Config;
2022-08-13 13:22:00 +00:00
import com.alttd.playershops.config.DatabaseConfig;
import com.alttd.playershops.config.MessageConfig;
2022-08-21 15:36:01 +00:00
import com.alttd.playershops.gui.GuiIcon;
2022-08-07 15:28:38 +00:00
import com.alttd.playershops.handler.ShopHandler;
2022-08-23 21:41:00 +00:00
import com.alttd.playershops.listener.*;
2022-08-13 13:22:00 +00:00
import com.alttd.playershops.shop.ShopType;
2022-08-23 21:41:00 +00:00
import com.alttd.playershops.storage.database.DatabaseManager;
import com.alttd.playershops.storage.database.DatabaseHelper;
2022-07-04 09:04:13 +00:00
import lombok.Getter;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class PlayerShops extends JavaPlugin {
2022-07-04 09:04:13 +00:00
@Getter
private static PlayerShops instance;
2022-07-04 09:04:13 +00:00
@Getter
private Economy econ = null;
2022-07-04 09:04:13 +00:00
@Getter
private ShopHandler shopHandler;
2022-08-13 14:10:23 +00:00
@Getter
private DatabaseManager databaseManager;
2022-08-23 21:41:00 +00:00
@Getter
private DatabaseHelper databaseHelper;
2022-07-05 09:06:39 +00:00
private ShopListener shopListener;
private PlayerListener playerListener;
2022-08-21 22:21:36 +00:00
private TransactionListener transactionListener;
2022-08-23 21:41:00 +00:00
private InventoryListener inventoryListener;
private GriefPreventionListener griefPreventionListener;
2022-07-05 09:06:39 +00:00
public void onEnable() {
instance = this;
if(!setupEconomy()) {
Bukkit.getLogger().warning("Error loading Vault and economy.\n Disabling plugin");
this.setEnabled(false);
return;
}
Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName());
reloadConfigs();
2022-08-23 21:41:00 +00:00
if(!setupDatabase()) {
Bukkit.getLogger().warning("Error setting up database connection.\n Disabling plugin");
this.setEnabled(false);
return;
}
2022-08-13 13:22:00 +00:00
shopHandler = new ShopHandler(instance);
registerListeners();
registerCommands();
2022-08-13 13:22:00 +00:00
}
2022-07-04 09:04:13 +00:00
2022-08-13 13:22:00 +00:00
public void onDisable() {
2022-08-23 21:41:00 +00:00
shopHandler.unloadShops();
2022-08-13 13:22:00 +00:00
unRegisterListeners();
Bukkit.getScheduler().cancelTasks(this);
2022-08-13 14:10:23 +00:00
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;
}
2022-08-23 21:41:00 +00:00
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() {
2022-07-08 08:59:45 +00:00
shopListener = new ShopListener(this);
playerListener = new PlayerListener(this);
2022-08-21 22:21:36 +00:00
transactionListener = new TransactionListener(this);
2022-08-23 21:41:00 +00:00
inventoryListener = new InventoryListener(this);
griefPreventionListener = new GriefPreventionListener(this); // TODO hook into GP
2022-07-05 09:06:39 +00:00
}
2022-08-13 13:22:00 +00:00
private void unRegisterListeners() {
2022-07-05 09:06:39 +00:00
shopListener.unregister();
playerListener.unregister();
2022-08-21 22:21:36 +00:00
transactionListener.unregister();
2022-08-23 21:41:00 +00:00
inventoryListener.unregister();
}
private void registerCommands() {
2022-11-12 23:59:53 +00:00
PlayerShopCommands.registerCommands();
}
public void reloadConfigs() {
Config.reload();
MessageConfig.reload();
2022-08-13 13:22:00 +00:00
DatabaseConfig.reload();
for (ShopType shopType : ShopType.values()) {
// preload ShopType to get the configs active
}
2022-08-21 15:36:01 +00:00
for (GuiIcon guiIcon : GuiIcon.values()) {
// preload to get config values generated
}
}
}