Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import Vanade.NickAPI.Listener.AsyncPlayerPreLoginListener;
- import Vanade.NickAPI.Listener.PlayerQuitListener;
- import net.minecraft.util.com.mojang.authlib.GameProfile;
- public class Nick
- extends JavaPlugin
- {
- public static Nick plugin;
- public static ArrayList<String> nicks = new ArrayList();
- public static HashMap<String, String> nickname = new HashMap();
- public static HashMap<String, String> nicktype = new HashMap();
- public static HashMap<String, GameProfile> gameprofile = new HashMap();
- public static File file = new File("plugins/Nick", "config.yml");
- public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- public static String prefix = "§5Nick §8• ";
- public static String noperm = prefix + "§cDu hast keinen Zugriff auf diesen Befehl.";
- 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();
- registerListener();
- for (String nicknames : cfg.getStringList("Nicknames")) {
- nicks.add(nicknames);
- }
- connect();
- createTableIfNotExists();
- }
- public void onDisable() {}
- public void registerListener()
- {
- Bukkit.getPluginManager().registerEvents(new AsyncPlayerPreLoginListener(), this);
- Bukkit.getPluginManager().registerEvents(new PlayerQuitListener(), this);
- }
- public static String randomNick()
- {
- Random r = new Random();
- String nick = (String)nicks.get(r.nextInt(nicks.size()));
- for (Player players : Bukkit.getServer().getOnlinePlayers()) {
- if (players.getDisplayName().equalsIgnoreCase(nick)) {
- return randomNick();
- }
- }
- return nick;
- }
- public static GameProfile getGameProfile(String player)
- {
- GameProfile profile = null;
- try
- {
- profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(player));
- }
- catch (IOException ex)
- {
- try
- {
- profile = GameProfileBuilder.fetch(UUID.randomUUID());
- }
- catch (IOException localIOException1) {}
- }
- return profile;
- }
- 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 ps1 = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Nicks (Spielername VARCHAR(100), UUID VARCHAR(100), Nickname VARCHAR(100), Art VARCHAR(100))");
- ps1.executeUpdate();
- ps1.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isUserExisting(UUID uuid)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isNickExisting(String nickname)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE Nickname = ?");
- ps.setString(1, nickname);
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return false;
- }
- public static void registerPlayer(String name, UUID uuid)
- {
- if (isUserExisting(uuid)) {
- return;
- }
- try
- {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO Nicks (Spielername, UUID, Nickname, Art) VALUES (?, ?, ?, ?)");
- ps.setString(1, name);
- ps.setString(2, uuid.toString());
- ps.setString(3, "-");
- ps.setString(4, "-");
- ps.execute();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void setNickName(Player p, String nickname)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE Nicks SET Nickname = ? WHERE UUID = ?");
- ps.setString(1, nickname);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void setNickType(Player p, String type)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("UPDATE Nicks SET Art = ? WHERE UUID = ?");
- ps.setString(1, type);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static String getNickname(UUID uuid)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- String nickname = result.getString("Nickname");
- result.close();
- ps.close();
- return nickname;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return "null";
- }
- public static String getNickType(UUID uuid)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- String type = result.getString("Art");
- result.close();
- ps.close();
- return type;
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return "null";
- }
- public static void setUnNicked(Player p)
- {
- try
- {
- PreparedStatement ps = connection.prepareStatement("DELETE FROM Nicks WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement