A one size fits all plugin for vanilla servers running Minecraft Beta 1.7.3
at master 7.2 kB view raw
1package dev.keii.goldenage; 2 3import dev.keii.goldenage.betaprotect.BetaProtect; 4import dev.keii.goldenage.commands.DatabaseCommand; 5import dev.keii.goldenage.commands.HistoryCommand; 6import dev.keii.goldenage.commands.ListCommand; 7import dev.keii.goldenage.commands.SeenCommand; 8import dev.keii.goldenage.config.Config; 9import dev.keii.goldenage.config.ConfigLoader; 10import dev.keii.goldenage.config.Env; 11import dev.keii.goldenage.dao.WorldDao; 12import dev.keii.goldenage.listeners.PlayerJoinListener; 13import dev.keii.goldenage.migration.Migrator; 14import dev.keii.goldenage.statistics.Statistics; 15import dev.keii.goldenage.utils.DatabaseUtility; 16import dev.keii.goldenage.utils.Logger; 17import lombok.Getter; 18import lombok.SneakyThrows; 19import org.bukkit.Bukkit; 20import org.bukkit.World; 21import org.bukkit.plugin.PluginManager; 22import org.bukkit.plugin.java.JavaPlugin; 23 24import java.io.*; 25import java.nio.charset.StandardCharsets; 26import java.sql.SQLException; 27 28public class GoldenAge extends JavaPlugin { 29 @Getter 30 private DatabaseUtility databaseUtility; 31 32 @Getter 33 private Migrator migrator; 34 35 @Getter 36 private static Logger logger; 37 38 @Getter 39 private Config config; 40 41 @Getter 42 private Statistics statistics; 43 44 @SneakyThrows 45 public boolean setupConfig() { 46 File configFile = new File("plugins/GoldenAge/config.yml"); 47 48 try { 49 if (configFile.exists()) { 50 return true; 51 } 52 53 GoldenAge.getLogger().info("Creating config..."); 54 55 InputStream configInputStream = getClassLoader().getResourceAsStream("config.yml"); 56 FileWriter configFileWriter = new FileWriter("plugins/GoldenAge/config.yml"); 57 58 if (configInputStream != null) { 59 try (BufferedReader reader = new BufferedReader( 60 new InputStreamReader(configInputStream, StandardCharsets.UTF_8))) { 61 StringBuilder content = new StringBuilder(); 62 String line; 63 GoldenAge.getLogger().info("Writing defaults to config..."); 64 while ((line = reader.readLine()) != null) { 65 content.append(line).append(System.lineSeparator()); 66 } 67 configFileWriter.write(content.toString()); 68 configFileWriter.close(); 69 } 70 71 configInputStream.close(); 72 GoldenAge.getLogger().info("Config created!"); 73 } 74 return true; 75 } catch (IOException e) { 76 GoldenAge.getLogger().severe("Failed to create config!"); 77 GoldenAge.getLogger().severe(e.getMessage()); 78 GoldenAge.getLogger().severe(e.toString()); 79 Bukkit.getPluginManager().disablePlugin(this); 80 throw e; 81 // return false; 82 } 83 } 84 85 @Override 86 public void onEnable() { 87 GoldenAge.logger = new Logger("[GoldenAge]"); 88 GoldenAge.getLogger().info("Enabling GoldenAge..."); 89 90 if (!getDataFolder().exists()) { 91 getDataFolder().mkdirs(); 92 } 93 94 // Return out of onEnable if setting up config failed 95 if (!setupConfig()) { 96 return; 97 } 98 99 try { 100 GoldenAge.getLogger().info("Loading config."); 101 this.config = ConfigLoader.loadConfig("plugins/GoldenAge/config.yml"); 102 } catch (IOException e) { 103 GoldenAge.getLogger().severe("Failed to read config!"); 104 GoldenAge.getLogger().severe(e.getMessage()); 105 Bukkit.getPluginManager().disablePlugin(this); 106 return; 107 } 108 109 File databaseFile = new File("plugins/GoldenAge/database.db"); 110 111 this.databaseUtility = new DatabaseUtility(this, "jdbc:sqlite:" + databaseFile.getAbsolutePath()); 112 this.databaseUtility.openConnection(); 113 114 this.migrator = new Migrator(this); 115 try { 116 this.migrator.setupMigrationsTable(); 117 } catch (SQLException e) { 118 GoldenAge.getLogger().severe("Failed to create migrations table!"); 119 GoldenAge.getLogger().severe(e.getMessage()); 120 Bukkit.getPluginManager().disablePlugin(this); 121 return; 122 } 123 124 if (config.getDatabase().isAutoMigrate()) { 125 Migrator migrator = new Migrator(this); 126 try { 127 GoldenAge.getLogger().info("Migrating database..."); 128 migrator.migrate(); 129 GoldenAge.getLogger().info("Finished migrating database."); 130 } catch (SQLException e) { 131 GoldenAge.getLogger().severe("Failed to migrate db!"); 132 GoldenAge.getLogger().severe(e.getMessage()); 133 Bukkit.getPluginManager().disablePlugin(this); 134 return; 135 } 136 } 137 138 WorldDao worldDao = new WorldDao(this.getDatabaseUtility()); 139 for (World world : Bukkit.getWorlds()) { 140 dev.keii.goldenage.models.World existingWorld = worldDao.getWorldByUuid(world.getUID()); 141 if (existingWorld != null) 142 continue; 143 144 // Create world in database if it doesn't exist 145 dev.keii.goldenage.models.World dbWorld = new dev.keii.goldenage.models.World(world.getName(), world.getUID()); 146 worldDao.insertWorld(dbWorld); 147 GoldenAge.getLogger().info("Created world '" + world.getName() + "' in database"); 148 } 149 150 if (config.getCommands().getList().isEnabled()) 151 this.getCommand("list").setExecutor(new ListCommand(this)); 152 if (config.getCommands().getSeen().isEnabled()) 153 this.getCommand("seen").setExecutor(new SeenCommand(this)); 154 if (config.getCommands().getHistory().isEnabled()) 155 this.getCommand("history").setExecutor(new HistoryCommand(this)); 156 if (config.getEnv().equals(Env.Development)) 157 this.getCommand("db").setExecutor(new DatabaseCommand(this)); 158 159 PluginManager pm = this.getServer().getPluginManager(); 160 pm.registerEvents(new PlayerJoinListener(this), this); 161 162 if (config.getStatistics().isEnabled()) { 163 this.statistics = new Statistics(this, config.getStatistics().getRemote(), config.getStatistics().getServerId(), config.getStatistics().getServerSecret()); 164 statistics.beginScheduler(); 165 GoldenAge.getLogger().info("Statistics has been enabled!"); 166 } 167 168 if (config.getBetaProtect().isEnabled()) { 169 BetaProtect betaProtect = new BetaProtect(this); 170 betaProtect.registerCommands(); 171 betaProtect.registerListeners(); 172 GoldenAge.getLogger().info("BetaProtect has been enabled!"); 173 } 174 175 GoldenAge.getLogger().info("GoldenAge has been enabled!"); 176 } 177 178 @Override 179 public void onDisable() { 180 if (this.databaseUtility != null) 181 this.databaseUtility.closeConnection(); 182 183 getLogger().info("GoldenAge has been disabled!"); 184 } 185}