Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.braden.warcore;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import com.braden.warcore.commands.AdminTools;
- import com.braden.warcore.commands.CMDHub;
- import com.braden.warcore.commands.StaffSetTools;
- import com.braden.warcore.events.ChatManager;
- import com.braden.warcore.events.DisableStuff;
- import com.braden.warcore.events.DoubleJump;
- import com.braden.warcore.events.InventoryEvents;
- import com.braden.warcore.events.VoidSpawn;
- import net.md_5.bungee.api.ChatColor;
- public class Main extends JavaPlugin implements Listener{
- public Connection connection;
- public String host;
- public String database;
- public String username;
- public String password;
- public Statement stmt = null;
- public int port;
- public void createTables(){
- String owners = "CREATE TABLE IF NOT EXISTS owners ("
- + "PlayerName LONGTEXT)";
- String admins = "CREATE TABLE IF NOT EXISTS admins ("
- + "PlayerName LONGTEXT)";
- String mods = "CREATE TABLE IF NOT EXISTS mods ("
- + "PlayerName LONGTEXT)";
- String defaults = "CREATE TABLE IF NOT EXISTS defaults ("
- + "PlayerName LONGTEXT)";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- stmt = connection.createStatement();
- //This line has the issue
- stmt.executeUpdate(owners);
- stmt.executeUpdate(admins);
- stmt.executeUpdate(mods);
- stmt.executeUpdate(defaults);
- System.out.println("Tables Created");
- }
- catch (SQLException e ) {
- System.out.println("An error has occured on Table Creation");
- e.printStackTrace();
- }
- catch (ClassNotFoundException e) {
- System.out.println("An Mysql drivers were not found");
- }
- }
- public void openConnection() throws SQLException, ClassNotFoundException {
- // host = "142.4.208.190";
- // port = 3306;
- // database = "db_16507";
- // username = "db_16507";
- // password = "18b648f139";
- host = "127.0.0.1";
- port = 3306;
- username = "root";
- password = "powfist900";
- database = "warcore";
- if (connection != null && !connection.isClosed()) {
- return;
- }
- synchronized (this) {
- if (connection != null && !connection.isClosed()) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- System.out.println("Connected!");
- stmt = connection.createStatement();
- createTables();
- }
- }
- public void onEnable(){
- Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
- events();
- commands();
- if(getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby") == null){
- getServer().getScoreboardManager().getMainScoreboard().registerNewTeam("lobby");
- }
- getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby").setCanSeeFriendlyInvisibles(true);
- try {
- openConnection();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void onDisable(){
- }
- void commands(){
- getCommand("goto").setExecutor(new AdminTools(this));
- getCommand("tp").setExecutor(new AdminTools(this));
- getCommand("gmc").setExecutor(new AdminTools(this));
- getCommand("gms").setExecutor(new AdminTools(this));
- getCommand("fly").setExecutor(new AdminTools(this));
- getCommand("hub").setExecutor(new CMDHub(this));
- getCommand("lobby").setExecutor(new CMDHub(this));
- getCommand("spawn").setExecutor(new CMDHub(this));
- getCommand("admin").setExecutor(new StaffSetTools(this));
- getCommand("owner").setExecutor(new StaffSetTools(this));
- getCommand("demote").setExecutor(new StaffSetTools(this));
- }
- void events(){
- PluginManager pm = Bukkit.getServer().getPluginManager();
- pm.registerEvents(new DoubleJump(this), this);
- pm.registerEvents(new DisableStuff(), this);
- pm.registerEvents(this, this);
- pm.registerEvents(new ChatManager(this), this);
- pm.registerEvents(new VoidSpawn(), this);
- pm.registerEvents(new InventoryEvents(this), this);
- }
- @EventHandler
- @SuppressWarnings("deprecation")
- public void onJoin(PlayerJoinEvent e){
- OfflinePlayer firstjoin = Bukkit.getOfflinePlayer(e.getPlayer().getName());
- if(!firstjoin.hasPlayedBefore()){
- try {
- connection.setAutoCommit(false);
- PreparedStatement defaultadd = connection.prepareStatement("INSERT INTO defaults(PlayerName) VALUES (?);");
- defaultadd.setString(1, firstjoin.getUniqueId().toString());
- defaultadd.executeUpdate();
- connection.commit();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- System.out.print("ADDED " + firstjoin.getName() + " to DEFAULTS.");
- } else {
- getLogger().info(e.getPlayer().getName() + " Joined.");
- }
- List<String> navlore = new ArrayList<String>();
- ItemStack navigator = new ItemStack(Material.COMPASS);
- ItemMeta navmeta = navigator.getItemMeta();
- navmeta.setDisplayName(ChatColor.DARK_RED + "Server Navigator");
- navlore.add(ChatColor.RED + "Navigate from server to server!");
- navmeta.setLore(navlore);
- navigator.setItemMeta(navmeta);
- List<String> lobbylore = new ArrayList<String>();
- ItemStack lobbynav = new ItemStack(Material.NETHER_STAR);
- ItemMeta lobbymeta = lobbynav.getItemMeta();
- lobbymeta.setDisplayName(ChatColor.DARK_RED + "Lobby Selectors");
- lobbylore.add(ChatColor.RED + "Select which lobby you would like to be in! (Meet up with friends etc.");
- lobbymeta.setLore(lobbylore);
- lobbynav.setItemMeta(lobbymeta);
- List<String> cosmeticlore = new ArrayList<String>();
- ItemStack cosmeticnav = new ItemStack(Material.CHEST);
- ItemMeta cosmeticmeta = cosmeticnav.getItemMeta();
- cosmeticmeta.setDisplayName(ChatColor.DARK_RED + "Cosmetics");
- cosmeticlore.add(ChatColor.RED + "Fun things you can play with in the hub!");
- int yaw = -88;
- int pitch = 3;
- Player p = e.getPlayer();
- p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 9999999, 5), true);
- getServer().getScoreboardManager().getMainScoreboard().getTeam("lobby").addPlayer(e.getPlayer());
- p.setAllowFlight(true);
- p.setFoodLevel(20);
- p.setHealth(20);
- p.teleport(new Location(p.getWorld(), -869.485, 69, 946.360, yaw, pitch));
- e.setJoinMessage("");
- p.getInventory().setItem(4, navigator);
- p.getInventory().setItem(8, lobbynav);
- if(p.getName().equalsIgnoreCase("future_war")){
- p.setPlayerListName(ChatColor.DARK_PURPLE + "[" + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "O" + ChatColor
- .RESET + "" + ChatColor.DARK_PURPLE + "] " + "Future");
- }
- else if(p.getName().equalsIgnoreCase("CosmicPulse")){
- p.setPlayerListName(ChatColor.DARK_PURPLE + "[" + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "O" + ChatColor
- .RESET + "" + ChatColor.DARK_PURPLE + "] " + "Pulse");
- }
- }
- }
- ----------------------------------------------------------------------
- package com.braden.tut;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- public Connection connection;
- public String host;
- public String database;
- public String username;
- public String password;
- public Statement stmt = null;
- public int port;
- public void onEnable(){
- try {
- connect();
- System.out.println("Successfully connected to " + this.database);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connect() throws SQLException, ClassNotFoundException {
- host = "Your database IP"; //EX: 127.0.0.1
- port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
- username = "root"; //DATABASE USERNAME
- password = "password"; //DATABASE PASSWORD
- database = "database"; //DATABASE NAME
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- }
- }
- ------------------------------------------------------------------------------
- Tutorial:
- So, I've been researching for the past week how to use MySQL in Bukkit plugins using JDBC.
- 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
- If you don't know what MySQL is, look here.
- To start, we need to link MySQL into your plugin and connect to the database.
- 1. Open up your Main class.
- 2. Put these in the top of your class:
- [code=java] public Connection connection;
- public String host;
- public String database;
- public String username;
- public String password;
- public Statement stmt = null;
- public int port;[/code]
- 2. Create a connect method like so:
- [code=java]public void connect() throws SQLException, ClassNotFoundException {[/code]
- 3. Run the connect method in your onEnable().
- 4. Surround the connect method (in the onEnable) with try/catch by hovering over it in your IDE.
- So as of now, your class should look like this:
- [code=java]package com.braden.tut;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- public Connection connection;
- public String host;
- public String database;
- public String username;
- public String password;
- public Statement stmt = null;
- public int port;
- public void onEnable(){
- try {
- connect();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connect() throws SQLException, ClassNotFoundException {
- }
- }[/code]
- Now it's time to give the plugin your database info, like so:
- 1. Label the strings/ints in your connect method that you created earlier, like so:
- [code]host = "Your database IP"; //EX: 127.0.0.1
- port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
- username = "root"; //DATABASE USERNAME
- password = "password"; //DATABASE PASSWORD
- database = "database"; //DATABASE NAME[/code]
- 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:
- [code=java]Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- [/code]
- And for future usage, we're going to create a statement (right under where you define connection):
- [code=java]stmt = connection.createStatement();[/code]
- Now that we've connected to the database, it's time to run the proper checks in our connect method, like so:
- [code=java]if (connection != null && !connection.isClosed()) {
- return;
- }
- synchronized (this) {
- if (connection != null && !connection.isClosed()) {
- return;
- } [/code]
- Now that we've come this far, your class should look something like this:
- [code=java]package com.braden.tut;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- public Connection connection;
- public String host;
- public String database;
- public String username;
- public String password;
- public Statement stmt = null;
- public int port;
- public void onEnable(){
- try {
- connect();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connect() throws SQLException, ClassNotFoundException {
- host = "Your database IP"; //EX: 127.0.0.1
- port = 3306; //YOUR DATABASE PORT, TYPICALLY 3306.
- username = "root"; //DATABASE USERNAME
- password = "password"; //DATABASE PASSWORD
- database = "database"; //DATABASE NAME
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- }
- }
- [/code]
- If you want, to show in Console that you've connected, feel free to put this in the "try" section of your onEnable:
- [code=java]System.out.println("Successfully connected to " + this.database);[/code]
- Now, if you inserted the information correctly, you should be able to connect to MySQL on your server startup!
- 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.
- You're going to want to link your Command class to your Main class, so put this near the top:
- [code=java] Main plugin;
- public CMDStaffSet(Main instance){
- plugin = instance;
- }[/code]
- 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:
- [code=java]package com.braden.tut;
- import java.sql.Statement;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- public class CMDStaffSet implements CommandExecutor {
- Main plugin;
- public CMDStaffSet(Main instance){
- plugin = instance;
- }
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
- Statement stmt = plugin.stmt;
- return true;
- }
- }
- [/code]
- Go ahead and create the command admin (this is very basic, use cmd.getName).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement