Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a2dfad
commit 4ed2375
Showing
9 changed files
with
780 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package io.github.andrewward2001.sqlecon; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
/** | ||
* Abstract Database class, serves as a base for any connection method (MySQL, | ||
* SQLite, etc.) | ||
* | ||
* @author -_Husky_- | ||
* @author tips48 | ||
*/ | ||
public abstract class Database { | ||
|
||
protected Connection connection; | ||
|
||
/** | ||
* Creates a new Database | ||
* | ||
*/ | ||
protected Database() { | ||
this.connection = null; | ||
} | ||
|
||
/** | ||
* Opens a connection with the database | ||
* | ||
* @return Opened connection | ||
* @throws SQLException | ||
* if the connection can not be opened | ||
* @throws ClassNotFoundException | ||
* if the driver cannot be found | ||
*/ | ||
public abstract Connection openConnection() throws SQLException, | ||
ClassNotFoundException; | ||
|
||
/** | ||
* Checks if a connection is open with the database | ||
* | ||
* @return true if the connection is open | ||
* @throws SQLException | ||
* if the connection cannot be checked | ||
*/ | ||
public boolean checkConnection() throws SQLException { | ||
return connection != null && !connection.isClosed(); | ||
} | ||
|
||
/** | ||
* Gets the connection with the database | ||
* | ||
* @return Connection with the database, null if none | ||
*/ | ||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
/** | ||
* Closes the connection with the database | ||
* | ||
* @return true if successful | ||
* @throws SQLException | ||
* if the connection cannot be closed | ||
*/ | ||
public boolean closeConnection() throws SQLException { | ||
if (connection == null) { | ||
return false; | ||
} | ||
connection.close(); | ||
return true; | ||
} | ||
|
||
|
||
/** | ||
* Executes a SQL Query<br> | ||
* | ||
* If the connection is closed, it will be opened | ||
* | ||
* @param query | ||
* Query to be run | ||
* @return the results of the query | ||
* @throws SQLException | ||
* If the query cannot be executed | ||
* @throws ClassNotFoundException | ||
* If the driver cannot be found; see {@link #openConnection()} | ||
*/ | ||
public ResultSet querySQL(String query) throws SQLException, | ||
ClassNotFoundException { | ||
if (!checkConnection()) { | ||
openConnection(); | ||
} | ||
|
||
Statement statement = connection.createStatement(); | ||
|
||
ResultSet result = statement.executeQuery(query); | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Executes an Update SQL Query<br> | ||
* See {@link java.sql.Statement#executeUpdate(String)}<br> | ||
* If the connection is closed, it will be opened | ||
* | ||
* @param query | ||
* Query to be run | ||
* @return Result Code, see {@link java.sql.Statement#executeUpdate(String)} | ||
* @throws SQLException | ||
* If the query cannot be executed | ||
* @throws ClassNotFoundException | ||
* If the driver cannot be found; see {@link #openConnection()} | ||
*/ | ||
public int updateSQL(String query) throws SQLException, | ||
ClassNotFoundException { | ||
if (!checkConnection()) { | ||
openConnection(); | ||
} | ||
|
||
Statement statement = connection.createStatement(); | ||
|
||
int result = statement.executeUpdate(query); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package io.github.andrewward2001.sqlecon; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
public class MoneyCommand implements CommandExecutor { | ||
|
||
private final SQLEconomy plugin; | ||
|
||
private String moneyUnit; | ||
|
||
public MoneyCommand(SQLEconomy plugin) { | ||
this.plugin = plugin; | ||
|
||
moneyUnit = SQLEconomy.moneyUnit; | ||
} | ||
|
||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | ||
if (cmd.getName().equalsIgnoreCase("money")) { | ||
if (args.length == 0) { | ||
Player player; | ||
if (sender instanceof Player) { | ||
player = (Player) sender; | ||
int money = SQLEconomyActions.getMoney(player.getUniqueId()); | ||
|
||
sender.sendMessage(ChatColor.GREEN + "Money: " + ChatColor.WHITE + money + " " + moneyUnit); | ||
|
||
return true; | ||
} else { | ||
sender.sendMessage(ChatColor.RED + "You must be a player to do that!"); | ||
|
||
return false; | ||
} | ||
|
||
} else if (args.length == 1) { | ||
|
||
if (args[0].equalsIgnoreCase("give")) { | ||
sender.sendMessage( | ||
ChatColor.RED + "Incorrect Usage! /money give [player] <amount> --> player is optional."); | ||
return false; | ||
} else if (args[0].equalsIgnoreCase("take") || args[0].equalsIgnoreCase("remove")) { | ||
sender.sendMessage( | ||
ChatColor.RED + "Incorrect Usage! /money remove [player] <amount> --> player is optional."); | ||
return false; | ||
} else { | ||
int money = SQLEconomyActions.getMoney(args[0]); | ||
|
||
sender.sendMessage(ChatColor.GREEN + "Money: " + ChatColor.WHITE + money + " " + moneyUnit); | ||
|
||
return true; | ||
} | ||
} else if (args.length == 2) { | ||
Player player; | ||
if (sender instanceof Player) { | ||
player = (Player) sender; | ||
if (args[0].equalsIgnoreCase("give")) { | ||
if (plugin.isInteger(args[1])) { | ||
SQLEconomyActions.giveMoney(player.getUniqueId(), Integer.parseInt(args[1])); | ||
sender.sendMessage("Gave " + ChatColor.GREEN + args[1] + ChatColor.WHITE + " " + moneyUnit | ||
+ " to yourself. Congratulations."); | ||
|
||
return true; | ||
} else { | ||
sender.sendMessage("Amount is not a number!"); | ||
|
||
return false; | ||
} | ||
} else if (args[0].equalsIgnoreCase("take") || args[0].equalsIgnoreCase("remove")) { | ||
if (plugin.isInteger(args[1])) { | ||
SQLEconomyActions.removeMoney(player.getUniqueId(), Integer.parseInt(args[1])); | ||
sender.sendMessage("Removed " + ChatColor.RED + args[1] + ChatColor.WHITE + " " + moneyUnit | ||
+ " from yourself."); | ||
|
||
return true; | ||
} else { | ||
sender.sendMessage("Amount is not a number!"); | ||
|
||
return false; | ||
} | ||
} | ||
} else { | ||
sender.sendMessage(ChatColor.RED + "You must be a player to do that!"); | ||
|
||
return false; | ||
} | ||
} else if (args.length == 3) { | ||
if (sender instanceof Player) { | ||
if (args[0].equalsIgnoreCase("give")) { | ||
if (plugin.isInteger(args[2])) { | ||
SQLEconomyActions.giveMoney(args[1], Integer.parseInt(args[2])); | ||
sender.sendMessage("Gave " + ChatColor.GREEN + args[2] + ChatColor.WHITE + " " + moneyUnit | ||
+ " to " + args[1] + "."); | ||
|
||
return true; | ||
} else { | ||
sender.sendMessage("Amount is not a number!"); | ||
|
||
return false; | ||
} | ||
} else if (args[0].equalsIgnoreCase("take") || args[0].equalsIgnoreCase("remove")) { | ||
if (plugin.isInteger(args[2])) { | ||
SQLEconomyActions.removeMoney(args[1], Integer.parseInt(args[2])); | ||
sender.sendMessage("Removed " + ChatColor.RED + args[2] + ChatColor.WHITE + " " + moneyUnit | ||
+ " from " + args[1] + "."); | ||
|
||
return true; | ||
} else { | ||
sender.sendMessage("Amount is not a number!"); | ||
|
||
return false; | ||
} | ||
} | ||
} else { | ||
sender.sendMessage(ChatColor.RED + "You must be a player to do that!"); | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.github.andrewward2001.sqlecon; | ||
|
||
import java.sql.SQLException; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import io.github.andrewward2001.sqlecon.mysql.*; | ||
|
||
import java.sql.Connection; | ||
|
||
public class SQLEconomy extends JavaPlugin implements Listener { | ||
|
||
private Plugin plugin; | ||
|
||
// Making variables for SQL connection static, escaping errors | ||
private static String host; | ||
private static String port; | ||
private static String database; | ||
private static String table; | ||
private static String user; | ||
private static String pass; | ||
private static String defMoney; | ||
|
||
public static String moneyUnit; | ||
|
||
private static MySQL MySQL; | ||
static Connection c; | ||
|
||
public void onDisable() { | ||
} | ||
|
||
public void onEnable() { | ||
this.saveDefaultConfig(); | ||
getConfig().options().copyDefaults(true); | ||
this.saveConfig(); | ||
|
||
host = getConfig().getString("DatabaseHostIP"); | ||
port = getConfig().getString("DatabasePort"); | ||
database = getConfig().getString("DatabaseName"); | ||
table = getConfig().getString("DatabaseTable"); | ||
user = getConfig().getString("DatabaseUsername"); | ||
pass = getConfig().getString("DatabasePassword"); | ||
defMoney = getConfig().getString("DefaultMoney"); | ||
|
||
moneyUnit = getConfig().getString("MoneyUnit"); | ||
|
||
MySQL = new MySQL(host, port, database, user, pass); | ||
try { | ||
c = MySQL.openConnection(); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
SQLEconomyActions.createTable(); | ||
|
||
this.getCommand("money").setExecutor(new MoneyCommand(this)); | ||
Bukkit.getServer().getPluginManager().registerEvents(new SQLEconomyListener(table, defMoney, c), this); | ||
} | ||
|
||
public static String getTable() { | ||
return table; | ||
} | ||
|
||
public SQLEconomyAPI getAPI() { | ||
return new SQLEconomyAPI(c); | ||
} | ||
|
||
public boolean isInteger(String str) { | ||
if (str == null) { | ||
return false; | ||
} | ||
int length = str.length(); | ||
if (length == 0) { | ||
return false; | ||
} | ||
int i = 0; | ||
if (str.charAt(0) == '-') { | ||
if (length == 1) { | ||
return false; | ||
} | ||
i = 1; | ||
} | ||
for (; i < length; i++) { | ||
char c = str.charAt(i); | ||
if (c < '0' || c > '9') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
Oops, something went wrong.