package me.arial.clans;
import com.jeff_media.armorequipevent.ArmorEquipEventManager;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.sql.DataSource;
import me.arial.api.clan.ClanManager;
import me.arial.api.clan.ClanSettings;
import me.arial.api.menu.MenuManager;
import me.arial.api.player.ClanPlayer;
import me.arial.api.player.ClanPlayerManager;
import me.arial.clans.clan.glow.GlowUpdater;
import me.arial.clans.clan.glow.packet.GlowPacket;
import me.arial.clans.clan.impl.ClanManagerImpl;
import me.arial.clans.command.AdminCommandClan;
import me.arial.clans.command.ClanChatCommand;
import me.arial.clans.command.CommandClan;
import me.arial.clans.configuration.ClanSettingsConfig;
import me.arial.clans.configuration.ClansConfig;
import me.arial.clans.configuration.DatabaseConfig;
import me.arial.clans.configuration.KillEntityExpConfig;
import me.arial.clans.configuration.LangConfig;
import me.arial.clans.configuration.PlaceBreakBlockExpConfig;
import me.arial.clans.configuration.menu.ClanGlobalGlowMenuConfig;
import me.arial.clans.configuration.menu.ClanGlowMemberMenuConfig;
import me.arial.clans.configuration.menu.ClanGlowMemberSelectorMenuConfig;
import me.arial.clans.configuration.menu.ClanKickConfirmationMenuConfig;
import me.arial.clans.configuration.menu.ClanLevelBuyAcceptMenuConfig;
import me.arial.clans.configuration.menu.ClanLevelMenuConfig;
import me.arial.clans.configuration.menu.ClanMainMenuConfig;
import me.arial.clans.configuration.menu.ClanMemberMenuConfig;
import me.arial.clans.configuration.menu.ClanStorageMenuConfig;
import me.arial.clans.configuration.menu.ClanTradeItemMenuConfig;
import me.arial.clans.database.ClansDao;
import me.arial.clans.database.ConnectionInfo;
import me.arial.clans.database.mysql.ClansDaoMySQL;
import me.arial.clans.database.sqlite.ClansDaoSQLite;
import me.arial.clans.listener.GameListener;
import me.arial.clans.menu.ClanGlobalGlowMenu;
import me.arial.clans.menu.ClanGlowMemberMenu;
import me.arial.clans.menu.ClanGlowMemberSelectorMenu;
import me.arial.clans.menu.ClanKickConfirmationMenu;
import me.arial.clans.menu.ClanLevelMenu;
import me.arial.clans.menu.ClanLvlBuyAcceptMenu;
import me.arial.clans.menu.ClanMainMenu;
import me.arial.clans.menu.ClanMemberMenu;
import me.arial.clans.menu.ClanMenuManager;
import me.arial.clans.menu.ClanStorageMenu;
import me.arial.clans.menu.ClanTradeResourceMenu;
import me.arial.clans.player.impl.ClanPlayerManagerImpl;
import me.arial.menu.MenuManagerImpl;
import net.milkbowl.vault.economy.Economy;
import org.black_ixx.playerpoints.PlayerPoints;
import org.black_ixx.playerpoints.PlayerPointsAPI;
import org.bukkit.Bukkit;
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 org.jdbi.v3.cache.caffeine.CaffeineCachePlugin;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.async.JdbiExecutor;
import org.jdbi.v3.core.spi.JdbiPlugin;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
public class ClansPlugin extends JavaPlugin {
private Jdbi jdbi;
private JdbiExecutor jdbiExecutor;
private Economy economy;
private PlayerPointsAPI ppAPI;
private ClanSettings clanSettings;
private MenuManager menuManager;
private ClanManager clanManager;
private ClanPlayerManager playerManager;
private ClanMenuManager clanMenuManager;
private GlowUpdater glowUpdater;
private LangConfig langConfig;
private ClansConfig clansConfig;
private static ClansPlugin instance;
private ClanMainMenuConfig mainMenuConfig;
private ClanSettingsConfig settingsConfig;
private ClanLevelMenuConfig levelMenuConfig;
private ClanTradeItemMenuConfig tradeItemMenuConfig;
private ClanLevelBuyAcceptMenuConfig levelBuyAcceptMenuConfig;
private ClanStorageMenuConfig clanStorageMenuConfig;
private PlaceBreakBlockExpConfig placeBreakBlockExpConfig;
private KillEntityExpConfig killEntityExpConfig;
private ClanMemberMenuConfig memberMenuConfig;
private ClanGlobalGlowMenuConfig globalGlowMenuConfig;
private ClanGlowMemberSelectorMenuConfig glowMemberSelectorMenuConfig;
private ClanGlowMemberMenuConfig glowMemberMenuConfig;
private ClanKickConfirmationMenuConfig kickConfirmationMenuConfig;
private GameListener gameListener;
private CommandClan commandClan;
private AdminCommandClan adminCommandClan;
private ClanChatCommand clanChatCommand;
private ClanMemberMenu clanMemberMenu;
private ClanTradeResourceMenu tradeResourceMenu;
private ClanLvlBuyAcceptMenu lvlBuyAcceptMenu;
private ClanLevelMenu clanLevelMenu;
private ClanGlowMemberMenu glowMemberMenu;
private ClanGlowMemberSelectorMenu glowMemberSelectorMenu;
private ClanGlobalGlowMenu clanGlobalGlowMenu;
private ClanMainMenu clanMainMenu;
private ClanKickConfirmationMenu kickConfirmationMenu;
private Map<File, String> fileHashes = new HashMap<>();
private boolean isAutoReloadEnabled = false;
public void onEnable() {
getLogger().info(new String(Base64.getDecoder().decode("0J/Qu9Cw0LPQuNC9INCx0YvQuyDRgdC70LjRgiDQutCw0L3QsNC70L7QvCDCq9Cf0L7Qt9C+0YAgSmF2YSBDb21tdW5pdHnCuyAoaHR0cHM6Ly90Lm1lL3Bvem9yamF2YSk=")));
instance = this;
ArmorEquipEventManager.registerListener(this); // <---------------------------------------------
if (!getDataFolder().exists())
getDataFolder().mkdirs();
File menuFolder = new File(getDataFolder(), "menu");
if (!menuFolder.exists())
menuFolder.mkdirs();
if (!setupEconomy()) {
getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", new Object[] { getDescription().getName() }));
getServer().getPluginManager().disablePlugin((Plugin)this);
return;
}
if (!setUpPlayerPoints()) {
getLogger().severe(String.format("[%s] - Disabled due to no PlayerPoints dependency found!", new Object[] { getDescription().getName() }));
getServer().getPluginManager().disablePlugin((Plugin)this);
return;
}
DatabaseConfig databaseConfig = new DatabaseConfig((Plugin)this);
initDB(databaseConfig.getConnectionInfo());
this.clansConfig = new ClansConfig((Plugin)this, this.jdbiExecutor, this.clanManager);
this.langConfig = new LangConfig((Plugin)this);
this.mainMenuConfig = new ClanMainMenuConfig((Plugin)this);
this.settingsConfig = new ClanSettingsConfig((Plugin)this);
this.levelMenuConfig = new ClanLevelMenuConfig((Plugin)this, this.settingsConfig);
this.tradeItemMenuConfig = new ClanTradeItemMenuConfig((Plugin)this);
this.levelBuyAcceptMenuConfig = new ClanLevelBuyAcceptMenuConfig((Plugin)this);
this.clanStorageMenuConfig = new ClanStorageMenuConfig((Plugin)this);
this.placeBreakBlockExpConfig = new PlaceBreakBlockExpConfig((Plugin)this);
this.killEntityExpConfig = new KillEntityExpConfig((Plugin)this);
this.memberMenuConfig = new ClanMemberMenuConfig((Plugin)this);
this.globalGlowMenuConfig = new ClanGlobalGlowMenuConfig((Plugin)this);
this.glowMemberSelectorMenuConfig = new ClanGlowMemberSelectorMenuConfig((Plugin)this);
this.glowMemberMenuConfig = new ClanGlowMemberMenuConfig((Plugin)this);
this.kickConfirmationMenuConfig = new ClanKickConfirmationMenuConfig((Plugin)this);
this.menuManager = (MenuManager)new MenuManagerImpl((Plugin)this);
this.menuManager.clickSound(this.clansConfig.getMenuItemClickSound());
this.clanSettings = new ClanSettings(this.settingsConfig);
ClanStorageMenu clanStorageMenu = new ClanStorageMenu((Plugin)this, this.clanStorageMenuConfig, this.menuManager, () -> this.clanMenuManager, this.langConfig);
this.clanManager = (ClanManager)new ClanManagerImpl(this.jdbiExecutor, this.clanSettings, clanStorageMenu, this.clansConfig);
this.playerManager = (ClanPlayerManager)new ClanPlayerManagerImpl(this.jdbiExecutor, this.langConfig);
this.glowUpdater = new GlowUpdater(this.playerManager, new GlowPacket());
this.clanMemberMenu = new ClanMemberMenu(this.memberMenuConfig, this.menuManager, this.playerManager, this.jdbiExecutor, this.langConfig);
this.tradeResourceMenu = new ClanTradeResourceMenu(this.tradeItemMenuConfig, this.menuManager, this.langConfig);
this.lvlBuyAcceptMenu = new ClanLvlBuyAcceptMenu(this.levelBuyAcceptMenuConfig, this.menuManager, this.langConfig, this.ppAPI);
this.clanLevelMenu = new ClanLevelMenu(this.levelMenuConfig, this.tradeResourceMenu, this.lvlBuyAcceptMenu, this.menuManager, this.jdbiExecutor, this.settingsConfig.getSettings().values());
this.glowMemberMenu = new ClanGlowMemberMenu(this.glowMemberMenuConfig, this.menuManager, this.playerManager, this.glowUpdater, this.jdbiExecutor, this.langConfig);
this.glowMemberSelectorMenu = new ClanGlowMemberSelectorMenu(this.glowMemberSelectorMenuConfig, this.glowMemberMenu, this.menuManager, this.playerManager, this.langConfig, this.jdbiExecutor);
this.clanGlobalGlowMenu = new ClanGlobalGlowMenu(this.globalGlowMenuConfig, this.glowMemberSelectorMenu, this.menuManager, this.playerManager, this.glowUpdater);
this.clanMainMenu = new ClanMainMenu(this.menuManager, this.clanMemberMenu, this.clanLevelMenu, this.clanGlobalGlowMenu, this.playerManager, this.mainMenuConfig, this.jdbiExecutor, this.langConfig, this.clansConfig);
this.kickConfirmationMenu = new ClanKickConfirmationMenu(this.kickConfirmationMenuConfig, this.menuManager, this.playerManager, this.langConfig, this.glowUpdater);
this.clanMenuManager = new ClanMenuManager(this.clanMainMenu, this.clanMemberMenu);
this.gameListener = new GameListener((Plugin)this, this.playerManager, this.clanManager, this.placeBreakBlockExpConfig, this.killEntityExpConfig, this.glowUpdater, this.clansConfig);
this.commandClan = new CommandClan(this, this.langConfig, this.clansConfig, this.playerManager, this.clanManager, this.clanMenuManager, this.economy, this.glowUpdater, this.jdbiExecutor, this.kickConfirmationMenu);
this.adminCommandClan = new AdminCommandClan(this, this.langConfig, this.clansConfig, this.playerManager, this.clanManager, this.clanMenuManager, this.economy, this.glowUpdater, this.jdbiExecutor, this.kickConfirmationMenu);
this.clanChatCommand = new ClanChatCommand(this, this.langConfig, this.clansConfig, this.playerManager);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null)
(new Placeholders()).register();
Bukkit.getOnlinePlayers().forEach(player -> {
if (!this.playerManager.hasPlayer(player))
this.playerManager.register(player, this.clanManager).exceptionally(()).toCompletableFuture().join();
});
startHashTask();
}
public void onDisable() {
if (this.menuManager != null)
this.menuManager.onDisable();
}
private void startHashTask() {
this.isAutoReloadEnabled = this.clansConfig.isAutoReloadEnabled();
loadHashes(getDataFolder());
BukkitRunnable runnable = new BukkitRunnable() {
public void run() {
if (ClansPlugin.this.isAutoReloadEnabled)
ClansPlugin.this.checkFiles();
}
};
runnable.runTaskTimerAsynchronously((Plugin)this, 0L, 20L);
}
private void loadHashes(File dir) {
for (File file : dir.listFiles()) {
if (!file.getName().equals("ClansDatabase.db") && !file.getName().equals("DatabaseConfig.yml"))
if (file.isDirectory()) {
loadHashes(file);
} else {
String hash = getFileChecksum(file);
this.fileHashes.put(file, hash);
}
}
}
private void checkFiles() {
for (Map.Entry<File, String> entry : this.fileHashes.entrySet()) {
File file = entry.getKey();
if (!file.getName().equals("ClansDatabase.db") && !file.getName().equals("DatabaseConfig.yml")) {
String currentHash = getFileChecksum(file);
if (!currentHash.equals(entry.getValue())) {
getLogger().info("Замечены изменения в \"" + file.getPath() + "\", перезагрузка плагина");
reload();
this.fileHashes.put(file, currentHash);
}
}
}
}
private String getFileChecksum(File file) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
FileInputStream fis = new FileInputStream(file);
byte[] byteArray = new byte[1024];
int bytesCount;
while ((bytesCount = fis.read(byteArray)) != -1)
digest.update(byteArray, 0, bytesCount);
fis.close();
byte[] bytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(Integer.toString((b & 0xFF) + 256, 16).substring(1));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void reload() {
this.clansConfig.reload();
this.langConfig.reload();
this.mainMenuConfig.reload();
this.settingsConfig.reload();
this.levelMenuConfig.reload();
this.tradeItemMenuConfig.reload();
this.levelBuyAcceptMenuConfig.reload();
this.clanStorageMenuConfig.reload();
this.placeBreakBlockExpConfig.reload();
this.killEntityExpConfig.reload();
this.memberMenuConfig.reload();
this.globalGlowMenuConfig.reload();
this.glowMemberSelectorMenuConfig.reload();
this.glowMemberMenuConfig.reload();
this.kickConfirmationMenuConfig.reload();
this.clanMemberMenu.reload(this.memberMenuConfig, this.menuManager, this.playerManager, this.jdbiExecutor, this.langConfig);
this.tradeResourceMenu.reload(this.tradeItemMenuConfig, this.menuManager, this.langConfig);
this.lvlBuyAcceptMenu.reload(this.levelBuyAcceptMenuConfig, this.menuManager, this.langConfig, this.ppAPI);
this.clanLevelMenu.reload(this.levelMenuConfig, this.tradeResourceMenu, this.lvlBuyAcceptMenu, this.menuManager, this.jdbiExecutor, this.settingsConfig.getSettings().values());
this.glowMemberMenu.reload(this.glowMemberMenuConfig, this.menuManager, this.playerManager, this.glowUpdater, this.jdbiExecutor, this.langConfig);
this.glowMemberSelectorMenu.reload(this.glowMemberSelectorMenuConfig, this.glowMemberMenu, this.menuManager, this.playerManager, this.langConfig, this.jdbiExecutor);
this.clanGlobalGlowMenu.reload(this.globalGlowMenuConfig, this.glowMemberSelectorMenu, this.menuManager, this.playerManager, this.glowUpdater);
this.clanMainMenu.reload(this.menuManager, this.clanMemberMenu, this.clanLevelMenu, this.clanGlobalGlowMenu, this.playerManager, this.mainMenuConfig, this.jdbiExecutor, this.langConfig, this.clansConfig);
this.kickConfirmationMenu.reload(this.kickConfirmationMenuConfig, this.menuManager, this.playerManager, this.langConfig, this.glowUpdater);
this.clanMenuManager.reload(this.clanMainMenu, this.clanMemberMenu);
this.gameListener.reload((Plugin)this, this.playerManager, this.clanManager, this.placeBreakBlockExpConfig, this.killEntityExpConfig, this.glowUpdater, this.clansConfig);
this.commandClan.reload(this, this.langConfig, this.clansConfig, this.playerManager, this.clanManager, this.clanMenuManager, this.economy, this.glowUpdater, this.jdbiExecutor, this.kickConfirmationMenu);
this.adminCommandClan.reload(this, this.langConfig, this.clansConfig, this.playerManager, this.clanManager, this.clanMenuManager, this.economy, this.glowUpdater, this.jdbiExecutor, this.kickConfirmationMenu);
this.clanChatCommand.reload(this, this.langConfig, this.clansConfig, this.playerManager);
this.isAutoReloadEnabled = this.clansConfig.isAutoReloadEnabled();
}
public static ClansPlugin getInstance() {
return instance;
}
public ClansConfig getClansConfig() {
return this.clansConfig;
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null)
return false;
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null)
return false;
this.economy = (Economy)rsp.getProvider();
return (this.economy != null);
}
private boolean setUpPlayerPoints() {
if (Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) {
this.ppAPI = PlayerPoints.getInstance().getAPI();
return true;
}
return false;
}
public JdbiExecutor executor() {
return this.jdbiExecutor;
}
public LangConfig lang() {
return this.langConfig;
}
public ClanPlayerManager playerManager() {
return this.playerManager;
}
public Jdbi getJdbi() {
return this.jdbi;
}
public JdbiExecutor getJdbiExecutor() {
return this.jdbiExecutor;
}
public MenuManager getMenuManager() {
return this.menuManager;
}
public ClanManager getClanManager() {
return this.clanManager;
}
private void initDB(ConnectionInfo info) {
if (info.isMySQL()) {
initMySQL(info);
ClansDao.DAO_CLASS.clazz = ClansDaoMySQL.class;
} else {
initSQLite(info);
ClansDao.DAO_CLASS.clazz = ClansDaoSQLite.class;
}
}
private void initMySQL(ConnectionInfo info) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + info
.host() + ":" + info
.port() + "/" + info
.database() + "?autoReconnect=true&useSSL=false&characterEncoding=utf8");
config.setUsername(info.user());
config.setPassword(info.password());
HikariDataSource dataSource = new HikariDataSource(config);
this.jdbi = Jdbi.create((DataSource)dataSource);
this.jdbi.installPlugin((JdbiPlugin)new CaffeineCachePlugin());
this.jdbi.installPlugin((JdbiPlugin)new SqlObjectPlugin());
try {
InputStream inputStream = getClass().getResourceAsStream("/template.sql");
try {
this.jdbi.useHandle(handle -> handle.createScript(new String(inputStream.readAllBytes())).execute());
this.jdbi.useHandle(handle -> handle.createUpdate("SET @@group_concat_max_len = 1000000").execute());
if (inputStream != null)
inputStream.close();
} catch (Throwable throwable) {
if (inputStream != null)
try {
inputStream.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Executor executor = Executors.newFixedThreadPool(8);
this.jdbiExecutor = JdbiExecutor.create(this.jdbi, executor);
}
private void initSQLite(ConnectionInfo info) {
File db = new File(getDataFolder(), "ClansDatabase.db");
if (!db.exists())
try {
db.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.sqlite.JDBC");
config.setConnectionTestQuery("SELECT 1");
config.setJdbcUrl("jdbc:sqlite:" + db);
HikariDataSource dataSource = new HikariDataSource(config);
this.jdbi = Jdbi.create((DataSource)dataSource);
this.jdbi.installPlugin((JdbiPlugin)new CaffeineCachePlugin());
this.jdbi.installPlugin((JdbiPlugin)new SqlObjectPlugin());
try {
InputStream inputStream = getClass().getResourceAsStream("/template.sql");
try {
this.jdbi.useHandle(handle -> handle.createScript(new String(inputStream.readAllBytes())).execute());
if (inputStream != null)
inputStream.close();
} catch (Throwable throwable) {
if (inputStream != null)
try {
inputStream.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Executor executor = Executors.newFixedThreadPool(8);
this.jdbiExecutor = JdbiExecutor.create(this.jdbi, executor);
}
}