Advertisement
Guest User

Untitled

a guest
Dec 27th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Random;
  11. import java.util.UUID;
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.configuration.file.FileConfiguration;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17. import Vanade.NickAPI.Listener.AsyncPlayerPreLoginListener;
  18. import Vanade.NickAPI.Listener.PlayerQuitListener;
  19. import net.minecraft.util.com.mojang.authlib.GameProfile;
  20.  
  21. public class Nick
  22. extends JavaPlugin
  23. {
  24. public static Nick plugin;
  25. public static ArrayList<String> nicks = new ArrayList();
  26. public static HashMap<String, String> nickname = new HashMap();
  27. public static HashMap<String, String> nicktype = new HashMap();
  28. public static HashMap<String, GameProfile> gameprofile = new HashMap();
  29. public static File file = new File("plugins/Nick", "config.yml");
  30. public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  31. public static String prefix = "§5Nick §8• ";
  32. public static String noperm = prefix + "§cDu hast keinen Zugriff auf diesen Befehl.";
  33. public static String host = cfg.getString("MySQL.Host");
  34. public static String port = cfg.getString("MySQL.Port");
  35. public static String database = cfg.getString("MySQL.Datenbank");
  36. public static String username = cfg.getString("MySQL.Benutzername");
  37. public static String password = cfg.getString("MySQL.Passwort");
  38. public static Connection connection;
  39.  
  40. public void onEnable()
  41. {
  42. plugin = this;
  43. saveDefaultConfig();
  44. reloadConfig();
  45. registerListener();
  46. for (String nicknames : cfg.getStringList("Nicknames")) {
  47. nicks.add(nicknames);
  48. }
  49. connect();
  50. createTableIfNotExists();
  51. }
  52.  
  53. public void onDisable() {}
  54.  
  55. public void registerListener()
  56. {
  57. Bukkit.getPluginManager().registerEvents(new AsyncPlayerPreLoginListener(), this);
  58. Bukkit.getPluginManager().registerEvents(new PlayerQuitListener(), this);
  59. }
  60.  
  61. public static String randomNick()
  62. {
  63. Random r = new Random();
  64. String nick = (String)nicks.get(r.nextInt(nicks.size()));
  65. for (Player players : Bukkit.getServer().getOnlinePlayers()) {
  66. if (players.getDisplayName().equalsIgnoreCase(nick)) {
  67. return randomNick();
  68. }
  69. }
  70. return nick;
  71. }
  72.  
  73. public static GameProfile getGameProfile(String player)
  74. {
  75. GameProfile profile = null;
  76. try
  77. {
  78. profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(player));
  79. }
  80. catch (IOException ex)
  81. {
  82. try
  83. {
  84. profile = GameProfileBuilder.fetch(UUID.randomUUID());
  85. }
  86. catch (IOException localIOException1) {}
  87. }
  88. return profile;
  89. }
  90.  
  91. public static Connection getConnection()
  92. {
  93. return connection;
  94. }
  95.  
  96. public static boolean isConnected()
  97. {
  98. return connection != null;
  99. }
  100.  
  101. public static void connect()
  102. {
  103. if (!isConnected()) {
  104. try
  105. {
  106. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  107. }
  108. catch (SQLException ex)
  109. {
  110. ex.printStackTrace();
  111. }
  112. }
  113. }
  114.  
  115. public static void disconnect()
  116. {
  117. if (isConnected()) {
  118. try
  119. {
  120. connection.close();
  121. }
  122. catch (SQLException ex)
  123. {
  124. ex.printStackTrace();
  125. }
  126. }
  127. }
  128.  
  129. public static void createTableIfNotExists()
  130. {
  131. if (isConnected()) {
  132. try
  133. {
  134. PreparedStatement ps1 = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Nicks (Spielername VARCHAR(100), UUID VARCHAR(100), Nickname VARCHAR(100), Art VARCHAR(100))");
  135. ps1.executeUpdate();
  136. ps1.close();
  137. }
  138. catch (SQLException ex)
  139. {
  140. ex.printStackTrace();
  141. }
  142. }
  143. }
  144.  
  145. public static boolean isUserExisting(UUID uuid)
  146. {
  147. try
  148. {
  149. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
  150. ps.setString(1, uuid.toString());
  151. ResultSet result = ps.executeQuery();
  152. boolean user = result.next();
  153. result.close();
  154. ps.close();
  155. return user;
  156. }
  157. catch (Exception ex)
  158. {
  159. ex.printStackTrace();
  160. }
  161. return false;
  162. }
  163.  
  164. public static boolean isNickExisting(String nickname)
  165. {
  166. try
  167. {
  168. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE Nickname = ?");
  169. ps.setString(1, nickname);
  170. ResultSet result = ps.executeQuery();
  171. boolean user = result.next();
  172. result.close();
  173. ps.close();
  174. return user;
  175. }
  176. catch (Exception ex)
  177. {
  178. ex.printStackTrace();
  179. }
  180. return false;
  181. }
  182.  
  183. public static void registerPlayer(String name, UUID uuid)
  184. {
  185. if (isUserExisting(uuid)) {
  186. return;
  187. }
  188. try
  189. {
  190. PreparedStatement ps = connection.prepareStatement("INSERT INTO Nicks (Spielername, UUID, Nickname, Art) VALUES (?, ?, ?, ?)");
  191. ps.setString(1, name);
  192. ps.setString(2, uuid.toString());
  193. ps.setString(3, "-");
  194. ps.setString(4, "-");
  195. ps.execute();
  196. ps.close();
  197. }
  198. catch (Exception ex)
  199. {
  200. ex.printStackTrace();
  201. }
  202. }
  203.  
  204. public static void setNickName(Player p, String nickname)
  205. {
  206. try
  207. {
  208. PreparedStatement ps = connection.prepareStatement("UPDATE Nicks SET Nickname = ? WHERE UUID = ?");
  209. ps.setString(1, nickname);
  210. ps.setString(2, p.getUniqueId().toString());
  211. ps.executeUpdate();
  212. ps.close();
  213. }
  214. catch (Exception ex)
  215. {
  216. ex.printStackTrace();
  217. }
  218. }
  219.  
  220. public static void setNickType(Player p, String type)
  221. {
  222. try
  223. {
  224. PreparedStatement ps = connection.prepareStatement("UPDATE Nicks SET Art = ? WHERE UUID = ?");
  225. ps.setString(1, type);
  226. ps.setString(2, p.getUniqueId().toString());
  227. ps.executeUpdate();
  228. ps.close();
  229. }
  230. catch (Exception ex)
  231. {
  232. ex.printStackTrace();
  233. }
  234. }
  235.  
  236. public static String getNickname(UUID uuid)
  237. {
  238. try
  239. {
  240. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
  241. ps.setString(1, uuid.toString());
  242. ResultSet result = ps.executeQuery();
  243. result.next();
  244. String nickname = result.getString("Nickname");
  245. result.close();
  246. ps.close();
  247. return nickname;
  248. }
  249. catch (Exception ex)
  250. {
  251. ex.printStackTrace();
  252. }
  253. return "null";
  254. }
  255.  
  256. public static String getNickType(UUID uuid)
  257. {
  258. try
  259. {
  260. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nicks WHERE UUID = ?");
  261. ps.setString(1, uuid.toString());
  262. ResultSet result = ps.executeQuery();
  263. result.next();
  264. String type = result.getString("Art");
  265. result.close();
  266. ps.close();
  267. return type;
  268. }
  269. catch (Exception ex)
  270. {
  271. ex.printStackTrace();
  272. }
  273. return "null";
  274. }
  275.  
  276. public static void setUnNicked(Player p)
  277. {
  278. try
  279. {
  280. PreparedStatement ps = connection.prepareStatement("DELETE FROM Nicks WHERE UUID = ?");
  281. ps.setString(1, p.getUniqueId().toString());
  282. ps.executeUpdate();
  283. ps.close();
  284. }
  285. catch (Exception ex)
  286. {
  287. ex.printStackTrace();
  288. }
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement