Java tutorial
/* * NexusStrikes * Copyright (C) 2014 AroyoMC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.aroyomc.nexus.strikes; import io.github.pieguy128.corelib.BeanManager; import io.github.pieguy128.corelib.bukkit.AbstractPlugin; import io.github.pieguy128.corelib.shaded.org.apache.commons.dbutils.DbUtils; import org.aroyomc.nexus.strikes.beans.MainConfigBean; import org.aroyomc.nexus.strikes.beans.StrikeDatabaseBean; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import lombok.Getter; import org.aroyomc.nexus.strikes.utils.SqlManager; public class NexusStrikesMain extends AbstractPlugin { @Getter private static NexusStrikesMain instance; @Getter private MainConfigBean configBean; private File configFile; @Getter private StrikeDatabaseBean databaseBean; @Getter private SqlManager sql; @Getter private MainConfigBean.Database.Type databaseType; private File databaseFile; public void doLoad() { instance = this; if (!getDataFolder().exists()) { getDataFolder().mkdirs(); } configFile = new File(getDataFolder(), "config.yml"); databaseFile = new File(getDataFolder(), "database.json"); saveDefaultConfig(); reloadConfig(); if (databaseType == MainConfigBean.Database.Type.FILE) { loadDatabaseFile(); } else if (databaseType == MainConfigBean.Database.Type.SQL) { try { sql = new SqlManager(configBean.getDatabase().getDriverClass(), configBean.getDatabase().getUrl(), configBean.getDatabase().getUsername(), configBean.getDatabase().getPassword()); sql.createTables(); } catch (SQLException | ExecutionException | InterruptedException ex) { getLogger().log(Level.SEVERE, "Could not connect to SQL database. Falling back to file format...", ex); databaseType = MainConfigBean.Database.Type.FILE; loadDatabaseFile(); } } databaseType = configBean.getDatabase().getType(); } public void doEnable() { instance = this; } public void doDisable() { instance = null; saveConfig(); if (databaseType == MainConfigBean.Database.Type.FILE) { saveDatabaseFile(); } else if (databaseType == MainConfigBean.Database.Type.SQL) { try { sql.close(); } catch (SQLException ex) { getLogger().log(Level.SEVERE, "Could not close connection to SQL server!", ex); } } } // Configuration management... // Main configuration (config.yml)... @Override public void saveDefaultConfig() { try { if (!configFile.exists() || BeanManager.load(BeanManager.ConfigType.YAML, configFile, MainConfigBean.class) == null) { configBean = new MainConfigBean(); // Strike actions... configBean.setStrikeActions(new HashMap<Integer, MainConfigBean.Action>()); configBean.getStrikeActions().put(1, new MainConfigBean.Action()); configBean.getStrikeActions().get(1).setType(MainConfigBean.Action.ActionType.WARN); configBean.getStrikeActions().put(2, new MainConfigBean.Action()); configBean.getStrikeActions().get(2).setType(MainConfigBean.Action.ActionType.MUTE); configBean.getStrikeActions().get(2).setTimeUnit(TimeUnit.MINUTES); configBean.getStrikeActions().get(2).setDuration(10.0); configBean.getStrikeActions().put(3, new MainConfigBean.Action()); configBean.getStrikeActions().get(3).setType(MainConfigBean.Action.ActionType.KICK); configBean.getStrikeActions().put(4, new MainConfigBean.Action()); configBean.getStrikeActions().get(4).setType(MainConfigBean.Action.ActionType.TEMPBAN); configBean.getStrikeActions().get(4).setTimeUnit(TimeUnit.HOURS); configBean.getStrikeActions().get(4).setDuration(5.0); configBean.getStrikeActions().put(5, new MainConfigBean.Action()); configBean.getStrikeActions().get(5).setType(MainConfigBean.Action.ActionType.BAN); configBean.getStrikeActions().put(6, new MainConfigBean.Action()); configBean.getStrikeActions().get(6).setType(MainConfigBean.Action.ActionType.COMMAND); configBean.getStrikeActions().get(6).setCommand("tell %guilty% Naughty-naughty! Sent by %police%"); // Database config... configBean.setDatabase(new MainConfigBean.Database()); configBean.getDatabase().setType(MainConfigBean.Database.Type.FILE); configBean.getDatabase().setUrl("jdbc:mysql://localhost:3306/mcsrv"); configBean.getDatabase().setRedisPort(6380); configBean.getDatabase().setDriverClass("com.mysql.jdbc.Driver"); configBean.getDatabase().setUsername("root"); configBean.getDatabase().setPassword("v3rys3cr3t"); configBean.getDatabase().setMaxRedisConnections(-1); saveConfig(); } } catch (IOException ex) { } } @Override public void saveConfig() { try { if (!configFile.exists()) { configFile.createNewFile(); } BeanManager.save(BeanManager.ConfigType.YAML, configFile, configBean); } catch (IOException ex) { getLogger().log(Level.SEVERE, "Could not save main configuration!", ex); } } @Override public void reloadConfig() { if (!configFile.exists()) { saveDefaultConfig(); return; } try { configBean = BeanManager.load(BeanManager.ConfigType.YAML, configFile, MainConfigBean.class); databaseType = configBean.getDatabase().getType(); } catch (IOException ex) { getLogger().log(Level.SEVERE, "Could not load main configuration! Disabling...", ex); getServer().getPluginManager().disablePlugin(this); } } // Strike database file (database.json) public void saveDatabaseFile() { try { if (!databaseFile.exists()) { databaseFile.createNewFile(); } BeanManager.save(BeanManager.ConfigType.JSON, databaseFile, databaseBean); } catch (IOException ex) { getLogger().log(Level.SEVERE, "Could not save strike database!", ex); } } public void loadDatabaseFile() { try { if (!databaseFile.exists()) { databaseFile.createNewFile(); databaseBean = new StrikeDatabaseBean(new HashMap<UUID, Map<Integer, String>>()); return; } databaseBean = BeanManager.load(BeanManager.ConfigType.JSON, databaseFile, StrikeDatabaseBean.class); } catch (IOException ex) { getLogger().log(Level.SEVERE, "Could not load strike database!", ex); } } }