Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.31 KB | None | 0 0
  1. package de.emeraldmc.nobots;
  2.  
  3. import de.emeraldmc.nobots.listeners.LoginListener;
  4. import de.emeraldmc.nobots.listeners.QuitListener;
  5. import de.emeraldmc.nobots.utils.*;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. import java.net.InetAddress;
  11. import java.net.InetSocketAddress;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14.  
  15. public class Main extends JavaPlugin {
  16.  
  17. private static Main instance;
  18. private Config config;
  19. private boolean attacked; //ToDo
  20. private boolean updating;
  21. private HashMap<Player, GoodPercentage> goodPlayers = new HashMap<>();
  22.  
  23. @Override
  24. public void onLoad() {
  25. instance = this;
  26. }
  27.  
  28. @Override
  29. public void onEnable() {
  30. config = new Config();
  31.  
  32. registerEvents();
  33.  
  34. ConfigManager.readConfig();
  35. ConfigManager.setConfig();
  36.  
  37. MySQL.createTables();
  38.  
  39. BlackList.updateBlackList();
  40. }
  41.  
  42. @Override
  43. public void onDisable() {
  44. }
  45.  
  46. private void registerEvents() {
  47. Bukkit.getPluginManager().registerEvents(new LoginListener(), this);
  48. Bukkit.getPluginManager().registerEvents(new QuitListener(), this);
  49. }
  50.  
  51. public static Main getInstance() {
  52. return instance;
  53. }
  54. public Config getConfiguration() {
  55. return config;
  56. }
  57. public void setAttacked(boolean attacked) {
  58. this.attacked = attacked;
  59. }
  60.  
  61. public boolean isUpdating() {
  62. return updating;
  63. }
  64.  
  65. public void setUpdating(boolean updating) {
  66. this.updating = updating;
  67. }
  68.  
  69. public HashMap<Player, GoodPercentage> getGoodPlayers() {
  70. return goodPlayers;
  71. }
  72. }
  73.  
  74. package de.emeraldmc.nobots.utils;
  75.  
  76. import de.emeraldmc.nobots.Main;
  77. import org.bukkit.Bukkit;
  78. import org.bukkit.entity.Player;
  79.  
  80. public class AutoWhiteList {
  81. private boolean started;
  82. private int taskId;
  83.  
  84. /**
  85. * @deprecated scheduleAsyncRepeatingTask
  86. */
  87. public void start() {
  88. if (started) stop();
  89. taskId = Bukkit.getScheduler().scheduleAsyncRepeatingTask(Main.getInstance(), new Runnable() {
  90. @Override
  91. public void run() {
  92. for (Player p : Bukkit.getOnlinePlayers()) {
  93. if (!WhiteList.isUUIDWhiteListed(p.getUniqueId())) {
  94. if (Main.getInstance().getGoodPlayers().get(p).calcPercentage() > 50) {
  95. WhiteList.addUUID(p.getUniqueId());
  96. for (Player op : Bukkit.getOnlinePlayers()) {
  97. if (op.isOp()) {
  98. ChatAPI.sendMessage(op, "Auto Whitelisted: "+p.getName());
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }, 0l, 120l); // 6 seconds
  106. }
  107. public void stop() {
  108. Bukkit.getServer().getScheduler().cancelTask(taskId);
  109. started = false;
  110. Debug.print("Stopped AutoWhiteList task ("+taskId+")");
  111. }
  112. }
  113.  
  114. package de.emeraldmc.nobots.utils;
  115.  
  116. import de.emeraldmc.nobots.Main;
  117. import org.bukkit.Bukkit;
  118.  
  119. import java.io.BufferedReader;
  120. import java.io.IOException;
  121. import java.io.InputStreamReader;
  122. import java.net.URL;
  123. import java.sql.PreparedStatement;
  124. import java.sql.ResultSet;
  125. import java.sql.SQLException;
  126.  
  127. public class BlackList {
  128. public static void addIp(String address) {
  129. if (address.length() > 15) return;
  130. if (BlackList.isIpBlackListed(address)) return;
  131. try {
  132. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("INSERT INTO NoBots_BlackList (IP) VALUES (?)");
  133. preparedStatement.setString(1, address);
  134. preparedStatement.executeUpdate();
  135. Debug.print("&7Added &c"+address+" &7to the BlackList");
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. public static void removeIp(String address) {
  141. if (address.length() > 15) return;
  142. if (!BlackList.isIpBlackListed(address)) return;
  143. try {
  144. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("DELETE FROM NoBots_BlackList WHERE IP = ?");
  145. preparedStatement.setString(1, address);
  146. preparedStatement.executeUpdate();
  147. Debug.print("Removed "+address+" from the BlackList");
  148. } catch (SQLException e) {
  149. e.printStackTrace();
  150. }
  151.  
  152. }
  153. public static boolean isIpBlackListed(String address) {
  154. try {
  155. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("SELECT * FROM NoBots_BlackList WHERE IP = ?");
  156. preparedStatement.setString(1, address);
  157. ResultSet rs = preparedStatement.executeQuery();
  158. return rs.next();
  159. } catch (SQLException e) {
  160. e.printStackTrace();
  161. }
  162. return false;
  163. }
  164. public static void updateBlackList() {
  165. Bukkit.getServer().getScheduler().runTaskAsynchronously(Main.getInstance(), new Runnable() {
  166. @Override
  167. public void run() {
  168. try {
  169. Main.getInstance().setUpdating(true);
  170. URL url = new URL("https://myip.ms/files/blacklist/csf/latest_blacklist.txt");
  171. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  172. String address;
  173. int z = 0;
  174. while ((address = in.readLine()) != null) {
  175. if (address.contains(":") || BlackList.isIpBlackListed(address) || address.startsWith("#")) {
  176. continue;
  177. }
  178. BlackList.addIp(address);
  179. z++;
  180. }
  181. in.close();
  182. Bukkit.getConsoleSender().sendMessage("§aBlackList updated"+"n"+"t"+"-> Added "+z+" bad addresses!");
  183. Main.getInstance().setUpdating(false);
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. });
  189. }
  190.  
  191. }
  192.  
  193. package de.emeraldmc.nobots.utils;
  194.  
  195. import org.bukkit.ChatColor;
  196. import org.bukkit.configuration.file.FileConfiguration;
  197.  
  198. import java.util.ArrayList;
  199. import java.util.UUID;
  200.  
  201. /**
  202. * @author Patrick Sommer
  203. */
  204. public class Config {
  205. private static FileConfiguration cfg = ConfigManager.getConfigFileConfiguration();
  206.  
  207. private boolean debug;
  208.  
  209. private boolean maxIp;
  210. private int maxIpLimit;
  211. private String maxIpKickMessage;
  212.  
  213. private int minimumAttackers;
  214.  
  215. private String updatingKickMessage;
  216.  
  217. private boolean enableCountryWhiteList;
  218. private ArrayList<String> whiteListedCountries;
  219. private String whiteListedCountriesKickMessage;
  220.  
  221. private ArrayList<UUID> whiteListedPlayers;
  222.  
  223. private String dbHost;
  224. private int dbPort;
  225. private String database;
  226. private String dbUser;
  227. private String dbPassword;
  228.  
  229. public Config() {
  230. debug = cfg.getBoolean("Config.debug");
  231.  
  232. maxIp = cfg.getBoolean("Config.MaxIp.enabled");
  233. maxIpLimit = cfg.getInt("Config.MaxIp.limit");
  234. maxIpKickMessage = cfg.getString("Config.MaxIp.kickMessage");
  235.  
  236. minimumAttackers = cfg.getInt("Config.Attacked.minimumAttackers");
  237.  
  238. updatingKickMessage = cfg.getString("Config.updatingKickMessage");
  239.  
  240. enableCountryWhiteList = cfg.getBoolean("Config.WhiteListedCountries.enabled");
  241. whiteListedCountries = new ArrayList<>(cfg.getStringList("Config.WhiteListedCountries.countries"));
  242. whiteListedCountriesKickMessage = cfg.getString("Config.WhiteListedCountries.kickMessage");
  243.  
  244. dbHost = cfg.getString("Config.MySQL.host");
  245. dbPort = cfg.getInt("Config.MySQL.port");
  246. database = cfg.getString("Config.MySQL.database");
  247. dbUser = cfg.getString("Config.MySQL.user");
  248. dbPassword = cfg.getString("Config.MySQL.password");
  249. }
  250.  
  251.  
  252.  
  253. public static FileConfiguration getCfg() {
  254. return cfg;
  255. }
  256.  
  257. public boolean isDebug() {
  258. return debug;
  259. }
  260.  
  261. public boolean isMaxIp() {
  262. return maxIp;
  263. }
  264.  
  265. public int getMaxIpLimit() {
  266. return maxIpLimit;
  267. }
  268.  
  269. public String getMaxIpKickMessage() {
  270. return maxIpKickMessage;
  271. }
  272.  
  273.  
  274. public int getMinimumAttackers() {
  275. return minimumAttackers;
  276. }
  277.  
  278.  
  279. public String getDbHost() {
  280. return dbHost;
  281. }
  282.  
  283. public int getDbPort() {
  284. return dbPort;
  285. }
  286.  
  287. public String getDatabase() {
  288. return database;
  289. }
  290.  
  291. public String getDbUser() {
  292. return dbUser;
  293. }
  294.  
  295. public String getDbPassword() {
  296. return dbPassword;
  297. }
  298.  
  299. public ArrayList<String> getWhiteListedCountries() {
  300. return whiteListedCountries;
  301. }
  302.  
  303. public boolean isEnableCountryWhiteList() {
  304. return enableCountryWhiteList;
  305. }
  306.  
  307. public String getWhiteListedCountriesKickMessage() {
  308. return whiteListedCountriesKickMessage;
  309. }
  310.  
  311. public String getUpdatingKickMessage() {
  312. return updatingKickMessage;
  313. }
  314. }
  315.  
  316. package de.emeraldmc.nobots.utils;
  317.  
  318. import org.bukkit.configuration.file.FileConfiguration;
  319. import org.bukkit.configuration.file.YamlConfiguration;
  320.  
  321. import java.io.File;
  322. import java.io.IOException;
  323. import java.util.ArrayList;
  324.  
  325. /**
  326. * Creates, loads and fills the Config file so the user can configure it
  327. * @author Patrick Sommer
  328. */
  329. public class ConfigManager {
  330. public static File getConfigFile() {
  331. return new File("plugins/NoBots", "config.yml");
  332. }
  333. public static FileConfiguration getConfigFileConfiguration() {
  334. return YamlConfiguration.loadConfiguration(getConfigFile());
  335. }
  336.  
  337. /**
  338. * Set the standard configuration
  339. */
  340. public static void setConfig() {
  341. FileConfiguration cfg = getConfigFileConfiguration();
  342. cfg.options().copyDefaults(true);
  343. cfg.addDefault("Config.debug", false);
  344. cfg.addDefault("Config.MaxIp.enabled", true);
  345. cfg.addDefault("Config.MaxIp.limit", 2);
  346. cfg.addDefault("Config.MaxIp.kickMessage", "&cYou are kicked!");
  347. cfg.addDefault("Config.Attacked.minimumAttackers", 5);
  348. ArrayList<String> whiteListedCountries = new ArrayList<>();
  349. whiteListedCountries.add("Austria");
  350. whiteListedCountries.add("Germany");
  351. whiteListedCountries.add("United States");
  352. cfg.addDefault("Config.WhiteListedCountries.enabled", true);
  353. cfg.addDefault("Config.WhiteListedCountries.countries", whiteListedCountries);
  354. cfg.addDefault("Config.WhiteListedCountries.kickMessage", "&cYou are not in a whitelisted country!");
  355. cfg.addDefault("Config.updatingKickMessage", "&cPlease come back in a minute, the Blacklist is updating!");
  356. cfg.addDefault("Config.MySQL.host", "127.0.0.1");
  357. cfg.addDefault("Config.MySQL.port", 3306);
  358. cfg.addDefault("Config.MySQL.database", "NoBots");
  359. cfg.addDefault("Config.MySQL.user", "root");
  360. cfg.addDefault("Config.MySQL.password", "toor");
  361. try {
  362. cfg.save(getConfigFile());
  363. } catch (IOException e) {
  364. e.printStackTrace();
  365. }
  366. }
  367.  
  368. /**
  369. * Read the configuration
  370. */
  371. public static void readConfig() {
  372. FileConfiguration cfg = getConfigFileConfiguration();
  373. }
  374. }
  375.  
  376. package de.emeraldmc.nobots.utils;
  377.  
  378. public class GoodPercentage {
  379. private int factors;
  380. private int applicables;
  381. private int percentage;
  382.  
  383. public GoodPercentage(int factors) {
  384. setFactors(factors);
  385. applicables = 0;
  386. calcPercentage();
  387. }
  388.  
  389.  
  390. public int calcPercentage() {
  391. percentage = (factors/applicables)*100;
  392. return percentage;
  393. }
  394. public void addApplicables() {
  395. if (applicables >= factors) return;
  396. applicables++;
  397. calcPercentage();
  398. }
  399. public void setFactors(int factors) {
  400. if (factors < 1) return;
  401. this.factors = factors;
  402. }
  403.  
  404. public int getFactors() {
  405. return factors;
  406. }
  407.  
  408. public int getApplicables() {
  409. return applicables;
  410. }
  411.  
  412. public int getPercentage() {
  413. return percentage;
  414. }
  415. }
  416.  
  417. package de.emeraldmc.nobots.utils;
  418.  
  419. import de.emeraldmc.nobots.Main;
  420. import org.bukkit.Bukkit;
  421.  
  422. import java.sql.Connection;
  423. import java.sql.DriverManager;
  424. import java.sql.SQLException;
  425.  
  426. public class MySQL {
  427. private static Connection connection = null;
  428.  
  429. private static String host = Main.getInstance().getConfiguration().getDbHost();
  430.  
  431. private static int port = Main.getInstance().getConfiguration().getDbPort();
  432.  
  433. private static String database = Main.getInstance().getConfiguration().getDatabase();
  434.  
  435. private static String user = Main.getInstance().getConfiguration().getDbUser();
  436.  
  437. private static String password = Main.getInstance().getConfiguration().getDbPassword();
  438.  
  439. private MySQL() {
  440. try {
  441. // load database drivers for ODBC interfaces
  442. Class.forName("com.mysql.jdbc.Driver");
  443.  
  444. // connect to ODBC database
  445. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?" + "user=" + user + "&" + "password=" + password);
  446. } catch (ClassNotFoundException e) {
  447. Bukkit.getConsoleSender().sendMessage("§c[-] §7Driver not found!");
  448. e.printStackTrace();
  449. } catch (SQLException e) {
  450. Bukkit.getConsoleSender().sendMessage("§c[-] §7Could not connect to database!");
  451. e.printStackTrace();
  452. }
  453. }
  454.  
  455. public static void closeConnection() {
  456. try {
  457. connection.close();
  458. Debug.print("Cloes MySQL connection");
  459. } catch (SQLException e) {
  460. e.printStackTrace();
  461. connection = null;
  462. }
  463. }
  464.  
  465. public static void createTables() {
  466. Connection conn = getInstance();
  467. try {
  468. // ToDo: conn.prepareStatement("CREATE TABLE IF NOT EXISTS NoBots_WhiteList (UUID VARCHAR (36))").execute();
  469. conn.prepareStatement("CREATE TABLE IF NOT EXISTS NoBots_BlackList (IP VARCHAR (15))").execute(); // ToDo: Add IPv6 support
  470. } catch (SQLException e) {
  471. e.printStackTrace();
  472. }
  473. }
  474. public static Connection getInstance() {
  475. if (connection == null) {
  476. new MySQL();
  477. }
  478. return connection;
  479. }
  480. }
  481.  
  482. package de.emeraldmc.nobots.utils;
  483.  
  484. import de.emeraldmc.nobots.Main;
  485.  
  486. import java.io.BufferedReader;
  487. import java.io.IOException;
  488. import java.io.InputStreamReader;
  489. import java.net.InetAddress;
  490. import java.net.MalformedURLException;
  491. import java.net.URL;
  492. import java.util.regex.Pattern;
  493.  
  494. public class ProxyCheck {
  495. public static boolean isProxy(String web, String result) {
  496. try {
  497. URL url = new URL(web);
  498. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  499. String inLine;
  500. String lineSum = new String();
  501. while ((inLine = in.readLine()) != null) {
  502. lineSum += inLine;
  503. }
  504. in.close();
  505.  
  506. return lineSum.contains(result);
  507. } catch (IOException e) {
  508. e.printStackTrace();
  509. }
  510. return false;
  511. }
  512. public static String getCountry(String address) {
  513. try {
  514. URL url = new URL("http://geoip.nekudo.com/api/"+address);
  515. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  516. String inLine;
  517. String lineSum = new String();
  518. while ((inLine = in.readLine()) != null) {
  519. lineSum += inLine;
  520. }
  521. in.close();
  522. if (lineSum.contains("error")) {
  523. return "Unavailable";
  524. }
  525. String[] arr = lineSum.split(",");
  526. String c = arr[1];
  527. c = c.replaceAll(Pattern.quote("{"), "");
  528. c = c.replaceAll(String.valueOf('"'), "");
  529. c = c.replaceAll("country:name:", "");
  530.  
  531. return c;
  532. } catch (MalformedURLException e) {
  533. e.printStackTrace();
  534. } catch (IOException e) {
  535. e.printStackTrace();
  536. }
  537. return null;
  538. }
  539. }
  540.  
  541. package de.emeraldmc.nobots.utils;
  542.  
  543. import java.sql.PreparedStatement;
  544. import java.sql.ResultSet;
  545. import java.sql.SQLException;
  546. import java.util.UUID;
  547.  
  548. public class WhiteList {
  549. public static void addUUID(UUID uuid) {
  550. if (WhiteList.isUUIDWhiteListed(uuid)) return;
  551. try {
  552. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("INSERT INTO NoBots_WhiteList (UUID) VALUES (?)");
  553. preparedStatement.setString(1, uuid.toString());
  554. preparedStatement.executeUpdate();
  555. Debug.print("Added "+uuid+" to the WhiteList");
  556. } catch (SQLException e) {
  557. e.printStackTrace();
  558. }
  559. }
  560. public static void removeUUID(UUID uuid) {
  561. if (!WhiteList.isUUIDWhiteListed(uuid)) return;
  562. try {
  563. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("DELETE FROM NoBots_WhiteList WHERE UUID = ?");
  564. preparedStatement.setString(1, uuid.toString());
  565. preparedStatement.executeUpdate();
  566. Debug.print("Removed "+uuid+" from the WhiteList");
  567. } catch (SQLException e) {
  568. e.printStackTrace();
  569. }
  570. }
  571. public static boolean isUUIDWhiteListed(UUID uuid) {
  572. try {
  573. PreparedStatement preparedStatement = MySQL.getInstance().prepareStatement("SELECT * FROM NoBots_WhiteList WHERE UUID = ?");
  574. preparedStatement.setString(1, uuid.toString());
  575. ResultSet rs = preparedStatement.executeQuery();
  576. return rs.next();
  577. } catch (SQLException e) {
  578. e.printStackTrace();
  579. }
  580. return false;
  581. }
  582. }
  583.  
  584. package de.emeraldmc.nobots.listeners;
  585.  
  586. import de.emeraldmc.nobots.Main;
  587. import de.emeraldmc.nobots.utils.WhiteList;
  588. import org.bukkit.Achievement;
  589. import org.bukkit.event.EventHandler;
  590. import org.bukkit.event.player.PlayerAchievementAwardedEvent;
  591. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  592.  
  593. public class AchievementAwardedEvent {
  594.  
  595. @EventHandler
  596. public void onAchievementAwarded(PlayerAchievementAwardedEvent e) {
  597. if (e.getAchievement() != Achievement.OPEN_INVENTORY && !WhiteList.isUUIDWhiteListed(e.getPlayer().getUniqueId())) {
  598. Main.getInstance().getGoodPlayers().get(e.getPlayer()).addApplicables();
  599. }
  600. }
  601. }
  602.  
  603. package de.emeraldmc.nobots.listeners;
  604.  
  605. import de.emeraldmc.nobots.Main;
  606. import de.emeraldmc.nobots.utils.WhiteList;
  607. import org.bukkit.entity.Player;
  608. import org.bukkit.event.EventHandler;
  609. import org.bukkit.event.Listener;
  610. import org.bukkit.event.block.BlockBreakEvent;
  611. import org.bukkit.event.player.PlayerAchievementAwardedEvent;
  612.  
  613. import java.util.ArrayList;
  614.  
  615. public class BlockBreakListener implements Listener {
  616.  
  617. private static ArrayList<Player> blockBreaked = new ArrayList<>();
  618.  
  619. @EventHandler
  620. public void onBlockBreak(BlockBreakEvent e) {
  621. if (!blockBreaked.contains(e.getPlayer()) && !WhiteList.isUUIDWhiteListed(e.getPlayer().getUniqueId())) {
  622. Main.getInstance().getGoodPlayers().get(e.getPlayer()).addApplicables();
  623. blockBreaked.add(e.getPlayer());
  624. }
  625. }
  626. }
  627.  
  628. package de.emeraldmc.nobots.listeners;
  629.  
  630. import de.emeraldmc.nobots.Main;
  631. import de.emeraldmc.nobots.utils.WhiteList;
  632. import org.bukkit.entity.Player;
  633. import org.bukkit.event.EventHandler;
  634. import org.bukkit.event.Listener;
  635. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  636.  
  637. import java.util.ArrayList;
  638.  
  639. public class CommandPreprocessListener implements Listener {
  640.  
  641. private static ArrayList<Player> commandPlayers = new ArrayList<>();
  642.  
  643. @EventHandler
  644. public void onCommandPreprocess(PlayerCommandPreprocessEvent e) {
  645. if (!commandPlayers.contains(e.getPlayer()) && (!e.getMessage().contains("register") || !e.getMessage().contains("login")) && !WhiteList.isUUIDWhiteListed(e.getPlayer().getUniqueId())) {
  646. Main.getInstance().getGoodPlayers().get(e.getPlayer()).addApplicables();
  647. }
  648. }
  649. }
  650.  
  651. package de.emeraldmc.nobots.listeners;
  652.  
  653. import de.emeraldmc.nobots.Main;
  654. import de.emeraldmc.nobots.utils.GoodPercentage;
  655. import org.bukkit.entity.Player;
  656. import org.bukkit.event.EventHandler;
  657. import org.bukkit.event.Listener;
  658. import org.bukkit.event.player.PlayerJoinEvent;
  659.  
  660. public class JoinListener implements Listener {
  661. @EventHandler
  662. public void onJoin(PlayerJoinEvent e) {
  663. Player p = e.getPlayer();
  664. GoodPercentage goodPercentage = new GoodPercentage(4); // 2 for AchievementAwards, 1 for BlockBreak, 1 for Command
  665. Main.getInstance().getGoodPlayers().put(p, goodPercentage);
  666. }
  667. }
  668.  
  669. package de.emeraldmc.nobots.listeners;
  670.  
  671. import de.emeraldmc.nobots.Main;
  672. import de.emeraldmc.nobots.utils.*;
  673. import org.bukkit.ChatColor;
  674. import org.bukkit.event.EventHandler;
  675. import org.bukkit.event.Listener;
  676. import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
  677. import java.net.InetAddress;
  678. import java.util.ArrayList;
  679.  
  680. public class LoginListener implements Listener {
  681.  
  682. // List of connected addresses
  683. public static ArrayList<String> addresses = new ArrayList<>(); //Format: 127.0.0.1
  684.  
  685. @EventHandler
  686. public void onLogin(AsyncPlayerPreLoginEvent e) {
  687. String player = e.getName();
  688. InetAddress address = e.getAddress();
  689. String strAddress = address.toString().replace("/", "");
  690. String country = ProxyCheck.getCountry(strAddress);
  691. Debug.print("Player: "+player+"n"+"t"+"-> Address: "+strAddress+"n"+"t"+"-> Country: "+country);
  692. if (strAddress.equals("127.0.0.1")) {
  693. return;
  694. }
  695. if (WhiteList.isUUIDWhiteListed(e.getUniqueId())) {
  696. return;
  697. }
  698.  
  699. if (Main.getInstance().isUpdating()) {
  700. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, ChatAPI.translateColor(Main.getInstance().getConfiguration().getUpdatingKickMessage()));
  701. return;
  702. }
  703. if (limitReached(strAddress)) {
  704. Debug.print("t"+"&cBlocked!");
  705. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, ChatAPI.translateColor(Main.getInstance().getConfiguration().getMaxIpKickMessage()));
  706. return;
  707. }
  708. if (BlackList.isIpBlackListed(strAddress)) {
  709. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Bots are not allowed!");
  710. return;
  711. }
  712. if (!isInAllowedCountry(strAddress, country)) {
  713. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, ChatAPI.translateColor(Main.getInstance().getConfiguration().getWhiteListedCountriesKickMessage()));
  714. return;
  715. }
  716. if (ProxyCheck.isProxy("http://api.stopforumspam.org/api?ip=" + strAddress, "<appears>yes</appears>")) {
  717. Debug.print("Bot found!: "+strAddress);
  718. BlackList.addIp(strAddress);
  719. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Bots are not allowed!");
  720. return;
  721. }
  722. if (ProxyCheck.isProxy("http://legacy.iphub.info/api.php?ip="+ strAddress +"&showtype=4", ""proxy":1")) {
  723. Debug.print("Bot found!: "+strAddress);
  724. BlackList.addIp(strAddress);
  725. e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Bots are not allowed!");
  726. return;
  727. }
  728.  
  729. addresses.add(strAddress);
  730. }
  731.  
  732. private boolean limitReached(String address) {
  733. int limit = Main.getInstance().getConfiguration().getMaxIpLimit();
  734. int i = 0;
  735. for (String strAddress : addresses) {
  736. if (strAddress.equals(address)) {
  737. i++;
  738. }
  739. }
  740. if (i >= Main.getInstance().getConfiguration().getMinimumAttackers()) {
  741. Main.getInstance().setAttacked(true);
  742. }
  743. return i > limit;
  744. }
  745. private boolean isInAllowedCountry(String address, String country) {
  746. if (country == null) return false;
  747. for (String c : Main.getInstance().getConfiguration().getWhiteListedCountries()) {
  748. if (c.equalsIgnoreCase(country)) {
  749. return true;
  750. }
  751. }
  752. return false;
  753. }
  754.  
  755. }
  756.  
  757. package de.emeraldmc.nobots.listeners;
  758.  
  759. import de.emeraldmc.nobots.utils.Debug;
  760. import org.bukkit.event.EventHandler;
  761. import org.bukkit.event.Listener;
  762. import org.bukkit.event.player.PlayerQuitEvent;
  763.  
  764. public class QuitListener implements Listener {
  765.  
  766. @EventHandler
  767. public void onQuit(PlayerQuitEvent e) {
  768. String address = e.getPlayer().getAddress().getAddress().toString().replace("/", "");
  769. LoginListener.addresses.remove(address);
  770. }
  771. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement