Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.93 KB | None | 0 0
  1. package com.braden.warcore;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.Location;
  13. import org.bukkit.Material;
  14. import org.bukkit.OfflinePlayer;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.event.EventHandler;
  17. import org.bukkit.event.Listener;
  18. import org.bukkit.event.player.PlayerJoinEvent;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.inventory.meta.ItemMeta;
  21. import org.bukkit.plugin.PluginManager;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23. import org.bukkit.potion.PotionEffect;
  24. import org.bukkit.potion.PotionEffectType;
  25.  
  26. import com.braden.warcore.commands.AdminTools;
  27. import com.braden.warcore.commands.CMDHub;
  28. import com.braden.warcore.commands.StaffSetTools;
  29. import com.braden.warcore.events.ChatManager;
  30. import com.braden.warcore.events.DisableStuff;
  31. import com.braden.warcore.events.DoubleJump;
  32. import com.braden.warcore.events.InventoryEvents;
  33. import com.braden.warcore.events.VoidSpawn;
  34.  
  35. import net.md_5.bungee.api.ChatColor;
  36.  
  37. public class Main extends JavaPlugin implements Listener{
  38. public Connection connection;
  39. public String host;
  40. public String database;
  41. public String username;
  42. public String password;
  43. public Statement stmt = null;
  44. public int port;
  45.  
  46.  
  47.  
  48. public void createTables(){
  49. String owners = "CREATE TABLE IF NOT EXISTS owners ("
  50. + "PlayerName LONGTEXT)";
  51. String admins = "CREATE TABLE IF NOT EXISTS admins ("
  52. + "PlayerName LONGTEXT)";
  53. String mods = "CREATE TABLE IF NOT EXISTS mods ("
  54. + "PlayerName LONGTEXT)";
  55. String defaults = "CREATE TABLE IF NOT EXISTS defaults ("
  56. + "PlayerName LONGTEXT)";
  57.  
  58. try {
  59. Class.forName("com.mysql.jdbc.Driver");
  60. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  61. stmt = connection.createStatement();
  62. //This line has the issue
  63. stmt.executeUpdate(owners);
  64. stmt.executeUpdate(admins);
  65. stmt.executeUpdate(mods);
  66. stmt.executeUpdate(defaults);
  67. System.out.println("Tables Created");
  68. }
  69. catch (SQLException e ) {
  70. System.out.println("An error has occured on Table Creation");
  71. e.printStackTrace();
  72. }
  73. catch (ClassNotFoundException e) {
  74. System.out.println("An Mysql drivers were not found");
  75. }
  76. }
  77.  
  78. public void openConnection() throws SQLException, ClassNotFoundException {
  79. // host = "142.4.208.190";
  80. // port = 3306;
  81. // database = "db_16507";
  82. // username = "db_16507";
  83. // password = "18b648f139";
  84. host = "127.0.0.1";
  85. port = 3306;
  86. username = "root";
  87. password = "powfist900";
  88. database = "warcore";
  89. if (connection != null && !connection.isClosed()) {
  90. return;
  91. }
  92.  
  93. synchronized (this) {
  94. if (connection != null && !connection.isClosed()) {
  95.  
  96. return;
  97.  
  98. }
  99. Class.forName("com.mysql.jdbc.Driver");
  100. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  101. System.out.println("Connected!");
  102. stmt = connection.createStatement();
  103. createTables();
  104.  
  105.  
  106.  
  107. }
  108. }
  109.  
  110.  
  111. public void onEnable(){
  112.  
  113.  
  114. Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
  115. events();
  116. commands();
  117. if(getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby") == null){
  118. getServer().getScoreboardManager().getMainScoreboard().registerNewTeam("lobby");
  119. }
  120. getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby").setCanSeeFriendlyInvisibles(true);
  121.  
  122. try {
  123. openConnection();
  124. } catch (ClassNotFoundException e) {
  125. e.printStackTrace();
  126. } catch (SQLException e) {
  127. e.printStackTrace();
  128. }
  129.  
  130. }
  131.  
  132. public void onDisable(){
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139. void commands(){
  140. getCommand("goto").setExecutor(new AdminTools(this));
  141. getCommand("tp").setExecutor(new AdminTools(this));
  142. getCommand("gmc").setExecutor(new AdminTools(this));
  143. getCommand("gms").setExecutor(new AdminTools(this));
  144. getCommand("fly").setExecutor(new AdminTools(this));
  145. getCommand("hub").setExecutor(new CMDHub(this));
  146. getCommand("lobby").setExecutor(new CMDHub(this));
  147. getCommand("spawn").setExecutor(new CMDHub(this));
  148.  
  149. getCommand("admin").setExecutor(new StaffSetTools(this));
  150. getCommand("owner").setExecutor(new StaffSetTools(this));
  151. getCommand("demote").setExecutor(new StaffSetTools(this));
  152. }
  153. void events(){
  154.  
  155. PluginManager pm = Bukkit.getServer().getPluginManager();
  156. pm.registerEvents(new DoubleJump(this), this);
  157. pm.registerEvents(new DisableStuff(), this);
  158. pm.registerEvents(this, this);
  159. pm.registerEvents(new ChatManager(this), this);
  160. pm.registerEvents(new VoidSpawn(), this);
  161. pm.registerEvents(new InventoryEvents(this), this);
  162. }
  163.  
  164. @EventHandler
  165. @SuppressWarnings("deprecation")
  166. public void onJoin(PlayerJoinEvent e){
  167.  
  168. OfflinePlayer firstjoin = Bukkit.getOfflinePlayer(e.getPlayer().getName());
  169. if(!firstjoin.hasPlayedBefore()){
  170.  
  171.  
  172. try {
  173. connection.setAutoCommit(false);
  174. PreparedStatement defaultadd = connection.prepareStatement("INSERT INTO defaults(PlayerName) VALUES (?);");
  175.  
  176. defaultadd.setString(1, firstjoin.getUniqueId().toString());
  177. defaultadd.executeUpdate();
  178. connection.commit();
  179.  
  180.  
  181.  
  182. } catch (SQLException e1) {
  183.  
  184. e1.printStackTrace();
  185. }
  186.  
  187. System.out.print("ADDED " + firstjoin.getName() + " to DEFAULTS.");
  188. } else {
  189. getLogger().info(e.getPlayer().getName() + " Joined.");
  190. }
  191. List<String> navlore = new ArrayList<String>();
  192. ItemStack navigator = new ItemStack(Material.COMPASS);
  193. ItemMeta navmeta = navigator.getItemMeta();
  194. navmeta.setDisplayName(ChatColor.DARK_RED + "Server Navigator");
  195. navlore.add(ChatColor.RED + "Navigate from server to server!");
  196. navmeta.setLore(navlore);
  197. navigator.setItemMeta(navmeta);
  198.  
  199. List<String> lobbylore = new ArrayList<String>();
  200. ItemStack lobbynav = new ItemStack(Material.NETHER_STAR);
  201. ItemMeta lobbymeta = lobbynav.getItemMeta();
  202. lobbymeta.setDisplayName(ChatColor.DARK_RED + "Lobby Selectors");
  203. lobbylore.add(ChatColor.RED + "Select which lobby you would like to be in! (Meet up with friends etc.");
  204. lobbymeta.setLore(lobbylore);
  205. lobbynav.setItemMeta(lobbymeta);
  206.  
  207. List<String> cosmeticlore = new ArrayList<String>();
  208. ItemStack cosmeticnav = new ItemStack(Material.CHEST);
  209. ItemMeta cosmeticmeta = cosmeticnav.getItemMeta();
  210. cosmeticmeta.setDisplayName(ChatColor.DARK_RED + "Cosmetics");
  211. cosmeticlore.add(ChatColor.RED + "Fun things you can play with in the hub!");
  212.  
  213. int yaw = -88;
  214. int pitch = 3;
  215. Player p = e.getPlayer();
  216. p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 9999999, 5), true);
  217. getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby").addPlayer(e.getPlayer());
  218. p.setAllowFlight(true);
  219. p.setFoodLevel(20);
  220. p.setHealth(20);
  221. p.teleport(new Location(p.getWorld(), -869.485, 69, 946.360, yaw, pitch));
  222. e.setJoinMessage("");
  223.  
  224. p.getInventory().setItem(4, navigator);
  225. p.getInventory().setItem(8, lobbynav);
  226.  
  227. if(p.getName().equalsIgnoreCase("future_war")){
  228. p.setPlayerListName(ChatColor.DARK_PURPLE + "[" + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "O" + ChatColor
  229. .RESET + "" + ChatColor.DARK_PURPLE + "] " + "Future");
  230. }
  231.  
  232. else if(p.getName().equalsIgnoreCase("CosmicPulse")){
  233. p.setPlayerListName(ChatColor.DARK_PURPLE + "[" + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "O" + ChatColor
  234. .RESET + "" + ChatColor.DARK_PURPLE + "] " + "Pulse");
  235.  
  236. }
  237.  
  238.  
  239. }
  240. }
  241.  
  242.  
  243.  
  244. ----------------------------------------------------------------------
  245.  
  246. package com.braden.tut;
  247.  
  248. import java.sql.Connection;
  249. import java.sql.DriverManager;
  250. import java.sql.SQLException;
  251. import java.sql.Statement;
  252.  
  253. import org.bukkit.plugin.java.JavaPlugin;
  254.  
  255. public class Main extends JavaPlugin {
  256. public Connection connection;
  257. public String host;
  258. public String database;
  259. public String username;
  260. public String password;
  261. public Statement stmt = null;
  262. public int port;
  263.  
  264.  
  265. public void onEnable(){
  266. try {
  267. connect();
  268. System.out.println("Successfully connected to " + this.database);
  269. } catch (ClassNotFoundException e) {
  270.  
  271. e.printStackTrace();
  272. } catch (SQLException e) {
  273.  
  274. e.printStackTrace();
  275. }
  276. }
  277. public void connect() throws SQLException, ClassNotFoundException {
  278. host = "Your database IP"; //EX: 127.0.0.1
  279. port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
  280. username = "root"; //DATABASE USERNAME
  281. password = "password"; //DATABASE PASSWORD
  282. database = "database"; //DATABASE NAME
  283.  
  284. Class.forName("com.mysql.jdbc.Driver");
  285. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  286.  
  287. }
  288. }
  289.  
  290. ------------------------------------------------------------------------------
  291.  
  292. Tutorial:
  293.  
  294. So, I've been researching for the past week how to use MySQL in Bukkit plugins using JDBC.
  295.  
  296. I tried following Husky's guide, but I didn't find a lot of resources on how to actually use MySQL, I just found how to link it. In this tutorial I will be going through and showing you the basics of MySQL
  297.  
  298. If you don't know what MySQL is, look here.
  299.  
  300. To start, we need to link MySQL into your plugin and connect to the database.
  301. 1. Open up your Main class.
  302.  
  303. 2. Put these in the top of your class:
  304. [code=java] public Connection connection;
  305. public String host;
  306. public String database;
  307. public String username;
  308. public String password;
  309. public Statement stmt = null;
  310. public int port;[/code]
  311.  
  312. 2. Create a connect method like so:
  313. [code=java]public void connect() throws SQLException, ClassNotFoundException {[/code]
  314.  
  315. 3. Run the connect method in your onEnable().
  316.  
  317. 4. Surround the connect method (in the onEnable) with try/catch by hovering over it in your IDE.
  318.  
  319. So as of now, your class should look like this:
  320. [code=java]package com.braden.tut;
  321.  
  322. import java.sql.Connection;
  323. import java.sql.SQLException;
  324. import java.sql.Statement;
  325.  
  326. import org.bukkit.plugin.java.JavaPlugin;
  327.  
  328. public class Main extends JavaPlugin {
  329. public Connection connection;
  330. public String host;
  331. public String database;
  332. public String username;
  333. public String password;
  334. public Statement stmt = null;
  335. public int port;
  336.  
  337.  
  338. public void onEnable(){
  339. try {
  340. connect();
  341. } catch (ClassNotFoundException e) {
  342. e.printStackTrace();
  343. } catch (SQLException e) {
  344. e.printStackTrace();
  345. }
  346. }
  347. public void connect() throws SQLException, ClassNotFoundException {
  348.  
  349.  
  350. }
  351. }[/code]
  352.  
  353. Now it's time to give the plugin your database info, like so:
  354.  
  355. 1. Label the strings/ints in your connect method that you created earlier, like so:
  356. [code]host = "Your database IP"; //EX: 127.0.0.1
  357. port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
  358. username = "root"; //DATABASE USERNAME
  359. password = "password"; //DATABASE PASSWORD
  360. database = "database"; //DATABASE NAME[/code]
  361.  
  362. Now that we have all the information stored, it's time to actually connect to the server (in the connect method), using these lines of code:
  363. [code=java]Class.forName("com.mysql.jdbc.Driver");
  364. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  365. [/code]
  366.  
  367. And for future usage, we're going to create a statement (right under where you define connection):
  368. [code=java]stmt = connection.createStatement();[/code]
  369. Now that we've connected to the database, it's time to run the proper checks in our connect method, like so:
  370. [code=java]if (connection != null && !connection.isClosed()) {
  371. return;
  372. }
  373.  
  374. synchronized (this) {
  375. if (connection != null && !connection.isClosed()) {
  376.  
  377. return;
  378.  
  379. } [/code]
  380.  
  381. Now that we've come this far, your class should look something like this:
  382. [code=java]package com.braden.tut;
  383.  
  384. import java.sql.Connection;
  385. import java.sql.DriverManager;
  386. import java.sql.SQLException;
  387. import java.sql.Statement;
  388.  
  389. import org.bukkit.plugin.java.JavaPlugin;
  390.  
  391. public class Main extends JavaPlugin {
  392. public Connection connection;
  393. public String host;
  394. public String database;
  395. public String username;
  396. public String password;
  397. public Statement stmt = null;
  398. public int port;
  399.  
  400.  
  401. public void onEnable(){
  402. try {
  403. connect();
  404. } catch (ClassNotFoundException e) {
  405.  
  406. e.printStackTrace();
  407. } catch (SQLException e) {
  408.  
  409. e.printStackTrace();
  410. }
  411. }
  412. public void connect() throws SQLException, ClassNotFoundException {
  413. host = "Your database IP"; //EX: 127.0.0.1
  414. port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
  415. username = "root"; //DATABASE USERNAME
  416. password = "password"; //DATABASE PASSWORD
  417. database = "database"; //DATABASE NAME
  418.  
  419. Class.forName("com.mysql.jdbc.Driver");
  420. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  421.  
  422. }
  423. }
  424. [/code]
  425. If you want, to show in Console that you've connected, feel free to put this in the "try" section of your onEnable:
  426. [code=java]System.out.println("Successfully connected to " + this.database);[/code]
  427.  
  428. Now, if you inserted the information correctly, you should be able to connect to MySQL on your server startup!
  429.  
  430. Now that we're connected, it's time to get into the juicy stuff. I personally use separate classes for my commands to keep things organized, so I'm going to go ahead and create a class named "CMDStaffSet", and let it implement CommandExecutor. Go ahead and setup your command class.
  431.  
  432. You're going to want to link your Command class to your Main class, so put this near the top:
  433. [code=java] Main plugin;
  434. public CMDStaffSet(Main instance){
  435. plugin = instance;
  436. }[/code]
  437.  
  438. Now, this is when the Statement you created earlier comes into play. In your onCommand, go ahead and call on your Statement. Your class should look like this:
  439. [code=java]package com.braden.tut;
  440.  
  441. import java.sql.Statement;
  442.  
  443. import org.bukkit.command.Command;
  444. import org.bukkit.command.CommandExecutor;
  445. import org.bukkit.command.CommandSender;
  446.  
  447. public class CMDStaffSet implements CommandExecutor {
  448. Main plugin;
  449. public CMDStaffSet(Main instance){
  450. plugin = instance;
  451. }
  452.  
  453. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
  454. Statement stmt = plugin.stmt;
  455.  
  456.  
  457. return true;
  458. }
  459. }
  460. [/code]
  461.  
  462. Go ahead and create the command admin (this is very basic, use cmd.getName).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement