mirror of
https://github.com/EpicEricEE/ShopChest.git
synced 2024-10-02 00:57:42 +02:00
599 lines
20 KiB
Java
599 lines
20 KiB
Java
package de.epiceric.shopchest;
|
|
|
|
import com.palmergames.bukkit.towny.Towny;
|
|
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
|
import com.wasteofplastic.askyblock.ASkyBlock;
|
|
import de.epiceric.shopchest.command.ShopCommand;
|
|
import de.epiceric.shopchest.config.Config;
|
|
import de.epiceric.shopchest.config.HologramFormat;
|
|
import de.epiceric.shopchest.config.Placeholder;
|
|
import de.epiceric.shopchest.event.ShopInitializedEvent;
|
|
import de.epiceric.shopchest.external.PlotSquaredShopFlag;
|
|
import de.epiceric.shopchest.language.LanguageUtils;
|
|
import de.epiceric.shopchest.language.LocalizedMessage;
|
|
import de.epiceric.shopchest.listeners.*;
|
|
import de.epiceric.shopchest.nms.JsonBuilder;
|
|
import de.epiceric.shopchest.shop.Shop;
|
|
import de.epiceric.shopchest.shop.Shop.ShopType;
|
|
import de.epiceric.shopchest.sql.Database;
|
|
import de.epiceric.shopchest.sql.MySQL;
|
|
import de.epiceric.shopchest.sql.SQLite;
|
|
import de.epiceric.shopchest.utils.*;
|
|
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
|
|
import de.epiceric.shopchest.external.WorldGuardShopFlag;
|
|
import fr.xephi.authme.AuthMe;
|
|
import me.ryanhamshire.GriefPrevention.GriefPrevention;
|
|
import me.wiefferink.areashop.AreaShop;
|
|
import net.milkbowl.vault.economy.Economy;
|
|
import org.bstats.Metrics;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.World;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
import pl.islandworld.IslandWorld;
|
|
import us.talabrek.ultimateskyblock.api.uSkyBlockAPI;
|
|
|
|
import java.io.*;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.HashMap;
|
|
|
|
public class ShopChest extends JavaPlugin {
|
|
|
|
private static ShopChest instance;
|
|
|
|
private Config config;
|
|
private HologramFormat hologramFormat;
|
|
private ShopCommand shopCommand;
|
|
private Economy econ = null;
|
|
private Database database;
|
|
private boolean isUpdateNeeded = false;
|
|
private String latestVersion = "";
|
|
private String downloadLink = "";
|
|
private ShopUtils shopUtils;
|
|
private FileWriter fw;
|
|
private WorldGuardPlugin worldGuard;
|
|
private Towny towny;
|
|
private AuthMe authMe;
|
|
private uSkyBlockAPI uSkyBlock;
|
|
private ASkyBlock aSkyBlock;
|
|
private IslandWorld islandWorld;
|
|
private GriefPrevention griefPrevention;
|
|
private AreaShop areaShop;
|
|
private ShopUpdater updater;
|
|
|
|
/**
|
|
* @return An instance of ShopChest
|
|
*/
|
|
public static ShopChest getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* Sets up the economy of Vault
|
|
* @return Whether an economy plugin has been registered
|
|
*/
|
|
private boolean setupEconomy() {
|
|
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
|
if (rsp == null) {
|
|
return false;
|
|
}
|
|
econ = rsp.getProvider();
|
|
return econ != null;
|
|
}
|
|
|
|
@Override
|
|
public void onLoad() {
|
|
instance = this;
|
|
|
|
config = new Config(this);
|
|
|
|
if (config.enable_debug_log) {
|
|
File debugLogFile = new File(getDataFolder(), "debug.txt");
|
|
|
|
try {
|
|
if (!debugLogFile.exists()) {
|
|
debugLogFile.createNewFile();
|
|
}
|
|
|
|
new PrintWriter(debugLogFile).close();
|
|
|
|
fw = new FileWriter(debugLogFile, true);
|
|
} catch (IOException e) {
|
|
getLogger().info("Failed to instantiate FileWriter");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
debug("Loading ShopChest version " + getDescription().getVersion());
|
|
|
|
Plugin worldGuardPlugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
|
|
if (worldGuardPlugin instanceof WorldGuardPlugin) {
|
|
worldGuard = (WorldGuardPlugin) worldGuardPlugin;
|
|
WorldGuardShopFlag.register(this, true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
debug("Enabling ShopChest version " + getDescription().getVersion());
|
|
|
|
if (!getServer().getPluginManager().isPluginEnabled("Vault")) {
|
|
debug("Could not find plugin \"Vault\"");
|
|
getLogger().severe("Could not find plugin \"Vault\"");
|
|
getServer().getPluginManager().disablePlugin(this);
|
|
return;
|
|
}
|
|
|
|
if (!setupEconomy()) {
|
|
debug("Could not find any Vault economy dependency!");
|
|
getLogger().severe("Could not find any Vault economy dependency!");
|
|
getServer().getPluginManager().disablePlugin(this);
|
|
return;
|
|
}
|
|
|
|
switch (Utils.getServerVersion()) {
|
|
case "v1_8_R1":
|
|
case "v1_8_R2":
|
|
case "v1_8_R3":
|
|
case "v1_9_R1":
|
|
case "v1_9_R2":
|
|
case "v1_10_R1":
|
|
case "v1_11_R1":
|
|
case "v1_12_R1":
|
|
break;
|
|
default:
|
|
debug("Server version not officially supported: " + Utils.getServerVersion() + "!");
|
|
debug("Plugin may still work, but more errors are expected!");
|
|
getLogger().warning("Server version not officially supported: " + Utils.getServerVersion() + "!");
|
|
getLogger().warning("Plugin may still work, but more errors are expected!");
|
|
}
|
|
|
|
loadExternalPlugins();
|
|
|
|
debug("Loading utils and extras...");
|
|
LanguageUtils.load();
|
|
|
|
saveResource("item_names.txt", true);
|
|
|
|
File hologramFormatFile = new File(getDataFolder(), "hologram-format.yml");
|
|
if (!hologramFormatFile.exists()) {
|
|
saveResource("hologram-format.yml", false);
|
|
}
|
|
|
|
hologramFormat = new HologramFormat(this);
|
|
|
|
loadMetrics();
|
|
checkForUpdates();
|
|
|
|
shopUtils = new ShopUtils(this);
|
|
shopCommand = new ShopCommand(this);
|
|
|
|
registerListeners();
|
|
initializeShops();
|
|
|
|
updater = new ShopUpdater(this);
|
|
updater.start();
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
debug("Disabling ShopChest...");
|
|
|
|
if (updater != null) {
|
|
debug("Stopping updater");
|
|
updater.cancel();
|
|
}
|
|
|
|
if (database != null) {
|
|
for (Shop shop : shopUtils.getShops()) {
|
|
shopUtils.removeShop(shop, false);
|
|
debug("Removed shop (#" + shop.getID() + ")");
|
|
}
|
|
|
|
database.disconnect();
|
|
}
|
|
|
|
if (fw != null && config.enable_debug_log) {
|
|
try {
|
|
fw.close();
|
|
} catch (IOException e) {
|
|
getLogger().severe("Failed to close FileWriter");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void loadExternalPlugins() {
|
|
if (worldGuard != null && !WorldGuardShopFlag.isLoaded()) {
|
|
WorldGuardShopFlag.register(this, false);
|
|
|
|
try {
|
|
// Reload WorldGuard regions, so that custom flags are applied
|
|
for (World world : getServer().getWorlds()) {
|
|
worldGuard.getRegionManager(world).load();
|
|
}
|
|
} catch (Exception e) {
|
|
getLogger().severe("Failed to reload WorldGuard region manager. WorldGuard support will probably not work!");
|
|
debug("Failed to load WorldGuard region manager");
|
|
debug(e);
|
|
}
|
|
}
|
|
|
|
Plugin townyPlugin = Bukkit.getServer().getPluginManager().getPlugin("Towny");
|
|
if (townyPlugin instanceof Towny) {
|
|
towny = (Towny) townyPlugin;
|
|
}
|
|
|
|
Plugin authMePlugin = Bukkit.getServer().getPluginManager().getPlugin("AuthMe");
|
|
if (authMePlugin instanceof AuthMe) {
|
|
authMe = (AuthMe) authMePlugin;
|
|
}
|
|
|
|
Plugin uSkyBlockPlugin = Bukkit.getServer().getPluginManager().getPlugin("uSkyBlock");
|
|
if (uSkyBlockPlugin instanceof uSkyBlockAPI) {
|
|
uSkyBlock = (uSkyBlockAPI) uSkyBlockPlugin;
|
|
}
|
|
|
|
Plugin aSkyBlockPlugin = Bukkit.getServer().getPluginManager().getPlugin("ASkyBlock");
|
|
if (aSkyBlockPlugin instanceof ASkyBlock) {
|
|
aSkyBlock = (ASkyBlock) aSkyBlockPlugin;
|
|
}
|
|
|
|
Plugin islandWorldPlugin = Bukkit.getServer().getPluginManager().getPlugin("IslandWorld");
|
|
if (islandWorldPlugin instanceof IslandWorld) {
|
|
islandWorld = (IslandWorld) islandWorldPlugin;
|
|
}
|
|
|
|
Plugin griefPreventionPlugin = Bukkit.getServer().getPluginManager().getPlugin("GriefPrevention");
|
|
if (griefPreventionPlugin instanceof GriefPrevention) {
|
|
griefPrevention = (GriefPrevention) griefPreventionPlugin;
|
|
}
|
|
|
|
Plugin areaShopPlugin = Bukkit.getServer().getPluginManager().getPlugin("AreaShop");
|
|
if (areaShopPlugin instanceof AreaShop) {
|
|
areaShop = (AreaShop) areaShopPlugin;
|
|
}
|
|
|
|
if (hasPlotSquared()) {
|
|
new PlotSquaredShopFlag().register(this);
|
|
}
|
|
}
|
|
|
|
private void loadMetrics() {
|
|
debug("Initializing Metrics...");
|
|
Metrics metrics = new Metrics(this);
|
|
|
|
metrics.addCustomChart(new Metrics.AdvancedPie("shop_type") {
|
|
@Override
|
|
public HashMap<String, Integer> getValues(HashMap<String, Integer> hashMap) {
|
|
int normal = 0;
|
|
int admin = 0;
|
|
|
|
for (Shop shop : shopUtils.getShops()) {
|
|
if (shop.getShopType() == ShopType.NORMAL) normal++;
|
|
else if (shop.getShopType() == ShopType.ADMIN) admin++;
|
|
}
|
|
|
|
hashMap.put("Admin", admin);
|
|
hashMap.put("Normal", normal);
|
|
|
|
return hashMap;
|
|
}
|
|
});
|
|
|
|
metrics.addCustomChart(new Metrics.SimplePie("database_type") {
|
|
@Override
|
|
public String getValue() {
|
|
return config.database_type.toString();
|
|
}
|
|
});
|
|
|
|
if (config.database_type == Database.DatabaseType.SQLite) {
|
|
debug("Using database type: SQLite");
|
|
getLogger().info("Using SQLite");
|
|
database = new SQLite(this);
|
|
} else {
|
|
debug("Using database type: MySQL");
|
|
getLogger().info("Using MySQL");
|
|
database = new MySQL(this);
|
|
if (config.database_mysql_ping_interval > 0) {
|
|
Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (database instanceof MySQL) {
|
|
((MySQL) database).ping();
|
|
}
|
|
}
|
|
}, config.database_mysql_ping_interval * 20L, config.database_mysql_ping_interval * 20L);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkForUpdates() {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
UpdateChecker uc = new UpdateChecker(ShopChest.this);
|
|
UpdateCheckerResult result = uc.check();
|
|
|
|
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CHECKING));
|
|
if (result == UpdateCheckerResult.TRUE) {
|
|
latestVersion = uc.getVersion();
|
|
downloadLink = uc.getLink();
|
|
isUpdateNeeded = true;
|
|
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedPlaceholder(Placeholder.VERSION, latestVersion)));
|
|
|
|
for (Player p : getServer().getOnlinePlayers()) {
|
|
if (p.hasPermission(Permissions.UPDATE_NOTIFICATION)) {
|
|
JsonBuilder jb = new JsonBuilder(ShopChest.this, LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedPlaceholder(Placeholder.VERSION, latestVersion)), LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CLICK_TO_DOWNLOAD), downloadLink);
|
|
jb.sendJson(p);
|
|
}
|
|
}
|
|
|
|
} else if (result == UpdateCheckerResult.FALSE) {
|
|
latestVersion = "";
|
|
downloadLink = "";
|
|
isUpdateNeeded = false;
|
|
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_NO_UPDATE));
|
|
} else {
|
|
latestVersion = "";
|
|
downloadLink = "";
|
|
isUpdateNeeded = false;
|
|
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_ERROR));
|
|
}
|
|
}
|
|
}.runTaskAsynchronously(this);
|
|
}
|
|
|
|
private void registerListeners() {
|
|
debug("Registering listeners...");
|
|
getServer().getPluginManager().registerEvents(new ShopUpdateListener(this), this);
|
|
getServer().getPluginManager().registerEvents(new ShopItemListener(this), this);
|
|
getServer().getPluginManager().registerEvents(new ShopInteractListener(this), this);
|
|
getServer().getPluginManager().registerEvents(new NotifyPlayerOnJoinListener(this), this);
|
|
getServer().getPluginManager().registerEvents(new ChestProtectListener(this, worldGuard), this);
|
|
|
|
if (!Utils.getServerVersion().equals("v1_8_R1")) {
|
|
getServer().getPluginManager().registerEvents(new BlockExplodeListener(this), this);
|
|
}
|
|
|
|
if (hasWorldGuard()) {
|
|
getServer().getPluginManager().registerEvents(new WorldGuardListener(this), this);
|
|
|
|
if (hasAreaShop()) {
|
|
getServer().getPluginManager().registerEvents(new AreaShopListener(this), this);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initializes the shops
|
|
*/
|
|
private void initializeShops() {
|
|
debug("Initializing Shops...");
|
|
shopUtils.reloadShops(false, true, new Callback(this) {
|
|
@Override
|
|
public void onResult(Object result) {
|
|
if (result instanceof Integer) {
|
|
int count = (int) result;
|
|
Bukkit.getServer().getPluginManager().callEvent(new ShopInitializedEvent(count));
|
|
getLogger().info("Initialized " + count + " Shops");
|
|
debug("Initialized " + count + " Shops");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Print a message to the <i>/plugins/ShopChest/debug.txt</i> file
|
|
* @param message Message to print
|
|
*/
|
|
public void debug(String message) {
|
|
if (config.enable_debug_log && fw != null) {
|
|
try {
|
|
Calendar c = Calendar.getInstance();
|
|
String timestamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(c.getTime());
|
|
fw.write(String.format("[%s] %s\r\n", timestamp, message));
|
|
fw.flush();
|
|
} catch (IOException e) {
|
|
getLogger().severe("Failed to print debug message.");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Print a {@link Throwable}'s stacktrace to the <i>/plugins/ShopChest/debug.txt</i> file
|
|
* @param throwable {@link Throwable} whose stacktrace will be printed
|
|
*/
|
|
public void debug(Throwable throwable) {
|
|
if (config.enable_debug_log && fw != null) {
|
|
PrintWriter pw = new PrintWriter(fw);
|
|
throwable.printStackTrace(pw);
|
|
pw.flush();
|
|
}
|
|
}
|
|
|
|
public HologramFormat getHologramFormat() {
|
|
return hologramFormat;
|
|
}
|
|
|
|
public ShopCommand getShopCommand() {
|
|
return shopCommand;
|
|
}
|
|
|
|
/**
|
|
* @return The {@link ShopUpdater} that schedules hologram and item updates
|
|
*/
|
|
public ShopUpdater getUpdater() {
|
|
return updater;
|
|
}
|
|
|
|
/**
|
|
* Set the {@link ShopUpdater} that schedules hologram and item updates
|
|
*/
|
|
public void setUpdater(ShopUpdater updater) {
|
|
this.updater = updater;
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'AreaShop' is enabled
|
|
*/
|
|
public boolean hasAreaShop() {
|
|
return areaShop != null && areaShop.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'GriefPrevention' is enabled
|
|
*/
|
|
public boolean hasGriefPrevention() {
|
|
return griefPrevention != null && griefPrevention.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return An instance of {@link GriefPrevention} or {@code null} if GriefPrevention is not enabled
|
|
*/
|
|
public GriefPrevention getGriefPrevention() {
|
|
return griefPrevention;
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'IslandWorld' is enabled
|
|
*/
|
|
public boolean hasIslandWorld() {
|
|
return islandWorld != null && islandWorld.isEnabled();
|
|
}
|
|
/**
|
|
* @return Whether the plugin 'ASkyBlock' is enabled
|
|
*/
|
|
public boolean hasASkyBlock() {
|
|
return aSkyBlock != null && aSkyBlock.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'uSkyBlock' is enabled
|
|
*/
|
|
public boolean hasUSkyBlock() {
|
|
return uSkyBlock != null && uSkyBlock.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return An instance of {@link uSkyBlockAPI} or {@code null} if uSkyBlock is not enabled
|
|
*/
|
|
public uSkyBlockAPI getUSkyBlock() {
|
|
return uSkyBlock;
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'PlotSquared' is enabled
|
|
*/
|
|
public boolean hasPlotSquared() {
|
|
Plugin p = getServer().getPluginManager().getPlugin("PlotSquared");
|
|
return p != null && p.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'AuthMe' is enabled
|
|
*/
|
|
public boolean hasAuthMe() {
|
|
return authMe != null && authMe.isEnabled();
|
|
}
|
|
/**
|
|
* @return Whether the plugin 'Towny' is enabled
|
|
*/
|
|
public boolean hasTowny() {
|
|
return towny != null && towny.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return Whether the plugin 'WorldGuard' is enabled
|
|
*/
|
|
public boolean hasWorldGuard() {
|
|
return worldGuard != null && worldGuard.isEnabled();
|
|
}
|
|
|
|
/**
|
|
* @return An instance of {@link WorldGuardPlugin} or {@code null} if WorldGuard is not enabled
|
|
*/
|
|
public WorldGuardPlugin getWorldGuard() {
|
|
return worldGuard;
|
|
}
|
|
|
|
/**
|
|
* @return ShopChest's {@link ShopUtils} containing some important methods
|
|
*/
|
|
public ShopUtils getShopUtils() {
|
|
return shopUtils;
|
|
}
|
|
|
|
/**
|
|
* @return Registered Economy of Vault
|
|
*/
|
|
public Economy getEconomy() {
|
|
return econ;
|
|
}
|
|
|
|
/**
|
|
* @return ShopChest's shop database
|
|
*/
|
|
public Database getShopDatabase() {
|
|
return database;
|
|
}
|
|
|
|
/**
|
|
* @return Whether an update is needed (will return false if not checked)
|
|
*/
|
|
public boolean isUpdateNeeded() {
|
|
return isUpdateNeeded;
|
|
}
|
|
|
|
/**
|
|
* Set whether an update is needed
|
|
* @param isUpdateNeeded Whether an update should be needed
|
|
*/
|
|
public void setUpdateNeeded(boolean isUpdateNeeded) {
|
|
this.isUpdateNeeded = isUpdateNeeded;
|
|
}
|
|
|
|
/**
|
|
* @return The latest version of ShopChest (will return null if not checked or if no update is available)
|
|
*/
|
|
public String getLatestVersion() {
|
|
return latestVersion;
|
|
}
|
|
|
|
/**
|
|
* Set the latest version
|
|
* @param latestVersion Version to set as latest version
|
|
*/
|
|
public void setLatestVersion(String latestVersion) {
|
|
this.latestVersion = latestVersion;
|
|
}
|
|
|
|
/**
|
|
* @return The download link of the latest version (will return null if not checked or if no update is available)
|
|
*/
|
|
public String getDownloadLink() {
|
|
return downloadLink;
|
|
}
|
|
|
|
/**
|
|
* Set the download Link of the latest version (will return null if not checked or if no update is available)
|
|
* @param downloadLink Link to set as Download Link
|
|
*/
|
|
public void setDownloadLink(String downloadLink) {
|
|
this.downloadLink = downloadLink;
|
|
}
|
|
|
|
/**
|
|
* @return The {@link Config} of ShopChest
|
|
*/
|
|
public Config getShopChestConfig() {
|
|
return config;
|
|
}
|
|
}
|