Advertisement
Guest User

Stocks Class

a guest
Jul 30th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package me.Austin.Stocks;
  2.  
  3. import java.io.File;
  4. import java.io.InputStream;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.logging.Logger;
  8.  
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. import com.mysql.jdbc.Connection;
  16. import com.mysql.jdbc.Statement;
  17.  
  18. public class Stocks extends JavaPlugin {
  19.  
  20. private static Stocks instance;
  21. Logger logger = this.getLogger();
  22. static final String DB_NAME = "jdbc:mysql://localhost:3306/minecraft";
  23. static final String USER = "root";
  24. static final String PASS = "password";
  25. Connection conn;
  26. Statement s;
  27.  
  28. public void onEnable() {
  29. logger.info("Stock market is enabling for the server");
  30. saveConfig();
  31. savemarketYML();
  32.  
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver"); // Gets the driver class
  35. getLogger().info("About to connect to database"); // These are just
  36. // for debugging
  37. // purposes.
  38.  
  39. conn = (Connection) DriverManager.getConnection(DB_NAME, USER, PASS); // Gets
  40. // a
  41. // connection
  42. // to
  43. // the
  44. // database
  45. // using
  46. // the
  47. // details
  48. // you
  49. // provided.
  50.  
  51. getLogger().info("Successfully connected.");
  52.  
  53. getLogger().info("About to create a statement");
  54.  
  55. s = (Statement) conn.createStatement(); // Creates a statement. You
  56. // can execute queries on
  57. // this.
  58.  
  59. getLogger().info("Successfully created statement.");
  60. } catch (Exception ex) {
  61. ex.printStackTrace();
  62. }
  63.  
  64. }
  65.  
  66. private FileConfiguration marketYML = null;
  67. private File marketYMLFile = null;
  68.  
  69. public void reloadMarketYMLFile() {
  70. if (marketYMLFile == null) {
  71. marketYMLFile = new File(getDataFolder(), "Market.yml");
  72. }
  73. marketYML = YamlConfiguration.loadConfiguration(marketYMLFile);
  74.  
  75. InputStream defConfigStream = this.getResource("Market.yml");
  76. if (defConfigStream != null) {
  77. @SuppressWarnings("deprecation")
  78. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  79. marketYML.setDefaults(defConfig);
  80. }
  81. }
  82.  
  83. private void savemarketYML() {
  84. if (marketYML == null) {
  85. this.reloadMarketYMLFile();
  86. }
  87. }
  88.  
  89. public FileConfiguration getMarketYML() {
  90. if (marketYML == null) {
  91. reloadMarketYMLFile();
  92. }
  93. return marketYML;
  94. }
  95.  
  96. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  97. if (commandLabel.equalsIgnoreCase("open")) {
  98. try {
  99. openMarket.openMarket(getConfig().getString("openMarketMSG"));
  100. } catch (SQLException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. }
  105. if (commandLabel.equalsIgnoreCase("buy")) {
  106. if (args.length != 0) {
  107. try {
  108. int number = Integer.parseInt(args[1]);
  109. buyStock.buy(args[0], number);
  110. } catch (SQLException e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. } else {
  115. sender.sendMessage("Error");
  116. }
  117. }
  118. return false;
  119. }
  120.  
  121. public static Stocks get() {
  122. return instance;
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement