V10Lift/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java

147 lines
6 KiB
Java
Raw Normal View History

package tech.sbdevelopment.v10lift;
2020-01-30 11:50:29 +00:00
2023-08-13 14:19:15 +00:00
import lombok.Getter;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SingleLineChart;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import tech.sbdevelopment.v10lift.commands.V10LiftCommand;
import tech.sbdevelopment.v10lift.commands.V10LiftTabCompleter;
import tech.sbdevelopment.v10lift.listeners.BlockBreakListener;
import tech.sbdevelopment.v10lift.listeners.EntityDamageListener;
import tech.sbdevelopment.v10lift.listeners.PlayerInteractListener;
import tech.sbdevelopment.v10lift.listeners.SignChangeListener;
import tech.sbdevelopment.v10lift.managers.DBManager;
import tech.sbdevelopment.v10lift.managers.DataManager;
import tech.sbdevelopment.v10lift.managers.VaultManager;
import tech.sbdevelopment.v10lift.sbutils.ConfigUpdater;
import tech.sbdevelopment.v10lift.sbutils.UpdateManager;
import tech.sbdevelopment.v10lift.sbutils.YamlFile;
2020-01-30 11:50:29 +00:00
import java.io.IOException;
2020-02-03 13:44:22 +00:00
import java.sql.SQLException;
import java.util.Collections;
2020-02-03 13:44:22 +00:00
2020-01-30 11:50:29 +00:00
public class V10LiftPlugin extends JavaPlugin {
2023-08-13 14:19:15 +00:00
@Getter
2020-01-30 11:50:29 +00:00
private static V10LiftPlugin instance;
2020-02-06 18:46:15 +00:00
private static YamlFile config;
private static DBManager dbManager;
2023-08-13 14:19:15 +00:00
@Getter
private static YamlFile messages;
2020-02-25 09:34:19 +00:00
private static boolean vault = false;
2020-01-30 11:50:29 +00:00
@Override
public void onEnable() {
instance = this;
//Load the config
2020-02-06 18:46:15 +00:00
config = new YamlFile("config");
config.loadDefaults();
2020-01-30 11:50:29 +00:00
//And update config
try {
ConfigUpdater.update(this, "config.yml", config.getJavaFile(), Collections.emptyList());
} catch (IOException e) {
Bukkit.getLogger().warning("[V10Lift] Couldn't update the config.yml. Please check the stacktrace below.");
e.printStackTrace();
}
//Load the messages
messages = new YamlFile("messages");
messages.loadDefaults();
//Load the database
2020-02-06 18:46:15 +00:00
dbManager = new DBManager("data");
2020-02-03 13:44:22 +00:00
try {
dbManager.load();
2020-02-25 12:14:49 +00:00
} catch (SQLException e) {
Bukkit.getLogger().warning("[V10Lift] Couldn't connect to the SQLite database. Please check the stacktrace below.");
2020-02-03 13:44:22 +00:00
e.printStackTrace();
}
2020-02-25 09:34:19 +00:00
//Load vault if found
if (VaultManager.setupPermissions()) {
2020-02-25 12:34:58 +00:00
Bukkit.getLogger().info("[V10Lift] Loading Vault hook for group whitelist support.");
2020-02-25 09:34:19 +00:00
vault = true;
}
//Load the command
2021-03-24 11:15:01 +00:00
getCommand("v10lift").setExecutor(new V10LiftCommand());
getCommand("v10lift").setTabCompleter(new V10LiftTabCompleter());
//Register the listeners
Bukkit.getPluginManager().registerEvents(new PlayerInteractListener(), this);
Bukkit.getPluginManager().registerEvents(new BlockBreakListener(), this);
Bukkit.getPluginManager().registerEvents(new SignChangeListener(), this);
Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this);
//Load metrics
2020-02-25 12:34:58 +00:00
Bukkit.getLogger().info("[V10Lift] Loading metrics. Can be disabled in the global bStats config.");
Metrics metrics = new Metrics(this, 6564);
2021-06-08 14:36:16 +00:00
metrics.addCustomChart(new SingleLineChart("lifts", () -> DataManager.getLifts().size()));
//Load the update checker
if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) {
UpdateManager updateManager = new UpdateManager(this, 72317);
updateManager.handleResponse((versionResponse, version) -> {
switch (versionResponse) {
case FOUND_NEW:
Bukkit.getLogger().warning("[V10Lift] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version.get());
if (getSConfig().getFile().getBoolean("UpdateChecker.DownloadOnUpdate")) {
Bukkit.getLogger().info("[V10Lift] Trying to download the update. This could take some time...");
updateManager.handleDownloadResponse((downloadResponse, fileName) -> {
switch (downloadResponse) {
case DONE:
Bukkit.getLogger().info("[V10Lift] Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName);
break;
case ERROR:
Bukkit.getLogger().severe("[V10Lift] Something went wrong when trying downloading the latest version.");
break;
case UNAVAILABLE:
Bukkit.getLogger().warning("[V10Lift] Unable to download the latest version.");
break;
}
}).runUpdate();
2020-04-15 10:50:04 +00:00
}
break;
case LATEST:
Bukkit.getLogger().info("[V10Lift] You are running the latest version [" + this.getDescription().getVersion() + "]!");
break;
case THIS_NEWER:
Bukkit.getLogger().info("[V10Lift] You are running a newer version [" + this.getDescription().getVersion() + "]! This is probably fine.");
break;
case UNAVAILABLE:
Bukkit.getLogger().severe("[V10Lift] Unable to perform an update check.");
break;
}
}).check();
}
2020-02-03 13:44:22 +00:00
Bukkit.getLogger().info("[V10Lift] Plugin loaded successfully!");
2020-01-30 11:50:29 +00:00
}
@Override
public void onDisable() {
dbManager.save();
dbManager.closeConnection();
2020-02-03 17:30:41 +00:00
instance = null;
2020-01-30 11:50:29 +00:00
}
2020-02-06 18:46:15 +00:00
public static YamlFile getSConfig() {
2020-01-30 11:50:29 +00:00
return config;
}
2020-02-03 14:40:49 +00:00
public static DBManager getDBManager() {
return dbManager;
}
2020-02-25 09:34:19 +00:00
public static boolean isVaultEnabled() {
return vault;
}
2020-01-30 11:50:29 +00:00
}