Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.zandervdm.stayput;
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.dao.DaoManager;
- import com.j256.ormlite.jdbc.JdbcConnectionSource;
- import com.j256.ormlite.support.ConnectionSource;
- import com.j256.ormlite.table.TableUtils;
- import java.io.File;
- import java.sql.SQLException;
- import java.util.Collection;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.logging.Logger;
- import nl.zandervdm.stayput.Commands.StayputCommand;
- import nl.zandervdm.stayput.Listeners.PlayerTeleportEventListener;
- import nl.zandervdm.stayput.Models.Position;
- import nl.zandervdm.stayput.Repositories.PositionRepository;
- import nl.zandervdm.stayput.Utils.ConfigManager;
- import nl.zandervdm.stayput.Utils.RuleManager;
- import org.bukkit.Server;
- import org.bukkit.command.PluginCommand;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- public class Main
- extends JavaPlugin
- {
- public static FileConfiguration config;
- protected ConfigManager configManager;
- protected RuleManager ruleManager;
- protected ConnectionSource connectionSource;
- protected Dao<Position, Integer> positionMapper;
- protected PositionRepository positionRepository;
- class AutoSyncDb extends TimerTask {
- public void run() {
- syncPlayerstoDatabase();
- }
- }
- public void onEnable()
- {
- setupClasses();
- setupConfig();
- setupListeners();
- setupCommands();
- setupDatabase();
- setupDao();
- setupTables();
- Timer timer = new Timer();
- timer.schedule(new AutoSyncDb(), 0, 10000);
- }
- public void onPlayerQuit(PlayerQuitEvent event) {
- syncPlayerstoDatabase();
- }
- public void onDisable()
- {
- syncPlayerstoDatabase();
- }
- public Dao<Position, Integer> getPositionMapper()
- {
- return this.positionMapper;
- }
- public PositionRepository getPositionRepository()
- {
- return this.positionRepository;
- }
- public RuleManager getRuleManager()
- {
- return this.ruleManager;
- }
- protected void setupClasses()
- {
- this.configManager = new ConfigManager(this);
- this.ruleManager = new RuleManager(this);
- this.positionRepository = new PositionRepository(this);
- }
- public void setupConfig()
- {
- this.configManager.createConfig();
- config = getConfig();
- if (config.getBoolean("debug")) {
- getLogger().info("Setting up config");
- }
- }
- protected void setupListeners()
- {
- getServer().getPluginManager().registerEvents(new PlayerTeleportEventListener(this), this);
- if (config.getBoolean("debug")) {
- getLogger().info("Setting up listeners");
- }
- }
- protected void setupCommands()
- {
- getCommand("stayput").setExecutor(new StayputCommand(this));
- }
- protected void setupDatabase()
- {
- if (config.getString("type").equals("sqlite"))
- {
- try
- {
- Class.forName("org.sqlite.JDBC");
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- File file = new File(getDataFolder(), "database.db");
- String datasource = "jdbc:sqlite:" + file;
- this.connectionSource = null;
- try
- {
- this.connectionSource = new JdbcConnectionSource(datasource);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- else if (config.getString("type").equals("mysql"))
- {
- String host = config.getString("mysql.host");
- Integer port = Integer.valueOf(config.getInt("mysql.port"));
- String database = config.getString("mysql.database");
- String username = config.getString("mysql.username");
- String password = config.getString("mysql.password");
- String datasource = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
- this.connectionSource = null;
- try
- {
- this.connectionSource = new JdbcConnectionSource(datasource, username, password);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- else
- {
- getLogger().warning("Invalid database connection type chosen!");
- }
- if (config.getBoolean("debug")) {
- getLogger().info("Setting up database");
- }
- }
- protected void setupDao()
- {
- this.positionMapper = null;
- try
- {
- this.positionMapper = DaoManager.createDao(this.connectionSource, Position.class);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- protected void setupTables()
- {
- try
- {
- TableUtils.createTableIfNotExists(this.connectionSource, Position.class);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- if (config.getBoolean("debug")) {
- getLogger().info("Setting up tables");
- }
- }
- protected void syncPlayerstoDatabase()
- {
- Collection<? extends Player> players = getServer().getOnlinePlayers();
- for (Player player : players) {
- if (player.hasPermission("stayput.use")) {
- getPositionRepository().updateLocationForPlayer(player, player.getLocation());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment