Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Coins
- implements CommandExecutor
- {
- public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args)
- {
- if ((cmd.getName().equalsIgnoreCase("coins")) &&
- ((sender instanceof Player)))
- {
- final Player p = (Player)sender;
- if (args.length == 0) {
- p.sendMessage(Coin.prefix + "�7Du hast derzeit �6" + Coin.getCoins(p) + " �7Coins");
- }
- if (args.length == 1)
- {
- String name = args[0];
- final Player t = Bukkit.getServer().getPlayerExact(name);
- if (t != null)
- {
- p.sendMessage(Coin.prefix + " �7Datenbank wird ausgelesen...");
- Bukkit.getScheduler().scheduleAsyncDelayedTask(Coin.plugin, new Runnable()
- {
- public void run()
- {
- p.sendMessage(Coin.prefix + "�e" + t.getName() + " �7hat derzeit �6" + Coin.getCoins(p) + " �7Coins");
- }
- }, 35L);
- }
- else
- {
- OfflinePlayer oplayer = Bukkit.getOfflinePlayer(name);
- UUID uuid = oplayer.getUniqueId();
- if (Coin.isPlayerExisting(uuid)) {
- p.sendMessage(Coin.prefix + "�e" + UUIDFetcher.getName(uuid) + " �7hat derzeit �6" + Coin.getCoins(t) + " �7Coins");
- } else {
- p.sendMessage(Coin.prefix + "�cDer angegebene Spieler ist der Datenbank unbekannt");
- }
- }
- }
- }
- if (cmd.getName().equalsIgnoreCase("addcoins")) {
- if (args.length == 2)
- {
- if (sender.hasPermission("lostdos.addcoins"))
- {
- Player t = Bukkit.getPlayer(args[0]);
- int i = Integer.valueOf(args[1]).intValue();
- if (t != null)
- {
- sender.sendMessage(Coin.prefix + "�7Du hast dem Spieler �e" + t.getName() + " �c" + i + " �7Coins geschenkt");
- if ((sender instanceof Player)) {
- t.sendMessage(Coin.prefix + "�7Du hast �c" + args[1] + " �7Coins von �e" + sender.getName() + " �7erhalten");
- }
- Coin.addCoins(t, i);
- }
- else
- {
- sender.sendMessage(Coin.prefix + "�cDieser Spieler ist nicht Online");
- }
- }
- else
- {
- sender.sendMessage(Coin.prefix + Coin.noperm);
- }
- }
- else {
- sender.sendMessage(Coin.prefix + "�cVerwende�8: �c/addcoins <�eSpieler�c> <�eAnzahl�c>");
- }
- }
- if (cmd.getName().equalsIgnoreCase("pay")) {
- if (args.length == 2)
- {
- Player t = Bukkit.getPlayer(args[0]);
- int i = Integer.valueOf(args[1]).intValue();
- if (t != null) {
- sender.sendMessage(Coin.prefix + "�7Du hast dem Spieler �e" + t.getName() + " �c" + i + " �7Coins bezahlt");
- } else {
- sender.sendMessage(Coin.prefix + "�cDieser Spieler ist nicht Online");
- }
- }
- else
- {
- sender.sendMessage(Coin.prefix + "�cVerwende�8: �c/pay <�eSpieler�c> <�eAnzahl�c>");
- }
- }
- return false;
- }
- }
- public class AsyncPlayerPreLoginListener
- implements Listener
- {
- @EventHandler
- public void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent e)
- {
- UUID uuid = e.getUniqueId();
- String name = e.getName();
- if (e.getLoginResult() == AsyncPlayerPreLoginEvent.Result.ALLOWED)
- {
- Coin.registerPlayer(uuid, name);
- Coin.setPlayerName(uuid, name);
- }
- }
- }
- public class Coin
- extends JavaPlugin
- {
- public static Coin plugin;
- public static File file = new File("plugins/Coins", "config.yml");
- public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- public static String prefix = "�8[�eCoins�8] ";
- public static String noperm = "�7Der gew�nschte Befehl ist uns nicht bekannt";
- public static String unknowncmd = "�cDieser Befehl existiert nicht.";
- public static String host = cfg.getString("MySQL.Host");
- public static String port = cfg.getString("MySQL.Port");
- public static String database = cfg.getString("MySQL.Datenbank");
- public static String username = cfg.getString("MySQL.Benutzername");
- public static String password = cfg.getString("MySQL.Passwort");
- public static Connection connection;
- public void onEnable()
- {
- plugin = this;
- saveDefaultConfig();
- reloadConfig();
- registerCommands();
- registerListener();
- Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
- {
- public void run() {}
- }, 12000L, 12000L);
- connect();
- createTableIfNotExists();
- Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
- {
- public void run() {}
- }, 12000L, 12000L);
- }
- public void onDisable() {}
- public static void registerCommands()
- {
- plugin.getCommand("coins").setExecutor(new Coins());
- plugin.getCommand("addcoins").setExecutor(new Coins());
- }
- public static void registerListener()
- {
- Bukkit.getPluginManager().registerEvents(new AsyncPlayerPreLoginListener(), plugin);
- }
- public static Connection getConnection()
- {
- return connection;
- }
- public static boolean isConnected()
- {
- return connection != null;
- }
- public static void connect()
- {
- if (!isConnected()) {
- try
- {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void disconnect()
- {
- if (isConnected()) {
- try
- {
- connection.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static void createTableIfNotExists()
- {
- if (isConnected()) {
- try
- {
- PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Coins (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
- ps.executeUpdate();
- ps.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isPlayerExisting(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- boolean isExisting = result.next();
- result.close();
- ps.close();
- return isExisting;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isPlayerExisting(UUID uuid)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- boolean isExisting = result.next();
- result.close();
- ps.close();
- return isExisting;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static void registerPlayer(UUID uuid, String name)
- {
- if (isPlayerExisting(uuid)) {
- return;
- }
- try
- {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO Coins (Spielername, UUID, Coins) VALUES (?, ?, ?)");
- ps.setString(1, name);
- ps.setString(2, uuid.toString());
- ps.setInt(3, 0);
- ps.execute();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void setPlayerName(UUID uuid, String name)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE Coins SET Spielername = ? WHERE UUID = ?");
- ps.setString(1, name);
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static int getCoins(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int coins = result.getInt("Coins");
- result.close();
- ps.close();
- return coins;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getCoins(UUID uuid)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int coins = result.getInt("Coins");
- result.close();
- ps.close();
- return coins;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
- public static void setCoins(Player p, int coins)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("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 setCoins(UUID uuid, int coins)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE Coins SET Coins = ? WHERE UUID = ?");
- ps.setInt(1, coins);
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void addCoins(Player p, int coins)
- {
- setCoins(p, getCoins(p) + coins);
- }
- public static void addCoins(UUID uuid, int coins)
- {
- setCoins(uuid, getCoins(uuid) + coins);
- }
- public static void removeCoins(Player p, int coins)
- {
- setCoins(p, getCoins(p) - coins);
- }
- public static void removeCoins(UUID uuid, int coins)
- {
- setCoins(uuid, getCoins(uuid) - coins);
- }
- }
- // INTERNAL ERROR //
- MySQL:
- Host: host
- Port: port
- Datenbank: database
- Benutzername: username
- Passwort: password
- name: Coins
- version: 1.0
- author: JailBreaker
- main: eu.lostdos.Coins.Coin
- commands:
- coins:
- description: Zeigt die Coins eines Spielers an.
- addcoins:
- description: Vergibt Spielern Coin.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement