mariolatif741

Main.class of StayPut Reloaded

Jul 9th, 2018
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package nl.zandervdm.stayput;
  2.  
  3. import com.j256.ormlite.dao.Dao;
  4. import com.j256.ormlite.dao.DaoManager;
  5. import com.j256.ormlite.jdbc.JdbcConnectionSource;
  6. import com.j256.ormlite.support.ConnectionSource;
  7. import com.j256.ormlite.table.TableUtils;
  8. import java.io.File;
  9. import java.sql.SQLException;
  10. import java.util.Collection;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13. import java.util.logging.Logger;
  14. import nl.zandervdm.stayput.Commands.StayputCommand;
  15. import nl.zandervdm.stayput.Listeners.PlayerTeleportEventListener;
  16. import nl.zandervdm.stayput.Models.Position;
  17. import nl.zandervdm.stayput.Repositories.PositionRepository;
  18. import nl.zandervdm.stayput.Utils.ConfigManager;
  19. import nl.zandervdm.stayput.Utils.RuleManager;
  20. import org.bukkit.Server;
  21. import org.bukkit.command.PluginCommand;
  22. import org.bukkit.configuration.file.FileConfiguration;
  23. import org.bukkit.entity.Player;
  24. import org.bukkit.plugin.PluginManager;
  25. import org.bukkit.plugin.java.JavaPlugin;
  26. import org.bukkit.event.player.PlayerJoinEvent;
  27. import org.bukkit.event.player.PlayerQuitEvent;
  28.  
  29. public class Main
  30.   extends JavaPlugin
  31. {
  32.   public static FileConfiguration config;
  33.   protected ConfigManager configManager;
  34.   protected RuleManager ruleManager;
  35.   protected ConnectionSource connectionSource;
  36.   protected Dao<Position, Integer> positionMapper;
  37.   protected PositionRepository positionRepository;
  38.  
  39.   class AutoSyncDb extends TimerTask {
  40.         public void run() {
  41.             syncPlayerstoDatabase();
  42.         }
  43.   }
  44.  
  45.   public void onEnable()
  46.   {
  47.     setupClasses();
  48.     setupConfig();
  49.     setupListeners();
  50.     setupCommands();
  51.     setupDatabase();
  52.     setupDao();
  53.     setupTables();
  54.     Timer timer = new Timer();
  55.     timer.schedule(new AutoSyncDb(), 0, 10000);
  56.   }
  57.  
  58.     public void onPlayerQuit(PlayerQuitEvent event) {
  59.           syncPlayerstoDatabase();
  60.     }
  61.  
  62.   public void onDisable()
  63.   {
  64.     syncPlayerstoDatabase();
  65.   }
  66.  
  67.   public Dao<Position, Integer> getPositionMapper()
  68.   {
  69.     return this.positionMapper;
  70.   }
  71.  
  72.   public PositionRepository getPositionRepository()
  73.   {
  74.     return this.positionRepository;
  75.   }
  76.  
  77.   public RuleManager getRuleManager()
  78.   {
  79.     return this.ruleManager;
  80.   }
  81.  
  82.   protected void setupClasses()
  83.   {
  84.     this.configManager = new ConfigManager(this);
  85.     this.ruleManager = new RuleManager(this);
  86.    
  87.     this.positionRepository = new PositionRepository(this);
  88.   }
  89.  
  90.   public void setupConfig()
  91.   {
  92.     this.configManager.createConfig();
  93.     config = getConfig();
  94.     if (config.getBoolean("debug")) {
  95.       getLogger().info("Setting up config");
  96.     }
  97.   }
  98.  
  99.   protected void setupListeners()
  100.   {
  101.     getServer().getPluginManager().registerEvents(new PlayerTeleportEventListener(this), this);
  102.     if (config.getBoolean("debug")) {
  103.       getLogger().info("Setting up listeners");
  104.     }
  105.   }
  106.  
  107.   protected void setupCommands()
  108.   {
  109.     getCommand("stayput").setExecutor(new StayputCommand(this));
  110.   }
  111.  
  112.   protected void setupDatabase()
  113.   {
  114.     if (config.getString("type").equals("sqlite"))
  115.     {
  116.       try
  117.       {
  118.         Class.forName("org.sqlite.JDBC");
  119.       }
  120.       catch (ClassNotFoundException e)
  121.       {
  122.         e.printStackTrace();
  123.       }
  124.       File file = new File(getDataFolder(), "database.db");
  125.       String datasource = "jdbc:sqlite:" + file;
  126.       this.connectionSource = null;
  127.       try
  128.       {
  129.         this.connectionSource = new JdbcConnectionSource(datasource);
  130.       }
  131.       catch (SQLException e)
  132.       {
  133.         e.printStackTrace();
  134.       }
  135.     }
  136.     else if (config.getString("type").equals("mysql"))
  137.     {
  138.       String host = config.getString("mysql.host");
  139.       Integer port = Integer.valueOf(config.getInt("mysql.port"));
  140.       String database = config.getString("mysql.database");
  141.       String username = config.getString("mysql.username");
  142.       String password = config.getString("mysql.password");
  143.       String datasource = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
  144.       this.connectionSource = null;
  145.       try
  146.       {
  147.         this.connectionSource = new JdbcConnectionSource(datasource, username, password);
  148.       }
  149.       catch (SQLException e)
  150.       {
  151.         e.printStackTrace();
  152.       }
  153.     }
  154.     else
  155.     {
  156.       getLogger().warning("Invalid database connection type chosen!");
  157.     }
  158.     if (config.getBoolean("debug")) {
  159.       getLogger().info("Setting up database");
  160.     }
  161.   }
  162.  
  163.   protected void setupDao()
  164.   {
  165.     this.positionMapper = null;
  166.     try
  167.     {
  168.       this.positionMapper = DaoManager.createDao(this.connectionSource, Position.class);
  169.     }
  170.     catch (SQLException e)
  171.     {
  172.       e.printStackTrace();
  173.     }
  174.   }
  175.  
  176.   protected void setupTables()
  177.   {
  178.     try
  179.     {
  180.       TableUtils.createTableIfNotExists(this.connectionSource, Position.class);
  181.     }
  182.     catch (SQLException e)
  183.     {
  184.       e.printStackTrace();
  185.     }
  186.     if (config.getBoolean("debug")) {
  187.       getLogger().info("Setting up tables");
  188.     }
  189.   }
  190.  
  191.   protected void syncPlayerstoDatabase()
  192.   {
  193.     Collection<? extends Player> players = getServer().getOnlinePlayers();
  194.     for (Player player : players) {
  195.       if (player.hasPermission("stayput.use")) {
  196.         getPositionRepository().updateLocationForPlayer(player, player.getLocation());
  197.       }
  198.     }
  199.   }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment