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

131 lines
4.1 KiB
Java
Raw Normal View History

package com.alttd.playershops;
import com.alttd.playershops.commands.PlayerShopCommand;
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-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;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
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.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
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;
private GriefPreventionListener griefPreventionListener;
2022-07-05 09:06:39 +00:00
2024-04-21 12:06:52 +00:00
@Getter
private DatabaseConfig databaseConfig;
public void onEnable() {
instance = this;
if(!setupEconomy()) {
2025-12-24 20:37:29 +00:00
getLogger().warning("Error loading Vault and economy.\n Disabling plugin");
this.setEnabled(false);
return;
}
2025-12-24 20:37:29 +00:00
getLogger().info("Hooked into Vault economy provided by " + econ.getName());
reloadConfigs();
2022-08-23 21:41:00 +00:00
if(!setupDatabase()) {
2025-12-24 20:37:29 +00:00
getLogger().warning("Error setting up database connection.\n Disabling plugin");
2022-08-23 21:41:00 +00:00
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);
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();
}
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();
2024-04-21 12:06:52 +00:00
databaseConfig = new DatabaseConfig(Bukkit.getServerName());
databaseConfig.reload();
2022-08-13 13:22:00 +00:00
for (ShopType shopType : ShopType.values()) {
// preload ShopType to get the configs active
}
}
}