Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package at.coins.coins;
- import net.md_5.bungee.api.connection.ProxiedPlayer;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- public class CoinsAPI {
- public static void createTable(){
- try{
- PreparedStatement ps = MySQL.getStatement("CREATE TABLE IF NOT EXISTS Coins (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
- ps.executeUpdate();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static void register(ProxiedPlayer p){
- try{
- PreparedStatement ps = MySQL.getStatement("INSERT INTO Coins (Spielername, UUID, Coins) VALUES (?, ?, ?)");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.setInt(3, 0);
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static boolean isRegistered(ProxiedPlayer p){
- try{
- PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE UUID= ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet rs = ps.executeQuery();
- boolean user = rs.next();
- rs.close();
- rs.close();
- return user;
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isRegistered(String name){
- try{
- PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE Spielername= ?");
- ps.setString(1, name);
- ResultSet rs = ps.executeQuery();
- boolean user = rs.next();
- rs.close();
- rs.close();
- return user;
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return false;
- }
- public static int getCoins(ProxiedPlayer p){
- try{
- PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE UUID= ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet rs = ps.executeQuery();
- rs.next();
- int coins = rs.getInt("Coins");
- rs.close();
- ps.close();
- return coins;
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return -1;
- }
- public static void setCoins(ProxiedPlayer p, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
- ps.setInt(1, coins);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static void addCoins(ProxiedPlayer p, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
- ps.setInt(1, getCoins(p) + coins);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static void removeCoins(ProxiedPlayer p, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
- ps.setInt(1, getCoins(p) - coins);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static int getCoins(String name){
- try{
- PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE Spielername= ?");
- ps.setString(1, name);
- ResultSet rs = ps.executeQuery();
- rs.next();
- int coins = rs.getInt("Coins");
- rs.close();
- ps.close();
- return coins;
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return -1;
- }
- public static void setCoins(String name, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
- ps.setInt(1, coins);
- ps.setString(2, name);
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static void addCoins(String name, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
- ps.setInt(1, getCoins(name) + coins);
- ps.setString(2, name);
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public static void removeCoins(String name, int coins){
- try{
- PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
- ps.setInt(1, getCoins(name) - coins);
- ps.setString(2, name);
- ps.executeUpdate();
- ps.close();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
- package at.coins.coins;
- import net.md_5.bungee.api.CommandSender;
- import net.md_5.bungee.api.connection.ProxiedPlayer;
- import net.md_5.bungee.api.plugin.Command;
- public class CoinsCMD extends Command {
- public CoinsCMD(String name) {
- super(name);
- }
- @Override
- public void execute(CommandSender cs, String[] args) {
- if(!(cs instanceof ProxiedPlayer)){
- }
- ProxiedPlayer p = (ProxiedPlayer)cs;
- if(args.length == 0){
- int coins = CoinsAPI.getCoins(p);
- p.sendMessage(Main.PREFIX + "§bDu hast aktuell §9" + coins + " §bCoins.");
- } else if(args.length == 1){
- if(cs.hasPermission("server.coins")) {
- if(args[0].equalsIgnoreCase("add")){
- p.sendMessage(Main.PREFIX + "§c/coins add <Spieler> <Anzahl>");
- }else if(args[0].equalsIgnoreCase("remove")){
- p.sendMessage(Main.PREFIX + "§c/coins remove <Spieler> <Anzahl>");
- }else if(args[0].equalsIgnoreCase("set")){
- p.sendMessage(Main.PREFIX + "§c/coins set <Spieler> <Anzahl>");
- }else{
- String name = args[0];
- if(CoinsAPI.isRegistered(name)){
- int coins = CoinsAPI.getCoins(name);
- p.sendMessage(Main.PREFIX + "§bDer Spieler §9" + name + "§b hat aktuell §9" + coins + " §bCoins.");
- }else{
- p.sendMessage(Main.PREFIX + "§cDieser Spieler ist nicht registriert.");
- }
- }
- } else {
- ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
- }
- }else if(args.length == 2){
- if(cs.hasPermission("server.coins")) {
- if(args[0].equalsIgnoreCase("add")){
- p.sendMessage(Main.PREFIX + "§c/coins add <Spieler> <Anzahl>");
- }else if(args[0].equalsIgnoreCase("remove")){
- p.sendMessage(Main.PREFIX + "§c/coins remove <Spieler> <Anzahl>");
- }else if(args[0].equalsIgnoreCase("set")){
- p.sendMessage(Main.PREFIX + "§c/coins set <Spieler> <Anzahl>");
- }else{
- p.sendMessage(Main.PREFIX + "§c/coins add|remove|set <Spieler> <Anzahl>");
- }
- } else {
- ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
- }
- }else if(args.length == 3){
- if(cs.hasPermission("server.coins")) {
- if(args[0].equalsIgnoreCase("add")){
- String name = args[1];
- if(CoinsAPI.isRegistered(name)){
- int coins = Integer.valueOf(args[2]);
- CoinsAPI.addCoins(name, coins);
- p.sendMessage(Main.PREFIX + "§bDu hast dem Spieler §9" + name + "§b erfolgreich §9" + coins + " §bCoins hinzugefügt.");
- }
- }else if(args[0].equalsIgnoreCase("remove")){
- String name = args[1];
- if(CoinsAPI.isRegistered(name)){
- int coins = Integer.valueOf(args[2]);
- CoinsAPI.removeCoins(name, coins);
- p.sendMessage(Main.PREFIX + "§bDu hast dem Spieler §9" + name + "§b erfolgreich §9" + coins + " §bCoins abgezogen.");
- }
- }else if(args[0].equalsIgnoreCase("set")){
- String name = args[1];
- if(CoinsAPI.isRegistered(name)){
- int coins = Integer.valueOf(args[2]);
- CoinsAPI.setCoins(name, coins);
- p.sendMessage(Main.PREFIX + "§bDu hast die Coins von §9" + name + "§b erfolgreich auf §9" + coins + " §bCoins gesetzt.");
- }
- }else{
- p.sendMessage(Main.PREFIX + "§c/coins add|remove|set <Spieler> <Anzahl>");
- }
- } else {
- ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
- }
- }
- }
- }
- package at.coins.coins;
- import net.md_5.bungee.api.connection.ProxiedPlayer;
- import net.md_5.bungee.api.event.ServerSwitchEvent;
- import net.md_5.bungee.api.plugin.Listener;
- import net.md_5.bungee.event.EventHandler;
- public class JoinEVENT implements Listener {
- @EventHandler
- public void onJoin(ServerSwitchEvent e) {
- ProxiedPlayer p = e.getPlayer();
- if(!CoinsAPI.isRegistered(p)){
- CoinsAPI.register(p);
- }
- }
- }
- package at.coins.coins;
- import net.md_5.bungee.api.ProxyServer;
- import net.md_5.bungee.api.plugin.Plugin;
- public class Main extends Plugin {
- public static String PREFIX = "§8[§1COINS§8] ";
- public static String NOPERM = PREFIX + "§cDu hast zu wenig Rechte!";
- @Override
- public void onEnable() {
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aGESTARTET : §2CODER: sowas");
- MySQL.connect();
- CoinsAPI.createTable();
- register();
- }
- @Override
- public void onDisable() {
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§4GESTOPPT");
- MySQL.disconnect();
- }
- public void register() {
- ProxyServer.getInstance().getPluginManager().registerCommand(this, new CoinsCMD("coins"));
- ProxyServer.getInstance().getPluginManager().registerListener(this, new JoinEVENT());
- }
- }
- package at.coins.coins;
- import net.md_5.bungee.api.ProxyServer;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL {
- public static String host = "localhost";
- public static String port = "3306";
- public static String database = "coins";
- public static String username = "root";
- public static String password = "1234";
- public static Connection con;
- public static boolean isConnected(){
- return con != null;
- }
- public static void connect(){
- if(!isConnected()){
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aEs konnte erfolgreich mit der Datenbank verbunden werden");
- } catch (SQLException e) {
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§cEs konnte nicht mit der Datenbank verbunden werden");
- }
- }
- }
- public static void disconnect(){
- try {
- con.close();
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aDie Verbindung zur Datenbank konnte erfolgreich geschlossen werden");
- } catch (SQLException e) {
- ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§cDie Verbindung zur Datenbank konnte nicht geschlossen werden");
- }
- }
- public static PreparedStatement getStatement(String sql){
- if(isConnected()){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement(sql);
- return ps;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public static ResultSet getResult(String sql){
- if(isConnected()){
- PreparedStatement ps;
- ResultSet rs;
- try {
- ps = getStatement(sql);
- rs = ps.executeQuery();
- return rs;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement