Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lupe.basicland.tokeny;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.TimeUnit;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerCommandPreprocessEvent;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- public class main extends JavaPlugin implements Listener{
- public static String username;
- public static String password;
- public static int port;
- public static String url;
- public static String table;
- public static String database;
- public static String host;
- public static String notenoughtokens;
- static Connection connection;
- public static int playerbalance;
- public void onEnable() {
- String root = this.getDataFolder().getAbsolutePath();
- File file =new File(root+"/config.yml");
- if(file.exists()){
- host = this.getConfig().getString("options.host");
- notenoughtokens = this.getConfig().getString("messages.not_enough_tokens");
- notenoughtokens = notenoughtokens.replace("&", "ยง");
- System.out.println("Host nalezen!");
- port = this.getConfig().getInt("options.port");
- System.out.println("port nalezen!");
- password = this.getConfig().getString("options.password");
- System.out.println("heslo nalezeno!");
- username = this.getConfig().getString("options.username");
- System.out.println("uzivatel nalezen!");
- database = this.getConfig().getString("options.database");
- System.out.println("databaze nalezena!");
- table = this.getConfig().getString("options.table");
- System.out.println("tabulka nalezena!");
- url = ("jdbc:mysql://"+host+":"+port);
- getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Nebyl nalezen driver pro mysql.");
- }
- try {
- connection = DriverManager.getConnection(url,username,password);
- System.out.println("Uspesne napojeno na databazi!");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }else{
- System.out.println("Creating new config.yml...");
- this.saveDefaultConfig();
- }
- }
- @EventHandler
- public void checkoncommand(PlayerCommandPreprocessEvent event) throws InterruptedException {
- String command = event.getMessage();
- command = command.substring(1);
- command = command.replace(" ", "_");
- if (this.getConfig().getInt(command+".price") != 0) {
- String commandtoexecute = this.getConfig().getString(command+".run_command");
- String player = ""+event.getPlayer().getName();
- commandtoexecute = commandtoexecute.replaceAll("%player%", player);
- event.setCancelled(true);
- String price = ""+this.getConfig().getInt(command+".price");
- String UUID = event.getPlayer().getUniqueId().toString();
- url = ("jdbc:mysql://"+host+":"+port);
- int pricefinal = Integer.parseInt(price);
- if (!price.equals("0")) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Nebyl nalezen driver pro mysql.");
- }
- try {
- connection = DriverManager.getConnection(url,username,password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- String sql = "select balance from "+database+"."+table+" where account_id='"+UUID+"'";
- try {
- PreparedStatement stmt = connection.prepareStatement(sql);
- ResultSet results = stmt.executeQuery(sql);
- results.next();
- playerbalance = results.getInt(1);
- } catch (SQLException e) {
- e.printStackTrace();
- };
- if (playerbalance >= pricefinal) {
- Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), commandtoexecute);
- TimeUnit.MILLISECONDS.sleep(20);
- String sqll = "update "+database+"."+table+" set balance="+(playerbalance-pricefinal)+" where account_id='"+UUID+"'";
- try {
- PreparedStatement stmt = connection.prepareStatement(sqll);
- stmt.executeUpdate(sqll);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- else {
- event.getPlayer().sendMessage(notenoughtokens);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement