Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gguurr.fantacore._main;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.RegisteredServiceProvider;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.gguurr.fantacore.CMDs.SetRank;
- import com.gguurr.fantacore.CMDs.SugarCMD;
- import com.gguurr.fantacore.events.PlayerJoin;
- import com.gguurr.fantahub._main.Chat;
- import com.gguurr.fantahub._main.Economy;
- import com.gguurr.fantahub._main.Permission;
- import com.google.common.io.ByteArrayDataInput;
- import com.google.common.io.ByteArrayDataOutput;
- import com.google.common.io.ByteStreams;
- public class Main extends JavaPlugin {
- public static Main instans;
- private Connection connection;
- private String host, database, username, password;
- private String prefix = "fanta_";
- private int port;
- @Override
- public void onEnable() {
- host = "sql.pebblehost.com";
- database = "customer_10028";
- username = "customer_10028";
- password = "4d0bc8e214";
- port = 3306;
- instans = this;
- this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
- this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
- if (!setupEconomy()) {
- getServer().getPluginManager().disablePlugin(this);
- return;
- }
- setupPermissions();
- setupChat();
- try {
- openConnection();
- Statement statement = connection.createStatement();
- String q = "CREATE TABLE IF NOT EXISTS " + prefix + "Money(amount INT, uuid CHAR(36));";
- statement.executeUpdate(q);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this);
- getCommand("money").setExecutor(new SugarCMD());
- getCommand("rank").setExecutor(new SetRank());
- }
- private boolean setupEconomy() {
- if (getServer().getPluginManager().getPlugin("Vault") == null) {
- return false;
- }
- RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
- if (rsp == null) {
- return false;
- }
- econ = rsp.getProvider();
- return econ != null;
- }
- private boolean setupChat() {
- RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
- chat = rsp.getProvider();
- return chat != null;
- }
- private boolean setupPermissions() {
- RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
- perms = rsp.getProvider();
- return perms != null;
- }
- @Override
- public void onDisable() {
- }
- public int getMoney(Player p) {
- try {
- Statement statement = connection.createStatement();
- String q = "SELECT amount FROM " + prefix + "Money WHERE uuid = \"" + p.getUniqueId().toString() + "\";";
- ResultSet rs = statement.executeQuery(q);
- int s = -1;
- while (rs.next()) {
- s = rs.getInt("amount");
- }
- return s;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return -1;
- }
- public boolean haveRecord(Player p) {
- try {
- Statement statement = connection.createStatement();
- String q = "SELECT * FROM " + prefix + "Money WHERE uuid = \"" + p.getUniqueId().toString() + "\";";
- ResultSet rs = statement.executeQuery(q);
- while (rs.next()) {
- return true;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- public void addMoney(Player p, int a) {
- setMoney(p, getMoney(p) + a);
- }
- public void createMoney(Player p) {
- try {
- String q = "INSERT INTO " + prefix + "Money(amount, uuid) VALUES(?,?)";
- PreparedStatement statement = connection.prepareStatement(q);
- statement.setInt(1, 0);
- statement.setString(2, p.getUniqueId().toString());
- statement.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void setMoney(Player p, int a) {
- try {
- String q = "UPDATE " + prefix + "Money SET amount = ? WHERE uuid = ?;";
- PreparedStatement statement = connection.prepareStatement(q);
- statement.setInt(1, a);
- statement.setString(2, p.getUniqueId().toString());
- statement.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void openConnection() throws SQLException, ClassNotFoundException {
- 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);
- }
- }
- @Override
- public void onPluginMessageReceived(String channel, Player arg1, byte[] message) {
- if (!channel.equals("BungeeCord")) {
- return;
- }
- ByteArrayDataInput in = ByteStreams.newDataInput(message);
- String subchannel = in.readUTF();
- if (subchannel.equals("PlayerCount")) {
- online = in.readInt();
- }
- }
- public void sendPlayer(Player p, String server) {
- ByteArrayDataOutput out = ByteStreams.newDataOutput();
- out.writeUTF("Connect");
- out.writeUTF(server);
- p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
- }
- public static Economy getEconomy() {
- return econ;
- }
- public static Permission getPermissions() {
- return perms;
- }
- public static Chat getChat() {
- return chat;
- }
- }
Add Comment
Please, Sign In to add comment