{
String DB_NAME = "jdbc:mysql://" + getConfig().getString("MySQL.Host") + ":" + getConfig().getInt("MySQL.Port") + "/" + getConfig().getString("MySQL.Database");
String USER = getConfig().getString("MySQL.Username");
String PASS = getConfig().getString("MySQL.Password");
Connection conn;
Statement s;
public void onEnable()
{
Bukkit.getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
saveConfig();
try
{
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(this.DB_NAME, this.USER, this.PASS);
this.s = this.conn.createStatement();
Bukkit.getLogger().warning("Successfuly connected to the mysql database!");
}
catch (Exception ex) {
ex.printStackTrace();
Bukkit.getLogger().warning("Cannot connect to MySQL database");
}
try
{
Statement check = this.conn.createStatement();
check.executeUpdate("CREATE TABLE IF NOT EXISTS `NetworkCoins` (UUID text, coins int);");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void addCoins(Player p, Integer amount)
{
try
{
String uuid = p.getUniqueId().toString();
Statement check = this.conn.createStatement();
ResultSet res = check.executeQuery("SELECT * FROM NetworkCoins WHERE UUID ='" + uuid + "';");
res.next();
if (res.getString("UUID") != null) {
int beforecoins = res.getInt("coins");
Statement update = this.conn.createStatement();
update.executeUpdate("UPDATE NetworkCoins SET coins = " + (beforecoins + amount.intValue()) + " WHERE UUID = '" + uuid + "';");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeCoins(Player p, Integer amount)
{
try {
String uuid = p.getUniqueId().toString();
Statement check = this.conn.createStatement();
ResultSet res = check.executeQuery("SELECT * FROM NetworkCoins WHERE UUID ='" + uuid + "';");
res.next();
if (res.getString("UUID") != null) {
int beforecoins = res.getInt("coins");
Statement update = this.conn.createStatement();
update.executeUpdate("UPDATE NetworkCoins SET coins = " + (beforecoins - amount.intValue()) + " WHERE UUID = '" + uuid + "';");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getCoins(Player p) throws SQLException {
String uuid = p.getUniqueId().toString();
Statement check = this.conn.createStatement();
ResultSet res = check.executeQuery("SELECT * FROM NetworkCoins WHERE UUID ='" + uuid + "';");
res.next();
int amount = res.getInt("Coins");
return Integer.valueOf(amount);
}
@EventHandler
public void PlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
try {
String uuid = p.getUniqueId().toString();
Statement check = this.conn.createStatement();
ResultSet res = check.executeQuery("SELECT UUID FROM NetworkCoins WHERE UUID = '" + uuid + "';");
if (!res.next()) {
Statement update = this.conn.createStatement();
update.executeUpdate("INSERT INTO NetworkCoins VALUES ('" + uuid + "',0);");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args)
{
if (cmd.getName().equalsIgnoreCase("coins"))
{
if (args.length == 0) {
try {
sender.sendMessage("§7Coins: §a" + getCoins((Player)sender));
}
catch (SQLException e) {
e.printStackTrace();
}
return true;
}
if ((args[0].equalsIgnoreCase("add")) &&
(sender.hasPermission("coins.add")))
{
if (args.length == 1) {
sender.sendMessage("§6Coins> /coins add {Player Name} {Amount}");
return true;
}
Player target = Bukkit.getPlayer(args[1]);
if (target == null) {
sender.sendMessage("§cPlayer not found!");
return true;
}
if (args.length == 2) {
sender.sendMessage("§6Coins> /coins add {Player Name} {Amount}");
return true;
}
int toAdd = Integer.parseInt(args[2]);
addCoins(target, Integer.valueOf(toAdd));
sender.sendMessage("§6Coins> You have added §a" + toAdd + " §6to §7" + args[1] + "'s §6account");
}
if ((args[0].equalsIgnoreCase("remove")) &&
(sender.hasPermission("coins.remove"))) {
if (args.length == 1) {
sender.sendMessage("§6Coins> /coins remove {Player Name} {Amount}");
return true;
}
Player target = Bukkit.getPlayer(args[1]);
if (args.length == 2) {
sender.sendMessage("§6Coins> /coins remove {Player Name} {Amount}");
return true;
}
int toRemove = Integer.parseInt(args[2]);
removeCoins(target, Integer.valueOf(toRemove));
sender.sendMessage("§6Coins> You have removed §a" + toRemove + " §6from §7" + args[1] + "'s §6account");
}
}
return false;
}
}